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

More Related Content

PDF
L13 interrupts-in-atmega328 p
PPT
Interrupts for PIC18
PPTX
Pic 18 microcontroller
PPTX
AVRTIMER.pptx
PDF
DAC Interfacing with 8051.pdf
PPTX
Interrupts in 8051
PPTX
Lpc 1768 timers
PPT
8051 Addressing Modes
L13 interrupts-in-atmega328 p
Interrupts for PIC18
Pic 18 microcontroller
AVRTIMER.pptx
DAC Interfacing with 8051.pdf
Interrupts in 8051
Lpc 1768 timers
8051 Addressing Modes

What's hot (20)

PPT
Addressing mode and instruction set using 8051
PPT
STM32 MCU Family
DOCX
ARM7-ARCHITECTURE
PPTX
Msp 430 architecture module 1
PPT
PIC 16F877A by PARTHIBAN. S.
PDF
Microprocessor Microcontroller Interview & Viva Question.pdf
PDF
AVR Microcontroller
PPTX
Timer programming for 8051 using embedded c
PPTX
8051 timer counter
PDF
Data types in verilog
PPTX
LCD Interacing with 8051
PPT
Microcontroller 8051
PPTX
Sensor interfacing in 8051
PDF
ARM CORTEX M3 PPT
PPT
Microcontroller-8051.ppt
PPTX
ATmega32-AVR microcontrollers-Part I
PPTX
Embedded c
PPTX
PIC16F877A interfacing with LCD
PPT
PIC Microcontrollers.ppt
PDF
L8 understanding-atmega328 p-1
Addressing mode and instruction set using 8051
STM32 MCU Family
ARM7-ARCHITECTURE
Msp 430 architecture module 1
PIC 16F877A by PARTHIBAN. S.
Microprocessor Microcontroller Interview & Viva Question.pdf
AVR Microcontroller
Timer programming for 8051 using embedded c
8051 timer counter
Data types in verilog
LCD Interacing with 8051
Microcontroller 8051
Sensor interfacing in 8051
ARM CORTEX M3 PPT
Microcontroller-8051.ppt
ATmega32-AVR microcontrollers-Part I
Embedded c
PIC16F877A interfacing with LCD
PIC Microcontrollers.ppt
L8 understanding-atmega328 p-1
Ad

Viewers also liked (20)

PDF
L12 c-language-programming-of-atmega328 p
PDF
L11 assembly-language-programming-of-atmega328 p
PDF
L10 assembly-language-programming-of-atmega328 p
PDF
L9 understanding-atmega328 p-2
PDF
L16 usart-atmega328 p
PDF
L1 intro-to-mpu-mcu
PDF
Introduction to-Tex-and-LaTeX
PDF
L6 primary-memory
PDF
L5 data-parallel-computers
PDF
Transformers
PDF
L4 speeding-up-execution
PDF
L3 instruction-execution-steps
PDF
Lecture-5 : Semiconductor Power Switching Devices-2
PDF
L7 starting-to-use-mcu
PDF
Lecture-3 : More Applications of Power Electronics
PDF
Lecture-7 : Semiconductor Power Switching Devices-4
PDF
Lecture-2 : Applications of Power Electronics
PDF
L14 kb-lcd-interfacing-with-atmega328 p
PDF
Three phase-circuits
PDF
Synchronous generators
L12 c-language-programming-of-atmega328 p
L11 assembly-language-programming-of-atmega328 p
L10 assembly-language-programming-of-atmega328 p
L9 understanding-atmega328 p-2
L16 usart-atmega328 p
L1 intro-to-mpu-mcu
Introduction to-Tex-and-LaTeX
L6 primary-memory
L5 data-parallel-computers
Transformers
L4 speeding-up-execution
L3 instruction-execution-steps
Lecture-5 : Semiconductor Power Switching Devices-2
L7 starting-to-use-mcu
Lecture-3 : More Applications of Power Electronics
Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-2 : Applications of Power Electronics
L14 kb-lcd-interfacing-with-atmega328 p
Three phase-circuits
Synchronous generators
Ad

