Page 269 - Programming Microcontrollers in C
P. 269
254 Chapter 5 Programming Large 8-Bit Systems
choose a somewhat simpler system for an example, but the general
approach used here is much the same as would be used in the design
of an automatic braking system.
Suppose that we have a DC motor that is being controlled by an
MC68HC11E9 and wish to be able to set the speed of the motor. The
motor has a magnetic sensor whose output will cycle once each
rotation of the motor. To determine the speed of the motor, we must
measure the cycle time of the output from the sensor. Let us examine
the minimum speed of the motor. The TCNT register is being clocked
at a frequency that is either one-fourth of the crystal frequency of the
system or it can be altered by a prescaler value of either 4, 8, or 16.
The 16-bit TCNT register will overflow every 65,536 clocks at its
input. Therefore, if we use an 8-MHz clock, the timer overflow period
will be 32.768 ms with no prescaler, 131.072 ms with a prescaler
value of 4, 262.144 ms with a prescaler value of 8, or 524.288 ms
with a prescaler value of 16. The minimum speed at which the motor
can move and still be detected unambiguously must be greater than
one revolution in the timer overflow time. If the motor rotates any
slower than this value, the differences in times between two inputs
will be such that the program cannot tell if the period is longer than
one TCNT overflow time or a very short time. It is possible to extend
the minimum unambiguous time that can be measured by the system
through the use of a timer overflow interrupt, but let’s examine an
approach without these steps and then look at the time extended
approach later.
The maximum practical time to be measured with the divide-by-16
prescaler is 500 milliseconds. The minimum rotation speed must be
greater than 1 revolution per 500 milliseconds or 120 revolutions per
minute. The motor that we will use in this example has a minimum
speed of 1000 rpm, so that the prescaler need not be as great as 16. We
will use a prescaler value of four, which will provide about a minimum
speed of 500 rpm so that there is a safety factor in our measurement.
With the divide-by-four prescaler, the minimum time that can be
measured is ( 4 [crystal to bus frequency division]* 4 [prescaler divide
ratio ]/ 8 MHz [clock frequency] ), which is 2 microseconds. At this
time interval, the maximum speed of the motor shaft that can be
measured is one revolution in 2 microseconds, or 500000 * 60
revolutions per minute. It is quite clear that such a system is better
suited to handle high-speed operations than low speed. If there is no