Frequently Asked Questions
How is the TIMER0 interrupt used to perform an event at some rate?
The following is generic code used to issue a quick pulse at a fixed rate:
#include <16Cxx.H>
#use Delay(clock=15000000)
#define HIGH_START 114
byte seconds, high_count;
#INT_RTCC
clock_isr() {
if(--high_count==0) {
output_high(PIN_B0);
delay_us(5);
output_low(PIN_B0);
high_count=HIGH_START;
}
}
main() {
high_count=HIGH_START;
set_rtcc(0);
setup_counters(RTCC_INTERNAL, RTCC_DIV_128);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
while(TRUE);
}
In this program, the pulse will happen about once a second. The math is as follows:
The timer is incremented at (CLOCK/4)/RTCC_DIV.
In this example, the timer is incremented (15000000/4)/128 or 29297 times a second (34us).
The interrupt happens every 256 increments. In this example, the interrupt happens 29297/256 or 114 times a second.
The interrupt function decrements a counter (HIGH_START times) until it is zero, then issues the pulse and resets the counter. In this example, HIGH_START is 114 so the pulse happens once a second. If HIGH_START were 57, the pulse would be about twice a second.





