What is PWM?
Pulse Width Modulation is simply a way of getting the micro-controller to manage pulsing a pin on and off at a set period and duty. The DipCortex using an LPC11U24 and LPC1347 each have four timers with four match registers each that can be used to create a PWM output on a pin. The period time describes the frequency of the pulses and the Duty is the length of the pulse. Using a timer or a PWM peripheral of a micro-controller has the benefit of not needing software intervention to toggle the pin, freeing the software up to execute less time critical tasks. The PWM output can be used for a variety of things, dimming an LED, controlling a motors speed or setting a servo’s position.
How do I create a PWM output?
There are four timers, CT16B0/1 & CT32B0/1, two are 16bit and two 32bit. Chapter 16 and 17 of the user manual describe their function in detail. Each timer has four match registers and each match register can control a pin. When the timer is being used to generate PWM signals it requires one of the four match registers to be used to specify the frequency.
In PWM mode the timer counts up when ever it matches a match register it will raise the pin associated with that match register, if the pin is set to be a match register output. One of the four match registers must be used to reset the timer to zero, upon reset all match outputs are lowered. Unless a match register is set to zero, in which case the output stays high. Setting the match register to same value as the period match will produce a pulse one timer count wide, a higher value we cause the pin to stay low for the duration.
By default the timer will count every peripheral clock cycle, for the DipCortex M0 that is 48MHz for the WIFI and M3 it is 72MHz. To change that and slow the clock down you can specify a prescaler value that will count every peripheral clock and when it reaches a set count will then increment the timer count. Setting the prescaler (PR register) to 1 effectively doubles the time it takes to increment the timer. In the diagram above with a 72MHz clock setting the PR register to 720 and the Period Match Register to 100 gives us a 1KHz PWM. Picking the PR value that fits depends on how much resolution, timer ticks, you want across your PWM period.
To help we have created a simple spreadsheet that lets calculates the counts in period showing the effect of using the prescaler. First pick a pin on the DipCortex to use as a PWM output by looking at the pin out of the product page selecting a pin with a yellow label.
This example shows you how to implement a single PWM output with LPCXpresso or another IDE in C with the CMSIS library.
For mbed, you can use the PwmOut class, an example of it’s use can be found here – DipCortex PWM example