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.

MSP430F5529: PWM/Timer interrupt Code not working

Part Number: MSP430F5529

From the code below, the goal is to generate beep based on the timer config function in line. The first value is the timer period, then next is the duty cycle and the third is the number of beep to produce.

So from the existing function, a timer period is set at 32768, duty cycle of 16384 and beep count is 4. But code didn't go as expected on my MSP430F5529 LP.

#include <driverlib.h>

//Tested on MSP430F5529LP

int tCount; dutyCycle;
int beepCount; int counter; int newDutyCycle;
int countval = 0; int

void init();
int timer_config(int newPeriod, int newDutyCycle, int beep);
void stop_timer();

void main(){

    init();
    timer_config(32768,16384, 4);             //Configure timer

    while(1){
    }

    __bis_SR_register(LPM0 + GIE); //Enter Low Power Mode
    __no_operation();           //Enter Debugger Mode
}

void init() {

    WDT_A_hold(WDT_A_BASE);     //Stop Watchdog Timer

    GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN0); //Set Pin 2.0 as output pin

    //Configure Timer
    Timer_A_initUpModeParam param = {0};
    param.clockSource = TIMER_A_CLOCKSOURCE_ACLK; // ~1045000Hz
    param.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1; //~18660Hz
    param.timerPeriod = 32768;
    param.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
    param.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE;
    param.timerClear = TIMER_A_DO_CLEAR;
    param.startTimer = false;
    Timer_A_initUpMode(TIMER_A1_BASE, &param);

    //Configure Capture Compare Register
    Timer_A_initCompareModeParam paramCCR1 = {0};
    paramCCR1.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_1;
    paramCCR1.compareInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
    paramCCR1.compareOutputMode = TIMER_A_OUTPUTMODE_RESET_SET;
    paramCCR1.compareValue = 16384;
    Timer_A_initCompareMode(TIMER_A1_BASE, &paramCCR1);
}

int timer_config(int newPeriod, int newDutyCycle, int beep){

    Timer_A_setCompareValue(TIMER_A1_BASE,
                                 TIMER_A_CAPTURECOMPARE_REGISTER_0,
                                 newPeriod);
    Timer_A_setCompareValue(TIMER_A1_BASE,
                                 TIMER_A_CAPTURECOMPARE_REGISTER_1,
                                 newDutyCycle);
    Timer_A_clear(TIMER_A1_BASE);
    Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);


    if (countval==beep)
    {
        countval==0;
        stop_timer();
    }
    }

void stop_timer(){
        Timer_A_stop(TIMER_A1_BASE);
        Timer_A_disableInterrupt(TIMER_A1_BASE);
    }

#pragma vector = TIMER1_A0_VECTOR
__interrupt void timer1_ISR(void) {
    GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0);
    Timer_A_clearTimerInterrupt(TIMER_A1_BASE);
}

#pragma vector = TIMER1_A1_VECTOR
__interrupt void timer0_ISR(void) {
    switch(__even_in_range(TA0IV, TA0IV_TAIFG)) {
    case TA0IV_NONE: break;
    case TA0IV_TACCR1:
        GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN0);
        countval++;
        Timer_A_clearTimerInterrupt(TIMER_A1_BASE);
        break;
    case TA0IV_TACCR2: break;
    case TA0IV_TACCR3: break;
    case TA0IV_TACCR4: break;
    case TA0IV_5:      break;
    case TA0IV_6:      break;
    case TA0IV_TAIFG:  break;
    default: _never_executed();
    }
}

  • Hi Damilare,

    The configuration of your PWM generation seems little strange. Why you set two channels and one is with duty, the other is with period.

    Timer_A_setCompareValue(TIMER_A1_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_0, newPeriod);
    Timer_A_setCompareValue(TIMER_A1_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, newDutyCycle);

    I think you should refer to the following code example, and it describe how to generate PWM without interrupt. And you can enable interrupt for your own application.

    timer_a_ex5_pwmMultipleUp.c

    B.R.

    Sal

  • Hello Sal,

    Thank you so much for your recommendation, but I've checked out the recommended example too during my compilation and it doesn't have what I intended achieve hence my approach.

    The two set of channels spotted are to accept new period, and new duty cycle passed into the timer configuration function. The goal is to make sure the duty cycle, period and beep count can be set based on the argument passed into the function.

  • Hello Sal,

    Thank you so much for your recommendation, but I've checked out the recommended example too during my compilation and it doesn't have what I intended achieve hence my approach.

    The two set of channels spotted are to accept new period, and new duty cycle passed into the timer configuration function. The goal is to make sure the duty cycle, period and beep count can be set based on the argument passed into the function.

  • Hi Damilare,

    I recommend you debug code based on CCS, and the previous code you supply seem little error of code writing. 

    Can you grasp and show the PWM signal generated by your code in launchpad or other evaluation board?

    And as for change the period and duty, you don't configure the period of the PWM in my angle of view, you just only change the two channel's duty, and keep period constant. In timer_config(), you should change the param.timerPeriod and then pwm period will update.

    B.R.

    Sal

  • Hello Sal, sorry about the error. You can remove the 'int' in line 7 of the code.

    Also for your recommendation, I'll check it out also revert here.

    Thank you.

**Attention** This is a public forum