SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
Timers/Counters in Atmega328
(Lecture-15)
R S Ananda Murthy
Associate Professor
Department of Electrical & Electronics Engineering,
Sri Jayachamarajendra College of Engineering,
Mysore 570 006
R S Ananda Murthy Timers/Counters in Atmega328
Timer/Counters in Atmega328P
Timer/Counter0 – 8 bit.
Timer/Counter1 – 16 bit.
Timer/Counter2 – 8 bit.
Timer/Counter can be used to cause time delay, to count
events, or to generate PWM signal.
R S Ananda Murthy Timers/Counters in Atmega328
General Features of Timer/Counter
Counter/Timer Register
Oscillator
External
Source
Counter/Timer Flag Interrupt
Prescaler
Clk
External Source is fed as Clk input to Timer/Counter
Register when used as counter.
Oscillator is fed as Clk input to Timer/Counter Register
when used as timer.
Prescaler divides the oscillator frequency by a specific
constant.
When Timer/Counter register reaches a specific value,a
flag will be set and optionally an interrupt can be issued.
R S Ananda Murthy Timers/Counters in Atmega328
Features of 16-bit Timer/Counter1 with PWM
Permits 16-bit PWM
Two independent Output Compare Units
Double Buffered Output Compare Registers
One Input Capture Unit
Input Capture Noise Canceler
Clear Timer on Capture Match (Auto Reload)
Glitch-free, Phase Correct Pulse Width Modulator
Variable PWM period
Frequency Generator
External Event Counter
Four independent interrupt sources (TOV1, OCF1A,
OCF1B, ICF1)
R S Ananda Murthy Timers/Counters in Atmega328
Timer/Counter1 Block Diagram
R S Ananda Murthy Timers/Counters in Atmega328
Registers Pertaining to Timer/Counter1
Timer/Counter1 High and Low bytes
Output Compare Register1A High and Low Bytes
Output Compare Register1B High and Low Bytes
R S Ananda Murthy Timers/Counters in Atmega328
Registers Pertaining to Timer/Counter1
Timer/Counter Control Register1A
Timer/Counter Control Register1B
Timer/Counter Control Register1C
R S Ananda Murthy Timers/Counters in Atmega328
Registers Pertaining to Timer/Counter1
Timer/Counter1 Interrupt Mask Register
Timer/Counter1 Interrupt Flag Register1
By setting appropriate bits in TIMSK1 the corresponding
interrupt can be enabled.
When ISR is executed corresponding to a timer/counter1
event, the corresponding flag is cleared in the TIFR1.
By setting appropriate bits in TIFR1 the corresponding flag
can be cleared.
R S Ananda Murthy Timers/Counters in Atmega328
Timer/Counter1 Mode 0 and Mode 4 Interrupt Vectors
ISR Name to be used in C Source of Interrupt
TIMER1_CAPT_vect Timer/Counter1 Capture Event
TIMER1_COMPA_vect Timer/Counter1 Compare Match A
TIMER1_COMPB_vect Timer/Counter1 Compare Match B
TIMER1_OVF_vect Timer/Counter1 Overflow
R S Ananda Murthy Timers/Counters in Atmega328
Modes of Operation of Timer/Counter1
Normal Mode (also known as Mode 0)
Clear Timer on Compare (CTC) Match Mode (also known
as Mode 4 and Mode 12)
In Mode 4 comparison is done with OCR1A.
In Mode 12 comparison is done with ICR1.
Fast PWM Mode
Phase Correct PWM Mode
Phase and Frequency Correct PWM Mode
The last three modes are PWM modes. Here we discuss only
Normal Mode and CTC mode. Mode selection can be made by
setting appropriate bits in TCCR1A and TCCR1B registers.
R S Ananda Murthy Timers/Counters in Atmega328
Setting Timer/Counter1 Clock Source and Prescaler
CS12 CS11 CS10 Effect
0 0 0 No clock (Timer/Counter stops)
0 0 1 clk (no prescaling)
0 1 0 clk/8
0 1 1 clk/64
1 0 0 clk/256
1 0 1 clk/1024
1 1 0 Ext. clk on T1 pin, falling edge
1 1 1 Ext. clk on T1 pin, rising edge
CS12, CS11 and CS10 bits are in TCCR1B register.
Timer/Counter1 is inactive when no clock source is
selected.
Timer/Counter1 starts when clock source is set.
R S Ananda Murthy Timers/Counters in Atmega328
Normal Mode (Mode 0)
This mode can be selected by making WGM13 = 0,
WGM12 = 0 in TCCR1B and WGM11 = 0, WGM10 = 0 in
TCCR1A.
In this mode there is no comparison with either ICR1 or
OCR1A.
In this mode the timer counts up for every clock cycle until
it reaches $FFFF (also referred to as MAX) and then
resets to $0000 in the next clock cycle.
When the timer value changes from $FFFF to $0000, the
TOV1 flag is set.
An interrupt can be generated when TOV1 flag is set by
setting TOIE1 bit in TIMSK1.
R S Ananda Murthy Timers/Counters in Atmega328
CTC Mode (Mode 4)
This mode can be selected by making WGM13 = 0,
WGM12 = 1 in TCCR1B and WGM11 = 0, WGM10 = 0 in
TCCR1A.
In this mode the timer counts up until it reaches the value
stored in OCR1A (also referred to as TOP) and then resets
from TOP to $0000.
When the timer resets from TOP to $0000, the OCF1A flag
will be set.
An interrupt can be generated when OCF1A flag is set by
setting OCIE1A bit in TIMSK1.
R S Ananda Murthy Timers/Counters in Atmega328
Example C Statements to Set Up Timer1
R S Ananda Murthy Timers/Counters in Atmega328
Maximum Delay using Timer 1 in Mode 0
Calculate maximum time delay that can be realized using Timer
1 in Mode 0 with prescaler value of 1024. Assume that MCU
clock frequency is 16 MHz.
Frequency of clock signal applied to Timer 1 is given by
fclk =
16×106
1024
= 15625 Hz
Therefore maximum delay possible is
tdelay(max) =
65536
15625
= 4.1943 s
R S Ananda Murthy Timers/Counters in Atmega328
Timer1 Initial Count in Mode 0 for a Given Delay
Assuming MCU clock frequency to be 16 MHz and prescaler
value of 1024, find the initial number N to be loaded in TCNT1H
and TCNT1L to get a time delay close to 1 second in Mode 0.
No. of steps required to cause a delay of 1 s is given by
n = 15625×1 = 15625 = 65535−N +1
Therefore
N = (49911)10 = 0xC2F7
So, TCNT1H = C2 and TCNT2L = F7.
R S Ananda Murthy Timers/Counters in Atmega328
Using Timer1 to cause Delay
Write C code to setup Timer1 (16-bit timer) of Atmega328P
MCU to give a 1 s time delay assuming that the MCU is
operating at 16 MHz clock with prescaler set to 1024. Use timer
interrupt to blink an LED connected to an output port line. Show
timer calculations and hardware connections clearly.
The number to be loaded in Output Compare Register1
(OCR1) is given by
OCR1 =
16×106
1024
×1 −1 = (15624)10 = (3D08)16
R S Ananda Murthy Timers/Counters in Atmega328
Timer/Counter1 Program Using Interrupt
R S Ananda Murthy Timers/Counters in Atmega328
Using Timer0 as Counter
Assuming that a 1 Hz clock pulse is fed into pin T0 of
Atmega328P MCU, write C code to use the TOV0 flag to extend
Timer0 to a 16-bit counter and display the counter on PORTC
and PORTD.
R S Ananda Murthy Timers/Counters in Atmega328
License
This work is licensed under a
Creative Commons Attribution 4.0 International License.
R S Ananda Murthy Timers/Counters in Atmega328

