SlideShare a Scribd company logo




A timer is a circuit that counts. This is said
from an electronics point of view.

It is widely used and is a very important
component of microprocessors and
microcontrollers.








Timers are classified based on their mode of
operation into synchronous and asynchronous timers.
Synchronous timers count with respect to the clock
while asynchronous timers depend upon the change
in the input.
They are also classified according to the maximum
number they can count. Eg. 8 bit timer,16 bit timers
etc
Timers are easy to implement and are done using
basic flip flop circuits .






They are used everywhere from modulating
signals to implement digital clocks, gaming,
phone/pc/tablet applications etc.

Small projects like range finders etc will also
use timers.
IC’s such as 555 are readily available in the
market and can be used to easily implement
timers.




There are 3 timers in the AVR of which 2 are
8 bit timers the other one is a 16 bit timer.

The timers found in the AVR or mostly in a
microcontroller or processor is of
synchronous type.




what do they mean? An 8 bit timer can count
to 2 to the power of 8 and a 16 bit timer can
count upto the 2 to the power of 16.

Basically timers in the controller are registers
and an 8 bit timer is a 8bit register and 16 bit
timer is 16bit register.








The 8 bit timer starts counting from zero and
goes upto 255.(that’s 256 counts)
The 16 bit timer starts counting from zero
and goes uptp 65535(65536 counts)
Once the timer reaches the maximum value it
“overflows” i.e. it restarts.
ANALOGY
For the timer to increase by one it takes one clock. That is the time period
gives the time it takes for the circuit to increment the timer register by one


Consider a processor with 4MHz clock. We
need a delay of 10ms. What will the timer
count be??



What will the timer count be when the
“required delay” is 50us



Which timer will you use ??



Can a delay of 100ms be directly
implemented with one of AVR timers.


So to know the maximum delay you could get
from the processor with a given clock
substitute TOP value of that particular timer
in the formula.

Maximum delay for a 4MHz processor
16bit timer?
8 bit timer?



What will we do for cases like the 100ms
delay?
The solution lies in reducing the frequency.

How do we do that?
That’s where tht prescaler comes into play.Do
understand that we don’t actually reduce the
frequency of the clock but we make the timer
to behave as if it is in a reduced frequency.




Also note that there will be a trade-off
between resolution and accuracy if you use a
precaler.

The prescaler is set by manipulating some
bits.


Now get the timer count for 100ms using one of
these prescalers.


PROBLEM STATEMENT:
Make an LED flash for every 10ms with
your atmega 8 internal oscillator featuring a
1MHz clock. Use only 8 bit timer TIMER
Do the calculations first.
 Without using a prescaler maximum
delay=256us
 Using a prescaler of 8 we’ll get a maximum
delay =2048us
 Using a prescaler of 64 max delay= 16.3ms
our requirement of 10ms delay fits in this
range.





No that we know our prescaler we should now
calculate our timer count.

Substituting 10ms in the formula we ge timer
count to be 155.25. we’ll round it out to 156
counts.
The most important one is TCCR0 register which is the Timer/counter
Control register for timer 0.
First we start the clock and set the prescalar which is done by setting
the three high lighted bits
Thus we have to set bits CS01 and CS00 to 1.
TCCR0|=1<<CS00|1<<CS01;


The register where the counting takes place
is the TCNT0 register.It counts automatically
and overflows and restarts again.

We intitalise this too.
TCNT0=0;


#include <avr/io.h>







void timer0_init()
{
// set up timer with no prescaling
TCCR0 |= ((1 << CS00)|(1<<CS01));










// initialize counter
TCNT0 = 0;
}
int main(void)
{
// connect led to pin PC0
DDRC |= (1 << 0);






// initialize timer
timer0_init();














}

// loop forever
while(1)
{
// check if the timer count reaches 156
if (TCNT0 >= 156)
{
PORTC ^= (1 << 0); // toggles the led
TCNT0 = 0;
// reset counter
}
}




There’s an alternative way of doing this.
Without using the prescalar or if the delay
cannot be got with the given prescalers.

Let’s do the same example without using
prescalars.







We previously calculated that without
prescaler maximum delay we get is 256us
A point to be noted is that everytime the
timer overflows an optional ISR is executed.
We will use that interrupts to get the delay
Let’s see how!
10ms/256us=39.0625
 So when the timer overflows 39 times it
would have counted
256*10^-6 *39= 9.984ms
 The remaining time is 10ms- 9.984ms=16us
 Now we substitute this again in the formula
