SlideShare a Scribd company logo
TimersCounters &
Interrupt
Programming
Contents
1. Introduction
2. 8 bit Timers
3. Interrupts.
4. 16 bit Timers
TimerCounters
1. Introduction(Time Terminology)
Frequency
Consider signal x(t) that repeats itself. This signal called a periodic with
period T if it satisfies:
x(t)=x(t+T).
To measure the frequency of a periodic signal, we count the number of
times a particular event repeats within a 1-s period.
Period
 The flip side of a frequency is a period.
 If an event occurs with a rate of 1 Hz, the period of that event is 1 s.
 To find a period, given a frequency, or vice versa, f=1/T.
TimerCounters
1. Introduction(Time Terminology)
Duty Cycle
 In many applications, periodic pulses are used as control signals.
 A good example is the use of a periodic pulse to control a servo motor.
 To control the direction and sometimes the speed of a motor, a periodic
pulse signal with a changing duty cycle over time is used.
TimerCounters
1. Introduction(Time Terminology)
Timing System Overview:
 The heart of the timing system is the crystal time base.
 The crystal’s frequency of a microcontroller is used to generate a
baseline clock signal.
 For a timer system, the system clock is used to update the contents
of a special register called a free-running counter.
 For input time-related activities, all microcontrollers typically have
timer hardware components that detect signal logic changes on one
or more input pins. Such components rely on a free-running counter
to capture external event times.
TimerCounters
1. Introduction(Time Terminology)
 For output timer functions, a microcontroller uses a comparator, a
free-running counter , logic switches, and special-purpose registers
to generate time-related signals on one or more output pins.
TimerCounters
1. Introduction(Applications)
 Measure an input signal timing event.
 Count the number of external signal occurrences.
 Generate timed signals---termed output compare.
 Generate PWM signals.
TimerCounters
1. Introduction
TimerCounter used for the following:
1) Count an event (by connecting external source to increment the
counter registers).
2) Generate time delay (Oscillators or internal clock is used to
increment the counter registers).
There are three timers on Atmega328:
1) Timer (0) which is 8 bit timer.
2) Timer (1) which is 16 bit timer.
3) Timer (2) which is 8 bit timer
TimerCounters
1. Introduction
TimerCounters
2. 8 bit Timers(Timer 0)
Features:
• Two Independent Output Compare Units
• Double Buffered Output Compare Registers
• Clear Timer on Compare Match (Auto Reload)
• Phase Correct Pulse Width Modulator (PWM)
• Variable PWM Period
• Frequency Generator
• Three Independent Interrupt Sources (TOV0, OCF0A, and OCF0B)
TimerCounters
The architecture of Timer (0) shown in the following figure:
2. 8 bit Timers(Timer 0)
TimerCounters
2. 8 bit Timers(Timer 0)
TimerCounters
2. 8 bit Timers(Timer 0)
TimerCounters
2. 8 bit Timers(Timer 0)
Normal Mode:the timer will continually
count from 0x00 (BOTTOM) to 0xFF
(TOP).
 When the TCNT0 returns to 0 on each
cycle of the counter, the Timer/Counter
Overflow Flag (TOV0) will be set.
 The normal mode is useful for
generating a periodic ‘‘clock tick’’ that
may be used to calculate elapsed real
time or provide delays within a system.
TimerCounters
2. 8 bit Timers(Timer 0)
Clear Timer on Compare Match:In the
CTC modem, the TCNT0 timer is reset to 0
every time the TCNT0 counter reaches the
value set in OCR0.
 The Output Compare Flag 0 (OCF0) is
set when this event occurs.
 The OCF0 flag is enabled by asserting
the Timer/Counter 0 Output Compare
Match Interrupt Enable (OCIE0) flag in
the TIMSK and when the I-bit in the
Status Register is set to 1
 The CTC mode is used to generate a
precision digital waveform such as a
periodic signal or a single pulse.
TimerCounters
2. 8 bit Timers(Timer 0)
Phase Correct PWM Mode: In the Phase
Correct PWM mode, the TCNT0 register
counts from 0x00 to 0xFF and back down
to 0x00 continually.
Every time the TCNT0 value matches the
value set in the OCR0 register, the OCF0
flag is set and a change in the PWM signal
occurs.
TimerCounters
2. 8 bit Timers(Timer 0)
Fast PWM:is used to generate a precision
PWMsignal of a desired frequency and duty
cycle.
 It is called the fast PWM because its
