Getting Started

(lily) #1

Chapter 7: Microcontroller Interrupts and Timers


// Sets the compare value

ng at the fastest rates in the lower

or LED, so the blink periods for each LED are:

LED2 = 125 Hz.

LED5 = 15.625 Hz.

LED7 = 3.90625 Hz.

e te
z and Hz.

// Enable timer0 compare interrupt
TIMSK0 = (1<<OCIE0A);


// Sets the compare value
set_OCR0A(count);


// Set Clear on Timer Compare (CTC) mode, CLK/8 prescaler
TCCR0A = (1<<WGM01)|(0<<WGM00)|(1<<CS01);


}


void set_OCR0A(unsigned char count)
{


OCR0A = count;
}


Now we can initialize the timer when the program starts and change the compare
value when we feel like it. Let’s reuse the PC_Comm code to generate an
annoying LED precision blinker that’s actually an 8-bit counter. As you will see,
or rather won’t see, you can’t see the LED blinki
4 bits, but you can see blinking in the slower upper 4 bits. The lowest bit toggles
the LED 1000 times a second, it is on for 1000th of a second then off for 1000th of
a second, which yields a blink period of 500 Hz. Each LED blinks at half the rate
of the pri


LED0 = 500 Hz.
LED1 = 250 Hz.

LED3 = 62.5 Hz.
LED4 = 31.25 Hz.

LED6 = 7.8125 Hz.

If w ll the Butterfly to set the compare to 125, then the interrupt occurs at 2000
H the fastest blink period becomes 1000

Free download pdf