Timer System Applications Additional Examples 1

advertisement
Timer System Applications
Additional Examples
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
1
Motor Speed
•
•
•
We can use input capture to measure the speed of a DC motor
A sensor to measure motor speed must be used so that the microcontroller can
control the speed
The motor speed can be fed back by using an optical encoder, infrared detector, or
a Hall-effect sensor
Magnets
Hall effect
sensor
Hall-effect
transistor
t
T/2
T is the time for one revolution
Figure
8.54 The output
waveform
ofMines
the Hall
effect transistor
Microcomputer Architecture
and Interfacing
Colorado
School of
Professor
William Hoff
2
Optical encoder
+5V
• An optical encoder sensor uses
an LED and a phototransistor
+5V
output
signal
• The LED and phototransistor
have matched wavelengths
• The wheel has a hole in it; when
the hole passes between the
LED and phototransistor, the
phototransistor conducts and
the output is pulled high
• Optical encoders can be
transmissive (break-beam) or
reflective
Microcomputer Architecture and Interfacing
integrated
package
Colorado School of Mines
Professor William Hoff
discrete
components
3
Measuring motor speed
• We use input capture to capture rising edges from the optical
encoder
• If there is one hole in the wheel, then the period of the
square wave is just the time for one revolution
• If there are N holes, then the time for one revolution is N
times the measured period
• Example:
–
–
–
–
We have a wheel with 4 holes
The measured period between holes is 5 ms
The time for one revolution is 4x5 = 20 ms
The motor speed is 1/(20 ms) = 50 Hz (i.e., 50 revolutions per second)
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
4
Example
• Let’s measure the period of an
optical encoder on PT3 (channel
3)
• Approach:
– We’ll look for rising edges
– Capture the start time, then the
end time, and then subtract
• Assume the period is at most
128 ms
– If the prescale factor = 1, we will
overflow at 2.73 ms
– So, set the prescale factor = 64 …
the counter won’t overflow until
174.8 ms
Microcomputer Architecture and Interfacing
Colorado School of Mines
/* Bottom three bits of TSCR2
(PR2,PR1,PR0) determine TCNT period
divide at 24MHz
000
1
42ns TOF 2.73ms
001
2
84ns TOF 5.46ms
010
4
167ns TOF 10.9ms
011
8
333ns TOF 21.8ms
100 16
667ns TOF 43.7ms
101 32
1.33us TOF 87.4ms
110 64
2.67us TOF 174.8ms
111 128
5.33us TOF 349.5ms
*/
Professor William Hoff
5
Algorithm steps
• Main
• Data variables
– Set up timer
– PERIOD
• TSCR1 register
• the measured
period (in units of
time clock cycles
• At prescale factor
of 64, one tick is
2.67 us)
– Enable timer by setting TEN bit = 1
– Enable fast flag clear by setting TFFCA bit = 1
• TSCR2 register: Set timer prescale factor = 64
– Set up channel 3 for input capture
• TIOS register: Set channel 3 to be input by setting IOS3
bit = 0
• TCTL4 register: Look for rising edges on channel 3
• TIE register: Enable interrupts on channel 3
– LASTTIME
• The time the last
edge was captured
– Go into an infinite loop
• IC3 ISR
– Get capture time from TC3
– Compute difference from LASTTIME, store into
PERIOD
– Save capture time into LASTTIME
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
6
Measuring long periods
• It is possible to measure very long periods with input capture, by
counting timer overflows
– In this class we won’t need to do this … we will just slow down (prescale)
the timer clock so that it won’t overflow
• But, here is how you would do it:
– Let
•
•
•
•
ovcnt
diff
edge1
edge2
= TCNT counter overflow count
= the difference of two consecutive edges
= the captured time of the first edge
= the captured time of the second edge
– The pulse width can be calculated by the following equations:
• Case 1: edge2 ≥ edge1
pulse width = ovcnt × 216 + diff
• Case 2: edge2 < edge 1
pulse width = (ovcnt – 1) × 216 + diff
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
7
Download