For some odd reason, it seems as if the timer interrupt is triggering too fast in my program. The code can be seen below:
#include "msp.h"
void main(void)
{
WDTCTL = WDTPW | WDTHOLD;
/* clock initialization */
CSKEY = 0x695A;
CSCTL0 = 0;
CSCTL0 |= DCORSEL_1 | DCOEN; // DCOR = 3 mhz
CSCTL1 |= DIVS_5 | SELS_3 | SELM_3; // SMCLK = 93.7 khz
// MCLK = 3 mhz
CSKEY = 0x0000; // lock the clock registers
while((!(CSSTAT & SMCLK_READY)) && (!(CSSTAT & MCLK_READY))); // wait for clocks to ready
/* timer initialization */
TA0CCR0 = 46875;
TA0CCTL0 = CCIE;
TA0CTL |= TASSEL__SMCLK | MC__UP | TACLR;
NVIC_ISER0 |= (1 << ((INT_TA0_0 - 16) & 31)); // enable timer (timer)
P2DIR |= BIT2;
P2OUT &= ~BIT2;
PCMCTL1 &= ~LOCKLPM5; // unlock ports
__enable_interrupts();
while(1)
__sleep();
}
void timer()
{
static uint8_t i = 0;
i++;
if (i == 2)
{
P2OUT ^= BIT2;
i = 0;
}
TA0CCTL0 &= ~CCIFG;
}
I expect the interrupt above to trigger every one second, however, it seems as if the LED that toggles in the interrupt blinks faster than that. Does anybody know what is going on with the code? Thank you!