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.
12 years, 10 months ago.
Question on PWM
Hello, I have a quick question about PWM functionality in mbed. In the following simplified example:
#include "mbed.h"
PwmOut servo(p21);
PwmOut motor(p22);
int main()
{
servo.period_ms(20);
motor.period_ms(10);
while(1)
{
servo.pulsewidth_ms(5);
motor.pulsewidth_ms(10);
}
}
I realized that this code doesn't work at all unless I set the period within the while loop. Also, even if I define the periods for the two pins, it would set the all the periods to which ever goes second. If I define period for motor to 10ms after defining the servo period, the servo period seems to get replaced by 10ms.
In order to deal with this issue, I made a helper function such as SetServo() that defines period and pulsewidth within the function only when it's being used.
My questions are: 1) why doesn't the code above work? 2) is my solution what people usually use? or is there a better solution?
Thank you very much for your time!
1 Answer
12 years, 10 months ago.
From the PWM section of the handbook.
Quote:
On the mbed LPC2368 and LPC1768, the PwmOut hardware is limited to share the period value between all outputs. Therefore, if you change the period of one output, you change them all. The pulsewidth can be set independently for each output.