maximum frequency is twice that of the
Phase Correct PWM mode.
 When the TCNT0 register value reaches
the value set in the OCR0 register, it will
cause a change in the PWM output as
prescribed by the system designer.
 It continues to count up to the TOP
value, at which time the Timer/Counter 0
Overflow Flag(TOV0) is set.
TimerCounters
2. 8 bit Timers(Timer 0)
TCNTn: TimerCounter register it counts up every pulse. Also it can
be loaded by a certain value.
In each TCNTn there are:
WGMnm: Wave generation mode (Normal mode ,CTC mode ,Fast
PWM ,..etc.).
CSnm: Prescalar(8,64,256,1024).
TOVn: Timer overflow flag, when a timer overflow a flag will be set.
TCCRn: TimerCounter control register ,its for setting mode of
operation by loading the proper value for
ex(Normal mode ,CTC mode ,Fast PWM ,..etc.)will be explained later.
TimerCounters
2. 8 bit Timers(Timer 0)
TIMSK :Timer/Counter Interrupt Mask Register register is used by all three
timer channels.
Timer 0 uses the Timer/Counter 0 Output Compare Match Interrupt Enable
(OCIE0) bit and the Timer/Counter 0 Overflow Interrupt Enable (TOIE0) bit.
TimerCounters
2. 8 bit Timers(Timer 0)
TimerCounters
2. 8 bit Timers(Timer 0)
TimerCounters
2. 8 bit Timers(Timer 0)
TimerCounters
2. 8 bit Timers(Timer 0)
TimerCounters
2. 8 bit Timers(Timer 0)
TimerCounters
2. 8 bit Timers(Timer 0)
TimerCounters
2. 8 bit Timers(Timer 0)
TimerCounters
2. 8 bit Timers (Normal Mode)
There are two equation to calculate the value of TCNT0:
1) In Decimal
256-(TdTc)=TCNT0
2) In Hexa
FF+1-(TdTc)=TCNT0
Td: Time on desired from MCU.
Tc: Timer Clock of the MCU ((1Fosc)*Prescalar=Tosc*Prescalar).
OSC: Oscillators (the Crystal).
Prescalar: is used to divide this clock frequency and produce a clock for
TIMER.
TimerCounters
2. 8 bit Timers (Normal Mode)
Ex(1):
Assuming xtal=8MHZ,and required to generate a square wave with period of
12.5 us.
Td=12.5us/2=6.25us.
Tc=1/8MHZ=0.125us.
256-(6.25/0.125)=206=0xCE.
TCNT0=0xCE.
EX(2):
Assuming xtal=8MHZ,Calculate the frequency of square wave knowing that the
TCNT0=0x3E
Td=(FF+1-3E)*0.125us=24.25us.
F=1/2*Td=12*24.25us=20.61KHZ.
TimerCounters
2. 8 bit Timers (Normal Mode)
Prescaler and generating a large time delay:
To get the maximum delay time
Td=(256-0)*Tc. (for xtal=8MHZ) ,Td=32us
F=12*Td=12*32us=15KHZ
To maximize the period prescaler is used
Using prescaler=8 Td=256us.
Prescaler=64 Td=2048 us.
Prescaler=256 Td=8192us.
Prescaler=1024 Td=32768us.
Ex(3):
Assuming xtal=8Mhz generate a square wave of 125 Hz,using timer 0 and
prescaler of 256
T=1125=8ms Td=4ms.
Tc=18MHZ *256=32us.
256-(4ms32us)=256-125=131.
TCNT0=0x83.
TimerCounters
Steps of Programming Timer 0 for Normal mode :
1) Load the TCCR0 (A,B) with the proper value according to the mode of
operation.
2) Load the TCNTn with proper values according to the oscillator
frequency(1/f*Prescalar=Tc*Prescalar) and the desired (Td)time required with or
without the prescalar.
3) If the timer overflow set the TOV to clear the timer and to count again in
(normal mode) Clear the flag or OCF0.
5) Repeat it again.
2. 8 bit Timers (Normal Mode)
Ex(4)
Find the time delay (Td) when the OCR0=9, assuming that
xtal=8Mhz
The TCNT0 will be cleared after 9+1=10 counts
Td=10*0.125us=1.25us.
EX(5)
Using the CTC mode find the OCR0 value to generate a time delay
of (25.6ms) using the prscaler 1024
Td=25.6ms
Tc=0.125us*1024=128us
Number of counts=Td/Tc=200
OCR0=200-1=99.
TimerCounters
2. 8 bit Timers (CTC mode)
TimerCounters
Steps of Programming Timer 0 for Normal mode :
1) Load the TCCR0 (A,B) with the proper value according to the mode of
operation.
2) Load the TCNTn with proper values according to the oscillator
frequency(1/f*Prescalar=Tc*Prescalar) and the desired (Td)time required with or
without the prescalar.
3) Set the OCF0 when the TCNT0 = OCR0 to clear the timer and count again.
4) Clear the flag or OCF0.
5) 5) Repeat it again.
2. 8 bit Timers (CTC Mode)
Interrupt
Interrupts are basically events that require immediate attention by the
microcontroller. When an interrupt event occurs the microcontroller pause
its current task and attend to the interrupt by executing an Interrupt
Service Routine (ISR) at the end of the ISR the microcontroller returns to
the task it had pause and continue its normal operations.
In order for the microcontroller to respond to an interrupt event the
interrupt feature of the microcontroller must be enabled along with the
specific interrupt. This is done by setting the Global Interrupt
Enabled bit and the Interrupt Enable bit of the specific interrupt.
Interrupt Types:
1. Internal Interrupt (e.g. Timer interrupt).
2. External Interrupt ( will be discussed in the next session).
Interrupt
Each Interrupt associated to vector number shown in the following
vector table (See other routines in the data sheet)
Interrupt
Why using Interrupt in
Timers??!!!
Interrupt VS Polling:
There are two methods to receive a service from microcontroller.
1) Polling method: the microcontroller continuously monitors the state of
the device ,when the state of the device met the condition the
microcontroller perform the service. After that it returns again to monitor
the state again.
2) Interrupt method: whenever any device needs a service, it notifies the
microcontroller by sending an interrupt signal. Upon receiving the signal
the microcontroller gets out the infinite loop and serves the device which is
associated with the interrupt it’s called interrupt service routine.
Interrupt
Advantages of Interrupts over Polling:
1) Every device can get the attention of the microcontroller by
sending an interrupt signal, when two devices send an interrupt signal
at same time the microcontroller serves the device with the high
priority.
2) Polling wastes the microcontroller time by polling a device(s)that
don’t need a service and tying it down from performing any other
tasks.
Interrupt
Interrupt
Steps of Executing an Interrupt:
1) The microcontroller finished the instruction that currently executing
and then saved the address of the next instruction (program counter) on
the stack.
2) It jumps to a fixed location in the memory (interrupt vector).The
interrupt vector directs the microcontroller to the address of ISR.
3) The microcontroller starts to executes the ISR until the it reaches the
last instruction RETI(return from interrupt).
4) The microcontroller returns to the place where its interrupted first, it
gets the program counter from the stack then starts to execute from that
address.
Interrupt
Enabling the Interrupts:
To enable the interrupt is should be unmasked as shown in the following
figure.
 SREG is the Status register ,to enable the interrupt (unmask the
interrupt) the bit D7(I) should be activated using sei() set interrupt then
use TMISK register to enable interrupts of Timer (0 or 1 or 2)this will
covered later.
 To disable the interrupt the bit D7(I) should be cleared by cli()clear the
