Atmega328 Timer/Counter Usage: Sistemas Embebidos Oscar Acevedo, PHD
Atmega328 Timer/Counter Usage: Sistemas Embebidos Oscar Acevedo, PHD
Atmega328 Timer/Counter Usage: Sistemas Embebidos Oscar Acevedo, PHD
Sistemas Embebidos
Oscar Acevedo, PhD
Timer/Counter Usage
• You should define the timer operation mode, clock source and prescaler, interrupts and
waveform generation
• Although timer 1 (16-bits) is used, the procedure is the same for other timers
• Only registers related to basic timer functionality will be presented
Timer1 Configuration – TCCR1A
• The largest prescaler value is 1024, so the timer may count at 16MHz/1024 = 15625Hz,
or 64uS period
• It will take 4.19424 seconds a full count
• To count up to 1S, the counter should count up to 1S/64uS= 15625
More Timer 1 Registers
TCNT1: TC1 counter value
int main(void)
{
// timer1 configuration
TCCR1A= 0B00000000;
TCCR1B= 0B00000110;
TCCR1C= 0;
TIMSK1= 0B00000111;
OCR1A= 85;
OCR1B= 170;
TCNT1= 0;