Display Board Pulse Width Modulation (PWM) Power/Speed Controller Module
Purpose: To demonstrate an easy way of using a Freescale RS08KA2 microcontroller to control the speed of a DC motor and brightness of 4 LEDs.
Brief Explanation: When one or more of the control buttons are pressed, the microcontroller will send 880 Hz pulses to the motor, LEDs, and speaker. Pressing more buttons generates pulses with longer ON time, which will make the motor run faster and the LEDs appear brighter. The speaker puts out a steady 880 Hz regardless of how many buttons are pressed because only the pulse width (duty cycle) is varied, not the frequency.
Background on PWM: Pulse width modulation is a technique commonly used for controlling DC motor speed. It is also used for other applications such as controlling any DC device and even high power (Class D) audio amplifiers.
PWM has the advantages of being very power efficient and also easily controlled by digital devices such as microcontrollers.
By definition, “duty cycle” is the portion of each cycle that the signal is ON compared to how long it is OFF, expressed as a percentage.
Instead of sending pure DC to a motor, PWM uses pulses that have precisely controlled ON and OFF times. If the pulses are on longer, then the motor will run faster. Conversely, narrowing the pulses’ on time will slow the motor down. A typical PWM signal might look something like this:
Control Pushbuttons
LEDs
RS08 Microcontroller
Motor
A more technical explanation is provided by www.wikipedia.org:
Pulse width modulation uses a square wave whose duty cycle is modulated
resulting in the variation of the average value of the waveform. If we consider a square waveform f(t) with a low value y
min, a high value y
maxand a duty cycle D (see figure 1), the average value of the waveform is given by:
As f(t) is a square wave, its value is y
maxfor and y
minfor . The above expression then becomes:
This latter expression can be fairly simplified in many cases where y
min= 0 as . From this, it is obvious that the average value of the signal ( ) is directly dependent on the duty cycle D.
Keep in mind that although the ON and OFF times may vary, the frequency of a PWM signal will remain constant. The actual frequency may change for each application, but in our demo board example, 880 Hz was chosen to allow the signal to be in the audible range for demonstration purposes.
Detailed Hardware Explanation:
Refer to the schematic diagram on one of the following pages. The heart of this module is a Freescale MC9RS08KA2CPC microcontroller configured with 5 inputs and 1 output. When power is first applied to the circuit, capacitor C1 pulls the PTA3/ACMPO/BKGD/MS pin high momentarily, thereby placing the microcontroller into Run mode.
The program will be explained in the next section, but basically the micro is continuously monitoring user input switches S1-S5. These switches are assigned binary values in this way:
Microcontroller Input Pushbutton Switch Binary value
PTA0 S1 1
PTA1 S2 2
PTA2 S3 4
PTA4 S4 8
PTA5 S5 16
If no button is pressed, that represents binary value zero, meaning that the operator is requesting to run the motor at 0% duty cycle. The controller will, therefore, put out pulses that are on 0% of the time and off 100% of the time. In other words, the output never turns on for 0% duty cycle.
When a user presses one or more buttons, the program converts those buttons to a non-zero binary number and turns output PTA3 on and off to generate an 880 Hz signal with a duty cycle determined by that number. If all buttons are simultaneously pressed, this will represent the largest possible 5-bit binary number and so the PTA3 output will be on much longer than it is off.
Pulses from IC1-PTA3 will turn TIP110 Q1 on and off through base current limiter R1. Q1 is driven hard into saturation and cutoff, so the only time it spends in the linear region is during transitions from off to on or on to off. Therefore, Q1 will dissipate very little heat and waste very little power. DC motor MOT1 receives the current pulses from Q1 and integrates them into an average value. The inductance of the motor windings and the inertia of the rotor ensure that the motor turns smoothly rather than trying to jerk at 880 Hz. The 880 Hz is not even apparent when observing the motor. LED1-LED4, on the other hand, can respond much more quickly than the motor. Although these LEDs appear dimmer and brighter when different duty cycles are selected, the truth is that they are actually switching on and off very rapidly. The apparent dimming is caused by the human eye performing a sort of optical integration and sending the brain an average value. To confirm for demonstration purposes that there actually are pulses of current going through the LEDs, speaker SPKR1 is in series with them. A soft 880 Hz tone may be heard from this speaker when the buttons are pressed as the speaker responds to those current pulses.
When Q1 switches on and sends current through MOT1, a magnetic field is built up in the windings of the motor. When Q1 is switched off again, the magnetic field collapses and attempts to send a high voltage spike through Q1 and any thing else that gets in the path. However, diode D1 conducts and shunts the voltage generated by the collapsing field, thereby protecting Q1 from that potentially destructive high voltage spike. A diode used this way is commonly called a
“freewheeling diode”.
The only component not yet mentioned is capacitor C2, and that is simply a decoupling capacitor to help prevent power supply noise from getting in or out of this module.
Software Explanation:
The RS08 microcontroller program was written using the free Freescale’s CodeWarrior C compiler/assembler. The following explanation will make more sense if you are familiar with C programming. It should be noted that the RS08 does not automatically generate PWM signals as some microcontrollers will do, so this program accomplishes the PWM generation.
The main function first calls an auto-generated function, MCU_init(), which initializes the device I/O, bus clock frequency, timer interrupts, etc.
void main(void) {
MCU_init(); /* call Device Initialization */
Next is a simple loop that runs indefinitely and does two things:
1. Sets a byte variable, bPwmDesired, to a weighted value proportional to the current state of the five pushbutton input switches. Note that the 75/32 multiplier will limit the duty cycle to 75% to protect the motor and normalize the number over a 0-31 range (the range of a 5-bit binary number).
bPwmDesired = ((PTAD_PTAD0) + (2*PTAD_PTAD1) + (4*PTAD_PTAD2) + (8*PTAD_PTAD4) + (16*PTAD_PTAD5)) * 75 / 32;
2. Calls function PwmCycle, passing it the desired duty cycle through the bPwmDesired variable. This function actually generates the output pulses for one cycle.
// run one PWM cycle at appropriate duty cycle PwmCycle(bPwmDesired);
The PwmCycle function contains a loop that divides the cycle into 100 equal intervals:
for(bLoopCounter = 0; bLoopCounter <= 100; bLoopCounter++)
Within that loop, a call is made to function PwmInterval, which returns a Boolean value and sets output PTA3 (also called PTAD_PTAD3) on or off as appropriate for the given duty cycle and current interval as determined by the loop counter:
PTAD_PTAD3 = PwmInterval(PwmDesired, bLoopCounter);
Function PwmInterval loops over and over until the overflow interrupt flag from the timer, SIP1_MTIM, is set. This timed interrupt is set to occur every 12.625 uS, making each of the 100 intervals per cycle just 12.625 uS long. Note that the RS08 does not have true vectored
interrupts. Instead, the interrupt bit must be polled and action taken accordingly.
After resetting the timer and acknowledging the interrupt, the function compares the desired duty cycle to the current 0-to-100 interval within the cycle and returns a 0 or 1 to indicate whether the output should be off or on at that point in the cycle.
byte PwmInterval(byte bDutyCycle, byte bCurrentInterval) {
while( ! SIP1_MTIM); // wait for overflow interrupt flag from timer MTIMSC_TRST = 1; // reset timer and acknowledge the interrupt if(bCurrentInterval >= bDutyCycle) // if end of ON time,
return 0; // turn OFF the bit of concern else return 1; // still in ON time for the bit of concern }
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
A A
B B
C C
D D
E E
F F
G G
Title:
Designed by:
Checked by:
Approved by:
Document No:
Date:
Sheet of
Revision:
Size:
5-bit PWM KA2 Display Board 5-bit PWM KA2 jwk
jwk jwk
0001 2007-12-19
1 1
1.0 A Desc.:
Electronics Workbench 801-111 Peter Street Toronto, ON M5V 2H1 (416) 977-5550 IC1
MC9RS08KA2CPC
PTA1/KBIP1/ACMP 7
PTA0/KBIP0/ACMP+
8
PTA2/KBIP2/TCLK/~RESET/VPP 1
VSS4VDD3
PTA3/ACMPO/BKGD/MS 2 PTA4/KBIP4 6 PTA5/KBIP5 5
SPKR1
Pin1
Pin2
C1 100nF VDD
3.3V
S1
S2
S3
S4
S5
R1 1kΩ
MOT1
M
LED1 LED2 LED3 LED4 R23.3Ω
D1 1N4007GP
C2 100nF
VCC 12V
Q1 TIP110
Deleted from final design because mounted speaker volume was very low