I have two timers that are working just fine. The code to init the B1 timer is:
/* Configure TimerB1 to generate 100 Hz Clock */ void jiffyClockInitTimerB1(void){ Timer_B_initUpModeParam param = {0}; param.clockSource = TIMER_B_CLOCKSOURCE_SMCLK; param.clockSourceDivider = TIMER_B_CLOCKSOURCE_DIVIDER_48; // 16uS / 62.5 KHz param.timerPeriod = 624; //625 ticks = 10 mS / 100 Hz param.timerInterruptEnable_TBIE = TIMER_B_TBIE_INTERRUPT_ENABLE; param.captureCompareInterruptEnable_CCR0_CCIE = TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE; param.startTimer = true; Timer_B_initUpMode(TIMER_B1_BASE, ¶m); HWREG16(TIMER_B1_BASE + OFS_TBxCTL) &= ~(TBIE); // Turn off overflow (goes to zero) IRQ }
This compiles and works just fine. Now I want to use the B2 timer, and have this code to get it running:
void sineWaveInitTimerB2(void){ Timer_B_initUpModeParam param = {0}; param.clockSource = TIMER_B_CLOCKSOURCE_SMCLK; param.clockSourceDivider = TIMER_B_CLOCKSOURCE_DIVIDER_12; // SMCLK at 3 MHz 4uS / 250.5 KHz param.timerPeriod = 71; // 284uS --> 3.52 KHz / 8 =440 Hz param.timerInterruptEnable_TBIE = TIMER_B_TBIE_INTERRUPT_ENABLE; param.captureCompareInterruptEnable_CCR0_CCIE = TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE; param.startTimer = true; Timer_B_initUpMode(TIMER_B2_BASE, ¶m); HWREG16(TIMER_B2_BASE + OFS_TBxCTL) &= ~(TBIE); // Turn off overflow (goes to zero) IRQ }
When I try to compile this code, I get an error that TIMER_B2_BASE is undefined.
I added this macro at the top this source file:
#define TIMER_B2_BASE (*((volatile uint16_t *)((uint16_t)0x0400 + (0x0000))))
It now compiles, but, no luck seeing the timer run (I get no IRQs).
Any idea why there are valid macros for TIMER_B0_BASE and TIMER_B1_BASE but not for TIMER_B2_BASE ?
FWIW, the interrupt handler for this is a copy of one that works with Timer 1. It looks like:
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=TIMER2_B0_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(TIMER2_B0_VECTOR)))
#endif
void TIMER0_B2_ISR(void){
// to DEBUG, pulse the P2.2 bit so we can see when we take timer interrupts and see how long it takes
GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN3);
// Important safety tip: the CCIFG interrupt flag is automatically reset when the TBxCCR0 IRQ is serviced
// Timer_B_clearTimerInterrupt(TIMER_B0_BASE);
HWREG16(TIMER_B2_BASE + OFS_TBxCCTL0) &= ~(CCIFG | COV); // COV is a safety
HWREG16(TIMER_B2_BASE + OFS_TBxCTL) &= ~TBIFG; // "counter went to zero" interrupt flag, saf
GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN3); // exit timer irq DEBUG
}
Note that there was no problem on compile with the
#pragma vector=TIMER2_B0_VECTOR
statement.