7 years, 2 months ago.

How to write a PWM control OFF to ON?

I know that about PWM control in page of 'PWMout': https://developer.mbed.org/handbook/PwmOut, but there is only a type of case for ON to OFF, I want to write a case that OFF to ON in a duty cycle (basic on image), how to write a PWM control program for OFF to ON? Please give me some code, thank you. /media/uploads/zeroex123/pwm.jpg

1 Answer

7 years, 2 months ago.

I hope this is what you wanted.

If you look at your diagram time ON+ time_OFF is always equal to 1.0 so time_OFF is (1.0-time_ON)

#include "mbed.h"
 
PwmOut led(LED1);
float time_OFF=0.2f; 
int main() {
    // specify period first
    led.period(4.0f);      // 4 second period
      // 20% off
    led.write(1.0f-time_OFF);      // 80% on duty cycle, relative to period
    //led = 0.5f;          // shorthand for led.write()
    //led.pulsewidth(2);   // alternative to led.write, set duty cycle time in seconds
    while(1);
}

I am sorry that I cannot use osciallator today, so I want to ask question according to this program. Does this program is start from OFF(low value) and end from ON(high value)? I see that there is also similar to example program (ON first and OFF end). I must start in OFF and end in ON such as image in one duty cycle since this is use for compare to other PWM at same duty cycle.

posted by so k 20 Feb 2017

Hi so k

I understand you want something different. I think there are a couple of ways to do that. a) How about inverting the PWM signal with a logic gate? b) What about manually switching the output to make the first cycle of the PWM?

posted by Ian Kilburn 20 Feb 2017

So, there is no method from program control (eg. inverting PWM,etc...)? Since I have finished hardware part. I need to design again that using your solution. About (b), I will consider if there is no solution from program.

posted by so k 20 Feb 2017

Hi so k, Sometimes the polarity and starting condition is an option in the PWM implementation in the micro controller. You might be able to change these for example by setting a register. Not all these options are supported in the mbed function. Those details are usually in the data sheet. Take a look also at the mbed source code for pwmOut and see if there is anything that can be modified for your micro controller.

You might be able to use SoftPWM https://developer.mbed.org/users/komaida424/code/SoftPWM/

posted by Ian Kilburn 20 Feb 2017