interrupt.
Interrupt
Registers associated with Timer interrupt
TimerCounters
Assignment
Do the same programs
of timer 0 on timer 2
TimerCounters
3. 16 bit Timers
Features:
• True 16-bit Design (i.e., Allows 16-bit PWM)
• Two independent Output Compare Units
• Double Buffered Output Compare Registers.
• One Input Capture Unit.
• Input Capture Noise Canceler.
• Clear Timer on Compare Match (Auto Reload).
• Phase Correct Pulse Width Modulator (PWM).
• Variable PWM Period.
• Frequency Generator.
• External Event Counter.
• Four independent interrupt Sources (TOV1, OCF1A, OCF1B, and
ICF1).
TimerCounters
3. 16 bit Timers
The architecture of Timer (1) shown in the following figure:
TimerCounters
3. 16 bit Timers
TimerCounters
3. 16 bit Timers
ICNC1: Input Capture Noise Canceler Setting this bit (to one) activates the
Input Capture Noise Canceler. When the noise canceler is activated, the input
from the Input Capture pin (ICP1) is filtered.
ICES1: Input Capture Edge Select This bit selects which edge on the Input
Capture pin (ICP1) that is used to trigger a capture event. When the ICES1
bit is written to zero, a falling (negative) edge is used as trigger, and when the
ICES1 bit is written to one, a rising (positive) edge will trigger the capture.
ICIE1: Timer/Counter1, Input Capture Interrupt Enable When this bit is
written to one, and the I-flag in the Status Register is set (interrupts globally
enabled), the Timer/Counter1 Input Capture interrupt is enabled.
ICF1: Timer/Counter1, Input Capture Flag This flag is set when a capture
event occurs on the ICP1 pin
TimerCounters
3. 16 bit Timers
TimerCounters
3. 16 bit Timers
TimerCounters
3. 16 bit Timers
TimerCounters
3. 16 bit Timers
TimerCounters
3. 16 bit Timers
TimerCounters
3. 16 bit Timers
TimerCounters
3. 16 bit Timers
Assignment
Generate a square wave of
1 sec using the normal
mode and the CTC mode.
Repeat it using interrupt
method.

