Lec3 Timers (2)
Lec3 Timers (2)
Low-speed bus
Bus
interface
Device
Digital timer/counters are used throughout embedded
Reset Time-out
Watchdog
CPU
timer
Timer Modes
• The Timers have 4 different modes of operation. 2 of these modes must be
chosen at all times in order to set the timer. These modes are :
• Periodic vs. One-shot mode
• In periodic mode, the Timers continues counting after it reaches timeout. It merely
clears the timeout flag of the Timer and restarts.
• In one-shot mode, Timer stops counting after timeout.
• Up vs. Down counting mode.
• In the up mode, the timer starts from zero and counts up to the value set in the Interval
Load Value Register.
• In the down mode, the timer counts down from the value set in the Interval Load Value
Register to zero.
Clock Types and Counter Modes
• Clock are 3-types based on specific process usage
• System clock - periodic ‘tick’ for the system clock
• Refresh clock - regular interrupt on every couple of cycles to perform a
dynamic memory refresh
• Audio clock - source of square waveforms for use as audio tones with the
built-in speaker.
• Counter have 6- modes
• Mode 0 (Interrupt on terminal count) - An initial value is loaded into the
counter register and this then immediately starts to count down at the
frequency determined by the clock input. When the counter reaches zero,
an interrupt is generated.
Output goes high
pulled low, the counter
Count reloaded with the initial value and the
3426523
process repeated.down
This is mode 3 with the 8253.
Counters Modes by 1 on
Lo a d initievery
a l v a lu e
(0000004)
clock O ut p ut g o e s lo w
• Mode 1 (Programmable one-shot ) - Create a single pulse with a 0000004
programmable
0000001 cycle
duration. The pulse length is first loaded into the counter. When the counter reaches
Output goes low
zero, the counter output goes low thus ending the pulse.
C o u nt 0000000
0000003
O ut p ut g o e s hig h
• Pulse duration is determined by the initial down
value loaded into the counter times the clock by 1 on
period. Clock e v e ry
0000002
• Can be used to provide pulse width c lo c k
modulation for power control where the Gate c y cle 0000001
gate is connected to a zero crossing or similar
detector or clock source to create a periodic
Output Length 0000000
= counter value x
signal.
clock period
• Mode 2 (Rate Generator) Programmable
- Divide by one-shot timer counter mode
N mode where N is defined by the initial C lo c kThe pulse duration is determined by the initial value loaded
value loaded into the counter. When the into the counter times the clock period. While this is a common
timer/counter mode, many devices such as the 8253 incorporat
counter reaches zero, the output is pulleda Oreset.
ut p ut
If the gate signal is pulled low and then high again to creat
low, the counter reloaded with
Ratethe initial(divide
generation value and
a newbyrising
N) edgethewhile
process repeated
the counter is counting down, the curren
count value is ignored and replaced by the initial value and th
Square wave rate generator
Counters Modes
• Mode 3 (Square wave rate generator) - Similar to mode 3 except that
the waveform is a square wave with a 50:50 mark/space ratio.
• Mode 4 (Software triggered strobe) - When mode 4 is enabled, the
counter will start to count as soon as it is loaded with its initial value.
When it reaches zero, the output is pulsed low for a single clock
period and then goes high again.
• can be used as a software-based watchdog timer where the output is
connected to a non-maskable interrupt line or a system reset
• Mode 5 (Hardware triggered strobe) - Mode 5 is similar to mode 4
except that the retriggering is done by the external gate pin acting as
a trigger signal.
Counting Modes
• 3- counting modes
• Up-counting mode: counter starts from 0 to a constant and then restarts
from 0. Software sets up the constant and stores it in a special register
called the auto-reload register (ARR). For example, if ARR is 4, the counter
value is 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, and repeats until the timer is disabled.
Counting Period of Sawtooth Waveform= (1 +ARR) x (1/fclk_cnt)
• Down-counting mode: counter starts from the auto-reload value down to 0
and then restarts from the auto-reload value. For example, if ARR is 4, the
counter value is 4, 3, 2, 1, 0, 4, 3, 2, 1, 0, and repeats until the timer is
disabled.
• Center-aligned counting mode: which performs up-counting and down-
counting alternatively. For example, if ARR is 4, the counter value is 0, 1, 2,
3, 4, 3, 2, 1, 0, and repeats until the timer is disabled. Counting Period of
Triangle Waveform= 2 x ARR x (1/fclk_cnt)
1
Counting Period =(1 + ARR) x f
Counting Modes
CLOCK_CNT
1
auto- ,
=
Counting Period (1 + ARR) x f
CLOCK_CNT
reload ,,-----------------' .. ________________ ,
value
(ARR)
auto-
reload
value
(ARR)
. -----------------------------------"!
1
Counting Period = 2 x ARR x f Underflow
CLOCK_CNT
Counter Update Events
• 3-update events
• Overflow: In up-counting mode, overflow occurs when the
counter is reset to 0.
• Underflow: In the down- counting mode, underflow occurs when
the counter is reset to ARR.
• Both: In the center-aligned counting mode, underflow and
overflow occur alternatively.
Timer/Counter Special Usages
• Compare Output
• Input Capture
• PWM
Output
15.1 Timer Organization and Counting Modes
Compare Output
asic diagram of the output compare of a timer. The timer counter
A timer is a free-run hardware counter that increments or decrements
apture/compare register (CCR) holdsThe
clock cycle. thecounter
value runs
that continuously
is compareduntil the timer is disabled.
• Comparator consistently
processcompares the counterwhen
restarts automatically valuethewith some
counter given0 during dow
reaches
constant, some maximum value during up-counting. Software can select the frequen
t channels shareanthe
• Generates sameclock
output free-run
or thattimer
the free-run
ansointerrupt counter. Therefore,
counter
if they are increments
equal. theor decrements at some desir
es the timer counter withIffour CCR
a timer registers
works simultaneously
as output compare, as shownandin Figure 15-1, the comparat
• Software can program the constant value to control the timing of outputs or
dent interrupts.
outputs based on thecompares
comparison results.value with some given constant, and generates an
the counter
interrupt if they are equal. Software can program the constant value to con
• Clock
imer to drive
counter the timer
(CLOCK_CNT) counter (CLOCK_CNT)
can beor slowed
of outputs interrupts.down bycana be slowed down by a
constant
constant
o generate factor
output called
that spansprescaler
over a longto aperiod.
long
Constant
period. Value
Generating
fcwcK PSC Set by software
A=B an interrupt if
fcwcK_CNT = +1 equal
Timer
Counter
es the timer's resolution, but decreases theClock
chance of overflow
Counter++ (or Counter--)
Signal
roves the energy efficiency. for each clock cycle
Applications
• Output Compare is used to Generate certain digital waveforms for control
purposes
• Many applications in microcontroller applications:
• Start analog devices
• Control speed of motors
• Control power output rate
• Communications
• Control servo
Output Compare
Example
• Example: Generate a waveform that is 1-cycle high, 2- cycle low, 3-cyle high, 1-
Example:
cycle low, Generate a waveform that is 1-cycle high, 2-
and repeating
• The MCU cycle low, 3-cyle
may generate outputhigh,
events 1-cycle low, and
(transitions) repeating
at 220 (current time), 221,
223, 226,
The227 and so
MCU on with
may initial output
generate state as events
low (transitions) at 220
(current time), 221, 223, 226, 227 and so on with initial
state as low
220 221 222 223 224 225 226 227
1 2 3 1
processor chip, external crystal oscillators, or some internal trigger signal such as the
Example
output of another timer. External clocks are preferred over internal clocks because
external clocks are more accurate than internal clocks.
16-bit Auto
Reload Register
Clock (ARR)
selection
Internal Clocks
Timing Diagram
Auto Reload \
Value (ARR) -------------- -
I I I
I I I
Compare/Capture _____ TI ___ _ _ ____ I_____ _ _____ TI ___ _
Value(CCR)
I I I
I I I
0
i
Timer Output ! Toggle i Toggle ! Toggle
"''"'
None one-pulse
None one pulse 30V
Mode i
Figure 15-2. A timer is used as input capture. The edge detector triggers the hardware to copy
Applications
• Many applications in microcontroller applications:
• Measure rotation rate
• Remote control
• Sonar devices
• Communications
• Generally, any input that can be treated as a series of events, where the
precise measure of event times is important
• An event is a transition of binary signal
Example: How many events make up the following
waveform
Example: The input is understood as events occurring at
the following times: 220, 221, 223, 226, and 227 with
• An input digitalized and then times captured
• initial
The inputstate as lowas events occurring at the following times: 220, 221,
is understood
223, 226, and 227 with initial state as low
Operation
• A timer can be used for triggering an output at a specified time to
general output signals (PWM output, comparator output)
• Input capture is to find the time span between two rising or falling
transitions in an internal or external signal.
• Capture occurs on (1) either rising or falling edges, (2) only falling edges, or
(3) only rising edges.
• When the desired transition is detected, the timer hardware automatically
captures this time instant by copying the value of the free-run counter
(CNT) to the compare and capture register (CCR).
• At the same time, the timer hardware generates an interrupt or DMA
request and sets the CCIF flag in the status register (SR)
• Similarly, the difference between a rising edge and a falling ed
Operation 1 pulse width.
Operation 2 Figure 15-18. Input capture measures the time span between two events.
http://class.ece.iastate.edu/cpre288 12
PWM
• PWM is a simple digital technique to control the value of analog variable.
• PWM uses a rectangular waveform to quickly switch a voltage source on and off to
produce a desired average voltage output.
• Although the output is binary at any time instant, the average output over a time span
can be any value between 0 and the maximum voltage.
• Percentage of time in the on state within one period is proportional to the mean value
of the voltage output. Consequently, when software changes the duration of the on
state, the output voltage is adjusted accordingly to emulate an analog signal.
• PWM is used to control motor speed, torque control, digital encoding in
telecommunication, DC-to-DC power conversion, audio amplification, LED
brightness control, etc.
must be at least 120 Hz to prevent the flickering effects that humans can see.
pulse on time (T0 n)
Duty Cycle
duty cycle = x 100%
pulse switching period (T5 )
T The average value of a simple PWM output based on a sawtooth carrier signa
on X 100%
Ton +Taff constant reference, as illustrated in Figure 15-9 and Figure 15-10, is linearly propo
• Average value of a simple 1
toPWM
the dutyoutput
cycle. based on a sawtooth carrier signal
and a constant reference,
pulse switching period = - - - - - - - - - - -
PW M switchingThe linearly
duty cycle
frequency proportional to the duty cycle.
is defined as follows:
Duty
• duty
ng the cycle
cycle, iscandefined
software control this as follows:
average value. In the LED example,
duty cycle =
pulse on time (T n)
x 100%
0
ness is determined by the PWM duty cycle. Figure 15-9 and Figure 15-10 give
pulse switching period (T5 )
mples in which the average output is 1/6 and 1/2, respectively.
T
Counter
on X 100%
Auto Reload
Switching period Ts
Carrier Signal
Ton +Taff
Value (ARR)
Com pa ref
where Reference
Signal
Capture
Value (CCR)
1
pulse switching period = - - - - - - - - - - -
PW M switching frequency
By changing the duty cycle, software can control this average value. In the LED ex
!
I
!
I
I
i
the brightness is determined by the PWM duty cycle. Figure 15-9 and Figure 15-
I :
Taff !Toni three examples in which the average output is 1/6 and 1/2, respectively.
v r--------<!r''-,:!
PWM Counter
Output Switching period Ts
Auto Reload Carrier Signal
Value (ARR) Reference
Average
Output Com pa ref Signal
--------- - ---------- - --------- - ------ Capture
Value (CCR)
eriod:
re • For up- or down-counting:
15-11. Reference output (OCREF) in PWM mode 1 and PWM mode 2
Auto-Reload Value
(ARR)
n-counting:
Period:
Clock Period of Timer
wn-counting:
PWM Period = (1 +ARR) x
1 + Prescaler CCR
Clock Period of Timer
PWM Period = (1 +ARR) x
nting mode: 1 + Prescaler
• For center-counting mode:
Clock Period of Timer
unting
PWMmode:
Period= 2 x ARR x - - - - - - - Reference
- Output
1 + Prescaler
Clock Period of Timer
(OCREF)
LED+
Image
LED-
VDD
1 1 1 1 0
VSS
DB0
DB1
DB2
DB3
DB4
DB5
DB6
DB7
RW
5V
the FONT
VO
RS
E
• N characters per line 1 0 1 0 0 Pattern
• K lines 1 0 0 1 0
• (20 x 2) 5V Write
1 0 0 0 1 Backlight
Only 0 0 0 0 0 (optional)
Microprocessor
NxK
Display Memory (DDRAM)
N Characters per line R
(It stores all characters GPIO
K lines
received.) Pins
11110
e.g., 20x2
10001
10001 LCD Display
11110 COM
o Read/Write (RW) connects to the ground if processor does not read data from LCD
HD44780 Connection Diagram
HD44780 Connection
(8-bitDiagram
data bus)(8-bit data bus)
• Read/Write (RW) connects to the
LCD Display
LED+
LED-
VDD
VSS
DB0
DB1
DB2
DB3
DB4
DB5
DB6
DB7
RW
5V
VO
RS
E
data from LCD
• LED+ and LED- pins provide 5V voltage
5V Backlight
(optional)
0x39 0x013 8
0x014 a
h
8 2x20
0x38
0x68 0x040 20x2
0x053
GPTM Module for TM4C123GH6PM