R E A L T MI E
LESSON 17 : E M B E D D E D S Y S
DESIGN CASE STUDY OF 8051
P u l se se M e a s u r e m e n t
T E M S
Sensors used for industrial and commercial control applications frequently produce pulses that contain information about the quantity sensed. Varying the sensor output frequency, using a constant duty cycle cycle but variable frequency pulses to indicate changes in the measured variable, is most common. Varying the duration of ‘the pulse p ulse width, width, resulting in constant frequency but variable duty cycle, is also used. In this section, we examine programs that deal with with both b oth techniques. Measuring Frequency Timers T0 and Tl can be used to measure external frequencies by configuring one timer as a counter and using the second timer to generate a timing interval over which the first can count. The frequency of the counted pulse train is then:
Unknown frequency frequency = Counter/ timer For example, if if the counter co unter .count s 200 pulses over over an interval of .1 second gen-erated by the timer, the frequency is: UF = 200/ .1 = 2000 2000 Hz Certain fundamental limitations govern the range of frequencies that can be measured. An input pulse must make, a I-to-O transition lasting two ma-chine ma-chine cycles, cycles, or f/ 24, to be counted. This restriction on pulse deviation yields a maximum frequency o(500~ilohertz using our 12 megahertz crystal (as-suming (as-suming a square wave input). The lowest frequency that can be counted is limited by the duration of the time interval generated, which can be exceedingly exceedingly long long using all the RAM RAM to count co unt timer rollovers (65.54 milliseconds x 2"32768). There is no practical limitati limitation on on t he lowest lowest frequency that can be counted. Happily, most frequency-va fr equency-variable riable sensors generate signals signals that th at fall inside of 0 of 0 to 500 kilohertz. Usually the, signals have a range of 1,000 to 10,000 hertz.A timing interval of 1 second generates a frequency count accurate to the nearest 1 hertz; an interval of .1 second yields yields a count accurate to the nearest 10 hertz. FREQ A program that measures an unknown frequency on p in 3.4 (TO), named freq, is shown next. The unknown frequency is counted in TO configured configured as a16-bitcounter. a16-bitcounter. TO is reset reset and begins counting the unknown frequency. A de-lay of .1 second is done, and Tstopped. The count in TO is the unknown frequency divided by 10.Tl is used, in an interruptmode, as a timer with an exact delay of .0001 seconds. Bank 1 registerS RO and Rl count 1,000 Tl interrupts to yield a delay of. 1 second. TO and Tl are reset and enabled to count and time at T = 0 in the program. After the .1 second delay is is up, TO is stopped, and tcontents of TO displayed on PI (LSB) and P2 (MSB).
Pulse ulse Width Measuremen t Theoretically, if the input pulse is known to be a perfect square wave, the pulse frequency can be measured by finding the time the wave is high (Th).
The frequency is then
© Copy Right: Rai University 72
3 . 3 7 5 /3 A.3 7 5 /3 B. 3 7 5
If Th is 200 microseconds, for example, then UF is 2500 hertz. The accuracy of the measurement will fall as the input wave departs from a 50% duty cycle.
R E
Notes
Timer X may be configured so that the internal clock is counted only when the corresponding INTX pin is high by setting the GATE X bit in TMOD. The accuracy of the measurement is within approximately one timer clock period, or 1 microsecond for a 12 megahertz crystal. This accuracy can only be at-tained if the measurement is started when the input wave is low and and stopped when the input next gbes low. Pulse widths widths greater than the capacity of the counter, which is 65.54 milliseconds for a 12 megahertz crystal, can be mea-sured by counting the overflows overflows of the t he Timer flag and adding the final contents in t he counter.The width of an unknown pulse is measured by enabling enabling a timer when the pulse generates an an interrup t on one of the INTX pins. The interrupt is pro-grammed to occur on a high-to-low edge on the INTX pin. The counter begins, counting when the unknown pulse goes high, enabling the counter to count when INTX goes high. The next pulse edge stops the counter. The counter will contain the width of the pulse to the nearest microsecond for a 12 MHz crystal. Width A program, named width measures the width of a pulse fed to pin 3.3 (INTI). Timer Tl is enabled to count on the first pulse edge, counts counts when t he pulse is high, high, and sto ps on t he second edge. Ports PI (LSB) and P2 (MSB) show, in hex, the width of the pulse in microseconds.
© Copy Right: Rai University 3 . 3 7 5 /3 A. 3 7 5 /3 B.3 7 5
73
A L T IM E E M B E D D
E D S Y S T E M S