This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Msp430f2274 program with IAR

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.

  • Hi Ivy,

    I would recommend you having a look on the TI sample code for MSP430F22x2/22x4 series (http://www.ti.com/lit/zip/slac123). There were some PWM examples which you should look at.

    Rgds
    aBUGSworstnightmare

  • Hi.

    i want to ask you a stupid question that whether my program need an ADC10?

    thanks

  • Hi Ivy,

    the stupid answer is: No! You don't need an ADC10 for doing PWM with your MCU!

    Rgds
    aBUGSworstnightmare

  • First, you shouldn't set TBCTL, TBCCR0 and TBCCTL2 on every loop. These are on-time intit settings.

    But then, well, your PWM frequency is 1200 SMCLK cycles. Yet your loop executes in , say, 20 cycles? So you're changing your PWM duty cycle about 60 times for each PWM wave cycle, resulting in a pretty interference pattern.

    After each change of the PWM duty cycle, you should give the hardware enough time to at least run through one complete pwm cycle, if not more.

    Also, you change the duty cycle unsynchronized to the current PWM output, resulting in distortions (elongating or shortening the current pulse up to 150% if you switch from 90% to 50% while the timer is above 50%). You should use an ISR which is triggered by CCR2 and which is setting the new CCR2 value from a globally set variable. This way a DC change happens always immediately after the falling(?) signal edge.

**Attention** This is a public forum