More Related Content

PPTX
microcontroller vs microprocessor
PDF
8051 assembly programming
PPTX
Motor Control Relay, Pwm, DC and Stepper Motors
PPTX
8051 Microcontroller PPT's By Er. Swapnil Kaware
PPTX
transistor transistor logic
PPTX
Subroutine in 8051 microcontroller
PPT
PPT
Interrupt programming with 8051 microcontroller
microcontroller vs microprocessor
8051 assembly programming
Motor Control Relay, Pwm, DC and Stepper Motors
8051 Microcontroller PPT's By Er. Swapnil Kaware
transistor transistor logic
Subroutine in 8051 microcontroller
Interrupt programming with 8051 microcontroller

What's hot (20)

PDF
Keypad Interfacing with 8051 Microcontroller
PPTX
Register Organisation of 8086 Microprocessor
PPTX
4.programmable dma controller 8257
PPT
8051 Microcontroller
PPTX
8051 Assembly Language Programming
PPT
Demultiplexing of buses of 8085 microprocessor
PPTX
Microcontroller 8051 and its interfacing
PPTX
I/O port programming in 8051
PPT
Memory organization of 8051
PPT
ARM7TDM
PDF
Difference b/w 8085 & 8086
PDF
Vlsi lab viva question with answers
PPTX
Pentium processor
PPTX
Lcd interfaing using 8051 and assambly language programming
PPTX
8051 memory
PPT
80286 microprocessor
PPT
Introduction to microprocessor
PDF
Unit 2 mpmc
DOCX
8096 microcontrollers notes
PDF
ADC and DAC interfacing.pdf
Keypad Interfacing with 8051 Microcontroller
Register Organisation of 8086 Microprocessor
4.programmable dma controller 8257
8051 Microcontroller
8051 Assembly Language Programming
Demultiplexing of buses of 8085 microprocessor
Microcontroller 8051 and its interfacing
I/O port programming in 8051
Memory organization of 8051
ARM7TDM
Difference b/w 8085 & 8086
Vlsi lab viva question with answers
Pentium processor
Lcd interfaing using 8051 and assambly language programming
8051 memory
80286 microprocessor
Introduction to microprocessor
Unit 2 mpmc
8096 microcontrollers notes
ADC and DAC interfacing.pdf
Ad

Viewers also liked (20)

