0% found this document useful (0 votes)
58 views

Using Timer

The document discusses the use of timers/counters on the ATmega16 microcontroller. It describes that timers can count clock signals to measure time and counters can count external events. The ATmega16 has three timer/counter units: Timer/Counter0 is 8-bit, Timer/Counter1 is 16-bit, and Timer/Counter2 is 8-bit. All three can use prescaling and create PWM signals and interrupts. An example uses Timer/Counter0 to blink LEDs on PORTA at 22,888Hz by comparing the timer's counter register value to toggle the LEDs on and off.

Uploaded by

Dody Asmoro
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Using Timer

The document discusses the use of timers/counters on the ATmega16 microcontroller. It describes that timers can count clock signals to measure time and counters can count external events. The ATmega16 has three timer/counter units: Timer/Counter0 is 8-bit, Timer/Counter1 is 16-bit, and Timer/Counter2 is 8-bit. All three can use prescaling and create PWM signals and interrupts. An example uses Timer/Counter0 to blink LEDs on PORTA at 22,888Hz by comparing the timer's counter register value to toggle the LEDs on and off.

Uploaded by

Dody Asmoro
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Using Timer/Counter

(ATmega16)

Dipl.- Inf. Dirk Wilking


Lehrstuhl Informatik XI
RWTH Aachen
[email protected]

WS 06/07

Timer Ù Counter

ƒ Timer
- Counts clock signal (measures time)
- Clock divider can be used (for longer time intervals)
- Example: switch an LED every second

ƒ Counter
- Counts external events
- Example: switch an LED after 20 external events

ƒ Usually every Timer can also be used as Counter and vice


versa

Folie 2

1
Atmega16´s Timer/Counter

ƒ Timer/Counter0:
- 8bit
ƒ Timer/Counter1:
- 16bit
ƒ Timer/Counter2:
- 8bit

ƒ All three units can


- use prescaler (up to 1024)
- create PWM signals
- create interrupts (overflow, compare event)

Folie 3

ATmega16 – Timer/Counter0
Control Register
Timer

Overflow IR

Counter Register

Compare Register

Folie 4

2
ATmega16 – Timer/Counter0
Control Register
Counter

Overflow IR

Counter Register

Compare Register

Folie 5

Timer/Counter

ƒ The according registers can be accessed like I/O ports


ƒ More information about the function in the following
example and in the ATmega16data sheet
ƒ Timer/Counter can also generate Interrupts. Find more in
the slides Using Interrupts.

Folie 6

3
Example Timer0
//blinks LEDs at PORTA with 22,888Hz (6MHz system clock)

#include <avr/io.h>

int main (void)


{
outp(0xFF,DDRA); //PORTA is output
outp(0x00,TCNT0); //Counter Register = 0
outp(0x05,TCCR0); //start Timer0 with prescaler = 1024
//counter is running from now on until it is stopped
//counter restarts with 0x00 after 0xFF has been reached
while(1)
{
if(TCNT0 < 100) //read and compare Counter Register
{
PORTA = 0xFF; //LEDs PORTA off
}
else
{
PORTA = 0x00; //LEDs PORTA on
}
}
}

Folie 7

You might also like