Hello. i wanna write a program running in 1kHz frequency and the duty cycle is changing from 10% to 50% to 90% and back to 10%, like a loop.
#include "msp430x22x4.h"
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P4DIR |= 0x20; // P4DIR = P4DIR | 0x20 set P4.5 output
P4SEL |= 0x20; // P4.5 TBx options
int x=0;
int z;
for(;;)
{
if (x==0)
{
z = 120; // TBCCR2 PWM duty cycle, 10%
x++;
}
else if (x==1)
{
z = 600; // TBCCR2 PWM duty cycle, 50%
x++;
}
else if (x==2)
{
z = 1080; // TBCCR2 PWM duty cycle, 90%
x=0;
}
TBCCR0 = 1199; // PWM Period
TBCCTL2 = OUTMOD_7; // TBCCR2 reset/set
TBCCR2 = z; // TBCCR2 PWM duty cycle, 10%
TBCTL = TBSSEL_2 + MC_1; // SMCLK, up mode
}
}
this is what i wrote. but the from the oscilloscop, the duty cycle and frequency are not like what i want. anyone can help to see what is wrong? thank you thank you.