9 years, 3 months ago.

Question about pwm

i just modified standard code

  1. include "mbed.h"

PwmOut mypwm(PWM_OUT);

DigitalOut myled(LED1);

int i = 1;

int main() {

mypwm.period_ms(100); mypwm.pulsewidth_ms(1);

printf("pwm set to %.2f %%\n", mypwm.read() * 100);

while(1) { myled = 1; wait_ms(1000); mypwm.pulsewidth_ms(i);

if (i>99) i=1;; i++; } }

I changed the source standard example PWM expected that the LED will change the brightness of the LED lights but always the same. What am I doing wrong? Help please

1 Answer

9 years, 3 months ago.

PWM value is a floating number from 0.01 to 0.99

Accepted Answer

Sorry...mypwm.period_ms(100); mypwm.pulsewidth_ms(1);

_ms - milliseconds? Where i must put a floating value?

is pulsewidth a step value?

posted by Лапшов Александрович 28 Jan 2015

In the example below, the rled pin contains the floating value. Example: rled(0.5). Also, the period is set up in main()

#include "mbed.h"
PwmOut rled(LED_RED);

//----
// moving RGB LED display.  Hacked from:  david dicarlo / FRDM_RGBLED

const float pi = 3.1415927;
float iLeds = 0.0;
float rLedPwm = 0.01;

void sinLEDs() {
    iLeds += 0.02;
    if(iLeds > (60.0 * pi)) iLeds = 0.0;
    rLedPwm = (1 + sin(2 * iLeds)) / 2; 
    rled = rLedPwm; 
}

//----
int main() {
    rled.period_us(5000);
    while(1) {
        sinLEDs();
        wait_ms(20);
    }
}
posted by Kevin Braun 28 Jan 2015