SlideShare uma empresa Scribd logo
1 de 40
Baixar para ler offline




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

Mais conteúdo relacionado

Mais procurados

Micro controller 8051 Interrupts
Micro controller 8051 InterruptsMicro controller 8051 Interrupts
Micro controller 8051 Interruptsdharmesh nakum
 
Pic microcontroller architecture
Pic microcontroller architecturePic microcontroller architecture
Pic microcontroller architectureDominicHendry
 
Interrupts at AVR
Interrupts at AVRInterrupts at AVR
Interrupts at AVRHamdy Fouad
 
PIC timer programming
PIC timer programmingPIC timer programming
PIC timer programmingAkash Puri
 
Programming 8051 Timers
Programming 8051 Timers Programming 8051 Timers
Programming 8051 Timers ViVek Patel
 
Counters &amp; time delay
Counters &amp; time delayCounters &amp; time delay
Counters &amp; time delayHemant Chetwani
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil KawareProf. Swapnil V. Kaware
 
PIC-18 Microcontroller
PIC-18 MicrocontrollerPIC-18 Microcontroller
PIC-18 MicrocontrollerASHISH RANJAN
 
Interfacing adc
Interfacing adcInterfacing adc
Interfacing adcPRADEEP
 
8 interrupt 8051
8 interrupt 80518 interrupt 8051
8 interrupt 8051daniemol
 
Microprocessor & Microcontoller short questions with answers
Microprocessor & Microcontoller short questions with answersMicroprocessor & Microcontoller short questions with answers
Microprocessor & Microcontoller short questions with answersMathankumar S
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 prsamurti
 
Introduction to Avr Microcontrollers
Introduction to Avr MicrocontrollersIntroduction to Avr Microcontrollers
Introduction to Avr MicrocontrollersMohamed Tarek
 
8051 Microcontroller Timer
8051 Microcontroller Timer8051 Microcontroller Timer
8051 Microcontroller Timeranishgoel
 

Mais procurados (20)

Micro controller 8051 Interrupts
Micro controller 8051 InterruptsMicro controller 8051 Interrupts
Micro controller 8051 Interrupts
 
Pic microcontroller architecture
Pic microcontroller architecturePic microcontroller architecture
Pic microcontroller architecture
 
Interrupts at AVR
Interrupts at AVRInterrupts at AVR
Interrupts at AVR
 
PIC timer programming
PIC timer programmingPIC timer programming
PIC timer programming
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
 
8051 interfacing
8051 interfacing8051 interfacing
8051 interfacing
 
Programming 8051 Timers
Programming 8051 Timers Programming 8051 Timers
Programming 8051 Timers
 
Counters &amp; time delay
Counters &amp; time delayCounters &amp; time delay
Counters &amp; time delay
 
ARM Processors
ARM ProcessorsARM Processors
ARM Processors
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware
 
PIC-18 Microcontroller
PIC-18 MicrocontrollerPIC-18 Microcontroller
PIC-18 Microcontroller
 
Arm instruction set
Arm instruction setArm instruction set
Arm instruction set
 
Interfacing adc
Interfacing adcInterfacing adc
Interfacing adc
 
8 interrupt 8051
8 interrupt 80518 interrupt 8051
8 interrupt 8051
 
8051 serial communication-UART
8051 serial communication-UART8051 serial communication-UART
8051 serial communication-UART
 
8051 timers
8051 timers8051 timers
8051 timers
 
Microprocessor & Microcontoller short questions with answers
Microprocessor & Microcontoller short questions with answersMicroprocessor & Microcontoller short questions with answers
Microprocessor & Microcontoller short questions with answers
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
 
Introduction to Avr Microcontrollers
Introduction to Avr MicrocontrollersIntroduction to Avr Microcontrollers
Introduction to Avr Microcontrollers
 
8051 Microcontroller Timer
8051 Microcontroller Timer8051 Microcontroller Timer
8051 Microcontroller Timer
 

Destaque

Timer & Interrupt Atmega16
Timer & Interrupt Atmega16Timer & Interrupt Atmega16
Timer & Interrupt Atmega16Ramadan Ramadan
 
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 TipsMaryanne Keeney Wetherald
 
Bell ringers
Bell ringersBell ringers
Bell ringersParks1993
 
Developing an avr microcontroller system
Developing an avr microcontroller systemDeveloping an avr microcontroller system
Developing an avr microcontroller systemnugnugmacmac
 
Chem bell ringers week 1
Chem bell ringers week 1Chem bell ringers week 1
Chem bell ringers week 1Paul Cummings
 
Bell ringer 2
Bell ringer 2Bell ringer 2
Bell ringer 2spoldon
 
multirate signal processing for speech
multirate signal processing for speechmultirate signal processing for speech
multirate signal processing for speechRudra Prasad Maiti
 
Student centered learning presentation copy
Student centered learning presentation copyStudent centered learning presentation copy
Student centered learning presentation copytrcash
 
Multirate digital signal processing
Multirate digital signal processingMultirate digital signal processing
Multirate digital signal processingMOHAN MOHAN
 
Mp3 player project presentation
Mp3 player project presentationMp3 player project presentation
Mp3 player project presentationAntonio Mondragon
 

Destaque (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
 

Semelhante a Timers in Microcontrollers: Classification, Operation & Applications

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).pptxSujalKumar73
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counterankit3991
 
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 programmingMohamed Ali
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersCorrado Santoro
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptHebaEng
 
Microcontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptxMicrocontrollers-MODULE4.pptx
Microcontrollers-MODULE4.pptxAmoghR3
 
PIC-Chapter_10.pptx
PIC-Chapter_10.pptxPIC-Chapter_10.pptx
PIC-Chapter_10.pptxAliBzeih7
 
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.pptxRahultater4
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorialFutura infotech
 
Unit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxUnit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxnaveen088888
 

Semelhante a Timers in Microcontrollers: Classification, Operation & Applications (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 Timers
8051 Timers8051 Timers
8051 Timers
 
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
 
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
 
Unit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxUnit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptx
 

Último

Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Último (20)

Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Timers in Microcontrollers: Classification, Operation & Applications

  • 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