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

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
Prof. Swapnil V. Kaware
 

Tendances (20)

I2C Protocol
I2C ProtocolI2C Protocol
I2C Protocol
 
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
 
Pic 18 microcontroller
Pic 18 microcontrollerPic 18 microcontroller
Pic 18 microcontroller
 
L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 
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
 
ARDUINO AND ITS PIN CONFIGURATION
 ARDUINO AND ITS PIN  CONFIGURATION ARDUINO AND ITS PIN  CONFIGURATION
ARDUINO AND ITS PIN CONFIGURATION
 
STM32 MCU Family
STM32 MCU FamilySTM32 MCU Family
STM32 MCU Family
 
Intel 8051 - pin description
Intel 8051  - pin descriptionIntel 8051  - pin description
Intel 8051 - pin description
 
Microcontroller 8051 and its interfacing
Microcontroller 8051 and its interfacingMicrocontroller 8051 and its interfacing
Microcontroller 8051 and its interfacing
 
8 bit microcontroller
8 bit microcontroller8 bit microcontroller
8 bit microcontroller
 
Microprocessors and microcontrollers
Microprocessors and microcontrollersMicroprocessors and microcontrollers
Microprocessors and microcontrollers
 
Interrupt programming with 8051 microcontroller
Interrupt programming with 8051  microcontrollerInterrupt programming with 8051  microcontroller
Interrupt programming with 8051 microcontroller
 
Introduction to arm architecture
Introduction to arm architectureIntroduction to arm architecture
Introduction to arm architecture
 
Uart
UartUart
Uart
 
Introduction to ARM LPC2148
Introduction to ARM LPC2148Introduction to ARM LPC2148
Introduction to ARM LPC2148
 
Pic microcontroller architecture
Pic microcontroller architecturePic microcontroller architecture
Pic microcontroller architecture
 
Introduction to embedded systems
Introduction to embedded systemsIntroduction to embedded systems
Introduction to embedded systems
 
8051 Microcontroller ppt
8051 Microcontroller ppt8051 Microcontroller ppt
8051 Microcontroller ppt
 
PIC 16F877 micro controller by Gaurav raikar
PIC 16F877 micro controller by Gaurav raikarPIC 16F877 micro controller by Gaurav raikar
PIC 16F877 micro controller by Gaurav raikar
 

En vedette

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
 
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
 
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
 
Synchronous generators
Synchronous generatorsSynchronous generators
Synchronous generators
 
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
 

Similaire à L15 timers-counters-in-atmega328 p

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

timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptx
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
 
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
 

Dernier

Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
chumtiyababu
 

Dernier (20)

Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 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
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
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
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 

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