7us Pulse width with MSP430G2452 Launchpad

Hello everyone!

I have a challange for all of you MSP pro’s out there. I want to make a PWM of sorts with a 7us pulse width, and a 2.77ms period. Here’s the catch, I need to vary/skip pulses. The “MU” arrays below indicate a 1 when I want a 7us pulse, and a 0 when I skip that pulse.

I am using an MSP430G2452 on a TI launchpad development board. I am a PIC guy, so I am not quite familiar with TI’s MSP430 registers. I’ve been working for hours at this and can’t quite seem to get it. Here’s what I have so far, feel free to markup/dissect/question any parts of my code :smiley:

-Jameson

#include <MSP430G2452.h>

void ConfigClocks(void);

void ConfigTimerA0(void);

unsigned int PULSE_WIDTH = 20; //60Mu/min pulse is 7us @1MHz, that is 7 counts on timer A2

unsigned int DEAD_TIME = 44208; //dead time after pulse makes 2.77ms

unsigned int MU_80[6] = {1, 0, 0, 0, 0, 0}; //60Mu/min pulse is 7us @8mHz, that is 56 counts on timer A2

unsigned int MU_160[6] = {1, 0, 0, 1, 0, 0};

unsigned int MU_240[6] = {1, 0, 1, 0, 1, 0};

unsigned int MU_320[6] = {1, 1, 0, 1, 1, 0};

unsigned int MU_400[6] = {1, 1, 1, 1, 1, 0};

unsigned int CYCLE_STATE = 0; //to remember which part of the pulse train we are on.

unsigned int BEAM_ON = 0;

void main(void)

{

WDTCTL = WDTPW + WDTHOLD; //stop watchdog timer

P1DIR = BIT0; //P1.0 output

P1OUT = 0;

ConfigClocks();

ConfigTimerA0();

_BIS_SR(GIE); //TURN ON INTERRUPTS!!!

while(1)

{

_NOP();

}

}

void ConfigClocks(void)

{

if(CALBC1_16MHZ == 0xFF || CALDCO_16MHZ == 0xFF)

{

P1OUT = 1;

while(1);

}

BCSCTL1 |= CALBC1_16MHZ; //use 1MHZ calibrated dco

DCOCTL |= CALDCO_16MHZ;

IFG1 &= ~OFIFG;

BCSCTL2 &= ~SELS;

BCSCTL2 |= DIVS0;

}

void ConfigTimerA0(void)

{

CCTL0 = OUTMOD_7 + CCIE;

CCR0 = DEAD_TIME;

TACTL = TASSEL_2 + MC_1 + TAIE;

}

#pragma vector=TIMER0_A0_VECTOR

__interrupt void Timer_A0 (void)

{

if(MU_400[CYCLE_STATE] == 1)

{

P1OUT |= BIT0; //led on

CCR1 = PULSE_WIDTH;

}

else

{

P1OUT &= ~BIT0; //led off

CCR1 = 0;

}

CCTL0 &= ~CCIFG;

if(CYCLE_STATE == 5)

{

CYCLE_STATE = 0;

}

else

{

CYCLE_STATE += 1;

}

}

So I got the pulses working properly (well…with 10us pulses instead of 7).

Now I would like to incorporate this into the capacitive touch code.

Any suggestions? Here’s my pulse code working below:

#include <MSP430G2452.h>

void ConfigClocks(void);

void ConfigTimerA0(void);

unsigned int PULSE_WIDTH = 111; //60Mu/min pulse is 7us @1MHz, that is 7 counts on timer A2

unsigned int OFF_TIME = 26625; //dead time after pulse makes 2.77ms

unsigned int CYCLE = 26736;

unsigned int MU_80[6] = {1, 0, 0, 0, 0, 0}; //60Mu/min pulse is 7us @8mHz, that is 56 counts on timer A2

unsigned int MU_160[6] = {1, 0, 0, 1, 0, 0};

unsigned int MU_240[6] = {1, 0, 1, 0, 1, 0};

unsigned int MU_320[6] = {1, 1, 0, 1, 1, 0};

unsigned int MU_400[6] = {1, 1, 1, 1, 1, 0};

unsigned int CYCLE_STATE = 0; //to remember which part of the pulse train we are on.

unsigned int BEAM_ON = 0;

unsigned int waitFlag = 0;

void main(void)

{

WDTCTL = WDTPW + WDTHOLD; //stop watchdog timer

P1DIR = BIT0; //P1.0 output

P1OUT = 0;

if(CALBC1_16MHZ == 0xFF || CALDCO_16MHZ == 0xFF)

{

P1OUT = 1;

while(1);

}

ConfigTimerA0();

_BIS_SR(GIE); //TURN ON INTERRUPTS!!!

ConfigClocks();

}

void ConfigClocks(void)

{

BCSCTL1 |= CALBC1_16MHZ; //use 1MHZ calibrated dco

DCOCTL |= CALDCO_16MHZ;

IFG1 &= ~OFIFG;

BCSCTL2 &= ~SELS;

BCSCTL2 |= DIVS0;

}

void ConfigTimerA0(void)

{

CCTL0 = OUTMOD_5 + CCIE;

CCR0 = OFF_TIME;

CCR1 = PULSE_WIDTH;

TACTL = TASSEL_2 + MC_1 + TAIFG;

}

#pragma vector=TIMER0_A0_VECTOR

__interrupt void Timer_A0 (void)

{

if(waitFlag == 1)

{

CCR0 = OFF_TIME; //let the rest of the period remain off

P1OUT &= ~BIT0;

waitFlag = 0;

}

else

{

if(MU_240[CYCLE_STATE] == 1)

{

P1OUT |= BIT0; //led on

CCR0 = PULSE_WIDTH;

waitFlag = 1;

}

else

{

P1OUT &= ~BIT0; //led off

CCR0 = CYCLE;

}

CYCLE_STATE = CYCLE_STATE + 1; //increment cycle state

if(CYCLE_STATE == 6)

{

CYCLE_STATE = 0;

}

}

}