Experiment 6: Motor Speed Control: Erratum
Experiment 6: Motor Speed Control: Erratum
Erratum:
PORTE is labelled incorrectly. The labels PE2-PE7 should actually read PE7-PE2 (the order is
reversed).
1. Aim
The Figure 1 shows the circuit for motor speed detection. The emitter is enabled when OpE is
high. To enable the emitter, you need to connect OpE to a port pin and configure this pin as
output. Notice that there are four holes on the wheel. When the infrared light from the emitter
goes through a hole, the detector pin (OpD) will be low. Otherwise, OpD will be high. To read
OpD, you need to connect it to a port pin and configure this pin as input.
Now power up the AVR Microcontroller Board as from last lab, wire up the POT to the MOT
using the patch cables provided. As you turn the POT, the current flowing to the motor will
change. As a result, the motor spins at different speeds. To calculate the motor speed, you need to
find the number of state changes of OpD within a fixed amount of time.
1
Hints for Motor Speed Testing
There are two ways to measure speed, counting holes in a given period of time or measuring time
for a complete revolution of the wheel (or several revolutions to average things out). Whichever
method you choose is up to you.
The Detector can be read by an interrupt. The detector will output a 1 when there isn’t a hole, so
you can use the falling edge of the detector to trigger an interrupt to count a hole.
1. Method 1: Timing can be done with Timer 0 or any of the other timers in the chip.
Count Overflow interrupts to measure a period of one second while counting the
number of holes that pass by using an external interrupt. Every second you can update
your speed on the LCD.
2. Method 2: Set up the timer to time over an appropriate period. Wait for the first
external interrupt to indicate the first hole and record the timer value. After four more
external interrupts you know that 1 revolution has occurred. Find the difference
between the start and end times (you may also need to count the number of timer
overflows) and you can calculate the period of rotation for the wheel. From this value
you can work out how many revolutions could occur in a second and display it.
The first method is fairly simple but will only update every second. The second method is a little
trickier, but gives you much higher accuracy and a much faster refresh rate.
Write an AVR assembly language program that measures the speed of the motor by counting the
number of holes that has been detected using the shaft encoder, and display the speed of the
motor on the LCD. You might need to go back to the previous experiment for how to initialize,
and write to LCD properly.
Assemble your program using AVR Studio, download the program to the AVR Microcontroller
Board and show your working program to the Laboratory assessor
Checkpoint 1: Signature:
Analog signals
An analog signal has a continuously varying value, with infinite resolution in both time and
magnitude. A nine-volt battery is an example of an analog device, in that its output voltage is not
precisely 9V, changes over time, and can take any real-numbered value. Similarly, the amount of
current drawn from a battery is not limited to a finite set of possible values. Analog signals are
distinguishable from digital signals because the latter always take values only from a finite set of
predetermined possibilities, such as the set 0V, 5V.
2
Analog voltages and currents can be used to control things directly, like the motor. In a simple
DC motor, a POT is connected to a variable resistor. As you turn the POT, the resistance goes up
or down. As that happens, the current flowing through the resistor increases or decreases. This
changes the amount of current driving the motor, thus increasing or decreasing the speed. An
analog circuit is one, like the motor, whose output is linearly proportional to its input. As intuitive
and simple as analog control may seem, it is not always economically attractive or otherwise
practical. For one thing, analog circuits tend to drift over time and can, therefore, be very difficult
to tune. Precision analog circuits, which solve that problem, can be very large, heavy (just think
of older home stereo equipment), and expensive. Analog circuits can also get very hot; the power
dissipated is proportional to the voltage across the active elements multiplied by the current
through them. Analog circuitry can also be sensitive to noise. Because of its infinite resolution,
any perturbation or noise on an analog signal necessarily changes the current value.
Digital signals
By controlling analog circuits digitally, system costs and power consumption can be drastically
reduced. What’s more, many microcontrollers and already include on-chip PWM controllers,
making implementation easy.
PWM is a way of digitally encoding analog signal levels. Through the use of high-resolution
counters, the duty cycle of a square wave is modulated to encode a specific analog signal level.
The PWM signal is still digital signal because, at any given instant of time, the full DC supply is
either fully on or fully off. The voltage or current source is supplied to the analog load by means
of a repeating series of on and off pulses. The on time is the time during which the DC power is
supplied and the off time is the period during which that the power is not supplied. Given a
sufficient bandwidth, any analog value can be encoded with PWM.
Figure 2 shows three different PWM signals with duty cycles of 10%, 50% and 90%,
respectively. These three PWM outputs encode three different analog signal values, at 10%,
50%, and 90% of the full strength. If, for example, the supply is 5V and the duty cycle is 10%, a
0.5V analog signal results.
3
PWM is a common technique for speed control. A good analogy is bicycle riding. You peddle
(exert energy) and then coast (relax) using your momentum to carry you forward. As you slow
down (due to wind resistance, friction, road shape) you peddle to speed up and then coast again.
The duty cycle is the ratio of peddling time to the total time (peddle+coast time). A 100% duty
cycle means you are peddling all the time, and 50% only half the time.
PWM for motor speed control works in a very similar way. Instead of peddling, your motor is
given a fixed voltage value (say +5 V) and starts spinning. The voltage is then removed and the
motor “coasts”. By continuing this voltage on-off duty cycle, motor speed is controlled. To
control the speed of a DC motor, we need a variable voltage DC power source. However, if you
take a 5V motor and switch on the power, the motor will start to speed up, motors do not respond
immediately so it will take a small time to reach full speed. If we switch the power off, the motor
will start to slow down. If we switch the power on the off quickly enough, the motor will run at
some speed part way between zero and full speed. This is exactly what a PWM controller does; it
switches the motor on in series of pulses. To control the motor speed it varies the width of the
pulses – hence Pulse Width Modulation.
PWM waves can be generated on the Atmega64 through the use of Timer 0 and its Fast PWM
mode or Phase Correct PWM mode. This is explained in the datasheet in the Timer 0 section
(pages 99-100). You should read this section before continuing.
Basically, you control the timer to vary the duty cycle of the signal on the output pin OC0 which
varies the average voltage on the pin which controls the motor speed. Timer 0 allows control of
the PWM on pin B4 of the microcontroller. Therefore you should connect PORT B4 to the motor
(Mot) to control it this way. Once the timer is set up to perform PWM, you can control the duty
cycle and therefore control the speed of the motor by using the OCR0 register. This can be set up
so that increasing the value of this register will increase the duty cycle of the output pin.
This register controls the timer. The bit values you need to control are described as follows.
• Bit 7 (FOC0): This bit is not used for PWM it should be 0.
• Bits 6 & 3 (WGM01 & WGM00): These two bits select a PWM mode. There are two
PWM modes: Fast PWM mode and Phase Correct PWM mode. You can use either mode
to control the motor speed. Refer to Table 52 on page 102 of ATMega64 Data Sheet for
the details.
• Bits 5 & 4 (COM01 & COM00): These two bits control the behavior of Output Compare
pin (OC0). OC0 generates the PWM wave. Tables 54 & 55 on page 103 of the
ATMega64 Data Sheet show when this pin is set and cleared in the Fast PWM mode and
the Phase Correct PWM mode.
• Bits 2:0 - CS02:0: These three bits select the prescaling value. It doesn’t really matter
what prescaling value you use for PWM, so you can just use the value ”001” for these
three bits.
The value of this register, together with COM01 and COMP00, determine the duty cycle.
4
Timer/Counter Interrupt Mask Register (TIMSK)
This allows you to create an interrupt if you wish, but you probably won’t need it. The interrupt
types are: Timer Overflow (TOIE0): Interrupts every time the timer overflows (i.e. at the
frequency of the clock. Output Compare (OCIE0): This interrupt occurs whenever the output is
going to change. You can get a better understanding of these interrupts from Figure 40.
Write an assembly program for DC motor speed control. You program should work as follows:
1. When it is started, the motor spins at the speed of 20 rps (revolutions per second)
clockwise.
2. Every time when the key 1 on the keypad is pressed, the motor speed is increased by 5
rps. The maximum speed is 80 rps.
3. Every time when the key 2 on the keypad is pressed, the motor speed is decreased by 5
rps. The minimum speed is 20 rps.
4. When the key 3 is pressed, the motor stops.
5. When the key 4 is pressed, the motor starts spinning at the speed of 20 rps.
Assemble your program using AVR Studio, download the program to the AVR Microcontroller
Board and show your working program to the Laboratory assessor.
Checkpoint 2: Signature: