PT 4
PT 4
Part IV--
IV--Timer
Timer Function
J
Jason J
J. G
Gu
Department of Electrical and Computer Engineering
Dalhousie University
Cl k iinput
Clock
Supplied from I/O pin: counter
Generated from MCU clock circ
circuit:
it timer mode
Using a timer to create time delays
Counts up: nn-bit
bit timer: counts up from 0 and reaches
2n – 1; overflows and rolls back to 0
Counts down
Pulse-Width
Pulse Width Modulation (PWM): waveform
continues by itself until the duty cycle or period
must be changed
Implemented through special circuitry added to the
timer system so that the CPU only has to set up the
duty cycle and period once
PWM module consists of an output pin, a
timer/counter a period register
timer/counter, register, a duty register
register.
There are single slope and dual slope PWM
Timer/counter (8
(8- or 16-bit)
16 bit) operation modes:
normal, CTC, fast PWM, phase-correct PWM, and phase-
and-frequency-correct PWM
8-bit timer/counter operation modes
Timer operation mode: selected by the Timer/Counter
control register A ( TCCRnA
TCCRnA,
n = 0, …, 5) and one or two bits in the Timer/Counter
control register B (TCCRnB,
n=0 0, …, 5)
example:
l write
it a sequence off AVR iinstructions
t ti
and C statements to be executed by ATMega644
MCU to generate a 2KHz waveform with 80%
duty cycle from the OC1B pin using timer 1,
assuming g fclk_I/O=16MHz
_
selecting clk_I/O/8 as source, high and low of the
2KHz wave form consist of 800 and 200 timer
clock cycles.
U i th
Using the normall mode
d tto play
l a song
Created by modifying the siren program
Song played using a speaker
Table with frequencies and durations of all the notes in a
music score
Program uses output-compare function to generate the digital
waveform with the specified frequency and duration of each
note
Song played in Assembling
Song played in C
Example
p : Write a C p program
g to measure
the pulse width of a signal connected to
the ICP1 pin of the MEGA2560 MCU MCU.
Using
g OCRnA to hold the TOP value
Advantage: generated PWM waveform is always
symmetric
Using ICRn to hold the TOP value
Works well when the TOP value is fixed
Extreme cases for the fast PWM
OCRnx register is set equal to the BOTTOM
S tti the
Setting th OCRnx
OCR equall tto TOP
Solution:
DDRB = 0x20; // configure PB5/OC1A pin for output
TCNT1 = 0;
0 // force
f ti
timer1 1 to
t countt up ffrom 0
ICR1 = 10000; // set counter TOP value to 10,000
OCR1A = 7000; // set duty cycle to 70%
TCCR1A = 0x82;; // select non‐inverting,g, fast PWM mode
TCCR1B = 0x1A; // select clk_IO/8 as clock source and ICR1A as TOP
example:
l write
it a sequence off AVR iinstructions
t ti
and C statements to be executed by ATMega640
MCU to generate a 2KHz waveform with 80%
duty cycle from the OC1B pin using timer 1,
assuming g fclk_I/O=16MHz
_
selecting clk_I/O/8 as source, high and low of the
2KHz wave form consist of 800 and 200 timer
clock cycles.
Based
B d on a d
dual-slope
l l operation
ti
Available only in a 16-bit timer
Counter counts repeatedly from BOTTOM to
TOP and then from TOP to BOTTOM
fPFCPWM = (fCLK_I/O ÷ timer prescaler) ÷
(2 × TOP)
The procedure of generating the PWM waveform
using PFCPWM is identical to the PCPWM mode
DC Motor
M t Driver
Di ICs
IC
Available in many current and voltage ratings
Example: SN754410 from TI
Driving a DC Motor Using the SN754410
Illustrated in Figure
g 11.22
The output waveform of the Hall-effect transistor
N
Normal
lOOperation:
ti
OC1A output WPM, OC1B output 0v
Reverse operation
OC1A output 0V, OC1B output WPM
Brake the motor
OC1A 0V, OC1B 5V.