The speed of a motor can be adjusted by powering it with a pulse width modulated signal. Figure 1 shows how this can be done. The field effect transistor (FET), IRLD024, acts as a switch. When the signal on the gate of the FET is high the switch is closed, current flows through the motor, and the motor speeds up. When the signal is low, the switch is open, no current flows, and the motor slows down. With a high enough frequency PWM signal the amount the motor speeds up and slows down in one period is negligible, and the motor turns at a constant speed. By adjusting the duty cycle the speed of the motor can be controlled.
To make a motor turn at a desired speed it is necessary to know how fast the motor is turning. Some motors have encoders which will generate hundreds of pulses in a single revolution of the motor. The motors you will use in this lab have an encoder which will generate one pulse per revolution. The encoder works as shown in Figure 2. A light emitting diode (LED) sends light to the base of a phototransistor. When the light shines on the base, the transistor turns on, and the voltage at the collector of the transistor is about 0.3 V, which is a digital 0. When something blocks the light from reaching the transistor base the transistor turns off, and the voltage on the collector goes to VCC, a digital 1. The motors you will use have such an optical encoder, in which the light from the LED to the transistor will be blocked once a revolution. Thus, you will be able to measure the speed of the motor. When connecting an analog signal (such as the output of a transistor collector) to a digital input (such as an HC12), it is a good idea to clean up the signal with a device called a Schmidt Trigger. A 74HC14 is an inverting Schmidt trigger which will be used to clean up the signal on the collector of the transistor.
In this lab you will use the PWM subsystem of the HC12 to control the speed of a small motor, and use the timer/pulse accumulator subsystem to measure the speed of the motor.
PB3:0 | Duty Cycle | PB3:0 | Duty Cycle |
0000 | 0% | 1000 | 80% |
0001 | 10% | 1001 | 90% |
0010 | 20% | 1010 | 100% |
0011 | 30% | 1011 | 100% |
0100 | 40% | 1100 | 100% |
0101 | 50% | 1101 | 100% |
0110 | 60% | 1110 | 100% |
0111 | 70% | 1111 | 100% |