PDF
AVR_Course_Day1 basic electronics
PDF
AVR_Course_Day2 what is pcb
PDF
AVR_Course_Day4 introduction to microcontroller
PPTX
Timer & Interrupt Atmega16
PDF
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
PDF
AVR_Course_Day5 avr interfaces
PDF
AVR_Course_Day8 motor drive and pwm techniques
PPT
AVR Fundamentals
PDF
Hardware interfacing basics using AVR
PDF
Lecture7
PPTX
Microcontoller and Embedded System
PDF
OpenPump: open-source hardware for medical devices
PPTX
Arduino Autonomous Robot
DOC
Pull up & pull-down resistors
PDF
APRS - the amateur radio tracking system
PPTX
37471656 interrupts
PDF
Complete Overview of MESH for Amateur Radio (Updated Nov. 2014)
PDF
An innovative introductory course to systems engineering teaching.pptx
PPTX
Colour sensor vivek
PDF
Kasser synergy amateur radio
AVR_Course_Day1 basic electronics
AVR_Course_Day2 what is pcb
AVR_Course_Day4 introduction to microcontroller
Timer & Interrupt Atmega16
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
AVR_Course_Day5 avr interfaces
AVR_Course_Day8 motor drive and pwm techniques
AVR Fundamentals
Hardware interfacing basics using AVR
Lecture7
Microcontoller and Embedded System
OpenPump: open-source hardware for medical devices
Arduino Autonomous Robot
Pull up & pull-down resistors
APRS - the amateur radio tracking system
37471656 interrupts
Complete Overview of MESH for Amateur Radio (Updated Nov. 2014)
An innovative introductory course to systems engineering teaching.pptx
Colour sensor vivek
Kasser synergy amateur radio
Ad

Similar to AVR_Course_Day7 timers counters and interrupt programming (20)

PPTX
timer counter (1).pptx
PDF
Avr timers
PPTX
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
PPTX
8051 Timers and Counters
PDF
8051 Timers / Counters
PPTX
Timers done by Priyanga KR
PDF
Timers and counters of microcontroller 8051
PDF
Timers in Arduino
PDF
L15 timers-counters-in-atmega328 p
PPTX
KTU_Microprocessor and Microcontrollers_Module2
PPT
lecture 12 counter_microcontroller2.ppt
PPTX
Timer counter in arm7(lpc2148)
PPT
Timers
PPT
8051 ch9-950217
PPT
8051 ch9
PPT
Microcontroller Timer Counter Modules and applications
PPT
8051 microcontroller timer summary presentation
PPTX
5th unit embedded system and iot design timer and controller
PDF
PWM_skgdfshkfdjkfgjsjienvdkncjdhrkshd.pdf
timer counter (1).pptx
Avr timers
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
8051 Timers and Counters
8051 Timers / Counters
Timers done by Priyanga KR
Timers and counters of microcontroller 8051
Timers in Arduino
L15 timers-counters-in-atmega328 p
KTU_Microprocessor and Microcontrollers_Module2
lecture 12 counter_microcontroller2.ppt
Timer counter in arm7(lpc2148)
Timers
8051 ch9-950217
8051 ch9
Microcontroller Timer Counter Modules and applications
8051 microcontroller timer summary presentation
5th unit embedded system and iot design timer and controller
PWM_skgdfshkfdjkfgjsjienvdkncjdhrkshd.pdf

Recently uploaded (20)

PDF
composite construction of structures.pdf
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Welding lecture in detail for understanding
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
web development for engineering and engineering
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
DOCX
573137875-Attendance-Management-System-original
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Geodesy 1.pptx...............................................
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
composite construction of structures.pdf
Lesson 3_Tessellation.pptx finite Mathematics
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Welding lecture in detail for understanding
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
CYBER-CRIMES AND SECURITY A guide to understanding
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
web development for engineering and engineering
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Mechanical Engineering MATERIALS Selection
Strings in CPP - Strings in C++ are sequences of characters used to store and...
573137875-Attendance-Management-System-original
Operating System & Kernel Study Guide-1 - converted.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Geodesy 1.pptx...............................................
Embodied AI: Ushering in the Next Era of Intelligent Systems

