Title: Timers
1Timers
2Outline
- Introduction
- What is a timer?
- How does it work?
- Types
- Timers and the HC11
- Main Timer
- Output Compare
- Input Capture
- Periodic Interrupts
- Pulse Accumulators
- Timer Applications and Examples
3Introduction to Timers
4Introduction
- What is a timer?
- A timer is an integrated circuit that uses an
oscillator measure elapsed time or count/time an
external event - What is an oscillator?
- Oscillators are used to generate a signal that
can regenerate and sustain itself - Usually made of quartz crystal
5Introduction
- How Does It Work?
- Applying a direct current to the crystal causes
it to vibrate - The oscillators frequency is determined by the
thickness and cut of the quartz - The frequency can also be set by using
combinations of resistors, inductors, and/or
capacitors
6Timers
- An external capacitor is used to determine the
off-on time intervals of the output pulses for
all IC timers - The time required for the capacitor to charge to
63.7 of the applied voltage is the time
constant, t
7Time Constant Example
- Time Constant
- The time constant of an RL circuit is also equal
to the inductance divided by the resistance. It
usually takes 5 time constants for the capacitor
to charge to the applied voltage. - Example
- R 1M?
- C 1 mF
- t 1,000,000 ? 0.000001
- 1 second
8Timer Types
- Also Known As Multivibrator
- Multivibrators are a class of circuits that are
designed to produce square waves or pulses. - There are 3 types of timers
- Monostable Multivibrators
- Astable Multivibrators
- Bistable Multivibrators
9Timer Types Monostable
- A Monostable Multivibrator
- A multivibrator with a single stable output
state. When triggered, the output will switch to
the unstable state for a set period of time,
which is determined by the resistor and capacitor
values. It remains in this state until a
separate signal triggers the timer. - One-Shot Pulse Output
- Delay On Output
- The duration of the output pulse, in seconds, is
approximately - T 1.1 ? R ? C (in seconds)
10Timer Types Monostable
- Monostable Multivibrator Circuit
11Timer Types Monostable
- Missing Pulse Detector
- Changes in the input pulse signal complete the
timing cycle - The monostable ON time is set approximately 1/3
longer than the expected time between trigger
pulses
12Timer Types Astable
- An Astable Multivibrator
- A multivibrator that produces a square wave
output from a DC voltage. - Astable Output Pulses
13Timer Types Astable
- An Astable Multivibrator
- The frequency of operation of the astable circuit
is dependent upon the values of R1, R2, and C. - f 1/(ln(2) x C x (R1 2 x R2))
- f 1/T
- f is in Hz. R is in ?
- C is in Farads T is in seconds
14Timer Types Astable
- Duty Cycle, D
- The duty cycle is defined as the ratio of the
time duration when output pulse is high to the
total period
15Anybody recognize this?
16The 555 Timer
- First introduced in the early 1970s the 555
timer is still widely used in many systems. - It is composed of over
- 20 transistors
- 15 resistors
- 2 diodes
17The 555 Timer Pin Functions
- Pin 1 Ground
- Connected directly to ground
- Pin 2 Trigger
- Input to the lower comparator and is used to set
the latch which causes the output to go high - Triggering happens when the voltage level is
taken from above to below a voltage level of 1/3
V
18The 555 Timer Pin Functions
- Pin 3 Output
- Output high is slightly lower (1.7V) than the
supply voltage - The state of the output pin will always reflect
the inverse of the logic state of the latch - Pin 4 Reset
- Used to reset the latch and return the output to
a low state - The reset voltage threshold is 0.7V, and a sink
current of 0.1mA
19The 555 Timer Pin Functions
- Pin 5 Control Voltage
- Direct access to the 2/3 V voltage divider point
in voltage control mode - Connect to ground through a 0.01 mF capacitor
when not in use - Pin 6 Threshold
- Input into the upper comparator
- Used to reset the latch, which drives the output
low
20The 555 Timer Pin Functions
- Pin 7 Discharge
- When the npn transistor it is connected is turned
on, the pin is shorted to ground - The timing capacitor is usually between pin 7 and
ground and is discharged when the transistor
turns on - Pin 8 V
- The positive supply voltage terminal to the 555
timer IC
21The 555 Timer Examples
- Schmitt Trigger
- The Schmitt trigger takes a noisy input and
creates a very clean output. - It uses the 1/3V and 2/3V thresholds to produce
the wave output
22The 555 Timer Examples
- A more stable timing output can be produced by
adding a transistor and diode the the RC network - The frequency can be varied over a wide range
will using a constant 50 duty cycle
23The 555 Timer Examples
- A one-shot monostable circuit that is continually
retriggered by input pulses - A missing pulse results in pin 3 going low until
a new input pulse is received
24The HC11 Timer
25Main Timer Block diagram
26Main Timer
- 16-bit free running counter
- It is a read only register using a double-byte
instruction like LDD or LDX - It is cleared on reset and count up continuously
- When FFFF is reached, counter rolls over to
0000
27Registers related to Main Timer
- Timer counter (TCNT)
- Timer flag register 2 (TFLG2)
- Timer mask register 2 (TMSK2)
28Counter Prescaler
- Allows 4 clocking rates
- of the timer counter
- E-Clock rate divided by 1, 4, 8, 16
- At reset the default prescale factor is 1
- Must be set during the first 64 E-Clock cycles
after reset
29Input Capture
- Used to record the time an event occurs
- When an edge is detected at a timer input pin,
the current value of the free-running counter is
stored in the corresponding - 16-bit input capture register
30- Can be read at any time by a pair of 8-bits
registers - Not affected by reset
31- Use Time Control Register (TCTTL2) to tell
- which edges you want to capture
32- Input capture status flags are automatically set
to one each time a selected edge is detected - Input capture interrupt enable bits
0
1
2
3
4
5
6
7
OC1F
OC2F
IC3F
TFLG1
1023
OC3F
OC4F
OC5F
IC1F
IC2F
33Output Compare
- There are 5 output compare registers
Bit 15
-
Bit 8
1016
-
-
-
-
-
TOC1
1017
Bit 7
Bit 0
-
-
-
-
-
-
Bit 15
Bit 8
1018
-
-
-
-
-
-
TOC2
1019
Bit 7
Bit 0
-
-
-
-
-
-
Bit 15
Bit 8
101A
-
-
-
-
-
-
TOC3
101B
Bit 7
Bit 0
-
-
-
-
-
-
Bit 15
Bit 8
101C
-
-
-
-
-
-
TOC4
101D
Bit 7
Bit 0
-
-
-
-
-
-
Bit 15
Bit 8
101E
-
-
-
-
-
-
TI4/O5
101F
Bit 7
Bit 0
-
-
-
-
-
-
34- Output capture status flags are automatically set
to one each time the number stored in one of the
compare registers is the same as the value of the
main timer - Output capture interrupt enable bits
35- When a successful output compare occurs, pin
actions on OC2-OC5 of Port A can be done
OC1M
OC1M7
OC1M6
OC1M5
OC1M4
OC1M3
100C
100D
OC1D7
OC1D6
OC1D5
OC1D4
OC1D3
OC1D
1020
TCTL1
36Forced Output
If you need to change state of Port A Pin BEFORE
output compare occurs use the Time Compare Force
Register
CFORC 100B
Output Compare 1
Output Compare 2
Output Compare 3
Output Compare 4
Output Compare 5
37Real-Time Interrupt
- Generates hardware interrupts at a fixed
- periodic rate with
-
B0
B1
PACTL
1026
38- TMSK2 1024
- Real-Time Interrupt Enable
- TFLG2 1025
- Real-Time Interrupt Flag
B6
RTII
B6
RTIF
39Pulse Accumulator
- 8-bit Counter
- 2 operating modes
- Event counting mode
- Gated time accumulation mode
40Registers
- PACNT 1027
- 8 Bit PA Count
- PACTL 1026
- Data Direction for PA7 Pin
- PA Enable
- PA Mode
- PA Edge
B6
B5
B4
B7
41- TFLG2 1025
- PA Overflow Flag
- PA Input Edge Flag
- TMSK2 1024
- PA Overflow Interrupt Enable
- PA Input Edge Interrupt Enable
42Applications and Examples
43Applications of Timers
- Timers are used for a variety of applications in
many situations - Required for any application requiring real-time
control - Can be used for both input and output timing
- Often multiple timers are needed for the various
functions required for real-time control - Timers are basically everywhere you look
watches, household appliances, manufacturing
systems, basically anything automated
44Applications of Timers
- Pulse Width Modulation (PWM) is a very common
method of generating an analog signal - Requires the use of timers as well as interrupts
- Can be used to run DC motors
- Hardware interrupt timing is important for many
timing applications - Reaction time or delay time between events
- Pulse Accumulator
45Pulse Width Modulation Example
- PWM utilizes the output compare ability of the
HC11 - There are 5 usable output compare systems (OC1
OC5), with a 16-bit timer output register
associated with each register x (TOCx) - This value is used to compare with the
free-running counter TCNT - An interrupt is generated when these values are
equal - The interrupt changes the status of the OCx pin
on the board
46Pulse Width Modulation Example
The OC1 system is unique and should not be used
as the other output compare systems
47Pulse Width Modulation Example
- To use the Output Compare
- The timer control register (TCTL1 _at_ 1020) must
be set - Make a copy of the count in the main timer (TCNT)
- Add the copied value to the desired delay time
and store the sum in OCx register - Finally, perform the action specified by the
TCTL1 register
TCTL1, 1020
OMx OLx 0 0 OCx does not affect pin
0 1 Toggle OCx pin on successful
compare 1 0 Pull low the OCx pin on
successful compare 1 1 Pull high the
OCx pin on successful compare
48Pulse Width Modulation Example
Start
Example Generate a 1KHz square wave form signal
with a 40 duty cycle (width of high pulse is 40
of total period). A flowchart of the procedure
is shown at left Taken from H. Huang,
MC68HC11 An Introduction
Set OC2 pin to high Set OC2 action to toggle
Clear OC2F flag
start OC2 output compare with a delay of 400 ms
no
OC2F 1?
yes
Clear OC2F flag Start OC2 output compare with a
delay of 600 ms
no
yes
OC2F 1?
Flowchart Of The Waveform Generation
49Pulse Width Modulation Example
regbas equ 1000 base address of I/O
register block PORTA equ 00 offset of
PORTA from regbas TOC2 equ 18 offset
of TOC2 from regbas TCNT equ 0E offset
of TCNT from regbas TFLG1 equ 23
offset of TFLG1 from regbas toggle equ 40
value to select the toggle action lotime equ
1200 value to set low time to 600 ms (2
Mhz clock) hitime equ 800 value to set
high time to 400 ms org 1040 ldx
regbas bset PORTA,X 40 set OC2 pint
to high bclr TFLG1,X BF clear OC2F
flag ldaa toggle select output compare
action to be staa TCTL1,X toggle ldd
TCNT,X start an OC2 operation which
toggles the OC2 pin addd hitime with a
delay of 800 E clock cycles std TOC2,X
high brclr TFLG1,X 40 high wait until
OC2F is set to 1 bclr TFLG1,X BF clear
OC2F flag ldd TOC2,X start another OC2
operation which toggles the OC2
pin addd lotime with a delay of 1200 E
cycles std TOC2,X
50Pulse Width Modulation Example
low brclr TFLG1,X 40 low wait until OC2F
is set to 1 bclr TFLG1,X BF clear
OC2F flag ldd TOC2,X start another
OC2 operation which toggles the
OC2 pin addd hitime with a delay of
800 E cycles std TOC2,X bra
high end
Original code taken from H. Huang,
MC68HC11 An Introduction Modified for the Axiom
CME-11E9-EVBU
It can be seen with more modification, a varying
duty cycle can be used, such as one that
generates a sine wave
51LED Flashing Example
- This example will show the versatility of the
timer subsystem of the HC11 microcontroller - It will flash a series of 8 LEDs in a regular
pattern - The Program is written in C and uses the hc11e9.h
header - A schematic of the circuit is shown below
52LED Flashing Example
include lthc11e9.hgt void delay(char
delay_time) void main() _H11TFLG1 0x80
/clear OC1F flag / TOC1 TCNT 20000
/Short 10 ms delay / while(1) unsigned
char flash_tab252 0xFF,25, 0x00,25,
0xFF,25, 0xFF,25, 0x00,25, 0xFF,25,
0x00,25, 0x80,100, 0x40,100, 0x20,100,
0x10,100, 0x08,100, 0x04,100, 0x04,100,
0x02,100, 0x01,100, 0x01,25, 0x02,100,
0x04,100, 0x08,100, 0x10,100, 0x20,100,
0x40,100, 0x80,100, 0x00,100 int
i for (i0 ilt25 i) _H11PORTB
flash_tabi0 /Store the value in Port B
/ delay(flash_tabi1)
53LED Flashing Example
This function creates a delay of a number of
milliseconds
void delay(char delay_time) _H11TFLG1
0x40 _H11TOC2 _H11TCNT 20000 while(delay_t
ime) while(!(_H11TFLG1 0x40)) _H11TFLG
1 0x40 --delay_time _H11TOC2
20000
54References
- van Roon, Tony. 555 Timer Tutorial.
http//www.uoguelph.ca/antoon/gadgets/555/555.htm
l - Huang, Han. MC68HC11 An Introduction
- Spasov, Peter. Microcontroller Technology The
68HC11