msp430 pwm need help

Discussion in 'Embedded Systems and Microcontrollers' started by cdz, Aug 21, 2013.

  1. cdz

    Thread Starter New Member

    Jul 29, 2013
    2
    0
    I want to generate duty cycle adjustable pwm with msp430. and I write a program using timer_a interrupt that change pwm duty cycle between 80% and 20% but it won't work.I think something is wrong with the interrupt i can't figure out.Here is the code:

    Code ( (Unknown Language)):
    1.  
    2. #include<msp430x16x.h>
    3. typedef unsigned int uint;
    4. typedef unsigned char uchar;
    5. uint i,j=0;
    6.  
    7.  
    8. void int_clk()
    9. {
    10.    BCSCTL1 &= ~XT2OFF;
    11.    do
    12.    {
    13.      IFG1&=~OFIFG;
    14.      for(i=0xff;i>0;i--);
    15.        
    16.    }
    17.    while((IFG1&OFIFG));
    18.   BCSCTL2|=SELM_2+SELS;//XT2 for m&smclk
    19. }
    20. void int_ta()
    21. {
    22.   TACTL=TASSEL_2+TACLR+MC_1+TAIE;//smclk;upmode;interruptenable
    23. ///8Mhz for timer_a(start!)
    24.   TACCTL1|=OUTMOD_7;//reset/set mode
    25.   TACCR0=499;//period
    26.   TACCR1=100;
    27. }
    28. void main()
    29. {
    30.  
    31.   WDTCTL=WDTPW+WDTHOLD;
    32.   int_clk();
    33.   int_ta();
    34.   P1DIR|=BIT6;
    35.   P1SEL|=BIT6;
    36.   _EINT();
    37.  
    38.        
    39.      
    40.  }
    41.  
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
    48. #pragma vector=TIMERA0_VECTOR
    49. __interrupt void timera0(void)
    50. {
    51.   if(TACCR1==100)
    52.     TACCR1=400;      //80% duty cycle
    53.     else
    54.       if(TACCR1==400)
    55.         TACCR1=100;  //20% duty cycle
    56.  
    57. }
    58.  
     
    Last edited by a moderator: Aug 22, 2013
  2. pavans2510

    New Member

    Aug 27, 2013
    3
    1
    Hi CDZ,
    First of all you cannot use same timer for PWM as well as timer overflow interrupt. Refer to link below, the last second example shows an example code for your application. Dont know which msp430 chip you are using though. If you are using IAR then you put break point inside the interrupt routine and check whether your code is getting interrupted.

    http://homepages.ius.edu/RWISMAN/C335/HTML/msp430Timer.HTM


    Happy coding,
    pa1
     
    cdz likes this.
Loading...