Contenu connexe

Tendances (20)

8051 Microcontroller I/O ports
8051 Microcontroller I/O ports8051 Microcontroller I/O ports
8051 Microcontroller I/O ports
 
I/O Ports
I/O Ports I/O Ports
I/O Ports
 
Interrupt Handling with LPC2148
Interrupt Handling with LPC2148Interrupt Handling with LPC2148
Interrupt Handling with LPC2148
 
Question Bank microcontroller 8051
Question Bank microcontroller 8051Question Bank microcontroller 8051
Question Bank microcontroller 8051
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
 
Registers-shift register
Registers-shift registerRegisters-shift register
Registers-shift register
 
PIC 16F877A by PARTHIBAN. S.
PIC 16F877A   by PARTHIBAN. S.PIC 16F877A   by PARTHIBAN. S.
PIC 16F877A by PARTHIBAN. S.
 
ARM Architecture
ARM ArchitectureARM Architecture
ARM Architecture
 
Adc interfacing
Adc interfacingAdc interfacing
Adc interfacing
 
microcontroller vs microprocessor
microcontroller vs microprocessormicrocontroller vs microprocessor
microcontroller vs microprocessor
 
ARM Processor
ARM ProcessorARM Processor
ARM Processor
 
MPMC Microprocessor
MPMC MicroprocessorMPMC Microprocessor
MPMC Microprocessor
 
