Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 3 months ago.
PWM duty cycle - LPC11u24
Using the simple code below to test two PWM pins on the LPC11U24, I see on the o'scope, the the duty cycles are different. Pin 25 signal is 20usec LOW, while the pin 26 signal is 20usec high. Is there some type of initial state setting required?
- include "mbed.h"
PwmOut pwmOne(p25); PwmOut pwmTwo(p26);
int main() {
pwmOne.period_us(100); pwmOne.pulsewidth_us(20);
pwmTwo.period_us(100); pwmTwo.pulsewidth_us(20);
while(1) { wait(0.000001);
} }
1 Answer
11 years, 3 months ago.
If I use the write(float value) function instead of the pulsewidth_us(int us) function, the duty cycle for both pins is as expected.
This was close but still no quite right. The problem seems to be the sequence order of the statements. This sequence produces the correct duty cycle on both pins. If I order the statement the period and then pulsewidth for the first object, then the second object, the first object gets and inverted duty cycle. <code> pwmOne.period_us(100); THIS SEQUENCE MATTERS. pwmTwo.period_us(100);
pwmOne.write(0.20f); pwmTwo.write(0.20f); </code>
posted by 03 Feb 2013So your code is
pwmOne.period_us(100); //THIS SEQUENCE MATTERS pwmTwo.period_us(100); pwmOne.write(0.20f); pwmTwo.write(0.20f);
The LPC11U24 PWM outputs are grouped according to the timer they use.
Pin under the same "Timer" share the same period. See page http://mbed.org/handbook/PwmOut
Timer/Match Register => Pinout Options
CT16B0/MR0 => p5 (P0_9)
CT16B0/MR1 => p6 (P0_8)
CT16B0/MR2 => p34 (P1_15)
CT16B1/MR0 => p36 (P0_21)
CT16B1/MR1 => p20 (P0_22) and p14 (P1_23)
CT32B0/MR0 => p25 (P1_24)
CT32B0/MR1 => p26 (P1_25) and USBTX (P0_19)
CT32B0/MR2 => p10 (P1_26)
All outputs that use the same timer will get the same period. The dutycycles can be different obviously. P25 and P26 share the same timer. So the second time you set the period will override the first time. This should not matter when you set both to the same period, but somehow something goes wrong with the dutycycle of the first output. The problem is probably due to a bug in the PWM lib for the LPC11U24.
Note that this processor has some hardware differences compared to the LPC1768. The PWM behaviour is one of the areas that are different.
posted by 03 Feb 2013
Pulsewidth should be 20us high on both. Have you checked the scope settings. Maybe one channel is in inverted mode.
posted by Wim Huiskamp 29 Jan 2013Thanks Wim. No, the scope configuration is fine. With a little more investigation, it seems the pin 25 PWM duty cycle is as expected if only executing by itself. When the pin 26 PWM is defined and executing, the pin 25 duty cycle is inverted. Weird but usable if this behavior is consistent.
posted by Chris Shipman 02 Feb 2013