0% found this document useful (0 votes)
32 views

Introduction To TIMER Before Exp 3

This document describes implementing a traffic light control system using timers on an Arduino without using delay functions. It explains timer basics like registers for Timer0 and Timer1. It provides the formula to calculate the timer count needed for a given delay based on the system clock frequency and timer clock period. Code examples are given to make an LED blink every 2ms using Timer0 and every 2s using Timer1. References for further reading on AVR timers are also included.

Uploaded by

saira israt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Introduction To TIMER Before Exp 3

This document describes implementing a traffic light control system using timers on an Arduino without using delay functions. It explains timer basics like registers for Timer0 and Timer1. It provides the formula to calculate the timer count needed for a given delay based on the system clock frequency and timer clock period. Code examples are given to make an LED blink every 2ms using Timer0 and every 2s using Timer1. References for further reading on AVR timers are also included.

Uploaded by

saira israt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

1

AMERICAN INTERNATIONAL UNIVERSITY – BANGLADESH(AIUB)


Where leaders are created

Implementation of a Traffic
Control System using Timer
BY NIRJHOR ROUF
2

Problem Statement
Make three LEDs blink and implement a traffic control system
while using Arduino system frequency (F_CPU) of 16 MHz
using timer to generate the delay without any application of
delay() function.
3

Problem Solution
 We will change the code used for controlling the traffic lights
in experiment 1 by simply replacing the delay functions with
a function written by us that will use timer.
 The
function will accept an input argument mentioning the
number of seconds of delay and run a nested if loop.
4
Previously used code in Exp. 1
To-do list:
• Initialize timer registers
• Write a new delay function using timer
• Change the marked lines
5
Timer Basics: Registers (Timer0)
 Timer/Counter Control Register A for Timer0 – TCCR0A: The
bits WGM02 (from TCCR0B), WGM01 and WGM00 decide which
mode the timer will run on.

 Timer/Counter Control Register B for Timer0 – TCCR0B: The


three Clock Select bits- CS02, CS01, CS00 select the clock
source and pre-scalar value to be used by the Timer/Counter
6
Timer Basics: Registers (Timer0)

Please note that if


you do not initialize
this register, all the
bits will remain as
zero and the
timer/counter will
remain stopped.
7
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 or
decreases automatically.
✓ Data can be both read or 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 of 16-bit) give direct access, both for read and write operations, to
the Timer/Counter unit 16-bit counter.
✓ The register resets to zero after each overflow.
10
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


11
Timer for Delay: Calculating Count
 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.
187.5 ns

62.5 ns 62.5 ns
62.5 ns

Count = 0 Count = 1 Count = 2


12
Timer for Delay: Calculating Count
 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
𝑇𝑖𝑚𝑒𝑟 𝑐𝑙𝑜𝑐𝑘 𝑝𝑒𝑟𝑖𝑜𝑑

Here, required delay = 1 ms and timer clock period = 62.5 ns,


so Timer Count = (1m/62.5n) – 1 = 15,999.
 So, the clock needs to tick 15,999 times to give a delay of
only 1 ms.
13
Timer for Delay: Calculating Count
 Maximum possible delay for Timer0 (which is an 8-bit
timer) at 16 MHz : 62.5 ns × 256 = 16 μs
 Maximum possible delay for Timer1 (which is a 16-bit
timer) at 16 MHz: 62.5 ns × 65,536 = 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 can
count to 65,535), it is not possible to get longer delays.
14
Timer for Delay: Calculating Count
 Tostay in the safe side, we use the highest available pre-scalar
and reduce timer clock frequency to 16M/1024 = 15625 Hz, with a
new timer clock period (= System clock period × pre-scalar) =
62.5ns×1024 = 64 μs. Now, the needed timer count = (1ms/64μs)
– 1 = 14.625. Now that Timer clock frequency = System clock
frequency/pre-scalar, we can update the equation
𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝑑 𝑑𝑒𝑙𝑎𝑦
𝑇𝑖𝑚𝑒𝑟 𝑐𝑜𝑢𝑛𝑡 = −1
𝑇𝑖𝑚𝑒𝑟 𝑐𝑙𝑜𝑐𝑘 𝑝𝑒𝑟𝑖𝑜𝑑
to
𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝑑 𝑑𝑒𝑙𝑎𝑦 × 𝑆𝑦𝑠𝑡𝑒𝑚 𝑐𝑙𝑜𝑐𝑘 𝑓𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦
𝑇𝑖𝑚𝑒𝑟 𝑐𝑜𝑢𝑛𝑡 = −1
𝑝𝑟𝑒𝑠𝑐𝑎𝑙𝑎𝑟
15
Timer for Delay: Calculating Count
Maximum possible delay for Timer0 at 15625 Hz: 64
μs× 256 = 16.384 ms
Maximum possible delay for Timer1 at 15625 Hz: 64
μs× 65536 = 4.194304 s
To get longer delays, we will use Timer1, and for delays
longer than 4 s, nested if statements can be used.
16
Timer Modes: Normal Mode
 The simplest mode of operation is the Normal mode. In this
mode, the counting direction is always up (incrementing),
and no counter clear is performed.
 The counter simply overflows when it passes its maximum
value and then restarts from zero.
 In normal mode of operation, the Timer/Counter Overflow
Flag (TOV0) will be set in the same timer clock cycle as the
TCNT0 becomes zero.
Problem Statement 1 17
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.

WGM01 and WGM00 set to zero for normal mode

WGM02 set to zero for


normal mode, CS02, CS01,
CS00 set to 1,0,1 for pre-
scalar 1024

Make LED blink


18
Problem Statement 2
 Make an LED to blink every 2 seconds while using Arduino
system frequency (F_CPU) 16 MHz, using a timer to
generate the delay without any application of delay()
function.
 Delay = 2 s, so Timer1 (remember that it is a 16-bit timer)
can be used with the pre-scalar value of 1024.
 Number of count required to get a delay of 2 s = (2,000,000
ms/64 μs) – 1 = 31,249.
19
Code:
Same code, only
register names
have 1 instead of
0 and delay
length changed!
Modified Code using Timer 20
Modified Code using Timer 21
Modified Code using Timer 22
23

References

 ATMega328 manual
 https://www.avrfreaks.net/forum/tut-c-newbies-guide-avr-timers
 http://maxembedded.com/2011/06/avr-timers-timer0/
24
Thanks for attending….

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy