Lab 5: Pulse Width Modulation
In this lab, we are going to learn about Pulse Width Modulation (PWM). Pulse width modulation is a commonly used technique for generating precisely timed, repetitive digital waveforms. A great advantage of PWM is that once the registers are set up and enabled, the PWM waveforms are generated without any CPU overhead, so we can then use valuable CPU time for other tasks. PWM is widely used in a variety of tasks such as motor control, and control of switching in power electronics circuits.
We will also learn how to use a Logic Analyzer. A logic analyzer is like a digital oscilloscope, and is especially handy for viewing the PWM waveforms.
1-Pre-lab
2-Tutorials
3-Program
Using DBug12, set up a PWM waveform on channel 0 (PWM0) with the following properties: (1) Period ~ 1 msec; (2) duty cycle ~ 25%. Also set up a waveform on channel 1 (PWM1) with a period of 2 msec and a duty cycle of ~ 25%. Use a value of E/64 for the prescaler select value in PWM Prescale Clock Select Register PWMPRCLK at address $A3 for both PWM0 and PWM1 (we used E/128 in other examples). Note that because you are using integer math, you may not get the values exactly, but they should be close (within a few percent or so). Also note that you basically guess the values you need in the Set up the logic analyzer so that it displays the PWM waveforms. Your checkout will consist of showing the TA your calculations, and demonstrating that the PWM waveforms have the correct periods and duty cycles.
Hints: (1) You usually need to GUESS initial values for the PWMSCLA and PWMPERx values. You then iterate until you achive the desired result; (2) usually you want a fairly large value for the PWMPERx value, say around 100 or more. This way, you can easily achieve the duty cycle you want. As an extreme example, if your value of PWMPERx was "2", then you could only achieve a duty cycle of 0% or 50% - there would be no way you could achieve a duty cycle of 25%. The inverse of the value of PWMPERx basically sets the resolution of the duty cycle. So, with a value of 100 in PWMPERx, you can get duty cycle resolution of 1%; a PWMPERx value of 200 would give a duty cycle resolution of 0.5%, etc.
4-What to Hand In
At the next lab, hand in a brief one-page report that shows the register values, addresses, etc. of the PWM registers you set up in Part 3 above. You should also show the formulae detailing how you calculated the period, frequency and duty cycle of the PWM waveform.