The processor ATmega328/P has three timer/counter, two 8 bit (Timer0 and Timer2) and one 16-bit TIMER1.
TIMER1 simplified block diagram for Normal Mode (count up).
TIMER1 16-bit counter can count edges pos/neg on T1 pin, or the processor clock pulses via a prescaler. The Prescaler can divide with 1, 1/8, 1/64, 1/256, 1/1024.
TCCR1B |= ((1 << CS12 )); // Set up timer clk prescaler as Fcpu/256 // "CS12 CS11 CS10" = "1 0 0"
To flash with 1 Hz you need toggle the LED 2 times/sec. Delay t = 0,5 s.
TIMER1 can be read/written as the 16-bit variable TCNT1. Every 0,5 s, wich will take 31249 TIMER1-tick, the LED is toggled and then TIMER1 reset.
if ( TCNT1 >= 31248){ PORTB ^= ( 1 << PB5 ); // period is 2*0.5 = one second TCNT1 = 0; // Reset timer value
The full program is available here:
Clear timer on compare mode, CTC.
To TIMER1 there is a compare unit. Instead of reading and comparing TCNT1 in the main loop, the compare unit can read TIMER1. The main program checks the interrupt flag OCF1A and toggles the LED. Reset of the TIMER1 is done automatically at compare.
The full program is available here:
Compare unit interrupt can completely offload the main program. The main program loop becomes empty _NOP();. TIMER1 is reset automaticaly at compare, and the LED is toggled in the interrupt subroutine.
The full program is available here:
William Sandqvist william@kth.se