and we’ll get 15 as the timer count.
 Thus at the 40th iteration and 15th tick we’ll
achieve our 10ms delay.




// global variable to count the number of overflows
volatile uint8_t tot_overflow;






















// TIMER0 overflow interrupt service routine
// called whenever TCNT0 overflows
ISR(TIMER0_OVF_vect)
{
// keep a track of number of overflows
tot_overflow++;
}
void timer0_init()
{
// set up timer with no prescaling
TCCR0 |= (1 << CS00);
sei();
TIMSK|=1<<TOIE0;
// initialize counter
TCNT0 = 0;
}








int main(void)
{
// connect led to pin PC0
DDRC |= (1 << 0);








// initialize timer
timer0_init();


















// loop forever
while(1)
{
// check if no. of overflows = 39
if (tot_overflow >= 39) // NOTE: '>=' is used
{
// check if the timer count reaches 15
if (TCNT0 >= 15)
{
PORTC ^= (1 << 0); // toggles the led
TCNT0 = 0;
// reset counter
tot_overflow = 0;
// reset overflow counter
}
}
}
}
The concept is the same with very minute differences. For
example the counting register TCNT1 will be a 16 bit register.

The control register is split into 2 8bit registers TCCR1A and
TCCR1B. For normal mode of operation knowledge about
TCCR1B will suffice


TIMSK-TIMER/COUNTER INTERRUPT MASK
REGISTER

This is used to enable the timer interrupt.TOIE0 is timer overflow
interrupt enable for 0 and TOIE1 is for timer 1. It is necessary to
set that bit inorder to enable the interrupt and execute the ISR


TIFR- timer interrupt flag register.

We are interested in the TOV bit. The Timer overflow bit.
This is set to 1 whenever the timer overflows . It is cleared
after it overflows.
The above two registers are shared by all the three timers.






CTC stands for Clear timer on Compare.
Remember previous example with the 10ms
with timer0?
The comparing can be done by the processor
itself and the corresponding instructions can
be executed.
Both CTC mode are almost the same except the fact that they
store the compare value in different registers.
The value to be compared is stored over here.
The OCF1A/B bit is set to 1 when the value stored in
OCR1A/B equals TCNT1 register.
Set these bits if you are using the interrupts.

