The hardware has 6 PWM channels, and the PwmOut Interface can be used on mbed pins p21-p26.
The default period is 0.020s, and the default pulsewidth is 0.
The PwmOut interface can express the pulse train in many ways depening on how it is to be used. The period and pulse width can be expressed directly in units of seconds, millisecond or microseconds. The pulsewidth can also be expressed as a percentage of the the period.
You can also specify the LED1-LED4 as PwmOut. Note that these are just different pinout options for the same underlying PWM hardware, so they are just alternative routing rather than extra PWM channels. So you can pin them out can't be used at the same time:
PWM H/W Channel
Pinout Options
PWM_1
P2_0/p26 or P1_18/LED1
PWM_2
P2_1/p25 or P1_20/LED2
PWM_3
P2_2/p24 or P1_21/LED3
PWM_4
P2_3/p23 or P1_23/LED4
PWM_5
P2_4/p22
PWM_6
P2_5/p21
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.
#include "mbed.h"
PwmOut servo(p21);
int main() {
servo.period(0.020); // servo requires a 20ms period
while (1) {
for(float offset=0.0; offset<0.001; offset+=0.0001) {
servo.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms
wait(0.25);
}
}
}
if I try to set the period with the above statement servo.period(0.020); I get an error statement
"This declaration has no storage class or type specifier (E77-D)"
any idea what is wrong?
Lothar
if I try to set the period with the above statement servo.period(0.020); I get an error statement
"This declaration has no storage class or type specifier (E77-D)"
any idea what is wrong?
Lothar
I'm going to have to guess as you haven't provided much details of the code that is giving you problems, but for example:
#include "mbed.h"
PwmOut servo(p21);
int main() {
servo.period(0.020); // servo requires a 20ms period
}
is valid code, whereas something like:
#include "mbed.h"
PwmOut servo(p21);
servo.period(0.020); // servo requires a 20ms period
int main() {
}
would give the error you describe. Program code must be within a function (such as main).
Hope that helps!
Simon
Hi Lothar,
I'm going to have to guess as you haven't provided much details of the code that is giving you problems, but for example:
<<code>>
#include "mbed.h"
PwmOut servo(p21);
int main() {
servo.period(0.020); // servo requires a 20ms period
}
<</code>>
is valid code, whereas something like:
<<code>>
#include "mbed.h"
PwmOut servo(p21);
servo.period(0.020); // servo requires a 20ms period
int main() {
}
<</code>>
would give the error you describe. Program code must be within a function (such as main).
Hope that helps!
Simon
I have 4 PWM channels controlling motor ESCs for a quadrotor and 2 servo channels for controlling a camera platform. I am attempting to reduce the period of the two camera servo PWM channels by setting the pulse width of these two channels to zero for 4 out 5 of the cycles. The motor updates are at 200Hz.
The synchronisation is performed by capturing a motor pulse edge and then setting the masking conditional which in turn is used to set the widths to zero for the camera channels.
So the questions:
1) are the pulse width updates guaranteed to occur within one cycle?
2) is there an easier way to detect when the positive pulse edge occurs without wiring the output pin to an detection input using an interrupt (an internal event perhaps)?
Thanks
Greg
I have 4 PWM channels controlling motor ESCs for a quadrotor and 2 servo channels for controlling a camera platform. I am attempting to reduce the period of the two camera servo PWM channels by setting the pulse width of these two channels to zero for 4 out 5 of the cycles. The motor updates are at 200Hz.
The synchronisation is performed by capturing a motor pulse edge and then setting the masking conditional which in turn is used to set the widths to zero for the camera channels.
So the questions:
1) are the pulse width updates guaranteed to occur within one cycle?
2) is there an easier way to detect when the positive pulse edge occurs without wiring the output pin to an detection input using an interrupt (an internal event perhaps)?
Thanks
Greg
1) When you set the pulsewidth, it is placed in a holding register in the pwm hardware which automatically updates the pulswidth at the start of the next period (so, yes)
2) The mbed APIs don't provide anything, so that may well be the easiest way. You may be able to find some specific features of the LPC1768 pwm hardware that gives you what you want (reading the counter registers? some form of interrupt?), but you'd need to investigate the LPC1768 manual here.
Sounds like an interesting project!
Simon
Hi Greg,
1) When you set the pulsewidth, it is placed in a holding register in the pwm hardware which automatically updates the pulswidth at the start of the next period (so, yes)
2) The mbed APIs don't provide anything, so that may well be the easiest way. You may be able to find some specific features of the LPC1768 pwm hardware that gives you what you want (reading the counter registers? some form of interrupt?), but you'd need to investigate the LPC1768 manual here.
Sounds like an interesting project!
Simon
I was just experimenting with the PwmOut function a moment ago. It seems that the function has some odd behavior when the PWM output pin is set to something other than p21-26 or LED1-4. I tried it with p20, and it seemed to affect all LED pins. I did not check the other PWM pins to see if they were affected. This result was unexpected as I thought the output would just not occur.
Hello all,
I was just experimenting with the PwmOut function a moment ago. It seems that the function has some odd behavior when the PWM output pin is set to something other than p21-26 or LED1-4. I tried it with p20, and it seemed to affect all LED pins. I did not check the other PWM pins to see if they were affected. This result was unexpected as I thought the output would just not occur.
I was just experimenting with the PwmOut function a moment ago. It seems that the function has some odd behavior when the PWM output pin is set to something other than p21-26 or LED1-4. I tried it with p20, and it seemed to affect all LED pins. I did not check the other PWM pins to see if they were affected. This result was unexpected as I thought the output would just not occur.
mbed is quite versatile with regards to assigning outputs to various pins; however it doesn't like it if you assign an output that *must* go through certain pins to a different one. It probably tries to output what it would do if it was going through the PWM circuitry that applies to p21-26?
<<quote mtl1>>
Hello all,
I was just experimenting with the PwmOut function a moment ago. It seems that the function has some odd behavior when the PWM output pin is set to something other than p21-26 or LED1-4. I tried it with p20, and it seemed to affect all LED pins. I did not check the other PWM pins to see if they were affected. This result was unexpected as I thought the output would just not occur.
<</quote>>
mbed is quite versatile with regards to assigning outputs to various pins; however it doesn't like it if you assign an output that *must* go through certain pins to a different one. It probably tries to output what it would do if it was going through the PWM circuitry that applies to p21-26?
Probably could be simpler (do I need the detach or does this just happen after the timeout for example?) but seems to work fine with now visible performance impact on anything else.
Greg
<<quote simon>>
Hi Greg,
....
Sounds like an interesting project!
Simon
<</quote>>
Thanks Simon,
I finally went for a software solution (below) and will now use all six PWM outputs for the motors allowing hexacopters etc.
I must put something up here but the Wiki is at:
http://code.google.com/p/uavp-mods/wiki/UAVXArm
For others the code I used is:
CameraTicker.attach_us(&CamPWMOnISR, 22000);
void CamPWMOnISR(void) {
PWMCamRoll.write(1);
CamRollTimeout.attach_us(&CamRollPWMOffISR, 1000 + PWM[CamRollC]);
PWMCamPitch.write(1);
CamPitchTimeout.attach_us(&CamPitchPWMOffISR, 1000 + PWM[CamPitchC]);
}
void CamRollPWMOffISR(void) {
PWMCamRoll.write(0);
CamRollTimeout.detach(); // redundant?
}
void CamPitchPWMOffISR(void) {
PWMCamPitch.write(0);
CamPitchTimeout.detach(); // redundant?
}
Probably could be simpler (do I need the detach or does this just happen after the timeout for example?) but seems to work fine with now visible performance impact on anything else.
Greg
I was just experimenting with the PwmOut function a moment ago. It seems that the function has some odd behavior when the PWM output pin is set to something other than p21-26 or LED1-4. I tried it with p20, and it seemed to affect all LED pins. I did not check the other PWM pins to see if they were affected. This result was unexpected as I thought the output would just not occur.
You are probably seeing mbed's "Blue Lights of Death" flashing pattern, as described in the handbook debugging page. This indicates a runtime error, such as trying to use a pin assignment not available for a certain interface. Sometimes an error message will also be printed on the usb serial port to help track it down.
Hope that makes things clear.
Simon
Hi Michael,
<<quote mtl1>>
I was just experimenting with the PwmOut function a moment ago. It seems that the function has some odd behavior when the PWM output pin is set to something other than p21-26 or LED1-4. I tried it with p20, and it seemed to affect all LED pins. I did not check the other PWM pins to see if they were affected. This result was unexpected as I thought the output would just not occur.
<</quote>>
You are probably seeing mbed's "Blue Lights of Death" flashing pattern, as described in the handbook [[http://mbed.org/handbook/Debugging|debugging]] page. This indicates a runtime error, such as trying to use a pin assignment not available for a certain interface. Sometimes an error message will also be printed on the usb serial port to help track it down.
Hope that makes things clear.
Simon
Does PWM operate using timers & interrupts? If I use PWM for several output pins, each with different duty cycles, are different timers involved, and does each different PWM output use/generate a different interrupt? Or is a single timer used along with a list of times to generate interrupts?
Bill
Does PWM operate using timers & interrupts? If I use PWM for several output pins, each with different duty cycles, are different timers involved, and does each different PWM output use/generate a different interrupt? Or is a single timer used along with a list of times to generate interrupts?
Bill
PWM outputs does not use any kind of interrupts. For generating PWM is used a dedicated timer.
You don't have to worry about interrupts when using PWM outputs.
PWM outputs does not use any kind of interrupts. For generating PWM is used a dedicated timer.
You don't have to worry about interrupts when using PWM outputs.
My frail understanding of USB is that there needs to be some housekeeping/maintenance between the OS and a USB device so that the OS knows the device is still present. This has to happen from time to time. I'm wondering whether a program that is running on the device for a while is able to fulfill this maintenance while it's executing its code. Interrupt driven techniques mean that this code can perform these maintenance calls in between interrupt driven code execution (like toggling pins involved in PWM). I want my embedded device to stay connected to a host throughout so the host can send it commands, data, etc., and I fear that while the device is running its internal stuff it may not be available to service this OS maintenance.
No doubt I have a muddy view; can anyone straighten me out?
Thanks.
Bill
Ok, another question about PWM, sort of.
My frail understanding of USB is that there needs to be some housekeeping/maintenance between the OS and a USB device so that the OS knows the device is still present. This has to happen from time to time. I'm wondering whether a program that is running on the device for a while is able to fulfill this maintenance while it's executing its code. Interrupt driven techniques mean that this code can perform these maintenance calls in between interrupt driven code execution (like toggling pins involved in PWM). I want my embedded device to stay connected to a host throughout so the host can send it commands, data, etc., and I fear that while the device is running its internal stuff it may not be available to service this OS maintenance.
No doubt I have a muddy view; can anyone straighten me out?
Thanks.
Bill
I've gotten a variant of one of the PWM examples going on my mbed. When I plug it into my Mac, a new device shows up in my /dev folder, and after I bring up a terminal and identify which device the mbed is. I can command the LEDs in my test circuit fine.
On a Windows 7 machine, when I plug in the mbed it shows up as a removable device like a thumb drive in the Device Manager. If I then bring up a terminal program, all I see are com ports, none of which is any use. Any pointers on this?
Thanks,
equipoise
And another thing...
I've gotten a variant of one of the PWM examples going on my mbed. When I plug it into my Mac, a new device shows up in my /dev folder, and after I bring up a terminal and identify which device the mbed is. I can command the LEDs in my test circuit fine.
On a Windows 7 machine, when I plug in the mbed it shows up as a removable device like a thumb drive in the Device Manager. If I then bring up a terminal program, all I see are com ports, none of which is any use. Any pointers on this?
Thanks,
-- equipoise
I found out how to get my mbed recognized and working on WIndows 7 - amazing how this "instructions" concept works. Still have questions about how or if there is a need to maintain a USB connection while code is running on the mbed.
Bill
I found out how to get my mbed recognized and working on WIndows 7 - amazing how this "instructions" concept works. Still have questions about how or if there is a need to maintain a USB connection while code is running on the mbed.
Bill
I wonder why there is not a function to GET the period... only one to get the dutycycle?
Or is there something I am missing?
Tue
The default period is 0.020s. Presumably if you changed it to something other than this you would know what you changed it to and so a function to GET the period would be redundant?
<<quote myren>>
I wonder why there is not a function to GET the period... only one to get the dutycycle?
Or is there something I am missing?
Tue
<</quote>>
The default period is 0.020s. Presumably if you changed it to something other than this you would know what you changed it to and so a function to GET the period would be redundant?
Hello from Switzerland
I need 4 PWM-outputs, all of the same base frequency. Now my problem: Each of the 4 PWM should have a delay one to each other. So the PWMs are not synchronous anymore. Can someone give me a tip, how to resolve this in a efficient way? Grazie Mille.
Hello Titi Tix; Thank you for your hardware-solution with external RC's and Schmitt-triggers. What i am looking for, is a solution by software. Looking at the user manual of the m-controller, you can find in the chapter PWM (page 511 / fig. 119) , that you can preset the different match registers... just i did not get a working result yet.
Hello from Switzerland
I need 4 PWM-outputs, all of the same base frequency. Now my problem: Each of the 4 PWM should have a delay one to each other. So the PWMs are not synchronous anymore. Can someone give me a tip, how to resolve this in a efficient way? Grazie Mille.
Hello Titi Tix; Thank you for your hardware-solution with external RC's and Schmitt-triggers. What i am looking for, is a solution by software. Looking at the user manual of the m-controller, you can find in the chapter PWM (page 511 / fig. 119) , that you can preset the different match registers... just i did not get a working result yet.
It depends on the delays. Try this way:
FirstPwmOut - R1- C2 to GND - triggerschmidt (74hc14) - out
This introduces a delay determined by RC . Use biger delay for SecondPwmOut and so on.
Also instead of 74hc14 you can use CD4093. Pay attention in software that the PWM obtained this way is inverted hardware or use another inverter for the output.
It depends on the delays. Try this way:
FirstPwmOut - R1- C2 to GND - triggerschmidt (74hc14) - out
This introduces a delay determined by RC . Use biger delay for SecondPwmOut and so on.
Also instead of 74hc14 you can use CD4093. Pay attention in software that the PWM obtained this way is inverted hardware or use another inverter for the output.
Hi there, I'm having some trouble understanding what happens when you set the PwmOut itself to a value. For example:
include "mbed.h"
PwmOut led(p21);
AnalogIn ain(p20);
main(){
while (1){
led=ain;
}
}
We are setting the 'led' to a value. But what in particular are we changing? The output voltage? Or some percentage?
Sorry if it's a stupid question, I'm new at this!
Thanks
Hi there, I'm having some trouble understanding what happens when you set the PwmOut itself to a value. For example:
#include "mbed.h"
PwmOut led(p21);
AnalogIn ain(p20);
main(){
while (1){
led=ain;
}
}
We are setting the 'led' to a value. But what in particular are we changing? The output voltage? Or some percentage?
Sorry if it's a stupid question, I'm new at this!
Thanks
Thanks, it's useful but it didn't answer my question unfortunately. What are we changing when we set the PWM to a value? I Understand the ratios/ percentages duty cycles and what not, but what is the actual value changing? (Read my other post if that didn't make sense)
Thanks!
Thanks, it's useful but it didn't answer my question unfortunately. What are we changing when we set the PWM to a value? I Understand the ratios/ percentages duty cycles and what not, but what is the actual value changing? (Read my other post if that didn't make sense)
Thanks!
led = ain is simply using the PwmOut operator= shorthand for led.write(ain), which is used to "Set the ouput duty-cycle, specified as a percentage (float)".
So for example, if you wrote:
led = 0.25;
...the duty cycle would be 25%, meaning the high-time pulsewidth would be 1/4 of the period.
Hope that helps!
Simon
Hi Oliver,
{{{led = ain}}} is simply using the PwmOut {{{operator=}}} shorthand for {{{led.write(ain)}}}, which is used to "Set the ouput duty-cycle, specified as a percentage (float)".
So for example, if you wrote:
{{{
led = 0.25;
}}}
...the duty cycle would be 25%, meaning the high-time pulsewidth would be 1/4 of the period.
Hope that helps!
Simon
I was wondering, is it possible to obtain a negative voltage without using an external inverter?
As an example, imagine I set the PWM to 50% duty cycle. This would produce around 1.35V. But I need
-1.35V. Is there an internal mechanism for this, or should I use an OpAmp? Thanks
Hey guys...
I was wondering, is it possible to obtain a negative voltage without using an external inverter?
As an example, imagine I set the PWM to 50% duty cycle. This would produce around 1.35V. But I need
-1.35V. Is there an internal mechanism for this, or should I use an OpAmp? Thanks
led = ain is simply using the PwmOut operator= shorthand for led.write(ain), which is used to "Set the ouput duty-cycle, specified as a percentage (float)".
So for example, if you wrote:
led = 0.25;
...the duty cycle would be 25%, meaning the high-time pulsewidth would be 1/4 of the period.
Hope that helps!
Simon
Thank you very much for the clear explanation, Simon!
<<quote simon>>
Hi Oliver,
{{{led = ain}}} is simply using the PwmOut {{{operator=}}} shorthand for {{{led.write(ain)}}}, which is used to "Set the ouput duty-cycle, specified as a percentage (float)".
So for example, if you wrote:
{{{
led = 0.25;
}}}
...the duty cycle would be 25%, meaning the high-time pulsewidth would be 1/4 of the period.
Hope that helps!
Simon
<</quote>>
Thank you very much for the clear explanation, Simon!
Hi guys!
I would like to know how i can switch off or switch on the PwmOut.
I think you can set the duty cycle to 0 or 1, which should give a steady signal (but I have not tested this).
<<quote raphael>>
Hi guys!
I would like to know how i can switch off or switch on the PwmOut.
<</quote>>
I think you can set the duty cycle to 0 or 1, which should give a steady signal (but I have not tested this).
I want to drive 18 servo's using PWMout.
As the mbed has 6 PWMout pins, I was thinking about using 2 Decade counters (CD 4017)
and feeding them each with one mbed PWMout pin which would then give me up to 2 x 10 = 20 servo's.
For this I need to be able to change the duty cycle before every time period.
I could connect the PWMout pin to a Interruptin pin and handle the duty cycle change in the ISR,
but I hate to loose 2 pins... Are there any better alternatives ?
Thanks.
Hi all,
I want to drive 18 servo's using PWMout.
As the mbed has 6 PWMout pins, I was thinking about using 2 Decade counters (CD 4017)
and feeding them each with one mbed PWMout pin which would then give me up to 2 x 10 = 20 servo's.
For this I need to be able to change the duty cycle before every time period.
I could connect the PWMout pin to a Interruptin pin and handle the duty cycle change in the ISR,
but I hate to loose 2 pins... Are there any better alternatives ?
Thanks.
I want to drive 18 servo's using PWMout.
As the mbed has 6 PWMout pins, I was thinking about using 2 Decade counters (CD 4017)
and feeding them each with one mbed PWMout pin which would then give me up to 2 x 10 = 20 servo's.
For this I need to be able to change the duty cycle before every time period.
I could connect the PWMout pin to a Interruptin pin and handle the duty cycle change in the ISR,
but I hate to loose 2 pins... Are there any better alternatives ?
Thanks.
Never mind, I have found a better solution. I will publish it shortly.
<<quote BartJanssens>>
Hi all,
I want to drive 18 servo's using PWMout.
As the mbed has 6 PWMout pins, I was thinking about using 2 Decade counters (CD 4017)
and feeding them each with one mbed PWMout pin which would then give me up to 2 x 10 = 20 servo's.
For this I need to be able to change the duty cycle before every time period.
I could connect the PWMout pin to a Interruptin pin and handle the duty cycle change in the ISR,
but I hate to loose 2 pins... Are there any better alternatives ?
Thanks.
<</quote>>
Never mind, I have found a better solution. I will publish it shortly.
I want to drive 18 servo's using PWMout.
As the mbed has 6 PWMout pins, I was thinking about using 2 Decade counters (CD 4017)
and feeding them each with one mbed PWMout pin which would then give me up to 2 x 10 = 20 servo's.
For this I need to be able to change the duty cycle before every time period.
I could connect the PWMout pin to a Interruptin pin and handle the duty cycle change in the ISR,
but I hate to loose 2 pins... Are there any better alternatives ?
Thanks.
Hey there, I also needed more than 6 servos for a project a while back. I wrote the UniServ library, which I published in the cook book.
This library follows the mBed servo API quite closely, but allows a servo on any I/O pin. I hope you find it useful :)
<<quote BartJanssens>>
Hi all,
I want to drive 18 servo's using PWMout.
As the mbed has 6 PWMout pins, I was thinking about using 2 Decade counters (CD 4017)
and feeding them each with one mbed PWMout pin which would then give me up to 2 x 10 = 20 servo's.
For this I need to be able to change the duty cycle before every time period.
I could connect the PWMout pin to a Interruptin pin and handle the duty cycle change in the ISR,
but I hate to loose 2 pins... Are there any better alternatives ?
Thanks.
<</quote>>
Hey there, I also needed more than 6 servos for a project a while back. I wrote the UniServ library, which I published in the cook book.
This library follows the mBed servo API quite closely, but allows a servo on any I/O pin. I hope you find it useful :)
The description of the parameter to write() shouldn't say:
"Set the ouput duty-cycle, specified as a percentage (float)"
as the expected range of values is 0.0 ... 1.0. Were it to be a percentage, the expected values would be 0.0 ... 100.0. "ouput" should also read "output".
A better specification of this method would be:
"Set the output duty-cycle, specified as a (float) fraction of 1.0"
The description of the parameter to write() shouldn't say:
"Set the ouput duty-cycle, specified as a percentage (float)"
as the expected range of values is 0.0 ... 1.0. Were it to be a percentage, the expected values would be 0.0 ... 100.0. "ouput" should also read "output".
A better specification of this method would be:
"Set the output duty-cycle, specified as a (float) fraction of 1.0"
Hello mbed Team! I'm getting to know this atomic ant :) and I have a question: Have you implemented the delete operator for this and other base classes? Specifically, I want to know if the implementation of the operator returns the hardware to the previous/default/non-operational state. Thank you!
Hello mbed Team! I'm getting to know this atomic ant :) and I have a question: Have you implemented the delete operator for this and other base classes? Specifically, I want to know if the implementation of the operator returns the hardware to the previous/default/non-operational state. Thank you!
No we don't. Do you think that would be useful? We could disable the pwm channel, but would be interesting to know if you'd want to go as far as returning the pin to an input?
Simon
Hi Luis,
No we don't. Do you think that would be useful? We could disable the pwm channel, but would be interesting to know if you'd want to go as far as returning the pin to an input?
Simon
PwmOut on the mbed-m0 and just when I needed it :)
I have tried it out and it doesn't quite work the way I expected based on the documentation above. It seems like pins such as p25, p26, and p10 which are on the same timer should have the same period but allow for different duty cycles. However when I try to use different duty cycles for each of these pins, the last write() call wins. As an example, the following sample code attempts to just blink one of the LEDs (the one connected to p10) but in actuality all three LEDs (p25, p26, and p10) blink.
PwmOut on the mbed-m0 and just when I needed it :)
I have tried it out and it doesn't quite work the way I expected based on the documentation above. It seems like pins such as p25, p26, and p10 which are on the same timer should have the same period but allow for different duty cycles. However when I try to use different duty cycles for each of these pins, the last write() call wins. As an example, the following sample code attempts to just blink one of the LEDs (the one connected to p10) but in actuality all three LEDs (p25, p26, and p10) blink.
<<code>>
#include <mbed.h>
int main()
{
PwmOut RedLED(p25);
PwmOut GreenLED(p26);
PwmOut WhiteLED(p10);
RedLED.write(0.0f);
GreenLED.write(0.0f);
WhiteLED.write(0.0f);
while(1)
{
wait(0.25f);
WhiteLED.write(1.0f);
wait(0.25f);
WhiteLED.write(0.0f);
}
}
<</code>>
it doesn't quite work the way I expected based on the documentation above.
It seems like pins such as p25, p26, and p10 which are on the same timer
should have the same period but allow for different duty cycles.
Originally, I thought of treating each timer as a separate PWM (with one period and one duty cycle), so this behaviour was the intended one.
Considering that LPC2368 and LPC1768 share the same period for all the 6 PWMs, I can see the point in expecting a separate duty cycle for each MR with a shared period.
I updated the LPC11U24 implementation to match this behaviour. [Rev 31]
Cheers,
Emilio
<<quote AdamGreen>>
it doesn't quite work the way I expected based on the documentation above.
It seems like pins such as p25, p26, and p10 which are on the same timer
should have the same period but allow for different duty cycles.
<</quote>>
Originally, I thought of treating each timer as a separate PWM (with one period and one duty cycle), so this behaviour was the intended one.
Considering that LPC2368 and LPC1768 share the same period for all the 6 PWMs, I can see the point in expecting a separate duty cycle for each MR with a shared period.
I updated the LPC11U24 implementation to match this behaviour. [Rev 31]
Cheers,
Emilio
I updated the LPC11U24 implementation to match this behaviour. [Rev 31]
Thanks! I upgraded to rev 31 and its works a charm!
<<quote emilmont>>
I updated the LPC11U24 implementation to match this behaviour. [Rev 31]
<</quote>>
Thanks! I upgraded to rev 31 and its works a charm!
Hello everyone. I would like to ask if it's possible to change output voltage of PwmOut. Have 3.3V but about 5V is needed (and I would prefer some kind of settings than amp =). Any suggestions? Thanks a lot!
Hello everyone. I would like to ask if it's possible to change output voltage of PwmOut. Have 3.3V but about 5V is needed (and I would prefer some kind of settings than amp =). Any suggestions? Thanks a lot!
The PwmOut is hardwired inside the LPC1768, which runs on 3.3V.
But you could attach a MOSFET, which connects to 5V, so this was the way around.
Lerche
The PwmOut is hardwired inside the LPC1768, which runs on 3.3V.
But you could attach a MOSFET, which connects to 5V, so this was the way around.
Lerche
The PWMOut LED code example will not run on the LPC11U24 - might make this a bit clearer near the example code. Might also indicate which pins can PWM in the pin function picture for the LPC11U24 somehow.
The PWMOut LED code example will not run on the LPC11U24 - might make this a bit clearer near the example code. Might also indicate which pins can PWM in the pin function picture for the LPC11U24 somehow.
Jim,
Yes, you are right about the example. It seems none of the on-board LEDS have PWM functionality on the LPC11U24. By the way, I got the Sparkfun Magician Chassis to work on the LPC11U24. I use p5, p6, 34 for the 1st PWM statement, and p25, p26, p10 for the 2nd PWM statement.
Don
Jim,
Yes, you are right about the example. It seems none of the on-board LEDS have PWM functionality on the LPC11U24. By the way, I got the Sparkfun Magician Chassis to work on the LPC11U24. I use p5, p6, 34 for the 1st PWM statement, and p25, p26, p10 for the 2nd PWM statement.
Don
hi
i am a very new user of mbed
i wanted to use pwm function
so, i cant really figure out how to set the pwm to be 21Mhz (86us). And for testing, how to use the read function to check the value of the pwm being outputted. The problem is that i do not know the command to print it on the terminal
hi
i am a very new user of mbed
i wanted to use pwm function
so, i cant really figure out how to set the pwm to be 21Mhz (86us). And for testing, how to use the read function to check the value of the pwm being outputted. The problem is that i do not know the command to print it on the terminal
I didn't use pwm anytime before but I think You want to set period to 86us? I hope this will help You.
#include "mbed.h"
// tx, rx serial terminal output via mbed USB connector
Serial pc(USBTX, USBRX);
// Create instance of PwmOut class named led with parameter specifiing output pin.
// Here LED1 on mbed but You can change to p21 - p26 pins on mbed to for example driving servo
PwmOut led(LED1);
// Main function executed once after mbed reset
int main() {
// Set baud rate for serial terminal output - must match terminal settings - 9600 default
pc.baud(9600);
// Here You can set period in us
led.period_us(86);
// Here You can set pulsewidth in us
led.pulsewidth_us(0);
//from here code will be executed in loop
while(1) {
// Create variable to store output duty cycle value in float format 0.00 - 1.00
// (multiplied by 100 can represent 0 - 100%
float pwm_output_value;
// For loop starts with float variable p set to 0.0f and is increased each time by 0.1f
// until variable p reach value 1.0f then for loop exits
for(float p = 0.0f; p < 1.0f; p += 0.1f) {
// Here we write PWM output duty cycle value in float format. As value is used variable p from for loop
// You can also use shorthand command for write ......... led = p;
led.write(p);
// Here we read PWM output duty cycle value in float format and store it to pwm_output_value variable
pwm_output_value = led.read();
// Here is value from pwm_output_value variable printed to terminal - %1.2f specifies format n.nn of value printed to terminal
// printf() slows down execution of code on slow baud rates like 9600 so use this to check that values are sent and then comment line with two //
pc.printf("\n\r PWM Output Walue is %1.2f \n\r", pwm_output_value);
// Here we wait
wait(0.1);
// For loop exits here and returns to execution of while(1) loop
}
// while(1) loop ends here and returns to start of while(1) loop - while(1) loop is then executed again
}
// main function ends here
}
Copy code above to new project and overwrite al code there.
Hello Muhammad,
I didn't use pwm anytime before but I think You want to set period to 86us? I hope this will help You.
<<code>>
#include "mbed.h"
// tx, rx serial terminal output via mbed USB connector
Serial pc(USBTX, USBRX);
// Create instance of PwmOut class named led with parameter specifiing output pin.
// Here LED1 on mbed but You can change to p21 - p26 pins on mbed to for example driving servo
PwmOut led(LED1);
// Main function executed once after mbed reset
int main() {
// Set baud rate for serial terminal output - must match terminal settings - 9600 default
pc.baud(9600);
// Here You can set period in us
led.period_us(86);
// Here You can set pulsewidth in us
led.pulsewidth_us(0);
//from here code will be executed in loop
while(1) {
// Create variable to store output duty cycle value in float format 0.00 - 1.00
// (multiplied by 100 can represent 0 - 100%
float pwm_output_value;
// For loop starts with float variable p set to 0.0f and is increased each time by 0.1f
// until variable p reach value 1.0f then for loop exits
for(float p = 0.0f; p < 1.0f; p += 0.1f) {
// Here we write PWM output duty cycle value in float format. As value is used variable p from for loop
// You can also use shorthand command for write ......... led = p;
led.write(p);
// Here we read PWM output duty cycle value in float format and store it to pwm_output_value variable
pwm_output_value = led.read();
// Here is value from pwm_output_value variable printed to terminal - %1.2f specifies format n.nn of value printed to terminal
// printf() slows down execution of code on slow baud rates like 9600 so use this to check that values are sent and then comment line with two //
pc.printf("\n\r PWM Output Walue is %1.2f \n\r", pwm_output_value);
// Here we wait
wait(0.1);
// For loop exits here and returns to execution of while(1) loop
}
// while(1) loop ends here and returns to start of while(1) loop - while(1) loop is then executed again
}
// main function ends here
}
<</code>>
Copy code above to new project and overwrite al code there.
You will also need to install driver for serial connection to terminal via mbed USB connector - more here [[http://mbed.org/handbook/Windows-serial-configuration]]
and install and set up terminal program - more here [[http://mbed.org/handbook/Terminals]]
Thank you so much for your detailed reply. I really appreciate how you wrote the code. Being a beginner, it really has helped me. I will get back to you for more questions :)
Nevertheless, Cheers for the help.
Hi Little llumpu,
Thank you so much for your detailed reply. I really appreciate how you wrote the code. Being a beginner, it really has helped me. I will get back to you for more questions :)
Nevertheless, Cheers for the help.
Bump: As "jim hamblen" mentioned above, please add a comment to the LED dim example above stating that it does not work on LPC11U24 due to the fact that the pin connected to the on-board LEDs do not support PWM.
Bump: As "jim hamblen" mentioned above, please add a comment to the LED dim example above stating that it does not work on LPC11U24 due to the fact that the pin connected to the on-board LEDs do not support PWM.
Newbee question. In the dimmer example at the very top of this page.
"p" is not declared, however "float" is used inside the for statement, then "f" after each value for p.
Is this a different way of declaring a variable? If so, why is p not simply declared as a float from the start?
Thanks, Rob
Newbee question. In the dimmer example at the very top of this page.
"p" is not declared, however "float" is used inside the for statement, then "f" after each value for p.
Is this a different way of declaring a variable? If so, why is p not simply declared as a float from the start?
Thanks, Rob
If second is correct then what is the difference between pulse width and duty cycle. Because duty cycle is the on time duration in percentage and VAVG = Duty cycle * VCC.
**Any one Please Rectify**\\
which defination is correct for // Pulse width//
# Pulse width is the length of activation within a signal.\\[[http://www.ehow.com/how_8618299_calculate-pulse-width.html]]
# Pulse width is acronym for period in square wave.\\[[http://www.electronics-tutorials.ws/waveforms/waveforms.html]]\\
* If second is correct then what is the difference between pulse width and duty cycle. Because duty cycle is the on time duration in percentage and V,,AVG,, = Duty cycle * V,,CC,,.\\
If second is correct then what is the difference between pulse width and duty cycle. Because duty cycle is the on time duration in percentage and VAVG = Duty cycle * VCC.
I took some readings to understand relation between Pulse width and Duty cycle:
Period
Pulse Width
Duty Cycle
Voltage(VC=3.1V)
0.02s
1ns
0.005
1.6mV
0.02s
2ns
.001
3.1mV
0.02s
3ns
.0015
4.6mV
Duty Cycle should be = Pulse Width / Period * 100
but values are showing Duty Cycle = Pulse Width / Period Any Explanation Please
<<quote harrisjunaid>>
**Any one Please Rectify**\\
which defination is correct for // Pulse width//
# Pulse width is the length of activation within a signal.\\[[http://www.ehow.com/how_8618299_calculate-pulse-width.html]]
# Pulse width is acronym for period in square wave.\\[[http://www.electronics-tutorials.ws/waveforms/waveforms.html]]\\
* If second is correct then what is the difference between pulse width and duty cycle. Because duty cycle is the on time duration in percentage and V,,AVG,, = Duty cycle * V,,CC,,.\\
<</quote>>
I took some readings to understand relation between Pulse width and Duty cycle:
|= Period |= Pulse Width |= Duty Cycle |= Voltage(V,,C,,=3.1V) |
|0.02s|1ns|0.005|1.6mV|
|0.02s|2ns|.001|3.1mV|
|0.02s|3ns|.0015|4.6mV|
Duty Cycle should be = Pulse Width / Period * 100\\
but values are showing Duty Cycle = Pulse Width / Period\\
**Any Explanation Please**
If second is correct then what is the difference between pulse width and duty cycle. Because duty cycle is the on time duration in percentage and VAVG = Duty cycle * VCC.
I took some readings to understand relation between Pulse width and Duty cycle:
Period
Pulse Width
Duty Cycle
Voltage(VC=3.1V)
0.02s
1ns
0.005
1.6mV
0.02s
2ns
.001
3.1mV
0.02s
3ns
.0015
4.6mV
Duty Cycle should be = Pulse Width / Period * 100
but values are showing Duty Cycle = Pulse Width / Period Any Explanation Please
From the table I conclude that dutycycle from PwmOut read function is not in percentage. As Vout = VC * Duty Cycle but results are showing Vout = VC * Duty Cycle * 100.
<<quote harrisjunaid>>
<<quote harrisjunaid>>
**Any one Please Rectify**\\
which defination is correct for // Pulse width//
# Pulse width is the length of activation within a signal.\\[[http://www.ehow.com/how_8618299_calculate-pulse-width.html]]
# Pulse width is acronym for period in square wave.\\[[http://www.electronics-tutorials.ws/waveforms/waveforms.html]]\\
* If second is correct then what is the difference between pulse width and duty cycle. Because duty cycle is the on time duration in percentage and V,,AVG,, = Duty cycle * V,,CC,,.\\
<</quote>>
I took some readings to understand relation between Pulse width and Duty cycle:
|= Period |= Pulse Width |= Duty Cycle |= Voltage(V,,C,,=3.1V) |
|0.02s|1ns|0.005|1.6mV|
|0.02s|2ns|.001|3.1mV|
|0.02s|3ns|.0015|4.6mV|
Duty Cycle should be = Pulse Width / Period * 100\\
but values are showing Duty Cycle = Pulse Width / Period\\
**Any Explanation Please**
<</quote>>
From the table I conclude that dutycycle from PwmOut read function is not in percentage. As V,,out,, = V,,C,, * Duty Cycle but results are showing V,,out,, = V,,C,, * Duty Cycle //* 100//.
Hello! Please somebody tell me how could i start the pwm channels individually let's say with an offset of 50ms. i need this to control the phases of a BLDC motor. (and by the way could somebody tell me why the led only blinks when i set the period to 1s and the duty cycle to 50%...shouldn't it be half lit and not blink on half a second?)
Hello! Please somebody tell me how could i start the pwm channels individually let's say with an offset of 50ms. i need this to control the phases of a BLDC motor. (and by the way could somebody tell me why the led only blinks when i set the period to 1s and the duty cycle to 50%...shouldn't it be half lit and not blink on half a second?)
thanks for the directions! but could anyone answer my second question? (i'm just curious)
<<quote hzrnbgy>>
some low level hacking to enable staggered PWM output
[[http://mbed.org/forum/helloworld/topic/3406/]]
<</quote>>
thanks for the directions! but could anyone answer my second question? (i'm just curious)
and by the way could somebody tell me why the led only blinks when i set the period to 1s and the duty cycle to 50%...shouldn't it be half lit and not blink on half a second?
50% duty cycle means that its going to be FULLY ON 50% of the time. so for a period of 1 second, the LED would be FULLY ON for half a second and FULLY OFF for the other half
if you try a higher PWM frequency, lets say 100 Hz (period of 10ms). turning the LED ON 50% of the time would give you the illusion of a half-lit LED because this ON-OFF cycle happens very fast (100 times a second in our case) and your eyes cannot distinguish the ON-OFF period discretely but a rather continuous light of 50% intensity
<<quote>>
and by the way could somebody tell me why the led only blinks when i set the period to 1s and the duty cycle to 50%...shouldn't it be half lit and not blink on half a second?
<</quote>>
50% duty cycle means that its going to be FULLY ON 50% of the time. so for a period of 1 second, the LED would be FULLY ON for half a second and FULLY OFF for the other half
if you try a higher PWM frequency, lets say 100 Hz (period of 10ms). turning the LED ON 50% of the time would give you the illusion of a half-lit LED because this ON-OFF cycle happens very fast (100 times a second in our case) and your eyes cannot distinguish the ON-OFF period discretely but a rather continuous light of 50% intensity
hello guys ,I have a problem when i want to control 2 servos, can someone help me with some simple example?
I'm using a RN42 bluetooth module to control the servo!
hello guys ,I have a problem when i want to control 2 servos, can someone help me with some simple example?
I'm using a RN42 bluetooth module to control the servo!
Hi guys, I need some help with my new mbed. How do i check my PWM is working? I test hello world program with my mbed. it didn't show anything but i still can turn on and off led. what is the problem? can anyone help me?
Hi guys, I need some help with my new mbed. How do i check my PWM is working? I test hello world program with my mbed. it didn't show anything but i still can turn on and off led. what is the problem? can anyone help me?
A note for the PWM:
building a application with various pulse width's and pulselength, i noticed that you have to load first the pulseperiod (tOn+tOff) , and after that the pulslength (tOn).
Not doeing so give's strange unwanted pulses.
A note for the PWM:
building a application with various pulse width's and pulselength, i noticed that you have to load first the pulseperiod (tOn+tOff) , and after that the pulslength (tOn).
Not doeing so give's strange unwanted pulses.
On the LPC11U24 this code works perfectly. 1ms period and 50% duty cycle.
#include "mbed.h"
PwmOut bob(p6);
int main() {
bob.period_ms(1);
bob.write(0.5);
while (1);
}
On the LPC11U24 this code doesn't work as it should. 640us period and 50% duty cycle.
#include "mbed.h"
PwmOut bob(p6);
int main() {
bob.period_ms(2);
bob.write(0.5);
while (1);
}
I found the answer. I will post it shortly.
On the LPC11U24 this code works perfectly. 1ms period and 50% duty cycle.
<<code>>
#include "mbed.h"
PwmOut bob(p6);
int main() {
bob.period_ms(1);
bob.write(0.5);
while (1);
}
<</code>>
On the LPC11U24 this code doesn't work as it should. 640us period and 50% duty cycle.
<<code>>
#include "mbed.h"
PwmOut bob(p6);
int main() {
bob.period_ms(2);
bob.write(0.5);
while (1);
}
<</code>>
I found the answer. I will post it shortly.
Just got an mbed and am testing out the PWM function. I used the example code from above:
<<code>>
#include "mbed.h"
PwmOut led(LED1);
int main() {
while(1) {
for(float p = 0.0f; p < 1.0f; p += 0.1f) {
led = p;
wait(0.1);
}
}
}
<</code>>
When I load the program on my mbed, I just get the flashing lights of death. I'm able to run to default program just fine:
<<code>>
#include "mbed.h"
DigitalOut myled(LED1);
int main()
{
while(1) {
myled = 1;
wait(0.2);
myled = 0;
wait(0.2);
}
}
<</code>>
Any clue why this is happening?
Just got an mbed and am testing out the PWM function. I used the example code from above.
When I load the program on my mbed, I just get the flashing lights of death. I'm able to run to default program just fine.
Any clue why this is happening?
The example is not working on the lpc11u24. It will only run on the lpc1768. The LEDs are not connected to PWM pins on the lpc11u24 mbed.
<<quote mvonpohle>>
Just got an mbed and am testing out the PWM function. I used the example code from above.
When I load the program on my mbed, I just get the flashing lights of death. I'm able to run to default program just fine.
Any clue why this is happening?
<</quote>>
The example is not working on the lpc11u24. It will only run on the lpc1768. The LEDs are not connected to PWM pins on the lpc11u24 mbed.
I'm trying to control a dc motor with pwm. such that,I increase the voltage from 0-3.3 and the speed of motor should increase. can someone put up a code.
I'm trying to control a dc motor with pwm. such that,I increase the voltage from 0-3.3 and the speed of motor should increase. can someone put up a code.
Please login to post comments.