Need help on MSP430 sinusodial pwm code

Discussion in 'Embedded Systems and Microcontrollers' started by Tahsina Hossain, Feb 17, 2015.

  1. Tahsina Hossain

    Thread Starter Member

    Jan 16, 2015
    40
    0
    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 (Text):
    1. #include  <msp430g2553.h>
    2.  
    3.  
    4. #define pwmPeriod 400                // carrier of 2.5kHz and reference of 50Hz. PWM period is 0.4 ms.
    5.  
    6. // Sinewave frequency is 50 Hz means 20 ms period. Halfcycle is 10 ms.
    7. //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};
    8.  
    9. 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};
    10.  
    11. int waveonepulsecounter = 0;
    12. int wavetwopulsecounter = 0;
    13.  
    14. void main(void)
    15. {
    16.   WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
    17.  
    18.   //Calibrate DCO for 1MHz operation
    19.   BCSCTL1 = CALBC1_1MHZ;
    20.   DCOCTL = CALDCO_1MHZ;
    21.  
    22.   // PWM signals to BIT2 and BIT6 of PORT 1 using single ISR and 10ms delay between the two signals
    23.   P1DIR |= BIT2 + BIT6;
    24.   P1SEL |= BIT2 + BIT6;
    25.  
    26.   TACTL = TASSEL_2 +  MC_1 + TACLR;              // SMCLK = 1 MHz, Upmode
    27.  
    28.   TACCR0 = pwmPeriod - 1;                        // setting PWM period to 0.4 ms, 400-1 clock ticks of SMCLK
    29.   TACCTL0 = CCIE;                                // Enable Interrupt for CCR0
    30.  
    31.   TACCTL1 = OUTMOD_7;                            // RESET/SET for CCR1
    32.   __delay_cycles(10000);                        // 10ms delay between interrupts
    33.   TACCTL2 = OUTMOD_7;                            // SET/RESET for CCR2
    34.  
    35.   _BIS_SR(LPM0_bits + GIE);                 // Enter LPM0 w/ interrupt
    36. }
    37.  
    38. // Timer A0 interrupt service routine
    39. #pragma vector=TIMER0_A0_VECTOR
    40. __interrupt void Timer_A0 (void)
    41. {
    42.     while(P1IFG)
    43.     {
    44.         if(P1IFG & BIT2)
    45.         {
    46.             TACCR1 = halfcycle[waveonepulsecounter];
    47.             waveonepulsecounter = waveonepulsecounter + 1;
    48.            
    49.             if (waveonepulsecounter == 25)
    50.                 waveonepulsecounter = 0;
    51.                            
    52.         }  
    53.        
    54.         if(P1IFG & BIT6)
    55.         {
    56.             TACCR2 = halfcycle[wavetwopulsecounter];
    57.             wavetwopulsecounter = wavetwopulsecounter + 1;
    58.            
    59.             if(wavetwopulsecounter == 25)
    60.                 wavetwopulsecounter = 0;
    61.            
    62.         }
    63.     }
    64. }
    65.  
     
Loading...