Chapter 7: Microcontroller Interrupts and Timers
interrupt to trip at the specified rate and toggle an
Hz. We set the Timer0
OCR0A = 250;
Finally we set the Timer/Counter Control Register A to the Clear Timer on
Compare waveform and the prescaler to divide the clock by 8:
// Set Clear on Timer Compare (CTC) mode, CLK/8 prescaler
TCCR0A = (1<<WGM01)|(0<<WGM00)|(1<<CS01);
But, heck Let’s get fancy and allow ourselves to change the compare value by
sending data from the PC. We write the MilliSec_init and the set_OCR)A
functions:
oid MilliSec_init(unsigned char count)
// Initialize Timer0.
Projects........................................................................................................
Precision Blinking...................................................................................
Let’s use interrupts and timers to provide precise control over the blink rate for an
LED. We’ll let the PC send data as a character string to the microcontroller and let
the microcontroller set a timer
LED.
First Let’s think about setting a timer to throw an interrupt every millisecond. The
USART initialization sets the system oscillator to 2 M
prescaler
to clk/8 which gives a 250 kHz input to the timer/counter. Then we set a compare
value of 250 so the timer throws an interrupt every 250 counts: 250000/250 =
1000, and we get interrupted a thousand times a second, almost like having a
toddler around.
We set timer0 to do a compare interrupt:
TIMSK0 = (1<<OCIE0A);
Then we set the timer0 compare register to 250:
v
{