DipCortex PWM (Pulse Width Modulation) using a Timer

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.

LPC1347 & LPC11U24 Timer PWM

DipCortex LPC1347 PWM Timer Spreadsheet

Download PWM Calc Spreadsheet

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.

Example Code

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

3 Responses to “DipCortex PWM (Pulse Width Modulation) using a Timer”

  1. ceri February 4, 2014 at 12:01 pm #

    I need to setup PWM Outputs on LPC1347,-M3
    I can only manage to setup a PWM on P0_18,

    Please can you provide am MBED example to set them up,

    Ideally, I want to generate several audible tones.

    Cheers

    Ceri

  2. ceri February 4, 2014 at 12:06 pm #

    How do i set up the PWM’s on the MBED compiler,

    I can only manage to get one to work,
    the data sheet appears to have ELEVEN PWM O/P’s ??

    [[http://mbed.org/users/mbed_official/code/mbed/file/824293ae5e43/TARGET_LPC1347/PeripheralNames.h]]

    Please help..

    Cheers

    Ceri

Leave a Reply

 
 

cheapest viagra
buy viagra mesa
non prescription viagra
buy cheap generic viagra
best place to buy viagra
viagra and cialis and
buy viagra usa
buy viagra prescription
buy cheap generic online viagra
buy viagra now
viagra by the pill
cheap pill viagra
buy viagra new york
buy viagra onli
cheap price viagra
free viagra
viagra and cialas
buy online online viagra viagra
buy deal viagra
buy viagra on-line
buy cheap generic viagra online
viagra for sale
buy discount viagra
buy viagra without prescription