AVR Timers0
AVR Timers0
AVR Timers0
AVR Lab 4
Timers/Counters in Atmega16
Atmega16 has three timers/counters
8-bit Timer/Counter0
16-bit Timer/Counter1
8-bit Timer/Counter2
Timers/Counter Functions
The Timers can be used for the following
functions
Normal Mode (Delay generation or Measuring
time)
Clear timer on compare match CTC (Delay
generation or Measuring time)
Count External events
PWM generation
Phase correct PWM
Fast PWM
Waveform Generation
Registers Associated with Timer 0
TCCR0
TCNT0
OCR0
TIFR
TIMSK
Registers Associated with Timers
Timer/Counter Control Register – TCCR0
The Timer/Counter Register gives direct access, both for read and
write operations, to the Timer/Counter unit 8-bit counter
Output Compare Register – OCR0
ISR(TIMER0_COMP_vect)
{
……
}
Example – Normal Mode (using interrupt)
#include<avr/io.h>
#include<avr/interrupt.h>
int main()
{
DDRA=0xFF;
TCNT0=231;
TIMSK=0x01;
SREG|=0x80;
TCCR0=0x03;
while(1)
{
}
}
ISR(TIMER0_OVF_vect)
{ PORTA=PINA ^ 0x80;
TCNT0=231;
}
Calc of No for OCR0 Register
Procedure
Nc = Time of delay (us) x Crystal freq (MHz)
If Nc < 256 use Timer0 without prescaler else use prescaler.
OCR0 = Nc
TCNT0 = 0;
The above two modes are used to drive the timer from
an external clock source.
By using these options the timer can be used as a
counter to count external events.
Example
Count the number of persons enter in a hall. A sensor is
placed at the entrance door of the hall, that sends a
pulse when it senses a person, attached to pin PB0.
Configure the timer so that it responds to the falling
edge. Output the number of persons on PORTD
Solution
For this we configure the timer to count on the falling edge of an
external source. Hence, CS02=1,CS01=1,CS00=0. (Clock
Select)
The number of persons that enter through the door will be equal
to the value of TCNT0.
Solution - Code
#include<avr/io.h>
int main()
{
DDRD=0xFF;
DDRB=0x00;
TCCR0=0x06;
while(1)
{
PORTD=TCNT0;
}
}
PWM Generation
PWM
Phase correct PWM mode
Fast PWM mode
Phase Correct PWM Mode
The phase correct PWM mode is based on a dual
slope operation
The counter counts repeatedly from BOTTOM to
MAX and then from MAX to BOTTOM
Role of COM01 & 00 in Phase Correct PWM
#include<avr/io.h>
void main(void)
{
DDRB|=0x08; //configure port B PIN 4 (OC0) as output
TCCR0=0b01100010; //PWM in phase correct mode prescalar=8
while(1)
{
OCR0=154;
}
}
Fast PWM Mode
The fast Pulse Width Modulation or fast PWM mode
provides a high frequency PWM waveform
generation option
The counter counts from BOTTOM to MAX then
restarts from BOTTOM. It is a single slope operation
Role of COM01 & 00 in Fast PWM
#include<avr/io.h>
void main(void)
{
DDRB|=0x08; //configure port B PIN 4 (OC0) as output
TCCR0=0b01101010; //PWM in Fast PWM mode prescalar=8
while(1)
{
OCR0=154;
}
}
Problems
Read an 8 bit value from PORTA, and generate a
wave with the corresponding duty cycle. Use Phase
correct mode and frequency of wave between
200Hz – 1KHz (Crystal freq = 8MHz)
Generate a wave with frequency between 200Hz -1KHz,
with duty cycle corresponding to the digital value of adc
channel 0. Use fast PWM mode.