ARM Exception and interrupts
ARM Exception and interrupts ARM Exception and interrupts
ARM Exception and interrupts
 
ARM Micro-controller
ARM Micro-controllerARM Micro-controller
ARM Micro-controller
 
Programmable Peripheral Interface 8255
 Programmable Peripheral Interface   8255 Programmable Peripheral Interface   8255
Programmable Peripheral Interface 8255
 
Pic microcontroller architecture
Pic microcontroller architecturePic microcontroller architecture
Pic microcontroller architecture
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
 
Introduction to Avr Microcontrollers
Introduction to Avr MicrocontrollersIntroduction to Avr Microcontrollers
Introduction to Avr Microcontrollers
 
Adc and dac
Adc and dacAdc and dac
Adc and dac
 
MICROCONTROLLER 8051
MICROCONTROLLER 8051MICROCONTROLLER 8051
MICROCONTROLLER 8051
 

En vedette

L12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 pL12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 prsamurti
 
L11 assembly-language-programming-of-atmega328 p
L11 assembly-language-programming-of-atmega328 pL11 assembly-language-programming-of-atmega328 p
L11 assembly-language-programming-of-atmega328 prsamurti
 
L10 assembly-language-programming-of-atmega328 p
L10 assembly-language-programming-of-atmega328 pL10 assembly-language-programming-of-atmega328 p
L10 assembly-language-programming-of-atmega328 prsamurti
 
L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2rsamurti
 
L16 usart-atmega328 p
L16 usart-atmega328 pL16 usart-atmega328 p
L16 usart-atmega328 prsamurti
 
L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1rsamurti
 
L1 intro-to-mpu-mcu
L1 intro-to-mpu-mcuL1 intro-to-mpu-mcu
L1 intro-to-mpu-mcursamurti
 
Introduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeXIntroduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeXrsamurti
 
L6 primary-memory
L6 primary-memoryL6 primary-memory
L6 primary-memoryrsamurti
 
L5 data-parallel-computers
L5 data-parallel-computersL5 data-parallel-computers
L5 data-parallel-computersrsamurti
 
Transformers
TransformersTransformers
Transformersrsamurti
 
L4 speeding-up-execution
L4 speeding-up-executionL4 speeding-up-execution
L4 speeding-up-executionrsamurti
 
L3 instruction-execution-steps
L3 instruction-execution-stepsL3 instruction-execution-steps
L3 instruction-execution-stepsrsamurti
 
Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2rsamurti
 
L7 starting-to-use-mcu
L7 starting-to-use-mcuL7 starting-to-use-mcu
L7 starting-to-use-mcursamurti
 
Lecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power ElectronicsLecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power Electronicsrsamurti
 
Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4rsamurti
 
Lecture-2 : Applications of Power Electronics
Lecture-2 : Applications of Power ElectronicsLecture-2 : Applications of Power Electronics
Lecture-2 : Applications of Power Electronicsrsamurti
 
L14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 pL14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 prsamurti
 
Three phase-circuits
Three phase-circuitsThree phase-circuits
Three phase-circuitsrsamurti
 

En vedette (20)

L12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 pL12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 p
 
L11 assembly-language-programming-of-atmega328 p
L11 assembly-language-programming-of-atmega328 pL11 assembly-language-programming-of-atmega328 p
L11 assembly-language-programming-of-atmega328 p
 
L10 assembly-language-programming-of-atmega328 p
L10 assembly-language-programming-of-atmega328 pL10 assembly-language-programming-of-atmega328 p
L10 assembly-language-programming-of-atmega328 p
 
L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2
 
L16 usart-atmega328 p
L16 usart-atmega328 pL16 usart-atmega328 p
L16 usart-atmega328 p
 
L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1
 
L1 intro-to-mpu-mcu
L1 intro-to-mpu-mcuL1 intro-to-mpu-mcu
L1 intro-to-mpu-mcu
 
Introduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeXIntroduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeX
 
L6 primary-memory
L6 primary-memoryL6 primary-memory
L6 primary-memory
 
L5 data-parallel-computers
L5 data-parallel-computersL5 data-parallel-computers
L5 data-parallel-computers
 
Transformers
TransformersTransformers
Transformers
 
L4 speeding-up-execution
L4 speeding-up-executionL4 speeding-up-execution
L4 speeding-up-execution
 
L3 instruction-execution-steps
L3 instruction-execution-stepsL3 instruction-execution-steps
L3 instruction-execution-steps
 
Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2
 
L7 starting-to-use-mcu
L7 starting-to-use-mcuL7 starting-to-use-mcu
L7 starting-to-use-mcu
 
Lecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power ElectronicsLecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power Electronics
 
Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4
 
Lecture-2 : Applications of Power Electronics
Lecture-2 : Applications of Power ElectronicsLecture-2 : Applications of Power Electronics
Lecture-2 : Applications of Power Electronics
 
L14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 pL14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 p
 
Three phase-circuits
Three phase-circuitsThree phase-circuits
Three phase-circuits
 

Similaire à L15 timers-counters-in-atmega328 p

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
 
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
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerJay Makwana
 
MICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptMICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptreemasajin1
 
Micro c lab7(timers)
Micro c lab7(timers)Micro c lab7(timers)
Micro c lab7(timers)Mashood
 
What isn’t told about timers in stm32 application
What isn’t told about timers in stm32 applicationWhat isn’t told about timers in stm32 application
What isn’t told about timers in stm32 applicationOmar BOUZOURRAA
 
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2KanchanPatil34
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and CountersShreyans Pathak
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming IIOmar Sanchez
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming IIOmar Sanchez
 

Similaire à L15 timers-counters-in-atmega328 p (20)

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
 
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
 
Lpc 1768 timers
Lpc 1768 timersLpc 1768 timers
Lpc 1768 timers
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 Microcontroller
 
MICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptMICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.ppt
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
 
Timers in Arduino
Timers in ArduinoTimers in Arduino
Timers in Arduino
 
8051 ch9
8051 ch98051 ch9
8051 ch9
 
8051 Timers
8051 Timers8051 Timers
8051 Timers
 
Micro c lab7(timers)
Micro c lab7(timers)Micro c lab7(timers)
Micro c lab7(timers)
 
9 timer programming
9 timer programming9 timer programming
9 timer programming
 
4.Timer_1.ppt
4.Timer_1.ppt4.Timer_1.ppt
4.Timer_1.ppt
 
AVRTIMER.pptx
AVRTIMER.pptxAVRTIMER.pptx
AVRTIMER.pptx
 
What isn’t told about timers in stm32 application
What isn’t told about timers in stm32 applicationWhat isn’t told about timers in stm32 application
What isn’t told about timers in stm32 application
 
UNIT-5.ppt
UNIT-5.pptUNIT-5.ppt
UNIT-5.ppt
 
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 

Plus de rsamurti

EE110-elementary-circuit-and-network-theory-(a)
EE110-elementary-circuit-and-network-theory-(a)EE110-elementary-circuit-and-network-theory-(a)
EE110-elementary-circuit-and-network-theory-(a)rsamurti
 
Trends in-power-electronics
Trends in-power-electronicsTrends in-power-electronics
Trends in-power-electronicsrsamurti
 
Synchronous generators
Synchronous generatorsSynchronous generators
Synchronous generatorsrsamurti
 
L2 types-of-computers
L2 types-of-computersL2 types-of-computers
L2 types-of-computersrsamurti
 
Lecture-4 : Semiconductor Power Switching Devices-1
Lecture-4 : Semiconductor Power Switching Devices-1Lecture-4 : Semiconductor Power Switching Devices-1
Lecture-4 : Semiconductor Power Switching Devices-1rsamurti
 
Lecture-1 : Introduction to Power Electronics
Lecture-1 : Introduction to Power ElectronicsLecture-1 : Introduction to Power Electronics
Lecture-1 : Introduction to Power Electronicsrsamurti
 

Plus de rsamurti (6)

EE110-elementary-circuit-and-network-theory-(a)
EE110-elementary-circuit-and-network-theory-(a)EE110-elementary-circuit-and-network-theory-(a)
EE110-elementary-circuit-and-network-theory-(a)
 
