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

More Related Content

PPTX
8051 MICROCONTROLLER ARCHITECTURE.pptx
MemonaMemon1
 
PPTX
Eye pattern
mpsrekha83
 
PPT
Microcontroller-8051.ppt
Dr.YNM
 
PDF
Introduction to Embedded Architecture
amrutachintawar239
 
PPTX
5.programmable interval timer 8253
MdFazleRabbi18
 
PPT
Fourier analysis of signals and systems
Babul Islam
 
PPTX
Introduction to Counters
ISMT College
 
PPTX
8251 USART
coolsdhanesh
 
8051 MICROCONTROLLER ARCHITECTURE.pptx
MemonaMemon1
 
Eye pattern
mpsrekha83
 
Microcontroller-8051.ppt
Dr.YNM
 
Introduction to Embedded Architecture
amrutachintawar239
 
5.programmable interval timer 8253
MdFazleRabbi18
 
Fourier analysis of signals and systems
Babul Islam
 
Introduction to Counters
ISMT College
 
8251 USART
coolsdhanesh
 

What's hot (20)

PPT
8051 microcontroller and it’s interface
Abhishek Choksi
 
PPTX
8051 Microcontroller PPT's By Er. Swapnil Kaware
Prof. Swapnil V. Kaware
 
PPT
Interfacing adc
PRADEEP
 
PPTX
Pic microcontroller architecture
DominicHendry
 
PPTX
TELEMETRY, TRACKING COMMAND & MONITORING
Alpana Ingale
 
PDF
Memory interfacing of microcontroller 8051
Nilesh Bhaskarrao Bahadure
 
PPT
Sequential circuits
Paresh Parmar
 
PPTX
Adc and dac
nitugatkal
 
PPTX
MICROCONTROLLER 8051- Architecture & Pin Configuration
AKHIL MADANKAR
 
PDF
8051 interfacing
KanchanPatil34
 
PPTX
Fourier Series for Continuous Time & Discrete Time Signals
Jayanshu Gundaniya
 
PPTX
Asynchronous Sequential Circuit-Unit 4 ppt
SIVALAKSHMIPANNEERSE
 
PPTX
Timer counter in arm7(lpc2148)
Aarav Soni
 
PPSX
Microprocessor Architecture-III
Dr.YNM
 
DOCX
Demultiplexer with vhdl code
Vishal Bait
 
PPTX
Counters
Ketaki_Pattani
 
PPTX
INTERRUPT ROUTINES IN RTOS EN VIRONMENT HANDELING OF INTERRUPT SOURCE CALLS
JOLLUSUDARSHANREDDY
 
PPTX
Comparison between the FPGA vs CPLD
Gowri Kishore
 
PPTX
Introduction to Digital Signal processors
PeriyanayagiS
 
PPTX
High pass-low-pass-filter
East West University-Dhaka Bangladesh
 
8051 microcontroller and it’s interface
Abhishek Choksi
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
Prof. Swapnil V. Kaware
 
Interfacing adc
PRADEEP
 
Pic microcontroller architecture
DominicHendry
 
TELEMETRY, TRACKING COMMAND & MONITORING
Alpana Ingale
 
Memory interfacing of microcontroller 8051
Nilesh Bhaskarrao Bahadure
 
Sequential circuits
Paresh Parmar
 
Adc and dac
nitugatkal
 
MICROCONTROLLER 8051- Architecture & Pin Configuration
AKHIL MADANKAR
 
8051 interfacing
KanchanPatil34
 
Fourier Series for Continuous Time & Discrete Time Signals
Jayanshu Gundaniya
 
Asynchronous Sequential Circuit-Unit 4 ppt
SIVALAKSHMIPANNEERSE
 
Timer counter in arm7(lpc2148)
Aarav Soni
 
Microprocessor Architecture-III
Dr.YNM
 
Demultiplexer with vhdl code
Vishal Bait
 
Counters
Ketaki_Pattani
 
INTERRUPT ROUTINES IN RTOS EN VIRONMENT HANDELING OF INTERRUPT SOURCE CALLS
JOLLUSUDARSHANREDDY
 
Comparison between the FPGA vs CPLD
Gowri Kishore
 
Introduction to Digital Signal processors
PeriyanayagiS
 
High pass-low-pass-filter
East West University-Dhaka Bangladesh
 
Ad

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

PDF
Avr timers
Utsav Jain
 
PDF
AVR_Course_Day7 timers counters and interrupt programming
Mohamed Ali
 
PPTX
AVRTIMER.pptx
Pratik Gohel
 
PPTX
Timer & Interrupt Atmega16
Ramadan Ramadan
 
PPTX
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
binduramesh13
 
PDF
L15 timers-counters-in-atmega328 p
rsamurti
 
DOCX
Arduino 101
josnihmurni2907
 
PPT
lecture 12 counter_microcontroller2.ppt
HebaEng
 
PPT
UNCC-IESLecture13 - Timers and Event Counters.ppt
AbhishekPankaj12
 
PPTX
Timers done by Priyanga KR
PriyangaKR1
 
PPTX
8051 timer counter
ankit3991
 
PPT
Microcontroller Timer Counter Modules and applications
vipulkondekar
 
PPT
8051 microcontroller timer summary presentation
anushkayadav3011
 
PDF
Timers in Arduino
VEDANSH SHARMA
 
PPTX
8051 Timers and Counters
Shreyans Pathak
 
PDF
8051 Timers / Counters
Patricio Lima
 
PPT
8051 Timer
Ramasubbu .P
 
PPT
Timers
PRADEEP
 
PDF
timer types ppt microcontroller 8051 .pdf
RajalakshmiSubramani12
 
PDF
8051 timers--2
Syed Basharat Hussain
 
Avr timers
Utsav Jain
 
AVR_Course_Day7 timers counters and interrupt programming
Mohamed Ali
 
AVRTIMER.pptx
Pratik Gohel
 
Timer & Interrupt Atmega16
Ramadan Ramadan
 
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
binduramesh13
 
L15 timers-counters-in-atmega328 p
rsamurti
 
Arduino 101
josnihmurni2907
 
lecture 12 counter_microcontroller2.ppt
HebaEng
 
UNCC-IESLecture13 - Timers and Event Counters.ppt
AbhishekPankaj12
 
Timers done by Priyanga KR
PriyangaKR1
 
8051 timer counter
ankit3991
 
Microcontroller Timer Counter Modules and applications
vipulkondekar
 
8051 microcontroller timer summary presentation
anushkayadav3011
 
Timers in Arduino
VEDANSH SHARMA
 
8051 Timers and Counters
Shreyans Pathak
 
8051 Timers / Counters
Patricio Lima
 
8051 Timer
Ramasubbu .P
 
Timers
PRADEEP
 
timer types ppt microcontroller 8051 .pdf
RajalakshmiSubramani12
 
8051 timers--2
Syed Basharat Hussain
 
Ad

Recently uploaded (20)

PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
PPT
Ppt for engineering students application on field effect
lakshmi.ec
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PDF
JUAL EFIX C5 IMU GNSS GEODETIC PERFECT BASE OR ROVER
Budi Minds
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
Ppt for engineering students application on field effect
lakshmi.ec
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
JUAL EFIX C5 IMU GNSS GEODETIC PERFECT BASE OR ROVER
Budi Minds
 

timer counter (1).pptx

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