Similar to L15 timers-counters-in-atmega328 p (20)

PPTX
Timer counter in arm7(lpc2148)
PPTX
timer counter (1).pptx
PPT
8051 ch9-950217
PDF
AVR_Course_Day7 timers counters and interrupt programming
PPT
Microcontroller 8051 Timer Counter Interrrupt
PPT
Microcontroller 8051 timer and counter module
PDF
Timer And Counter in 8051 Microcontroller
PPT
MICROCONTROLLER TIMERS.ppt
PDF
8051 timers--2
PDF
Embedded C Programming Module 6 Presentation
PDF
Timers in Arduino
PPT
8051 ch9
PPTX
89C51 PROGRAMMING in Unit-4 of Microprocessor
PPT
MICROCONTROLLER TIMERS.GHGHGHGHGHGHGHGHGppt
PPTX
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
PDF
8051 Timers, Interrupts and Serial Communication
PDF
PPTX
Micro c lab7(timers)
PDF
9 timer programming
PPT
UNCC-IESLecture13 - Timers and Event Counters.ppt
Timer counter in arm7(lpc2148)
timer counter (1).pptx
8051 ch9-950217
AVR_Course_Day7 timers counters and interrupt programming
Microcontroller 8051 Timer Counter Interrrupt
Microcontroller 8051 timer and counter module
Timer And Counter in 8051 Microcontroller
MICROCONTROLLER TIMERS.ppt
8051 timers--2
Embedded C Programming Module 6 Presentation
Timers in Arduino
8051 ch9
89C51 PROGRAMMING in Unit-4 of Microprocessor
MICROCONTROLLER TIMERS.GHGHGHGHGHGHGHGHGppt
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
8051 Timers, Interrupts and Serial Communication
Micro c lab7(timers)
9 timer programming
UNCC-IESLecture13 - Timers and Event Counters.ppt

Recently uploaded (20)

PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
PPTX
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
PDF
Unit1 - AIML Chapter 1 concept and ethics
PPTX
Chapter-8 Introduction to Quality Standards.pptx
PPTX
MAD Unit - 3 User Interface and Data Management (Diploma IT)
PDF
IAE-V2500 Engine Airbus Family A319/320
PPT
UNIT-I Machine Learning Essentials for 2nd years
PDF
Lesson 3 .pdf
PPTX
chapter 1.pptx dotnet technology introduction
PPTX
Micro1New.ppt.pptx the mai themes of micfrobiology
PDF
Cryptography and Network Security-Module-I.pdf
PDF
MACCAFERRY GUIA GAVIONES TERRAPLENES EN ESPAÑOL
PPTX
AI-Reporting for Emerging Technologies(BS Computer Engineering)
PDF
Project_Mgmt_Institute_-Marc Marc Marc .pdf
PDF
Research on ultrasonic sensor for TTU.pdf
PPTX
Solar energy pdf of gitam songa hemant k
PPTX
Environmental studies, Moudle 3-Environmental Pollution.pptx
PPTX
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
PPTX
Principal presentation for NAAC (1).pptx
PDF
Mechanics of materials week 2 rajeshwari
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
Unit1 - AIML Chapter 1 concept and ethics
Chapter-8 Introduction to Quality Standards.pptx
MAD Unit - 3 User Interface and Data Management (Diploma IT)
IAE-V2500 Engine Airbus Family A319/320
UNIT-I Machine Learning Essentials for 2nd years
Lesson 3 .pdf
chapter 1.pptx dotnet technology introduction
Micro1New.ppt.pptx the mai themes of micfrobiology
Cryptography and Network Security-Module-I.pdf
MACCAFERRY GUIA GAVIONES TERRAPLENES EN ESPAÑOL
AI-Reporting for Emerging Technologies(BS Computer Engineering)
Project_Mgmt_Institute_-Marc Marc Marc .pdf
Research on ultrasonic sensor for TTU.pdf
Solar energy pdf of gitam songa hemant k
Environmental studies, Moudle 3-Environmental Pollution.pptx
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
Principal presentation for NAAC (1).pptx
Mechanics of materials week 2 rajeshwari

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