ISR (TIMER1_COMPA_vect)
{
//to do
}
Those pins can be used to directly
be set or cleared without any extra
code.
We go back to TCCR1A register.`

Setting the above bits will enable the hardware mode.


FINAL coding lies in your hands. You can use
the timer as you wish.



Hope you guys enjoyed it.



Image Courtesy: maxembedded.com

More Related Content

What's hot

8051 MICROCONTROLLER ARCHITECTURE.pptx
 8051 MICROCONTROLLER ARCHITECTURE.pptx 8051 MICROCONTROLLER ARCHITECTURE.pptx
8051 MICROCONTROLLER ARCHITECTURE.pptx
MemonaMemon1
 
8086 pin details
8086 pin details8086 pin details
8086 pin details
AJAL A J
 
Programming 8051 Timers
Programming 8051 Timers Programming 8051 Timers
Programming 8051 Timers
ViVek Patel
 
Pic microcontroller architecture
Pic microcontroller architecturePic microcontroller architecture
Pic microcontroller architecture
DominicHendry
 
AVR_Course_Day7 timers counters and interrupt programming
AVR_Course_Day7 timers counters and  interrupt programmingAVR_Course_Day7 timers counters and  interrupt programming
AVR_Course_Day7 timers counters and interrupt programming
Mohamed Ali
 
Interrupt programming with 8051 microcontroller
Interrupt programming with 8051  microcontrollerInterrupt programming with 8051  microcontroller
Interrupt programming with 8051 microcontroller
Ankit Bhatnagar
 
Microprocessor & microcontroller
Microprocessor & microcontroller Microprocessor & microcontroller
Microprocessor & microcontroller
Nitesh Kumar
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 Microcontroller
Jay Makwana
 
Microprocessor 8085 complete
Microprocessor 8085 completeMicroprocessor 8085 complete
Microprocessor 8085 complete
Shubham Singh
 
8051 timers
8051 timers8051 timers
DAC , Digital to analog Converter
DAC , Digital to analog ConverterDAC , Digital to analog Converter
DAC , Digital to analog Converter
Hossam Zein
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
Gopal Krishna Murthy C R
 
Interrupts in 8051
Interrupts in 8051Interrupts in 8051
Interrupts in 8051
Sudhanshu Janwadkar
 
Analog to digital converters, adc
Analog to digital converters, adcAnalog to digital converters, adc
Analog to digital converters, adc
Saumya Ranjan Behura
 
8259 Programmable Interrupt Controller
8259 Programmable Interrupt Controller8259 Programmable Interrupt Controller
8259 Programmable Interrupt Controller
abhikalmegh
 
Timer programming for 8051 using embedded c
Timer programming for 8051 using embedded cTimer programming for 8051 using embedded c
Timer programming for 8051 using embedded c
Vikas Dongre
 
8051 serial communication-UART
8051 serial communication-UART8051 serial communication-UART
8051 serial communication-UART
Pantech ProLabs India Pvt Ltd
 
8251 USART
8251 USART8251 USART
8251 USART
coolsdhanesh
 
Chapter 1 microprocessor introduction
Chapter 1 microprocessor introductionChapter 1 microprocessor introduction
Chapter 1 microprocessor introduction
Shubham Singh
 
DRAM
DRAMDRAM

What's hot (20)

8051 MICROCONTROLLER ARCHITECTURE.pptx
 8051 MICROCONTROLLER ARCHITECTURE.pptx 8051 MICROCONTROLLER ARCHITECTURE.pptx
8051 MICROCONTROLLER ARCHITECTURE.pptx
 
8086 pin details
8086 pin details8086 pin details
8086 pin details
 
Programming 8051 Timers
Programming 8051 Timers Programming 8051 Timers
Programming 8051 Timers
 
Pic microcontroller architecture
Pic microcontroller architecturePic microcontroller architecture
Pic microcontroller architecture
 
AVR_Course_Day7 timers counters and interrupt programming
AVR_Course_Day7 timers counters and  interrupt programmingAVR_Course_Day7 timers counters and  interrupt programming
AVR_Course_Day7 timers counters and interrupt programming
 
Interrupt programming with 8051 microcontroller
Interrupt programming with 8051  microcontrollerInterrupt programming with 8051  microcontroller
Interrupt programming with 8051 microcontroller
 
Microprocessor & microcontroller
Microprocessor & microcontroller Microprocessor & microcontroller
Microprocessor & microcontroller
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 Microcontroller
 
Microprocessor 8085 complete
Microprocessor 8085 completeMicroprocessor 8085 complete
Microprocessor 8085 complete
 
8051 timers
8051 timers8051 timers
8051 timers
 
DAC , Digital to analog Converter
DAC , Digital to analog ConverterDAC , Digital to analog Converter
DAC , Digital to analog Converter
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
 
Interrupts in 8051
Interrupts in 8051Interrupts in 8051
Interrupts in 8051
 
Analog to digital converters, adc
Analog to digital converters, adcAnalog to digital converters, adc
Analog to digital converters, adc
 
8259 Programmable Interrupt Controller
8259 Programmable Interrupt Controller8259 Programmable Interrupt Controller
8259 Programmable Interrupt Controller
 
Timer programming for 8051 using embedded c
Timer programming for 8051 using embedded cTimer programming for 8051 using embedded c
Timer programming for 8051 using embedded c
 
8051 serial communication-UART
8051 serial communication-UART8051 serial communication-UART
8051 serial communication-UART
 
8251 USART
8251 USART8251 USART
8251 USART
 
Chapter 1 microprocessor introduction
Chapter 1 microprocessor introductionChapter 1 microprocessor introduction
Chapter 1 microprocessor introduction
 
DRAM
DRAMDRAM
DRAM
 

Viewers also liked

Timer & Interrupt Atmega16
Timer & Interrupt Atmega16Timer & Interrupt Atmega16
Timer & Interrupt Atmega16
Ramadan Ramadan
 
Timer
TimerTimer
IGCSE ICT
IGCSE ICTIGCSE ICT
IGCSE ICT
megabyte
 
Bell ringer activities
Bell ringer activitiesBell ringer activities
Bell ringer activities
Mónica Quesada
 
2014 Publicity Club of New England Bell Ringer Entry Tips
2014 Publicity Club of New England Bell Ringer Entry Tips2014 Publicity Club of New England Bell Ringer Entry Tips
2014 Publicity Club of New England Bell Ringer Entry Tips
Maryanne Keeney Wetherald
 
Bell ringers
Bell ringersBell ringers
Bell ringers
Parks1993
 
Developing an avr microcontroller system
Developing an avr microcontroller systemDeveloping an avr microcontroller system
Developing an avr microcontroller system
nugnugmacmac
 
Chem bell ringers week 1
Chem bell ringers week 1Chem bell ringers week 1
Chem bell ringers week 1
Paul Cummings
 
Bell ringer 2
Bell ringer 2Bell ringer 2
Bell ringer 2
spoldon
 
JAVA Media Player
JAVA Media PlayerJAVA Media Player
JAVA Media Player
Nausad Ahamed
 
Multirate dtsp
Multirate dtspMultirate dtsp
Multirate dtsp
Anjali Yadav
 
multirate signal processing for speech
multirate signal processing for speechmultirate signal processing for speech
multirate signal processing for speech
Rudra Prasad Maiti
 
Student centered learning presentation copy
Student centered learning presentation copyStudent centered learning presentation copy
Student centered learning presentation copy
trcash
 
Multirate digital signal processing
Multirate digital signal processingMultirate digital signal processing
Multirate digital signal processing
MOHAN MOHAN
 
Timers
TimersTimers
Timers
afzal pa
 
Mp3 player project presentation
Mp3 player project presentationMp3 player project presentation
Mp3 player project presentation
Antonio Mondragon
 
Choppers
ChoppersChoppers
Android mp3 player
Android mp3 playerAndroid mp3 player
Android mp3 player
Subhrajit Das
 
Keys to student centered learning final
Keys to student centered learning finalKeys to student centered learning final
Keys to student centered learning final
Bethany Marcusson-Mercedes
 
Android Media player
Android Media playerAndroid Media player
Android Media player
Maharshi Pancholi
 

Viewers also liked (20)

Timer & Interrupt Atmega16
Timer & Interrupt Atmega16Timer & Interrupt Atmega16
Timer & Interrupt Atmega16
 
Timer
TimerTimer
Timer
 
IGCSE ICT
IGCSE ICTIGCSE ICT
IGCSE ICT
 
Bell ringer activities
Bell ringer activitiesBell ringer activities
Bell ringer activities
 
2014 Publicity Club of New England Bell Ringer Entry Tips
2014 Publicity Club of New England Bell Ringer Entry Tips2014 Publicity Club of New England Bell Ringer Entry Tips
2014 Publicity Club of New England Bell Ringer Entry Tips
 
Bell ringers
Bell ringersBell ringers
Bell ringers
 
Developing an avr microcontroller system
Developing an avr microcontroller systemDeveloping an avr microcontroller system
Developing an avr microcontroller system
 
Chem bell ringers week 1
Chem bell ringers week 1Chem bell ringers week 1
Chem bell ringers week 1
 
Bell ringer 2
Bell ringer 2Bell ringer 2
Bell ringer 2
 
JAVA Media Player
JAVA Media PlayerJAVA Media Player
JAVA Media Player
 
Multirate dtsp
Multirate dtspMultirate dtsp
Multirate dtsp
 
multirate signal processing for speech
multirate signal processing for speechmultirate signal processing for speech
multirate signal processing for speech
 
Student centered learning presentation copy
Student centered learning presentation copyStudent centered learning presentation copy
Student centered learning presentation copy
 
Multirate digital signal processing
Multirate digital signal processingMultirate digital signal processing
Multirate digital signal processing
 
Timers
TimersTimers
Timers
 
Mp3 player project presentation
Mp3 player project presentationMp3 player project presentation
Mp3 player project presentation
 
Choppers
ChoppersChoppers
Choppers
 
Android mp3 player
Android mp3 playerAndroid mp3 player
Android mp3 player
 
Keys to student centered learning final
Keys to student centered learning finalKeys to student centered learning final
Keys to student centered learning final
 
Android Media player
Android Media playerAndroid Media player
Android Media player
 

Similar to Avr timers

Timers
TimersTimers
Timers
PRADEEP
 
Timers
TimersTimers
Timers
Islam Samir
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
Aarav Soni
 
timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptx
SujalKumar73
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
ankit3991
 
8051 Microcontroller Timer
8051 Microcontroller Timer8051 Microcontroller Timer
8051 Microcontroller Timer
anishgoel
 
8051 Timers
8051 Timers8051 Timers
AVRTIMER.pptx
AVRTIMER.pptxAVRTIMER.pptx
AVRTIMER.pptx
Pratik Gohel
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F Microcontrollers
Corrado Santoro
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
Syed Basharat Hussain
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.ppt
HebaEng
 
Microcontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptxMicrocontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptx
AmoghR3
 
PIC-Chapter_10.pptx
PIC-Chapter_10.pptxPIC-Chapter_10.pptx
PIC-Chapter_10.pptx
AliBzeih7
 
8051e
8051e8051e
8051e
rupalir
 
8051 ch9
8051 ch98051 ch9
8051 ch9
860540760
 
9 timer programming
9 timer programming9 timer programming
9 timer programming
Channabasappa Kudarihal
 
5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx
Rahultater4
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorial
Futura infotech
 
Uc
UcUc
Timers
TimersTimers
Timers
Vima Mali
 

Similar to Avr timers (20)

Timers
TimersTimers
Timers
 
Timers
TimersTimers
Timers
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 
timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptx
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
 
8051 Microcontroller Timer
8051 Microcontroller Timer8051 Microcontroller Timer
8051 Microcontroller Timer
 
8051 Timers
8051 Timers8051 Timers
8051 Timers
 
AVRTIMER.pptx
AVRTIMER.pptxAVRTIMER.pptx
AVRTIMER.pptx
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F Microcontrollers
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.ppt
 
Microcontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptxMicrocontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptx
 
PIC-Chapter_10.pptx
PIC-Chapter_10.pptxPIC-Chapter_10.pptx
PIC-Chapter_10.pptx
 
8051e
8051e8051e
8051e
 
8051 ch9
8051 ch98051 ch9
8051 ch9
 
9 timer programming
9 timer programming9 timer programming
9 timer programming
 
5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorial
 
Uc
UcUc
Uc
 
Timers
TimersTimers
Timers
 

Recently uploaded

Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
deepaannamalai16
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Vivekanand Anglo Vedic Academy
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 

Recently uploaded (20)

Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 

Avr timers

  • 1.
  • 2.   A timer is a circuit that counts. This is said from an electronics point of view. It is widely used and is a very important component of microprocessors and microcontrollers.
  • 3.     Timers are classified based on their mode of operation into synchronous and asynchronous timers. Synchronous timers count with respect to the clock while asynchronous timers depend upon the change in the input. They are also classified according to the maximum number they can count. Eg. 8 bit timer,16 bit timers etc Timers are easy to implement and are done using basic flip flop circuits .
  • 4.    They are used everywhere from modulating signals to implement digital clocks, gaming, phone/pc/tablet applications etc. Small projects like range finders etc will also use timers. IC’s such as 555 are readily available in the market and can be used to easily implement timers.
  • 5.   There are 3 timers in the AVR of which 2 are 8 bit timers the other one is a 16 bit timer. The timers found in the AVR or mostly in a microcontroller or processor is of synchronous type.
  • 6.   what do they mean? An 8 bit timer can count to 2 to the power of 8 and a 16 bit timer can count upto the 2 to the power of 16. Basically timers in the controller are registers and an 8 bit timer is a 8bit register and 16 bit timer is 16bit register.
  • 7.
  • 8.     The 8 bit timer starts counting from zero and goes upto 255.(that’s 256 counts) The 16 bit timer starts counting from zero and goes uptp 65535(65536 counts) Once the timer reaches the maximum value it “overflows” i.e. it restarts. ANALOGY
  • 9. For the timer to increase by one it takes one clock. That is the time period gives the time it takes for the circuit to increment the timer register by one
  • 10.  Consider a processor with 4MHz clock. We need a delay of 10ms. What will the timer count be??  What will the timer count be when the “required delay” is 50us  Which timer will you use ??  Can a delay of 100ms be directly implemented with one of AVR timers.
  • 11.  So to know the maximum delay you could get from the processor with a given clock substitute TOP value of that particular timer in the formula. Maximum delay for a 4MHz processor 16bit timer? 8 bit timer? 
  • 12.  What will we do for cases like the 100ms delay? The solution lies in reducing the frequency. How do we do that? That’s where tht prescaler comes into play.Do understand that we don’t actually reduce the frequency of the clock but we make the timer to behave as if it is in a reduced frequency.
  • 13.   Also note that there will be a trade-off between resolution and accuracy if you use a precaler. The prescaler is set by manipulating some bits.
  • 14.  Now get the timer count for 100ms using one of these prescalers.
  • 15.  PROBLEM STATEMENT: Make an LED flash for every 10ms with your atmega 8 internal oscillator featuring a 1MHz clock. Use only 8 bit timer TIMER
  • 16. Do the calculations first.  Without using a prescaler maximum delay=256us  Using a prescaler of 8 we’ll get a maximum delay =2048us  Using a prescaler of 64 max delay= 16.3ms our requirement of 10ms delay fits in this range. 
  • 17.   No that we know our prescaler we should now calculate our timer count. Substituting 10ms in the formula we ge timer count to be 155.25. we’ll round it out to 156 counts.
  • 18. The most important one is TCCR0 register which is the Timer/counter Control register for timer 0. First we start the clock and set the prescalar which is done by setting the three high lighted bits
  • 19. Thus we have to set bits CS01 and CS00 to 1. TCCR0|=1<<CS00|1<<CS01;
  • 20.  The register where the counting takes place is the TCNT0 register.It counts automatically and overflows and restarts again. We intitalise this too. TCNT0=0;
  • 21.  #include <avr/io.h>      void timer0_init() { // set up timer with no prescaling TCCR0 |= ((1 << CS00)|(1<<CS01));         // initialize counter TCNT0 = 0; } int main(void) { // connect led to pin PC0 DDRC |= (1 << 0);     // initialize timer timer0_init();
  • 22.            } // loop forever while(1) { // check if the timer count reaches 156 if (TCNT0 >= 156) { PORTC ^= (1 << 0); // toggles the led TCNT0 = 0; // reset counter } }
  • 23.   There’s an alternative way of doing this. Without using the prescalar or if the delay cannot be got with the given prescalers. Let’s do the same example without using prescalars.
  • 24.     We previously calculated that without prescaler maximum delay we get is 256us A point to be noted is that everytime the timer overflows an optional ISR is executed. We will use that interrupts to get the delay Let’s see how!
  • 25. 10ms/256us=39.0625  So when the timer overflows 39 times it would have counted 256*10^-6 *39= 9.984ms  The remaining time is 10ms- 9.984ms=16us  Now we substitute this again in the formula and we’ll get 15 as the timer count.  Thus at the 40th iteration and 15th tick we’ll achieve our 10ms delay. 
  • 26.   // global variable to count the number of overflows volatile uint8_t tot_overflow;                  // TIMER0 overflow interrupt service routine // called whenever TCNT0 overflows ISR(TIMER0_OVF_vect) { // keep a track of number of overflows tot_overflow++; } void timer0_init() { // set up timer with no prescaling TCCR0 |= (1 << CS00); sei(); TIMSK|=1<<TOIE0; // initialize counter TCNT0 = 0; }      int main(void) { // connect led to pin PC0 DDRC |= (1 << 0);      // initialize timer timer0_init();
  • 27.                 // loop forever while(1) { // check if no. of overflows = 39 if (tot_overflow >= 39) // NOTE: '>=' is used { // check if the timer count reaches 15 if (TCNT0 >= 15) { PORTC ^= (1 << 0); // toggles the led TCNT0 = 0; // reset counter tot_overflow = 0; // reset overflow counter } } } }
  • 28. The concept is the same with very minute differences. For example the counting register TCNT1 will be a 16 bit register. The control register is split into 2 8bit registers TCCR1A and TCCR1B. For normal mode of operation knowledge about TCCR1B will suffice
  • 29.
  • 30.  TIMSK-TIMER/COUNTER INTERRUPT MASK REGISTER This is used to enable the timer interrupt.TOIE0 is timer overflow interrupt enable for 0 and TOIE1 is for timer 1. It is necessary to set that bit inorder to enable the interrupt and execute the ISR
  • 31.  TIFR- timer interrupt flag register. We are interested in the TOV bit. The Timer overflow bit. This is set to 1 whenever the timer overflows . It is cleared after it overflows. The above two registers are shared by all the three timers.
  • 32.    CTC stands for Clear timer on Compare. Remember previous example with the 10ms with timer0? The comparing can be done by the processor itself and the corresponding instructions can be executed.
  • 33.
  • 34. Both CTC mode are almost the same except the fact that they store the compare value in different registers.
  • 35. The value to be compared is stored over here.
  • 36. The OCF1A/B bit is set to 1 when the value stored in OCR1A/B equals TCNT1 register.
  • 37. Set these bits if you are using the interrupts. ISR (TIMER1_COMPA_vect) { //to do }
  • 38. Those pins can be used to directly be set or cleared without any extra code.
  • 39. We go back to TCCR1A register.` Setting the above bits will enable the hardware mode.
  • 40.  FINAL coding lies in your hands. You can use the timer as you wish.  Hope you guys enjoyed it.  Image Courtesy: maxembedded.com