Timer¶
Typical Code¶
// Timer Initialization function
void timer_init(){
// select the clock source
CKCON &= ~0x08; /* Timer 0 uses SYSCLK/12 as source */
// select timer and set the working mode
TMOD &= 0xF0; /* Clear bits 0-3 of the Timer Mode Register */
TMOD |= 0x01; /* Set Timer 0 to Mode 1 (16-bit counter/timer) */
// stop timer
TR0 = 0; /* Disable Timer 0 */
TMR0 = 0; /* Clear all 16 bits of Timer 0’s count */
// TL0 = 0; TH0 = 0; /* Or Clear low & high bytes of Timer 0 separately */
}
// in the function where you want to use your timer
TR0 = 1; // enable timer 0
The Interrupt Service Routines read as
void ISR_name(void) __interrupt 1
{
// the interrupt order of timer 0 is 1. These values can be found in "Interrupts Priority Table"
// Put code here
}