Need help on MSP430 sinusodial pwm code

Thread Starter

Tahsina Hossain

Joined Jan 16, 2015
40
Hi All,

I am trying to generate 2 SPWM signals out of MSP430 pin 1.2 and 1.6 like the following figure:

msp430signals.png

I am trying to use same port (port 1) for generating the signals with the following code which is not working. Please suggest how to fix to code to get my desired output.

Code:
#include  <msp430g2553.h>


#define pwmPeriod 400                // carrier of 2.5kHz and reference of 50Hz. PWM period is 0.4 ms.

// Sinewave frequency is 50 Hz means 20 ms period. Halfcycle is 10 ms.
//const unsigned int sinewave[51] = {200, 223, 245, 266, 287, 306, 323, 339, 352, 363, 371, 377, 380, 380, 377, 371, 363, 352, 339, 323, 306, 287, 266, 245, 223, 200, 177, 155, 134, 113, 94, 77, 61, 48, 37, 29, 23, 20, 20, 23, 29, 37, 48, 61, 77, 94, 113, 134, 155, 177};

const unsigned int halfcycle[26] = {200, 223, 245, 266, 287, 306, 323, 339, 352, 363, 371, 377, 380, 380, 377, 371, 363, 352, 339, 323, 306, 287, 266, 245, 223};

int waveonepulsecounter = 0;
int wavetwopulsecounter = 0;

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

  //Calibrate DCO for 1MHz operation
  BCSCTL1 = CALBC1_1MHZ;
  DCOCTL = CALDCO_1MHZ;

  // PWM signals to BIT2 and BIT6 of PORT 1 using single ISR and 10ms delay between the two signals
  P1DIR |= BIT2 + BIT6;
  P1SEL |= BIT2 + BIT6;
 
  TACTL = TASSEL_2 +  MC_1 + TACLR;              // SMCLK = 1 MHz, Upmode
 
  TACCR0 = pwmPeriod - 1;                        // setting PWM period to 0.4 ms, 400-1 clock ticks of SMCLK
  TACCTL0 = CCIE;                                // Enable Interrupt for CCR0
 
  TACCTL1 = OUTMOD_7;                            // RESET/SET for CCR1
  __delay_cycles(10000);                        // 10ms delay between interrupts
  TACCTL2 = OUTMOD_7;                            // SET/RESET for CCR2
 
  _BIS_SR(LPM0_bits + GIE);                 // Enter LPM0 w/ interrupt
}

// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0 (void)
{
    while(P1IFG)
    {
        if(P1IFG & BIT2)
        {
            TACCR1 = halfcycle[waveonepulsecounter];
            waveonepulsecounter = waveonepulsecounter + 1;
           
            if (waveonepulsecounter == 25)
                waveonepulsecounter = 0;
                           
        }   
       
        if(P1IFG & BIT6)
        {
            TACCR2 = halfcycle[wavetwopulsecounter];
            wavetwopulsecounter = wavetwopulsecounter + 1;
           
            if(wavetwopulsecounter == 25)
                wavetwopulsecounter = 0;
           
        }
    }
}
 
Top