AVR Timers0

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Atmega16 Timers

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

This Register selects the modes of the timer and


the clock/prescaler to the timer
Registers Associated with Timers
 Timer/Counter Register – TCNT0

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

The Output Compare Register contains an 8-bit value that is


continuously compared with the counter value (TCNT0). A match
can be used to generate an output compare interrupt, or to generate
a waveform output on the OC0 pin.
Registers Associated with Timers
 Timer/Counter Interrupt Flag Register – TIFR

Contains the Flags for compare match and overflow

 Timer/Counter Interrupt Mask Register – TIMSK


Flags Associated with Timers
Registers Associated with Timers
 Timer/Counter Control Register – TCCR0

This Register selects the modes of the timer and


the clock/prescaler to the timer
Selection of Modes
 TCCR0 (Timer/counter Control Register)
Selects mode of Timer

 WGM01 and WGM00 bits in the TCCR0 register selects


the mode of the timer
Prescaler Options
 CS02,CS01 and CS00 selects the prescaler
Calc of No for TCNT0 Register
 Procedure
 Nc = Time of delay (us) x Crystal freq (MHz)
 If Nc < 256 use Timer0 without prescaler else use prescaler.
 Nr = 256 – Nc (value to be loaded in TCNT0)

 Use Prescaler if Nc > 256


 Np = Nc / prescaler
 Nr = 256- Np
Example – Normal Mode
Write a C program to toggle only PortA.7 bit continuously every 200 us. Use Timer0,
Normal Mode, and 1:64 prescaler to create the delay. Assume XTAL = 8MHz.
Solution:
 Np = Nc / prescaler -> (200 us x 8 MHz)/64 = 25 ->Nr = 256- 25 = 231
#include<avr/io.h>
void delay0();
int main()
{ DDRA=0xFF;
while(1)
{ delay0();
PORTA = PINA ^ 0x80; 1000-0000
}
}
void delay0()
{ TCNT0=231;
TCCR0=0x03; // Prescaler = 64, Mode = 0
while(((TIFR & (1<<0))==0)) ; //00000001
TCCR0 = 0; // stop Timer
TIFR = 01; // clear TOV0
}
ISR For Timers/Counter0
 ISR For Timer Overflow Interrupt
ISR(TIMER0_OVF_vect)
{
…..
}
 ISR For Timer Output Compare Match Interrupt

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;

 Use Prescaler if Nc > 256


 Np = Nc / prescaler
 OCR0 = Np
 TCNT0 = 0;
Example – CTC Mode
Write a C program to toggle only PortA.7 bit continuously every 200 us. Use Timer0,
Normal Mode, and 1:64 prescaler to create the delay. Assume XTAL = 8MHz.
Solution:
 Np = Nc / prescaler -> (200 us x 8 MHz)/64 = 25 ->OCR0 = Nr = 25
#include<avr/io.h>
void delay0();
int main()
{ DDRA=0xFF;
while(1)
{ delay0();
PORTA = PINA ^ 0x80; 1000-0000
}
}
void delay0()
{ TCNT0=0;
OCR0 = 25
TCCR0=0b00001011; // Prescaler = 64, Mode = 2 CTC Mode
while(((TIFR & (1<<OCF0))==0)) ; //00000010
TCCR0 = 0; // stop Timer
TIFR = 02; // clear OCF0
}
Problems
 Write a C program to toggle only PortA.7 bit
continuously every 5ms. Use Timer0, Normal
Mode, and suitable pre-scaler to create the
delay. Assume XTAL = 8MHz.

 Complete the task in the example using CTC mode.


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 – Timer as a counter
 PWM generation
 Phase correct PWM
 Fast PWM
 Waveform Generation
Counter Mode – Drive Timer from External Source

 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

Compare Output Mode, Phase Correct PWM Mode


Phase Correct PWM Mode
 Frequency of the wave in phase correct mode is
given by

where N is the pre-scaler. It means you can control


frequency of the output wave using pre-scaler

 Duty cycle of the PWM is controlled using OCR0


Example – Phase Correct PWM
 In this example we want to generate a PWM in phase
correct mode with frequency approx 2kHz and 60 %
duty cycle. CF is 8MHz

#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

Compare Output Mode, Fast PWM Mode


Fast PWM Mode
 Frequency of the wave in Fast PWM mode is given
by

where N is the pre-scaler. It means you can control


frequency of the output wave using pre-scaler

 Duty cycle of the PWM is controlled using OCR0


Example – Fast PWM
 Now if we consider same example in Fat PWM mode
then we have

#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.

You might also like