8 Bit Timer Countor
8 Bit Timer Countor
Differences:
-TCNT0 can run off an external 32Khz clock (Tosc) or the internal clock
after it has passed through the prescaler.
-TCNT2 can run off of an external or the internal clock.
Since TCNT0 can run off a clock asynchronous to the CPU, some issues occur:
-control register writes are delayed by two Tosc edges, @ 32Khz, = 61uS!
-entering power save modes must be delayed after writing control registers
-async clock may take up to 1sec to stabilize at power up
32Khz crystal
8-Bit Timer/Counter
0 all easily implemented.
Counters, timers, PWM generators are
The external clock oscillator is optimized for 32.768 Khz watch crystals.
Applying a 32.768 Khz oscillator output to the Tosc pins is not recommended.
8-Bit Timer/Counter
Four Registers: 0 ASSR
TCNT0, OCR0, TCCR0,
-If equal, the output compare flag is set (OCF0) and an interrupt can be
issued.
OCR0=0xF00
0OCR0=0x7F0
0OCR0=0x00F
F
OCR0=0x003
F
while(1) {
while (! (TIFR & (1<<TOV0))){} //spin till overflow
TIFR = (1<<TOV0); //clear by writing a one to TOV0
count++; //extend counter
if((count % 64) == 0)
PORTB ^= 0x01; //toggle PB0 each time
} //while
} // main
8-Bit Timer/Counter
Code 0
examples
// tcnt0_normal_int.c
// use interrupts now
// setup TCNT0 in normal mode and blink PB0 LED at 1 sec intervals
// blink frequency = (32768)/(2^8 * 64) = 1.000000 blinks per sec
//
#include <avr/io.h>
#include <avr/interrupt.h>
ISR(TIMER0_OVF_vect){
count++; //extend counter
if((count % 64) == 0)
PORTB ^= 0x01; //toggle PB0 each time this happens
}//TIMER0_OVF_vect
int main() {
DDRB = 0x01; //set port B bit zero to output
TCCR0 |= (1<<CS00); //normal mode, no prescaling
ASSR |=(1<<AS0); //use ext oscillator
TIMSK |= (1<<TOIE0);//allow interrupts on overflow