Pulse Width Modulation
Applications
Lecture 21
EE 383
Learning Objectives
What is DTMF?
How to use PWM to generate DTMF?
How to use PWM to control a servo
motor?
How to use PWM to control a DC
motor?
PWM Applications
Pulse-Width Modulation (PWM) can be used with any application that requires generation of a periodic
rectangular waveform.
With additional filter circuitry, rectangular waveforms can be used to generate triangular, sinusoidal, and other waveform types.
Design of these filters is outside the scope of this class.
PWM applications include:
Servo control (standard or continuous-rotation) DC motor speed control
Generation of audio tones Many others…
Generating Audio Tones
A “pure” audio tone is a sinusoid of a particular frequency. Sinusoids can be roughly approximated by 50% duty-cycle rectangular waves. The approximation can be improved by filtering the rectangular wave.
The range of tone frequencies for human hearing falls
roughly in the range 20Hz – 20 kHz. The extremes of this range are quite variable by individual. Aging tends to reduce the range, especially at higher frequencies.
The Dragon12 PWM can easily generate waveforms in the 20Hz – 20 kHz range and, with a proper transducer (e.g. a speaker), can be used to produce audio output.
Touch-Tone Telephone
1 2 3 4 5 6 7 8 9 */E 0 #/F A B C D 1 2 3 4 5 6 7 8 9 * 0 #DTMF Touch-Tone Standard
Dual-Tone Multiple-Frequency (DTMF) encoding is used to transmit the state of the keypad when a button is
pressed.
Each keypress is indicated by a unique combination of two sinusoidal frequency components.
One sinusoid indicates the keypad row and the other indicates the keypad column.
DTMF Frequencies
1209 Hz 1336 Hz 1477 Hz 1633 Hz
697 Hz
1
2
3
A
770 Hz
4
5
6
B
852 Hz
7
8
9
C
941 Hz
*/E
0
#/F
D
Approximating DTMF with
PWM
DTMF requires the sum of two sinusoids. PWM produces rectangular waveforms.
Two PWM channels (row and column) with appropriate
Dragon12plus - First
Approximation of DTMF
For a simple approximation of DTMF on the
Dragon12plus, we can take advantage of the fact
that we can set a separate frequency for each
PWM channel.
In addition, the Dragon12plus laboratory kits
include two small piezoelectric speakers equipped
to plug directly into the “servo” connectors on the
Dragon12plus.
Driving these two speakers with two PWM
square-wave (50% duty cycle) signals is an
acceptable first approximation for DTMF.
Selecting DTMF frequencies
Frequency (Hz) Period (µs) 24 MHz cycles
697 Hz 1434.72 µs 34433.28 cycles 770 Hz 1298.70 µs 31168.83 cycles 852 Hz 1173.70 µs 28169.01 cycles 941 Hz 1062.69 µs 25504.78 cycles 1209 Hz 827.13 µs 19851.12 cycles 1336 Hz 748.50 µs 17964.07 cycles 1477 Hz 677.05 µs 16249.15 cycles 1633 Hz 612.37 µs 14696.88 cycles
Because of the tight constraint on period accuracy (1.5%) we should consider using the concatenated PWM channels with 16-bit period counters for greater accuracy.
Selecting PWM parameters for
DTMF
Frequency (Hz) Period (µs) 24 MHz cycles (PWMPER/PWMDTY) 697 Hz 1434.72 µs 34433.28 cycles (34434/17217) 770 Hz 1298.70 µs 31168.83 cycles (31168/15584) 852 Hz 1173.70 µs 28169.01 cycles (28170/14085) 941 Hz 1062.69 µs 25504.78 cycles (25504/12752) 1209 Hz 827.13 µs 19851.12 cycles (19852/9926) 1336 Hz 748.50 µs 17964.07 cycles (17964/8982) 1477 Hz 677.05 µs 16249.15 cycles (16250/8125) 1633 Hz 612.37 µs 14696.88 cycles (14696/7348)
The period counts here are the closest even integer so that the duty cycle counts provide for exactly 50% duty cycle. This will produce a sinusoidal signal with minimum distortion after filtering.
Setting up PWM for DTMF
Let us choose to use concatenated PWM channels to give us the needed accuracy.
Use PWM channels 4 and 5 for the DTMF row. Output is on pin 5. Use clock A or SA.
Use PWM channels 6 and 7 for the DTMF column. Output is on pin 7. Use clock B or SB.
PWM Setup Code For DTMF
BSET PWMCTL, #$C0 ;set CON67 and CON45 BSET PWMPOL, #$F0 ;4,5,6,7 active high
;50% duty cycle so polarity doesn’t matter BCLR PWMCLK, #$F0 ;4,5,6,7 use clock A/B
BCLR PWMPRCLK, #$FF ;clock scale = 2^0, clock is 24 MHz ;clock period now (1/24) microseconds MOVW #34433, PWMPER4 ;697 Hz, signal generated at CH5
MOVW #17217, PWMDTY4 ;50% duty cycle
MOVW #14696, PWMPER6 ;1633 Hz, signal generated at CH7 MOVW #7348, PWMDTY6 ;50% duty cycle
BSET PWME, #$F0 ;4,5,6,7 enabled JSR DELAY_1s
Motor Control Applications
Motors convert electrical energy into mechanical energy.
Motor control is normally concerned with one or more of the following:
Motor shaft position
Motor shaft rotational speed
Available rotational power (torque)
Control is usually optimized for one of these factors and it is generally not possible to control all three quantities
simultaneously.
PWM is easily adapted to two simple motor types, DC motors and Servo motors.
Servo Motors
Servo motors employ internal feedback control electronics to allow the motor to be controlled by a simple pulse
sequence.
The servo motor control signal has a fixed frequency of 50 Hz and a duty cycle that varies from (nominal) 5% to 10%. Varying the duty cycles causes the servo motor to respond by changing some aspect of its operation.
For a standard servo, the control signal is used to
determine the position of the motor shaft. This is often constrained to less than one full revolution. Rotational speed is typically low. Torque is typically high.
For a continuous-turn servo the control signal is used to determine rotational speed. Shaft position is uncontrolled. Torque is typically high.
Servo
3-pin connector GND, 5V, signal
Port P “Servo” Headers on
Dragon12
3
3--pin connectorspin connectors
GND, 5V, signal (PWM) GND, 5V, signal (PWM)
Servo Connections
The typical electrical connection for a servo motor is a 3-wire interface:
Ground
Power (typically 5-6V DC)
Control input (50Hz, nominal 5%-10% duty cycle)
On the servo motors purchased from www.parallax.com, the ground connection is a black wire, the 5V connection is a red wire, and the control input is a white or yellow wire.
The Dragon12plus provides 4 connectors that provide servo-compatible control. The signal lines for these connectors are Port P pins 4-7.
Driving Servos from
Dragon12plus
The Dragon12 provides 4 connectors that provide servo-compatible control. The signal lines for these connectors are Port P pins 4-7. Servo motors can draw considerable power, especially when loaded. Care must be taken when driving servos directly from the Dragon12plus to prevent overloading the onboard voltage regulator.
If you plug in a servo to the Dragon12plus board and it draws too much power, it is likely that the Dragon12plus will reset when the output of the voltage regulator is pulled too low.
When you are asked to use servos in the laboratory, a separate power board for the servos will be provided.
Controlling a Standard Servo
Motor
The position of the servo motor shaft is controlled by a single pulse-width modulated control signal.
A 50 Hz pulse train with a period of 20ms with an active high pulse of 1.5ms (7.5% duty cycle) will cause the servo to move to the center of its range of motion then hold the shaft at that point.
Longer duty cycles (> 1.5ms/20ms) will cause the servo to rotate counterclockwise.
Shorter duty cycles(< 1.5ms/20ms) will cause the servo to rotate clockwise.
The official valid range of duty cycles is from 5% (1ms/20ms) to 10% (2ms/20ms). The relative position of the servo is
proportional to the difference from the center point to the extremes.
Controlling a Continuous-Rotation
Servo
The speed and direction of the servo are controlled by a single pulse-width modulated control signal.
A 50 Hz pulse train with a period of 20ms with an active high pulse of 1.5ms (7.5% duty cycle) will cause the servo to hold at a constant fixed point.
Longer duty cycles (> 1.5ms/20ms) will cause the servo to rotate counterclockwise.
Shorter duty cycles(< 1.5ms/20ms) will cause the servo to rotate clockwise.
The official valid range of duty cycles is from 5% (1ms/20ms) to 10% (2ms/20ms). The relative speed of the servo is
proportional to the difference from the center point to the extremes.
Example Code
MOVB #$30, PWMPRCLK ;PWM CLOCK B SCALE = 8
MOVW #60000, PWMPER6 ;PWM CHANNEL 6&7 PERIOD = 20ms
MOVW #4500, PWMDTY6 ;PWM CHANNEL 6&7 DUTY CYCLE 7.5%
BSET PWMCTL, #%10000000 ;CONCATENATE 6&7
BSET PWMPOL, #%11000000 ;CHANNEL 6&7 ACTIVE HIGH
BCLR PWMCLK, #%11000000 ;CHANNEL 6&7 CLOCK B
DC Motor Control
Generally, a DC motor is designed so that when a constant voltage is applied to the motor, the shaft will turn at a
constant speed.
A DC motor will be rated to have a maximum input voltage. A DC motor may be operated at a reduced voltage to
produce a reduced speed. However, this is very inefficient and below a certain level will not work at all as the applied voltage will not produce sufficient torque to turn the motor, especially when loaded.
A better way to control the speed of a DC motor is to rapidly turn it fully off and fully on to produce a desired average speed while maintaining full torque while it is on. PWM is appropriate for this.
Controlling a DC Motor using
PWM
The duty cycle of a PWM control for a DC motor determines the fraction of the maximum supplied energy provided to the motor to make the shaft turn.
If the duty cycle is chosen too short, the motor may not be able to develop enough torque to make the shaft turn before the pulse ends. If the period is chosen too long, the motor will stop between pulses rather than continuing to spin from inertia.
The appropriate PWM period and duty cycle are very dependent on the physical properties of the motor.
Larger motors have greater inertia so, in general, pulses must have longer duration than for small motors to overcome the inertia initially but need shorter pulses to maintain rotation.