msp430 pwm need help

cdz

Joined Jul 29, 2013
2
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:

Rich (BB code):
#include<msp430x16x.h>
typedef unsigned int uint;
typedef unsigned char uchar;
uint i,j=0;

void int_clk()
{
BCSCTL1 &= ~XT2OFF;
do
{
IFG1&=~OFIFG;
for(i=0xff;i>0;i--);

}
while((IFG1&OFIFG));
BCSCTL2|=SELM_2+SELS;//XT2 for m&smclk
}
void int_ta()
{
TACTL=TASSEL_2+TACLR+MC_1+TAIE;//smclk;upmode;interruptenable
///8Mhz for timer_a(start!)
TACCTL1|=OUTMOD_7;//reset/set mode
TACCR0=499;//period
TACCR1=100;
}
void main()
{

WDTCTL=WDTPW+WDTHOLD;
int_clk();
int_ta();
P1DIR|=BIT6;
P1SEL|=BIT6;
_EINT();

}

#pragma vector=TIMERA0_VECTOR
__interrupt void timera0(void)
{
if(TACCR1==100)
TACCR1=400;      //80% duty cycle
else
if(TACCR1==400)
TACCR1=100;  //20% duty cycle

}

Last edited by a moderator:

pavans2510

Joined Aug 27, 2013
3
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