AVR_Course_Day7 timers counters and interrupt programming

  • 2. Contents 1. Introduction 2. 8 bit Timers 3. Interrupts. 4. 16 bit Timers
  • 3. TimerCounters 1. Introduction(Time Terminology) Frequency Consider signal x(t) that repeats itself. This signal called a periodic with period T if it satisfies: x(t)=x(t+T). To measure the frequency of a periodic signal, we count the number of times a particular event repeats within a 1-s period. Period  The flip side of a frequency is a period.  If an event occurs with a rate of 1 Hz, the period of that event is 1 s.  To find a period, given a frequency, or vice versa, f=1/T.
  • 4. TimerCounters 1. Introduction(Time Terminology) Duty Cycle  In many applications, periodic pulses are used as control signals.  A good example is the use of a periodic pulse to control a servo motor.  To control the direction and sometimes the speed of a motor, a periodic pulse signal with a changing duty cycle over time is used.
  • 5. TimerCounters 1. Introduction(Time Terminology) Timing System Overview:  The heart of the timing system is the crystal time base.  The crystal’s frequency of a microcontroller is used to generate a baseline clock signal.  For a timer system, the system clock is used to update the contents of a special register called a free-running counter.  For input time-related activities, all microcontrollers typically have timer hardware components that detect signal logic changes on one or more input pins. Such components rely on a free-running counter to capture external event times.
  • 6. TimerCounters 1. Introduction(Time Terminology)  For output timer functions, a microcontroller uses a comparator, a free-running counter , logic switches, and special-purpose registers to generate time-related signals on one or more output pins.
  • 7. TimerCounters 1. Introduction(Applications)  Measure an input signal timing event.  Count the number of external signal occurrences.  Generate timed signals---termed output compare.  Generate PWM signals.
  • 8. TimerCounters 1. Introduction TimerCounter used for the following: 1) Count an event (by connecting external source to increment the counter registers). 2) Generate time delay (Oscillators or internal clock is used to increment the counter registers). There are three timers on Atmega328: 1) Timer (0) which is 8 bit timer. 2) Timer (1) which is 16 bit timer. 3) Timer (2) which is 8 bit timer
  • 10. TimerCounters 2. 8 bit Timers(Timer 0) Features: • Two Independent Output Compare Units • Double Buffered Output Compare Registers • Clear Timer on Compare Match (Auto Reload) • Phase Correct Pulse Width Modulator (PWM) • Variable PWM Period • Frequency Generator • Three Independent Interrupt Sources (TOV0, OCF0A, and OCF0B)
  • 11. TimerCounters The architecture of Timer (0) shown in the following figure: 2. 8 bit Timers(Timer 0)
  • 12. TimerCounters 2. 8 bit Timers(Timer 0)
  • 13. TimerCounters 2. 8 bit Timers(Timer 0)
  • 14. TimerCounters 2. 8 bit Timers(Timer 0) Normal Mode:the timer will continually count from 0x00 (BOTTOM) to 0xFF (TOP).  When the TCNT0 returns to 0 on each cycle of the counter, the Timer/Counter Overflow Flag (TOV0) will be set.  The normal mode is useful for generating a periodic ‘‘clock tick’’ that may be used to calculate elapsed real time or provide delays within a system.
  • 15. TimerCounters 2. 8 bit Timers(Timer 0) Clear Timer on Compare Match:In the CTC modem, the TCNT0 timer is reset to 0 every time the TCNT0 counter reaches the value set in OCR0.  The Output Compare Flag 0 (OCF0) is set when this event occurs.  The OCF0 flag is enabled by asserting the Timer/Counter 0 Output Compare Match Interrupt Enable (OCIE0) flag in the TIMSK and when the I-bit in the Status Register is set to 1  The CTC mode is used to generate a precision digital waveform such as a periodic signal or a single pulse.
  • 16. TimerCounters 2. 8 bit Timers(Timer 0) Phase Correct PWM Mode: In the Phase Correct PWM mode, the TCNT0 register counts from 0x00 to 0xFF and back down to 0x00 continually. Every time the TCNT0 value matches the value set in the OCR0 register, the OCF0 flag is set and a change in the PWM signal occurs.
  • 17. TimerCounters 2. 8 bit Timers(Timer 0) Fast PWM:is used to generate a precision PWMsignal of a desired frequency and duty cycle.  It is called the fast PWM because its maximum frequency is twice that of the Phase Correct PWM mode.  When the TCNT0 register value reaches the value set in the OCR0 register, it will cause a change in the PWM output as prescribed by the system designer.  It continues to count up to the TOP value, at which time the Timer/Counter 0 Overflow Flag(TOV0) is set.
  • 18. TimerCounters 2. 8 bit Timers(Timer 0) TCNTn: TimerCounter register it counts up every pulse. Also it can be loaded by a certain value. In each TCNTn there are: WGMnm: Wave generation mode (Normal mode ,CTC mode ,Fast PWM ,..etc.). CSnm: Prescalar(8,64,256,1024). TOVn: Timer overflow flag, when a timer overflow a flag will be set. TCCRn: TimerCounter control register ,its for setting mode of operation by loading the proper value for ex(Normal mode ,CTC mode ,Fast PWM ,..etc.)will be explained later.
  • 19. TimerCounters 2. 8 bit Timers(Timer 0) TIMSK :Timer/Counter Interrupt Mask Register register is used by all three timer channels. Timer 0 uses the Timer/Counter 0 Output Compare Match Interrupt Enable (OCIE0) bit and the Timer/Counter 0 Overflow Interrupt Enable (TOIE0) bit.
  • 20. TimerCounters 2. 8 bit Timers(Timer 0)
  • 21. TimerCounters 2. 8 bit Timers(Timer 0)
  • 22. TimerCounters 2. 8 bit Timers(Timer 0)
  • 23. TimerCounters 2. 8 bit Timers(Timer 0)
  • 24. TimerCounters 2. 8 bit Timers(Timer 0)
  • 25. TimerCounters 2. 8 bit Timers(Timer 0)
  • 26. TimerCounters 2. 8 bit Timers(Timer 0)
  • 27. TimerCounters 2. 8 bit Timers (Normal Mode) There are two equation to calculate the value of TCNT0: 1) In Decimal 256-(TdTc)=TCNT0 2) In Hexa FF+1-(TdTc)=TCNT0 Td: Time on desired from MCU. Tc: Timer Clock of the MCU ((1Fosc)*Prescalar=Tosc*Prescalar). OSC: Oscillators (the Crystal). Prescalar: is used to divide this clock frequency and produce a clock for TIMER.
  • 28. TimerCounters 2. 8 bit Timers (Normal Mode) Ex(1): Assuming xtal=8MHZ,and required to generate a square wave with period of 12.5 us. Td=12.5us/2=6.25us. Tc=1/8MHZ=0.125us. 256-(6.25/0.125)=206=0xCE. TCNT0=0xCE. EX(2): Assuming xtal=8MHZ,Calculate the frequency of square wave knowing that the TCNT0=0x3E Td=(FF+1-3E)*0.125us=24.25us. F=1/2*Td=12*24.25us=20.61KHZ.
  • 29. TimerCounters 2. 8 bit Timers (Normal Mode) Prescaler and generating a large time delay: To get the maximum delay time Td=(256-0)*Tc. (for xtal=8MHZ) ,Td=32us F=12*Td=12*32us=15KHZ To maximize the period prescaler is used Using prescaler=8 Td=256us. Prescaler=64 Td=2048 us. Prescaler=256 Td=8192us. Prescaler=1024 Td=32768us. Ex(3): Assuming xtal=8Mhz generate a square wave of 125 Hz,using timer 0 and prescaler of 256 T=1125=8ms Td=4ms. Tc=18MHZ *256=32us. 256-(4ms32us)=256-125=131. TCNT0=0x83.
  • 30. TimerCounters Steps of Programming Timer 0 for Normal mode : 1) Load the TCCR0 (A,B) with the proper value according to the mode of operation. 2) Load the TCNTn with proper values according to the oscillator frequency(1/f*Prescalar=Tc*Prescalar) and the desired (Td)time required with or without the prescalar. 3) If the timer overflow set the TOV to clear the timer and to count again in (normal mode) Clear the flag or OCF0. 5) Repeat it again. 2. 8 bit Timers (Normal Mode)
  • 31. Ex(4) Find the time delay (Td) when the OCR0=9, assuming that xtal=8Mhz The TCNT0 will be cleared after 9+1=10 counts Td=10*0.125us=1.25us. EX(5) Using the CTC mode find the OCR0 value to generate a time delay of (25.6ms) using the prscaler 1024 Td=25.6ms Tc=0.125us*1024=128us Number of counts=Td/Tc=200 OCR0=200-1=99. TimerCounters 2. 8 bit Timers (CTC mode)
  • 32. TimerCounters Steps of Programming Timer 0 for Normal mode : 1) Load the TCCR0 (A,B) with the proper value according to the mode of operation. 2) Load the TCNTn with proper values according to the oscillator frequency(1/f*Prescalar=Tc*Prescalar) and the desired (Td)time required with or without the prescalar. 3) Set the OCF0 when the TCNT0 = OCR0 to clear the timer and count again. 4) Clear the flag or OCF0. 5) 5) Repeat it again. 2. 8 bit Timers (CTC Mode)
  • 33. Interrupt Interrupts are basically events that require immediate attention by the microcontroller. When an interrupt event occurs the microcontroller pause its current task and attend to the interrupt by executing an Interrupt Service Routine (ISR) at the end of the ISR the microcontroller returns to the task it had pause and continue its normal operations. In order for the microcontroller to respond to an interrupt event the interrupt feature of the microcontroller must be enabled along with the specific interrupt. This is done by setting the Global Interrupt Enabled bit and the Interrupt Enable bit of the specific interrupt. Interrupt Types: 1. Internal Interrupt (e.g. Timer interrupt). 2. External Interrupt ( will be discussed in the next session).
  • 34. Interrupt Each Interrupt associated to vector number shown in the following vector table (See other routines in the data sheet)
  • 36. Interrupt VS Polling: There are two methods to receive a service from microcontroller. 1) Polling method: the microcontroller continuously monitors the state of the device ,when the state of the device met the condition the microcontroller perform the service. After that it returns again to monitor the state again. 2) Interrupt method: whenever any device needs a service, it notifies the microcontroller by sending an interrupt signal. Upon receiving the signal the microcontroller gets out the infinite loop and serves the device which is associated with the interrupt it’s called interrupt service routine. Interrupt
  • 37. Advantages of Interrupts over Polling: 1) Every device can get the attention of the microcontroller by sending an interrupt signal, when two devices send an interrupt signal at same time the microcontroller serves the device with the high priority. 2) Polling wastes the microcontroller time by polling a device(s)that don’t need a service and tying it down from performing any other tasks. Interrupt
  • 38. Interrupt Steps of Executing an Interrupt: 1) The microcontroller finished the instruction that currently executing and then saved the address of the next instruction (program counter) on the stack. 2) It jumps to a fixed location in the memory (interrupt vector).The interrupt vector directs the microcontroller to the address of ISR. 3) The microcontroller starts to executes the ISR until the it reaches the last instruction RETI(return from interrupt). 4) The microcontroller returns to the place where its interrupted first, it gets the program counter from the stack then starts to execute from that address.
  • 39. Interrupt Enabling the Interrupts: To enable the interrupt is should be unmasked as shown in the following figure.  SREG is the Status register ,to enable the interrupt (unmask the interrupt) the bit D7(I) should be activated using sei() set interrupt then use TMISK register to enable interrupts of Timer (0 or 1 or 2)this will covered later.  To disable the interrupt the bit D7(I) should be cleared by cli()clear the interrupt.
  • 41. TimerCounters Assignment Do the same programs of timer 0 on timer 2
  • 42. TimerCounters 3. 16 bit Timers Features: • True 16-bit Design (i.e., Allows 16-bit PWM) • Two independent Output Compare Units • Double Buffered Output Compare Registers. • One Input Capture Unit. • Input Capture Noise Canceler. • Clear Timer on Compare Match (Auto Reload). • Phase Correct Pulse Width Modulator (PWM). • Variable PWM Period. • Frequency Generator. • External Event Counter. • Four independent interrupt Sources (TOV1, OCF1A, OCF1B, and ICF1).
  • 43. TimerCounters 3. 16 bit Timers The architecture of Timer (1) shown in the following figure:
  • 45. TimerCounters 3. 16 bit Timers ICNC1: Input Capture Noise Canceler Setting this bit (to one) activates the Input Capture Noise Canceler. When the noise canceler is activated, the input from the Input Capture pin (ICP1) is filtered. ICES1: Input Capture Edge Select This bit selects which edge on the Input Capture pin (ICP1) that is used to trigger a capture event. When the ICES1 bit is written to zero, a falling (negative) edge is used as trigger, and when the ICES1 bit is written to one, a rising (positive) edge will trigger the capture. ICIE1: Timer/Counter1, Input Capture Interrupt Enable When this bit is written to one, and the I-flag in the Status Register is set (interrupts globally enabled), the Timer/Counter1 Input Capture interrupt is enabled. ICF1: Timer/Counter1, Input Capture Flag This flag is set when a capture event occurs on the ICP1 pin
  • 52. TimerCounters 3. 16 bit Timers Assignment Generate a square wave of 1 sec using the normal mode and the CTC mode. Repeat it using interrupt method.