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 Timer A Interrupt with Driver Library

Hello,

I am trying to understand how to use Timer A0 with the Driver Library APIs by toggling an LED (P1.0) every 15 seconds. The code runs, but the interrupt is never triggered. I think I want to use Up Mode with compare to trigger an interrupt every 15 seconds. Here is the code that sets up the clock:

UCS_initClockSignal(UCS_FLLREF, UCS_REFOCLK_SELECT, UCS_CLOCK_DIVIDER_1); // 32.768kHz
UCS_initClockSignal(UCS_ACLK, UCS_REFOCLK_SELECT, UCS_CLOCK_DIVIDER_16); // 32.768kHz / 16 = 2.048kHz
UCS_initFLLSettle(UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ, UCS_MCLK_FLLREF_RATIO); // 8MHz

The way I calculated the time for the interrupt is: 65535 / 15 = 4369, where 15 is the number of seconds after an interrupt should occur. The result would be the compare value. Here is the code for configuring the timer:

// Configure timer
Timer_A_initUpModeParam upModeParam = {0};
upModeParam.clockSource = TIMER_A_CLOCKSOURCE_ACLK;
upModeParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
upModeParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
upModeParam.timerClear = TIMER_A_DO_CLEAR;
upModeParam.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE;
upModeParam.startTimer = false;
Timer_A_initUpMode(TIMER_A0_BASE, &upModeParam);

Timer_A_clearCaptureCompareInterrupt(TIMER_A0_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_0);

// Initialize compare mode
Timer_A_initCompareModeParam compModeParam = {0};
compModeParam.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_0;
compModeParam.compareInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
compModeParam.compareOutputMode = TIMER_A_OUTPUTMODE_OUTBITVALUE;
compModeParam.compareValue = COMPARE_VALUE;
Timer_A_initCompareMode(TIMER_A0_BASE, &compModeParam);

// Start timer
upModeParam.startTimer = true;

I added a counter so that the LED would toggle once the condition is satisfied. Here is the code I have for the interrupt handler:

#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER_A0(void)
{
	if (timerA_cntr == 1)
	{
		GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0);
		timerA_cntr = 0;
	}
	else
	{
		timerA_cntr++;
	}
}

Here is the code for the main loop:

// Stop the watchdog timer
WDT_A_hold(WDT_A_BASE);

// Initialize clocks
init_clock();

// Initialize GPIO
init_gpio();

// Initialize Timer A
init_timer_a();

__enable_interrupt();

while (1)
{
    // Enter Low-power Mode 3
    __bis_SR_register(LPM3_bits);
}

Can anyone offer suggestions of where I am going wrong?

Anson

  • Anson,

    I do not know those libraries, but when you feed your timer from the 32k768 ACLK, then you can use a clock divider of 8 (maximum) for the timer, giving you 32768 / 8 = 4096 clock ticks per second. Multiplied by 15 results in 61440 which is still within the maximum of 65535 for the 16 bit wide CCRx register. Now you can configure the timer to a) run in up mode, setting CCRx to 61439 (1 less because the module needs one tick to jump back) or b) use the continuous mode and add the next 61440 ticks to your CCRx register inside the ISR. Both are compare mode operations. Inside the ISR you can then toggle the LED.

    Dennis

  • // Start timer
    upModeParam.startTimer = true;

    This does not actually start the timer. Setting a value in upModeParam would affect the next call to Timer_A_initUpMode(), if there were any.

    To start the timer without re-initializing, call Timer_A_startCounter().

  • Thank you, Dennis for your help! It turned out one of the issues I was having was the Compare Value wasn't set properly. The MCU I'm using can have a clock divider for the ACLK up to 32. So, 32,768 / 16 = 2048 clock ticks per second. Then, I multiplied by 15 to get 30,720. Before, I had it set to 4369, which was way too low.
  • Thank you, Clemens for your help! Calling the Timer_A_startCounter() function was the answer to one of my problems. I'll need to the documentation better next time.

**Attention** This is a public forum