MAES - MID - LECTURE 02 - v3
MAES - MID - LECTURE 02 - v3
INTRODUCTION TO TIMERS
6 June 2023
INTRODUCTION
• A timer/counter is like a clock,
and can be used to measure
time events. The timer can be
programmed by some special
registers. The controller of the
Arduino is the ATmega328
which has 3 timers, called
timer0, timer1 and timer2.
• Timer0 and timer2 are 8bit
timers, whereas timer1 is a 16bit
timer. The most important
difference between 8bit and
16bit timer is the timer
resolution.
• 8-bit timer is capable of counting
28=256 steps from 0 to 255. 16
bit timer is capable of counting
216=65536 steps from 0 to
2
65535.
USEFULNESS OF TIMER :
• Timer is an important concept in the field of electronics. Every electronic component of
a sequential logic circuit works on a time base to keep all the work synchronized.
• An advantage of the timer is that it is totally independent of the CPU. Thus, it runs
parallel to the CPU and there is no CPU’s intervention, which makes the timer quite
accurate.
• This is why using a timer is preferred for long delays instead of simply using the
delay() function. The drawback of delay() is that your loop gets halted, and functions
above and below the delay() are not being executed during this interval.
• A timer approach is a little harder to implement but the main loop keeps executing and
only excludes the code and functions which a programmer wants to exclude. If the
programmer needs multiple tasks to occur at the same time or if the programmers'
application requires that you constantly read/save data from inputs, using the delay()
function should be avoided.
3
TIMER BASICS: OVERFLOW
• Due to this counting feature, timers are also known as counters. Once they reach their
maximum possible value, the program does not stop executing and the timer count simply
returns to its initial value of zero. In such a situation, we say that the timer/counter
overflows.
• For example, Timer0 is an 8-bit timer, meaning that it can count up from zero to 28-1, or
255. Once that number is reached, the count resets back to zero and starts counting
again.
4
HOW TIMER WORKS:
• Timers on the Arduino count up from zero, incrementing with every clock cycle of the
oscillating crystal that drives the Arduino.
• If smaller frequencies are necessary, it is possible to “divide” the clock through an approach called
pre-scaling.
• How quickly timer reaches the target count depends on the clock divider. With no divider, the
clock would go through 16 million cycles per second (16MHz), and would overflow and reset this
counter many times per second.
• The three timers (called timer0, timer1 and timer2) of Atmega328 can be either
operated in normal mode, CTC mode or PWM mode.
• For lab experiment 3 we will operate in normal mode (counter) using Timer0.
5
TIMER BASICS: REGISTERS (TIMER0)
• The timer can be programmed by some special registers, where we can configure the
pre-scaler for the timer, or the mode of operation and many other things necessary for
proper operation.
• The registers of interest for our purposes are:
• Timer/Counter Register – TCNT0 : to store timer count
• Timer/Counter Control Register – TCCR0A and TCCR0B : to define operation
mode and pre-scaler
• Timer/Counter Interrupt Flag Register– TIFR0 : to observe if there is any
overflow
• Output Compare Register - OCR0A and OCR0B: to match the timer count with
some custom value (for CTC or PWM mode, not needed for normal mode)
6
TIMER BASICS: REGISTERS (TIMER0)
• Timer/Counter Register – TCNT0 : This is where the 8-bit counter of the timer
resides. The value of the counter is stored here and increases/decreases
automatically. Data can be both read/written from this register. The register resets to
zero after each overflow.
8
TIMER BASICS: REGISTERS (TIMER0)
• Timer/Counter Interrupt Flag Register– TIFR0 : TOV0 bit is set (one) whenever TIMER0 overflows.
• Output Compare Register - OCR0A and OCR0B: Both Output Compare Registers A and B contain 8-bit
values that are continuously compared with the counter value (TCNT0).
• The OCF0A or OCF0B bit in TIFR register is set (one) whenever the count matches the stored value.
9
TIMER BASICS: REGISTERS (TIMER1)
• Most of the registers are very similar to Timer0.
• Timer/Counter Register – TCNT1H and TCNT1L (TCNT1): The main difference
between Timer0 and Timer1 is in the timer/counter register. The two Timer/Counter
I/O locations (TCNT1H and TCNT1L, combined TCNT1) give direct access, both for
read and for write operations, to the Timer/Counter unit 16-bit counter. The register
resets to zero after each overflow.
6a. Signals that timer count has 6b. Signals that timer count has
reached maximum reached minimum
1. Current
timer count
Comparator 3b. Sends signal if inputs equal
2b. Count stored in
8b. generate a
OCRnB
PWM or variable
frequency output
decides which mode the timer will
run on and decides the pre-scaler
for timer clock
TIMER FOR DELAY: CALCULATING COUNT
• Timer needs a clock pulse for each transition from one number to the next, so we want
to establish a formula relating the necessary timer count for a specific delay with the
timer clock period/frequency.
• For F_CPU = 16 MHz, system clock period = 1/16M = 62.5 ns. So, if timer clock is the
same as system clock, it takes only 62.5 ns for every transition (0 to 1, 1 to 2, etc.).
187.5 ns
∗ 𝟏
62.5 ns 62.5 ns 62.5 ns
𝑻=
𝒇
Count = 0 Count = 1 Count = 2
• We can see that 3 time periods are needed to count to 2, so timer count = (number of
periods needed to reach the count) -1.
• Also, we can see that to get a delay of 187.5 ns, we need 3 periods lasting 62.5 ns, so
the number of periods needed = Required delay/Timer clock period.
TIMER FOR DELAY: CALCULATING COUNT
(WITHOUT PRESCALER)
• Suppose we need a delay of 1 ms. To get an idea of how long it takes, let’s calculate the timer
count from the following formula:
∗ 𝟏
𝑇𝑖𝑚𝑒𝑟 𝑐𝑜𝑢𝑛𝑡 =
𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝑑 𝑑𝑒𝑙𝑎𝑦
−1 𝑻=
𝑇𝑖𝑚𝑒𝑟 𝑐𝑙𝑜𝑐𝑘 𝑝𝑒𝑟𝑖𝑜𝑑 𝒇
Or, 𝑻𝒊𝒎𝒆𝒓 𝒄𝒐𝒖𝒏𝒕 = (𝑹𝒆𝒒𝒖𝒊𝒓𝒆𝒅 𝒅𝒆𝒍𝒂𝒚 × 𝑺𝒚𝒔𝒕𝒆𝒎 𝒄𝒍𝒐𝒄𝒌 𝒇𝒓𝒆𝒒𝒖𝒆𝒏𝒄𝒚) − 𝟏
Here required delay = 1 ms and timer clock period = 62.5 ns, so Timer Count = (1m/62.5n)-1= 15999.
• So, the clock needs to tick 15999 times to give a delay of only 1 ms.
• Maximum possible delay for timer0 at 16MHz: 62.5 ns * 256 = 16μs
• Maximum possible delay for timer1 at 16MHz: 62.5 ns * 65536 = 4.096 ms
• If we plan to get delays simply by directly counting at system frequency, it is difficult to use an 8-bit
timer (as it has an upper limit of 255, after which it overflows and resets count to zero). Even with a
16-bit timer (which is capable of counting up to 65535), it is not possibly get longer delays.
TIMER FOR DELAY: CALCULATING COUNT
(WITH PRESCALER)
• To stay in the safe side, we use the highest available prescalar and reduce timer clock
frequency to 16M/1024 = 15625Hz, with a new timer clock period (= System clock
period * prescalar)= 62.5ns*1024= 64 μs. Now the needed timer count = (1m/ 64 μ) -1
= 15.6249.Now that Timer clock frequency = System clock frequency/ prescalar, we
can update the equation to:
6 June 2023
19
System Clock Options
The block diagram
represents the principal
clock systems in the AVR
and their distribution.
All the clocks need not be
active at a given time.
To reduce the power
consumption, the clocks to
modules not being used
can be halted by using
different sleep modes.
Problem Statement 1
Make an LED blink every 2 milliseconds while using Arduino system frequency
(F_CPU) 16 MHz, using timer to generate the delay without any application of
delay() function. Delay = 2 ms, so Timer0 can be used with pre-scalar 1024.
Number of count needed to reach 2 ms = (2,000 ms/64 μs) – 1 = 30.25 ≈ 31.
• Make an LED blink every 2 seconds while using Arduino system frequency (F_CPU)
16 MHz, using timer to generate the delay without any application of delay() function.
• Delay = 2 s, so Timer1 can be used with prescalar 1024.
• Number of count needed to reach 2s = (2/64 μ)-1=31249
Code:
Same code, only
register names
have 1 instead of 0
and delay length
changed!
6 June 2023 23
QUESTIONS
Q1. Prepare the necessary registers associated with the 8-bit
timer to achieve the necessary time counts. Consider the timer to
be running at normal mode with highest prescaling.
Answer:
Q2. If malware is found in the system, the system is designed to sound an alarm every 4s. This 4s count
has to be achieved from a timer directly (without the help of a loop). Select an appropriate timer from
the 2 available timers and set up the necessary registers associated with the used timer to achieve the 3s
time count. You may consider the clock frequency and Prescaler values mentioned in 1(a). Additionally,
compute the maximum possible time which can be counted using this timer.
REFERENCES
• ATMega328 manual
• https://www.avrfreaks.net/forum/tut-c-newbies-guide-avr-timers
• http://maxembedded.com/2011/06/avr-timers-timer0/
• https://cdn.sparkfun.com/assets/c/a/8/e/4/Atmel-42735-8-bit-AVR-Microcontroller-
ATmega328-328P_Datasheet.pdf.
THANKS FOR ATTENDING….
6 June 2023 27