Display Board Pulse Width Modulation (PWM) Power/Speed Controller Module RS0 Microcontroller LEDs Motor Control Pushbuttons Purpose: To demonstrate an easy way of using a Freescale RS0K2 microcontroller to control the speed of a DC motor and brightness of LEDs. Brief Explanation: When one or more of the control buttons are pressed, the microcontroller will send 0 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 0 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. typical PWM signal might look something like this:
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 max and a duty cycle D (see figure ), the average value of the waveform is given by: s f(t) is a square wave, its value is y max for. The above expression then becomes: and y min for 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, 0 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 MC9RS0K2CPC microcontroller configured with 5 inputs and output. When power is first applied to the circuit, capacitor C pulls the PT/CMPO/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 S-S5. These switches are assigned binary values in this way: Microcontroller Input Pushbutton Switch Binary value PT0 S PT S2 2 PT2 S PT S PT5 S5 6 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 00% 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 PT on and off to generate an 0 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 PT output will be on much longer than it is off. Pulses from IC-PT will turn TIP0 Q on and off through base current limiter R. Q 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, Q will dissipate very little heat and waste very little power. DC motor MOT receives the current pulses from Q 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 0 Hz. The 0 Hz is not even apparent when observing the motor. LED-LED, on the other hand, can respond much more quickly than the motor. lthough 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 SPKR is in series with them. soft 0 Hz tone may be heard from this speaker when the buttons are pressed as the speaker responds to those current pulses. When Q switches on and sends current through MOT, a magnetic field is built up in the windings of the motor. When Q is switched off again, the magnetic field collapses and attempts to send a high voltage spike through Q and any thing else that gets in the path. However, diode D conducts and shunts the voltage generated by the collapsing field, thereby protecting Q from that potentially destructive high voltage spike. 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 RS0 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 RS0 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:. Sets a byte variable, bpwmdesired, to a weighted value proportional to the current state of the five pushbutton input switches. Note that the 5/2 multiplier will limit the duty cycle to 5% to protect the motor and normalize the number over a 0- range (the range of a 5-bit binary number). bpwmdesired = ((PTD_PTD0) + (2*PTD_PTD) + (*PTD_PTD2) + (*PTD_PTD) + (6*PTD_PTD5)) * 5 / 2; 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 00 equal intervals: for(bloopcounter = 0; bloopcounter <= 00; bloopcounter++) Within that loop, a call is made to function PwmInterval, which returns a Boolean value and sets output PT (also called PTD_PTD) on or off as appropriate for the given duty cycle and current interval as determined by the loop counter: PTD_PTD = PwmInterval(PwmDesired, bloopcounter); Function PwmInterval loops over and over until the overflow interrupt flag from the timer, SIP_MTIM, is set. This timed interrupt is set to occur every 2.625 us, making each of the 00 intervals per cycle just 2.625 us long. Note that the RS0 does not have true vectored interrupts. Instead, the interrupt bit must be polled and action taken accordingly. fter resetting the timer and acknowledging the interrupt, the function compares the desired duty cycle to the current 0-to-00 interval within the cycle and returns a 0 or to indicate whether the output should be off or on at that point in the cycle. byte PwmInterval(byte bdutycycle, byte bcurrentinterval) { while(! SIP_MTIM); // wait for overflow interrupt flag from timer MTIMSC_TRST = ; // reset timer and acknowledge the interrupt if(bcurrentinterval >= bdutycycle) // if end of ON time, return 0; // turn OFF the bit of concern else return ; // still in ON time for the bit of concern }
0 2 5 6 B C C2 00nF VDD.V IC C 00nF N00GP D VCC 2V M MOT Deleted from final design because mounted speaker volume was very low SPKR R2.Ω Pin Pin2 LED B C D S PT0/KBIP0/CMP+ PT/KBIP/CMP VDD PT2/KBIP2/TCLK/~RESET/VPP PT5/KBIP5 5 VSS PT/CMPO/BKGD/MS 2 PT/KBIP 6 R kω Q TIP0 LED LED2 D S2 MC9RS0K2CPC LED E E S F S Electronics Workbench 0- Peter Street Toronto, ON M5V 2H (6) 9-5550 F S5 Title: 5-bit PWM K2 Desc.: Display Board 5-bit PWM K2 G Designed by: Checked by: Document No: 000 Date: 200-2-9 Revision: Size:.0 G pproved by: Sheet of 0 2 5 6