SlideShare a Scribd company logo
Introduction to Counter/Timers
INTRODUCTION:
• Counter/timer hardware is a crucial component of most embedded
systems. In some cases a timer is needed to measure elapsed time; in
others we want to count or time some external events. Here's a
primer on the hardware.
• The timers are the heart of automation. How we could take in an
input, perform mathematical functions on the data and, perform an
action. Timers give us an extra level of control by giving us not only
control over what happens but when it happens.
• Counter/timer hardware is a crucial component of most embedded
systems. In some cases, a timer measures elapsed time (counting
processor clock ticks). In others, we want to count or time external
events. The names counter and timer can be used interchangeably
when talking about the hardware. The difference in terminology has
more to do with how the hardware is used in a given application.
• A timer is a specialized type of clock which is used to
measure time intervals. A timer that counts from zero
upwards for measuring time elapsed is often called
a stopwatch. It is a device that counts down from a
specified time interval and used to generate a time delay,
for example, an hourglass is a timer.
• A counter is a device that stores (and sometimes displays)
the number of times a particular event or process
occurred, with respect to a clock signal. It is used to count
the events happening outside the microcontroller. In
electronics, counters can be implemented quite easily
using register-type circuits such as a flip-flop.
Difference between a Timer and a Counter
The Atmega328P has a total of
Three timer/counters named Timer/counter 0, Timer/counter 1, and Timer/counter 2.
The first and last of these are both 8-bit timer/counters and have a maximum value of
255, while Timer/Counter 1 is 16 bits and its maximum is 65,535
Figure 1 The timer shown consists of a loadable 8-bit count register, an input clock signal,
and an output signal. Software loads the count register with an initial value between 0x00
and 0xFF. Each subsequent transition of the input clock signal increments that value.
When the 8-bit count overflows, the output signal is asserted. The output signal may
thereby trigger an interrupt at the processor or set a bit that the processor can read.
To restart the timer, software reloads the count register with the same or a different initial
value. If a counter is an up counter, it counts up from the initial value toward 0xFF. A
down counter counts down, toward 0x00.A typical counter will have some means to start
the counter running once it is loaded, usually by setting a bit in a control register.
This is not shown in the figure. A real counter would generally also provide a way for the
processor to read the current value of the count register at any time, over the data bus.
THEORY OF OPERATION:
The clock source (from the internal clock or an external source) sends pulses to
the prescaler which divides the pulses by a determined amount. This input is
sent to the control circuit which increments the TCNTn register.
When the register hits its TOP value it resets to 0 and sends a TOVn (timer
overflow) signal which could be used to trigger an interrupt.
Unfortunately, the AVR timer does process time in hours, minutes or seconds
(what we are use to). The prescaler.
OCRn = [ (clock_speed / Prescaler_value) * Desired_time_in_Seconds ] - 1
Now for the bad news OCRn has to be a whole number. If you end up with a
decimal number it means that your desired timer will not be exact. The other
thing is that your number has to be able to fit into the register. So 255 for an 8bit
timers and 65535 for the 16bit timer.
Timer/Counter Registers
• TCNT0 – Timer/Counter Register (8-bit) (The actual timer value is stored here.)
• OCR0A – Output Compare Register A
• OCR0B – Output Compare Register B
• TCCR0A/B – Timer/Counter Control Registers(Prescaler is configured here.)
• TIMSK0 – Timer/Counter Interrupt Mask Register (To enable/disable timer
interrupts. )
• TOV interrupt Compare A&B interrupts
• TIFR0 – Timer/Counter Interrupt Flag Register (Indicates a pending timer
interrupt.)
• ICR1 - Input Capture Register (only for 16 bit timer)
• Normal Mode:
When the prescaler receives a pulse from a clock cycle and passes it onto the
Control Logic. The Control Logic increments the TCNTn register by 1. When TCNTn
hits the TOP (0xFF in the 8 bit timers and 0xFFFF in the 16 bit timer) it overflows to
0 and sets the TOVn bit in the TIFR register. Useful for generating interrupts every
N time units .Set TCNT0 to an initial value (255 – N) .
The problem with Normal Mode is that it is very hard to use for an exact interval,
because of this Normal Mode is only useful if you need a none-specific time
interval (say you don't care if it happens every 1 or 2 ms as long as it happens at
the same time each time) its nice and easy option. Because, of this limitation
many programmers choose to use CTC mode for their timers.
• CTC Mode :
CTC stands for "Clear Timer on Compare" and it does the following. When the
prescaler receives a pulse from a clock cycle and passes it onto the Control
Logic. The Control Logic increments the TCNTn register by 1. The TCNTn register is
compared to the OCRn register, when a compare match occurs the TOVn bit is set
in the TIFR register.
• OCF0A interrupt flag set when TCNT0 reset to 0
• Pin OC0A can be made to toggle when counter resets
• Generate output waveform
• Fast PWM Mode
Timer increments
Wraps around at 0xFF (3) or OCR0A (7)
Start again at 0
Pin OC0x generates waveform
Set (reset) when timer=0
Reset (set) when timer=OCR0x
Due to the high frequency of this mode is best used for DAC, fading LEDs,
rectification and Power regulation.
Clock select and timer frequency
• Different clock sources can be independently selected for
each timer.
• To calculate the timer frequency (e.g. 2Hz using timer1) you
need .
• CPU frequency: 16 MHz for Arduino UNO.
• Maximum timer counter value: 256 for 8bit timer, 65536
for 16bit.
• A prescaler value: either 256 or 1024.
• Divide CPU frequency with a prescaler (16000000 / 256 =
62500).
• Divide result through the desired frequency (62500 / 2Hz =
31250).
• Verify the result against the maximum timer counter value
(31250 < 65536 success). If fail, choose bigger prescaler
TIMER0 (8BIT PWM):
Unlike the ATmega8 the ATmega168/328's Timer0 does have a OCR0 register therefore it
is capable of running in normal and CTC mode.
TOV0 can generate a Timer Overflow interrupt. In order to activate the timer0 interrupts
you need to SET(1) the TOIE0 bit within the TIMSK register.
• Software:
ATmega168/328 Code:
// this code sets up a timer0 for 1ms @ 16Mhz clock cycle
// in order to function as a time delay at the begining of the main loop
// using no interrupts
#include <avr/io.h>
int main(void)
{
while (1)
{
// Set the Timer Mode to CTC
TCCR0A |= (1 << WGM01);
// Set the value that you want to count to
OCR0A = 0xF9;
// start the timer
TCCR0B |= (1 << CS01) | (1 << CS00);
// set prescaler to 64 and start the timer
while ( (TIFR0 & (1 << TOV0) ) > 0) // wait for the overflow event
{
}
TIFR0 |= (1 << TOV0);
// reset the overflow flag
TIMER1 (16BIT PWM):
• Timer/Counter1 is the big daddy of timers. It can run in Normal mode (0xFFFF) and 2 CTC
modes. The difference between the 2 CTC modes that mode 4 uses the OCR1A register for its
compare value and mode 12 uses the ICR1 register.
• In normal mode TOV1 can generate a Overflow interrupt. In order to activate the timer1
overflow interrupts you need to SET(1) the TOIE1 bit within the TIMSK1 register.
In CTC (mode 4) mode OCIF1A can generate an interrupt when it detects a compare match. In
order to activate the timer1 CTC interrupt SET(1) the OCF1A bit within the TIMSK1 register.
•
• In CTC (mode 12) mode TICIE1 can generate an interrupt when it detects a compare match. In
order to activate the timer1 CTC interrupt SET(1) the TICIE1 bit within the TIMSK1 register.
•
•
TIMER2 (8BIT PWM):
• Timer/Counter2 is the preferred timer among programmers
for short time delays because, its prescaler has the greatest
number of options . It can run in Normal mode or CTC
modes.
• In normal mode TOV2 can generate a Overflow
interrupt. In order to activate the timer1 overflow
interrupts you need to SET(1) the TOIE1 bit within the
TIMSK2 register.
In CTC mode OCIF2 can generate an interrupt when it
detects a compare match. In order to activate the timer1
CTC interrupt SET(1) the OCF2 bit within the TIMSK register.
timer counter (1).pptx
timer counter (1).pptx
timer counter (1).pptx

More Related Content

What's hot

8051 Timer
8051 Timer8051 Timer
8051 Timer
Ramasubbu .P
 
Adc interfacing
Adc interfacingAdc interfacing
Adc interfacing
Monica Gunjal
 
8051 Microcontroller ppt
8051 Microcontroller ppt8051 Microcontroller ppt
8051 Microcontroller ppt
Rahul Kumar
 
digital Counter
digital Counterdigital Counter
digital Counter
shamshad alam
 
Key board interfacing with 8051
Key board interfacing with 8051Key board interfacing with 8051
Key board interfacing with 8051
DominicHendry
 
8085-microprocessor
8085-microprocessor8085-microprocessor
8085-microprocessor
ATTO RATHORE
 
8085 Interfacing with I/O Devices or Memory
8085 Interfacing with I/O Devices or Memory8085 Interfacing with I/O Devices or Memory
8085 Interfacing with I/O Devices or Memory
Saumay Paul
 
8051 architecture
8051 architecture8051 architecture
8051 architecture
sb108ec
 
PLC ARCHITECTURE AND HARDWARE COMPONENTS
PLC ARCHITECTURE AND HARDWARE COMPONENTSPLC ARCHITECTURE AND HARDWARE COMPONENTS
PLC ARCHITECTURE AND HARDWARE COMPONENTS
Akshay Dhole
 
Addressing modes 8085
Addressing modes 8085Addressing modes 8085
Addressing modes 8085
ShivamSood22
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
vishalgohel12195
 
Interfacing with peripherals: analog to digital converters and digital to ana...
Interfacing with peripherals: analog to digital converters and digital to ana...Interfacing with peripherals: analog to digital converters and digital to ana...
Interfacing with peripherals: analog to digital converters and digital to ana...
NimeshSingh27
 
8086 micro processor
8086 micro processor8086 micro processor
8086 micro processor
Poojith Chowdhary
 
8051 Assembly Language Programming
8051 Assembly Language Programming8051 Assembly Language Programming
8051 Assembly Language Programming
Ravikumar Tiwari
 
8 interrupt 8051
8 interrupt 80518 interrupt 8051
8 interrupt 8051
daniemol
 
8051 memory
8051 memory8051 memory
8051 memory
Mayank Garg
 
LCD Interacing with 8051
LCD Interacing with 8051LCD Interacing with 8051
LCD Interacing with 8051
Sudhanshu Janwadkar
 
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
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051
Dr. AISHWARYA N
 
8085 interrupts
8085 interrupts8085 interrupts
8085 interruptsRam Babu
 

What's hot (20)

8051 Timer
8051 Timer8051 Timer
8051 Timer
 
Adc interfacing
Adc interfacingAdc interfacing
Adc interfacing
 
8051 Microcontroller ppt
8051 Microcontroller ppt8051 Microcontroller ppt
8051 Microcontroller ppt
 
digital Counter
digital Counterdigital Counter
digital Counter
 
Key board interfacing with 8051
Key board interfacing with 8051Key board interfacing with 8051
Key board interfacing with 8051
 
8085-microprocessor
8085-microprocessor8085-microprocessor
8085-microprocessor
 
8085 Interfacing with I/O Devices or Memory
8085 Interfacing with I/O Devices or Memory8085 Interfacing with I/O Devices or Memory
8085 Interfacing with I/O Devices or Memory
 
8051 architecture
8051 architecture8051 architecture
8051 architecture
 
PLC ARCHITECTURE AND HARDWARE COMPONENTS
PLC ARCHITECTURE AND HARDWARE COMPONENTSPLC ARCHITECTURE AND HARDWARE COMPONENTS
PLC ARCHITECTURE AND HARDWARE COMPONENTS
 
Addressing modes 8085
Addressing modes 8085Addressing modes 8085
Addressing modes 8085
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
 
Interfacing with peripherals: analog to digital converters and digital to ana...
Interfacing with peripherals: analog to digital converters and digital to ana...Interfacing with peripherals: analog to digital converters and digital to ana...
Interfacing with peripherals: analog to digital converters and digital to ana...
 
8086 micro processor
8086 micro processor8086 micro processor
8086 micro processor
 
8051 Assembly Language Programming
8051 Assembly Language Programming8051 Assembly Language Programming
8051 Assembly Language Programming
 
8 interrupt 8051
8 interrupt 80518 interrupt 8051
8 interrupt 8051
 
8051 memory
8051 memory8051 memory
8051 memory
 
LCD Interacing with 8051
LCD Interacing with 8051LCD Interacing with 8051
LCD Interacing with 8051
 
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
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051
 
8085 interrupts
8085 interrupts8085 interrupts
8085 interrupts
 

Similar to timer counter (1).pptx

AVRTIMER.pptx
AVRTIMER.pptxAVRTIMER.pptx
AVRTIMER.pptx
Pratik Gohel
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
Aarav Soni
 
8051 Timers
8051 Timers8051 Timers
Timers in Arduino
Timers in ArduinoTimers in Arduino
Timers in Arduino
VEDANSH SHARMA
 
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
 
Timer & Interrupt Atmega16
Timer & Interrupt Atmega16Timer & Interrupt Atmega16
Timer & Interrupt Atmega16
Ramadan Ramadan
 
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
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorial
Futura infotech
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
Syed Basharat Hussain
 
Timers
TimersTimers
Timers
Vima Mali
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
BASKARS53
 
UNIT 5 Interfacing and Mixed Signal Controller.pptx
UNIT 5 Interfacing and Mixed Signal Controller.pptxUNIT 5 Interfacing and Mixed Signal Controller.pptx
UNIT 5 Interfacing and Mixed Signal Controller.pptx
Gowrishankar C
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counterankit3991
 
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
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
rsamurti
 
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
naveen088888
 

Similar to timer counter (1).pptx (20)

AVRTIMER.pptx
AVRTIMER.pptxAVRTIMER.pptx
AVRTIMER.pptx
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 
8051e
8051e8051e
8051e
 
8051 Timers
8051 Timers8051 Timers
8051 Timers
 
Timers in Arduino
Timers in ArduinoTimers in Arduino
Timers in Arduino
 
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
 
Timer & Interrupt Atmega16
Timer & Interrupt Atmega16Timer & Interrupt Atmega16
Timer & Interrupt Atmega16
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 Microcontroller
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorial
 
8051 ch9
8051 ch98051 ch9
8051 ch9
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
 
Timers
TimersTimers
Timers
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
UNIT 5 Interfacing and Mixed Signal Controller.pptx
UNIT 5 Interfacing and Mixed Signal Controller.pptxUNIT 5 Interfacing and Mixed Signal Controller.pptx
UNIT 5 Interfacing and Mixed Signal Controller.pptx
 
Timers
TimersTimers
Timers
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
 
Avr timers
Avr timersAvr timers
Avr timers
 
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
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
 
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
 

Recently uploaded

CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 

Recently uploaded (20)

CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 

timer counter (1).pptx

  • 2. INTRODUCTION: • Counter/timer hardware is a crucial component of most embedded systems. In some cases a timer is needed to measure elapsed time; in others we want to count or time some external events. Here's a primer on the hardware. • The timers are the heart of automation. How we could take in an input, perform mathematical functions on the data and, perform an action. Timers give us an extra level of control by giving us not only control over what happens but when it happens. • Counter/timer hardware is a crucial component of most embedded systems. In some cases, a timer measures elapsed time (counting processor clock ticks). In others, we want to count or time external events. The names counter and timer can be used interchangeably when talking about the hardware. The difference in terminology has more to do with how the hardware is used in a given application.
  • 3. • A timer is a specialized type of clock which is used to measure time intervals. A timer that counts from zero upwards for measuring time elapsed is often called a stopwatch. It is a device that counts down from a specified time interval and used to generate a time delay, for example, an hourglass is a timer. • A counter is a device that stores (and sometimes displays) the number of times a particular event or process occurred, with respect to a clock signal. It is used to count the events happening outside the microcontroller. In electronics, counters can be implemented quite easily using register-type circuits such as a flip-flop.
  • 4. Difference between a Timer and a Counter
  • 5.
  • 6. The Atmega328P has a total of Three timer/counters named Timer/counter 0, Timer/counter 1, and Timer/counter 2. The first and last of these are both 8-bit timer/counters and have a maximum value of 255, while Timer/Counter 1 is 16 bits and its maximum is 65,535 Figure 1 The timer shown consists of a loadable 8-bit count register, an input clock signal, and an output signal. Software loads the count register with an initial value between 0x00 and 0xFF. Each subsequent transition of the input clock signal increments that value. When the 8-bit count overflows, the output signal is asserted. The output signal may thereby trigger an interrupt at the processor or set a bit that the processor can read. To restart the timer, software reloads the count register with the same or a different initial value. If a counter is an up counter, it counts up from the initial value toward 0xFF. A down counter counts down, toward 0x00.A typical counter will have some means to start the counter running once it is loaded, usually by setting a bit in a control register. This is not shown in the figure. A real counter would generally also provide a way for the processor to read the current value of the count register at any time, over the data bus.
  • 7. THEORY OF OPERATION: The clock source (from the internal clock or an external source) sends pulses to the prescaler which divides the pulses by a determined amount. This input is sent to the control circuit which increments the TCNTn register. When the register hits its TOP value it resets to 0 and sends a TOVn (timer overflow) signal which could be used to trigger an interrupt. Unfortunately, the AVR timer does process time in hours, minutes or seconds (what we are use to). The prescaler. OCRn = [ (clock_speed / Prescaler_value) * Desired_time_in_Seconds ] - 1
  • 8. Now for the bad news OCRn has to be a whole number. If you end up with a decimal number it means that your desired timer will not be exact. The other thing is that your number has to be able to fit into the register. So 255 for an 8bit timers and 65535 for the 16bit timer. Timer/Counter Registers • TCNT0 – Timer/Counter Register (8-bit) (The actual timer value is stored here.) • OCR0A – Output Compare Register A • OCR0B – Output Compare Register B • TCCR0A/B – Timer/Counter Control Registers(Prescaler is configured here.) • TIMSK0 – Timer/Counter Interrupt Mask Register (To enable/disable timer interrupts. ) • TOV interrupt Compare A&B interrupts • TIFR0 – Timer/Counter Interrupt Flag Register (Indicates a pending timer interrupt.) • ICR1 - Input Capture Register (only for 16 bit timer)
  • 9. • Normal Mode: When the prescaler receives a pulse from a clock cycle and passes it onto the Control Logic. The Control Logic increments the TCNTn register by 1. When TCNTn hits the TOP (0xFF in the 8 bit timers and 0xFFFF in the 16 bit timer) it overflows to 0 and sets the TOVn bit in the TIFR register. Useful for generating interrupts every N time units .Set TCNT0 to an initial value (255 – N) . The problem with Normal Mode is that it is very hard to use for an exact interval, because of this Normal Mode is only useful if you need a none-specific time interval (say you don't care if it happens every 1 or 2 ms as long as it happens at the same time each time) its nice and easy option. Because, of this limitation many programmers choose to use CTC mode for their timers. • CTC Mode : CTC stands for "Clear Timer on Compare" and it does the following. When the prescaler receives a pulse from a clock cycle and passes it onto the Control Logic. The Control Logic increments the TCNTn register by 1. The TCNTn register is compared to the OCRn register, when a compare match occurs the TOVn bit is set in the TIFR register. • OCF0A interrupt flag set when TCNT0 reset to 0 • Pin OC0A can be made to toggle when counter resets • Generate output waveform
  • 10. • Fast PWM Mode Timer increments Wraps around at 0xFF (3) or OCR0A (7) Start again at 0 Pin OC0x generates waveform Set (reset) when timer=0 Reset (set) when timer=OCR0x Due to the high frequency of this mode is best used for DAC, fading LEDs, rectification and Power regulation.
  • 11. Clock select and timer frequency • Different clock sources can be independently selected for each timer. • To calculate the timer frequency (e.g. 2Hz using timer1) you need . • CPU frequency: 16 MHz for Arduino UNO. • Maximum timer counter value: 256 for 8bit timer, 65536 for 16bit. • A prescaler value: either 256 or 1024. • Divide CPU frequency with a prescaler (16000000 / 256 = 62500). • Divide result through the desired frequency (62500 / 2Hz = 31250). • Verify the result against the maximum timer counter value (31250 < 65536 success). If fail, choose bigger prescaler
  • 12. TIMER0 (8BIT PWM): Unlike the ATmega8 the ATmega168/328's Timer0 does have a OCR0 register therefore it is capable of running in normal and CTC mode. TOV0 can generate a Timer Overflow interrupt. In order to activate the timer0 interrupts you need to SET(1) the TOIE0 bit within the TIMSK register.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. • Software: ATmega168/328 Code: // this code sets up a timer0 for 1ms @ 16Mhz clock cycle // in order to function as a time delay at the begining of the main loop // using no interrupts #include <avr/io.h> int main(void) { while (1) { // Set the Timer Mode to CTC TCCR0A |= (1 << WGM01); // Set the value that you want to count to OCR0A = 0xF9; // start the timer TCCR0B |= (1 << CS01) | (1 << CS00); // set prescaler to 64 and start the timer while ( (TIFR0 & (1 << TOV0) ) > 0) // wait for the overflow event { } TIFR0 |= (1 << TOV0); // reset the overflow flag
  • 19. • Timer/Counter1 is the big daddy of timers. It can run in Normal mode (0xFFFF) and 2 CTC modes. The difference between the 2 CTC modes that mode 4 uses the OCR1A register for its compare value and mode 12 uses the ICR1 register. • In normal mode TOV1 can generate a Overflow interrupt. In order to activate the timer1 overflow interrupts you need to SET(1) the TOIE1 bit within the TIMSK1 register. In CTC (mode 4) mode OCIF1A can generate an interrupt when it detects a compare match. In order to activate the timer1 CTC interrupt SET(1) the OCF1A bit within the TIMSK1 register. • • In CTC (mode 12) mode TICIE1 can generate an interrupt when it detects a compare match. In order to activate the timer1 CTC interrupt SET(1) the TICIE1 bit within the TIMSK1 register. • •
  • 20.
  • 21.
  • 22.
  • 23.
  • 25. • Timer/Counter2 is the preferred timer among programmers for short time delays because, its prescaler has the greatest number of options . It can run in Normal mode or CTC modes. • In normal mode TOV2 can generate a Overflow interrupt. In order to activate the timer1 overflow interrupts you need to SET(1) the TOIE1 bit within the TIMSK2 register. In CTC mode OCIF2 can generate an interrupt when it detects a compare match. In order to activate the timer1 CTC interrupt SET(1) the OCF2 bit within the TIMSK register.