Trends in-power-electronics
Trends in-power-electronicsTrends in-power-electronics
Trends in-power-electronics
 
Synchronous generators
Synchronous generatorsSynchronous generators
Synchronous generators
 
L2 types-of-computers
L2 types-of-computersL2 types-of-computers
L2 types-of-computers
 
Lecture-4 : Semiconductor Power Switching Devices-1
Lecture-4 : Semiconductor Power Switching Devices-1Lecture-4 : Semiconductor Power Switching Devices-1
Lecture-4 : Semiconductor Power Switching Devices-1
 
Lecture-1 : Introduction to Power Electronics
Lecture-1 : Introduction to Power ElectronicsLecture-1 : Introduction to Power Electronics
Lecture-1 : Introduction to Power Electronics
 

Dernier

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projectssmsksolar
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086anil_gaur
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 

Dernier (20)

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 

L15 timers-counters-in-atmega328 p

  • 1. Timers/Counters in Atmega328 (Lecture-15) R S Ananda Murthy Associate Professor Department of Electrical & Electronics Engineering, Sri Jayachamarajendra College of Engineering, Mysore 570 006 R S Ananda Murthy Timers/Counters in Atmega328
  • 2. Timer/Counters in Atmega328P Timer/Counter0 – 8 bit. Timer/Counter1 – 16 bit. Timer/Counter2 – 8 bit. Timer/Counter can be used to cause time delay, to count events, or to generate PWM signal. R S Ananda Murthy Timers/Counters in Atmega328
  • 3. General Features of Timer/Counter Counter/Timer Register Oscillator External Source Counter/Timer Flag Interrupt Prescaler Clk External Source is fed as Clk input to Timer/Counter Register when used as counter. Oscillator is fed as Clk input to Timer/Counter Register when used as timer. Prescaler divides the oscillator frequency by a specific constant. When Timer/Counter register reaches a specific value,a flag will be set and optionally an interrupt can be issued. R S Ananda Murthy Timers/Counters in Atmega328
  • 4. Features of 16-bit Timer/Counter1 with PWM Permits 16-bit PWM Two independent Output Compare Units Double Buffered Output Compare Registers One Input Capture Unit Input Capture Noise Canceler Clear Timer on Capture Match (Auto Reload) Glitch-free, Phase Correct Pulse Width Modulator Variable PWM period Frequency Generator External Event Counter Four independent interrupt sources (TOV1, OCF1A, OCF1B, ICF1) R S Ananda Murthy Timers/Counters in Atmega328
  • 5. Timer/Counter1 Block Diagram R S Ananda Murthy Timers/Counters in Atmega328
  • 6. Registers Pertaining to Timer/Counter1 Timer/Counter1 High and Low bytes Output Compare Register1A High and Low Bytes Output Compare Register1B High and Low Bytes R S Ananda Murthy Timers/Counters in Atmega328
  • 7. Registers Pertaining to Timer/Counter1 Timer/Counter Control Register1A Timer/Counter Control Register1B Timer/Counter Control Register1C R S Ananda Murthy Timers/Counters in Atmega328
  • 8. Registers Pertaining to Timer/Counter1 Timer/Counter1 Interrupt Mask Register Timer/Counter1 Interrupt Flag Register1 By setting appropriate bits in TIMSK1 the corresponding interrupt can be enabled. When ISR is executed corresponding to a timer/counter1 event, the corresponding flag is cleared in the TIFR1. By setting appropriate bits in TIFR1 the corresponding flag can be cleared. R S Ananda Murthy Timers/Counters in Atmega328
  • 9. Timer/Counter1 Mode 0 and Mode 4 Interrupt Vectors ISR Name to be used in C Source of Interrupt TIMER1_CAPT_vect Timer/Counter1 Capture Event TIMER1_COMPA_vect Timer/Counter1 Compare Match A TIMER1_COMPB_vect Timer/Counter1 Compare Match B TIMER1_OVF_vect Timer/Counter1 Overflow R S Ananda Murthy Timers/Counters in Atmega328
  • 10. Modes of Operation of Timer/Counter1 Normal Mode (also known as Mode 0) Clear Timer on Compare (CTC) Match Mode (also known as Mode 4 and Mode 12) In Mode 4 comparison is done with OCR1A. In Mode 12 comparison is done with ICR1. Fast PWM Mode Phase Correct PWM Mode Phase and Frequency Correct PWM Mode The last three modes are PWM modes. Here we discuss only Normal Mode and CTC mode. Mode selection can be made by setting appropriate bits in TCCR1A and TCCR1B registers. R S Ananda Murthy Timers/Counters in Atmega328
  • 11. Setting Timer/Counter1 Clock Source and Prescaler CS12 CS11 CS10 Effect 0 0 0 No clock (Timer/Counter stops) 0 0 1 clk (no prescaling) 0 1 0 clk/8 0 1 1 clk/64 1 0 0 clk/256 1 0 1 clk/1024 1 1 0 Ext. clk on T1 pin, falling edge 1 1 1 Ext. clk on T1 pin, rising edge CS12, CS11 and CS10 bits are in TCCR1B register. Timer/Counter1 is inactive when no clock source is selected. Timer/Counter1 starts when clock source is set. R S Ananda Murthy Timers/Counters in Atmega328
  • 12. Normal Mode (Mode 0) This mode can be selected by making WGM13 = 0, WGM12 = 0 in TCCR1B and WGM11 = 0, WGM10 = 0 in TCCR1A. In this mode there is no comparison with either ICR1 or OCR1A. In this mode the timer counts up for every clock cycle until it reaches $FFFF (also referred to as MAX) and then resets to $0000 in the next clock cycle. When the timer value changes from $FFFF to $0000, the TOV1 flag is set. An interrupt can be generated when TOV1 flag is set by setting TOIE1 bit in TIMSK1. R S Ananda Murthy Timers/Counters in Atmega328
  • 13. CTC Mode (Mode 4) This mode can be selected by making WGM13 = 0, WGM12 = 1 in TCCR1B and WGM11 = 0, WGM10 = 0 in TCCR1A. In this mode the timer counts up until it reaches the value stored in OCR1A (also referred to as TOP) and then resets from TOP to $0000. When the timer resets from TOP to $0000, the OCF1A flag will be set. An interrupt can be generated when OCF1A flag is set by setting OCIE1A bit in TIMSK1. R S Ananda Murthy Timers/Counters in Atmega328
  • 14. Example C Statements to Set Up Timer1 R S Ananda Murthy Timers/Counters in Atmega328
  • 15. Maximum Delay using Timer 1 in Mode 0 Calculate maximum time delay that can be realized using Timer 1 in Mode 0 with prescaler value of 1024. Assume that MCU clock frequency is 16 MHz. Frequency of clock signal applied to Timer 1 is given by fclk = 16×106 1024 = 15625 Hz Therefore maximum delay possible is tdelay(max) = 65536 15625 = 4.1943 s R S Ananda Murthy Timers/Counters in Atmega328
  • 16. Timer1 Initial Count in Mode 0 for a Given Delay Assuming MCU clock frequency to be 16 MHz and prescaler value of 1024, find the initial number N to be loaded in TCNT1H and TCNT1L to get a time delay close to 1 second in Mode 0. No. of steps required to cause a delay of 1 s is given by n = 15625×1 = 15625 = 65535−N +1 Therefore N = (49911)10 = 0xC2F7 So, TCNT1H = C2 and TCNT2L = F7. R S Ananda Murthy Timers/Counters in Atmega328
  • 17. Using Timer1 to cause Delay Write C code to setup Timer1 (16-bit timer) of Atmega328P MCU to give a 1 s time delay assuming that the MCU is operating at 16 MHz clock with prescaler set to 1024. Use timer interrupt to blink an LED connected to an output port line. Show timer calculations and hardware connections clearly. The number to be loaded in Output Compare Register1 (OCR1) is given by OCR1 = 16×106 1024 ×1 −1 = (15624)10 = (3D08)16 R S Ananda Murthy Timers/Counters in Atmega328
  • 18. Timer/Counter1 Program Using Interrupt R S Ananda Murthy Timers/Counters in Atmega328
  • 19. Using Timer0 as Counter Assuming that a 1 Hz clock pulse is fed into pin T0 of Atmega328P MCU, write C code to use the TOV0 flag to extend Timer0 to a 16-bit counter and display the counter on PORTC and PORTD. R S Ananda Murthy Timers/Counters in Atmega328
  • 20. License This work is licensed under a Creative Commons Attribution 4.0 International License. R S Ananda Murthy Timers/Counters in Atmega328