MSP430 interrupt driven PWM

this is the code for a PWM, using the WDT as an interval timer.

//options for interval timer
/* WDT is clocked by fSMCLK (assumed 1MHz)
WDT_MDLY_32 (WDTPW+WDTTMSEL+WDTCNTCL)  32ms interval (default)
WDT_MDLY_8 (WDTPW+WDTTMSEL+WDTCNTCL+WDTIS0)  8ms ”
WDT_MDLY_0_5 (WDTPW+WDTTMSEL+WDTCNTCL+WDTIS1)  0.5ms ”
WDT_MDLY_0_064 (WDTPW+WDTTMSEL+WDTCNTCL+WDTIS1+WDTIS0)  0.064ms ”
WDT is clocked by fACLK (assumed 32KHz)
WDT_ADLY_1000 (WDTPW+WDTTMSEL+WDTCNTCL+WDTSSEL)  1000ms ”
WDT_ADLY_250 (WDTPW+WDTTMSEL+WDTCNTCL+WDTSSEL+WDTIS0)  250ms ”
WDT_ADLY_16   16ms ”
WDT_ADLY_1_9   1.9ms ”
interval x PERIOD = period interval    .064ms x 200 = 12.8ms or 78.125hz */
#include <msp430x20x2.h>
#include <signal.h> //add for interrupt
#define PERIOD 200  //defines frequency
volatile  int timer = 0; //counts how many interrupts have been called
//resets when it counts to PERIOD
volatile  int duty_cycle = 0;// max = PERIOD - 1 and min = 1
void main(void)
{
WDTCTL = WDT_MDLY_0_064; //interval setting
P1DIR |= BIT0;
P1OUT |= BIT0;
IE1 |= WDTIE;
eint(); //enable interrupt
}//end main
interrupt(WDT_VECTOR) watchdog_timer(void)
{
++timer;
if (timer == PERIOD) { timer = 0; P1OUT |= BIT0;  }
if (timer == duty_cycle ) { P1OUT ^= BIT0; }
}//end interrupt

a quick demo using my rgb light

sorry for the sound, i forgot to turn off my TV lol


4 Responses to MSP430 interrupt driven PWM

  1. i build view error below:
    Warning[Pe223]: function “eint” declared implicitly C:Documents and SettingsAdministratorDesktopproject mspled rgbmain.c 13
    Error[Pe079]: expected a type specifier C:Documents and SettingsAdministratorDesktopproject mspled rgbmain.c 15
    Error[Pe260]: explicit type is missing (“int” assumed) C:Documents and SettingsAdministratorDesktopproject mspled rgbmain.c 15
    Error[Pe141]: unnamed prototyped parameters not allowed when body is present C:Documents and SettingsAdministratorDesktopproject mspled rgbmain.c 15
    Error[Pe130]: expected a “{” C:Documents and SettingsAdministratorDesktopproject mspled rgbmain.c 15
    Error while running C/C++ compiler

    help me!!

    • What compiler are you using?
      It looks like you are using either CCS or IAR, this code is not compatible with either one

      you may have to enable the interrupt another way (gie bit), and you will have to change how the interrupt is called (#pragma)

      please let me know how you get it to work and i will update my post to add your changes

  2. this is my IAR working code:

    #include

    #define PERIOD 200 //defines frequency
    int timer = 0; //counts how many interrupts have been called
    //resets when it counts to PERIOD
    volatile int duty_cycle = 0;// max = PERIOD – 1 and min = 1
    void main(void){
    WDTCTL = WDT_MDLY_32; //interval setting
    P1DIR |= BIT0;
    P1OUT |= BIT0;
    IE1 |= WDTIE;
    __enable_interrupt();
    while(1);
    }

    #pragma vector = WDT_VECTOR
    __interrupt void watchdog_timer_ISR(void)
    {
    ++timer;
    if (timer == PERIOD){
    timer = 0;
    P1OUT |= BIT0;
    }
    if (timer == duty_cycle ){
    P1OUT ^= BIT0;
    }
    }