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

Timers in AVR

Uploaded by

Rohit Padile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Timers in AVR

Uploaded by

Rohit Padile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Timers in AVR

AVR Timer

• AVR timer can be used to calculate accurate delay in the program


• It can also be used as a counter to count external events
• Timer0, Timer1, Timer2
• T0 and T2 are 8 bit timers while T1 is a 16 bit timer
• T0 and T1 are used as counter but T2 can not be used as a counter
General view of AVR Counters and Timers
Registers of Timers

• T0 and T2 are 8 bit (TCNT0, TCNT2)


• T1 is 16 bit (TCNT1
• OCRn (Output Compare Register), n=0, 1, 2
• TCCR0
• TCCR1A & TCCR1B
• TCCR2
Registers of Timers
Timer0 Internal View
Timer1 Internal View
Timer2 Internal View
Timer0 Registers
TOV0 (Timer0 Overflow)
• The flag is set to 1 when the counter overflows, going from $FF to $00 and it remains set until the
software clears it
• To clear the flag we need to write 1 to it and 0 to other bits
• Ex. LDI R20, 0x01
OUT TIFR, R20

Normal mode
• In this mode, the contents of timer/counter increments with the clock. It counts until it reaches $FF
• When it rolls over from $FF to $00, it sets a flag bit TOV0
Steps to program in normal mode:
1. Load TCNT0 register with initial count value.
2. Load TCCR0 register indicating mode and prescaler option to be used.
3. Keep monitoring the timer overflow flag TOV0 to see if it is raised. Get out of the
loop when TOV0 becomes high.
4. Stop the timer by disconnecting the clock source with following instruction
LDI R16,0x00
OUT TCCR0,R16
5. Clear TOV0 flag for the next round by writing 1 to that bit of TIFR register.
6. Go back to step 1 and repeat the same.
Timer 0 Normal Mode Programming in AVR Microcontroller
1. Tclock=1/Ftimer (If we use prescaler than we need to divide base frequency with
that and finally inverse it to have Tclock)
2. Divide the desired time delay by Tclock. This says how many clocks we need.
3. Perform 256 - n, where n is the decimal value we got in step 2
4. Convert result of step 3 in to hex
5. Set TCNTO = Hex number of step 4
Example in C

#include <avr/io.h>
#define F_CPU 8000000UL
void TODelay(); // function declaration
int main(void)
{
DDRB |= (1<<3); // Make PB3 an o/p port
while(1)
{
PORTB = PORTB ^ (1<<3); // Toggle PB3
TODelay();
}

void TODelay () // create delay of 6.25 µS when called


{
TCNTO = 0xCE; // takes 50 counts to generate 6.25 µS delay
TCCRO = 0x01; // Timero, normal mode, no prescalar
while ((TIFR&(1<<TOV0))==0); // Wait for TOVO to roll over
TCCRO = 0; // Stop TIMER O
TIFR = 0X01; // Clear TOV0
}
CTC mode

• The OCR0 register is used with CTC mode


• As with the Normal mode, in the CTC mode, the timer is incremented with a
clock.
• But it counts up until the content of the TCNT0 register becomes equal to
the content of OCR0 (compare match occurs); then, the timer will be
cleared and the OCF0 flag will be set when the next clock occurs
• The OCF0 flag is located in the TIFR register
Steps:
1. Load TCNTO register with 0 initial value
2. Load TCCRO register indicating CTC mode and prescaler option to be used
3. Keep monitoring the timer overflow flag OCFO to see if it is raised. Get out
of the loop when OCFO becomes high.
4. Stop the timer by disconnecting the clock source with following
instruction
LDI R16,0x00
OUT TCCR0,R16
5. Clear OCFO flag for the next round by writing 1 to that bit of TIFR register
6. Go back to step 2 and repeat the same steps.
Timer0 as a Counter
Assuming that 1 Hz clock pulse is fed into pin T0(PBO), write a C program for counter0 in normal
mode to count the pulses on falling edge and display the state of the TCNT0 count on PORTC
#include <avr/io.h>
#define F_CPU 8000000UL
int main(void)
(
DDRC = 0XFF;
DDRB&= ~(1<<0);
TCCRO = 0X06;
TCNT0 = 0;
while(1)
{
do
{
PORTC = TCNT0;
}
while(TIFR&(1<<TOVO))==0) //Wait for TOVO = 1 TIFR = 1<<TOVO;
}
return 0;
}
Timer2 programming

• It can be used as a real-time counter, to do so , we should connect a crystal


of 32.768 kHz to the TOSC1 and TOSC2 pins of AVR and set the AS2 bit
Assuming that XTAL = 8 MHz, write a program to generate a square wave with
a period of 10ms and 10% duty cycle on pin PB3

Selection of prescaler options:


.INCLUDE "M32DEF.INC"
LDI R20, HIGH(RAMEND)
OUT SPH, R20
LDI R20, LOW(RAMEND)
OUT SPL, R20
SBI DDRB, 3
XX: SBI PORTB,3
LDI R16, 1
RCALL DELAY
CBI PORTB, 3
LDI R16, 9
RALL DELAY
RJMP XX

DELAY: LDI R20, 6


OUT TCNT2, R20 // Load timer0
LDI R20, 0X03
OUT TCCR2, R20 // Timer2, Normal mode, int clock, 32 prescaler
AGAIN: IN R20, TIFR // Read TIFR
SBRS R20, TOV2 // if TOV2 bit is set skip next instruction
RJMP AGAIN
LDI R20,0X00
OUT TCCR2,R20 // Stop timer
LDI R20, (1<<TOV2)
OUT TIFR, R20 // Clear TOV2 flag by writing a 1 to TIFR
DCR R16
BRNE DELAY
RET
#include <avr/io.h>
#define F_CPU 8000000UL
void T0Delay(unsigned char); // function declaration

int main(void)
{
DDRB |= (1<<3); // Make PB3 an o/p port while(1)
{
PORTB| = (1<<3); // Set PB3
T0Delay(1);
PORTB&=~(1<<3);
T0Delay(9);
}
return 0;
}

void TODelay (unsigned char j)


{
unsigned char i,j;
for(i=0;i<j;i++)
{
TCNT2 = 6; // takes 250 counts to generate 1ms delay
TCCR2 = 0x03; // Timer2, normal mode, 32 prescalar
while ((TIFR&(1<<TOV2))==0); // Wait for TOV2 to roll over
TCCR2 = 0; // Stop TIMER 2
TIFR = 0X01; // Clear TOV2
}
}
Timer1 programming
• Timer1 is a 16-bit timer and has lots of capabilities
• Its 16-bit register is split into two bytes, these are referred to as TCNTIL (Timerl low byte) and
TCNTIH (Timer) high byte)
• Timer1 also has two control registers named TCCRIA (Timer/counter control register) and
TCCRIB
• The TOV1 (timer overflow) flag bit goes HIGH when overflow occurs
• Timer1 also has the prescaler options of 1:1, 1:8, 1:64, 1:256, and 1:1024
• There are two OCR registers in Timer1: OCRIA and OCRIB. There are two separate flags
OCR1A and OCR1B
Write a C program to toggle PB4 bit continuously every second. Use timer1, normal mode.
Assume XTAL = 8MHz.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy