8 years, 8 months ago.

Templete code does not work in my board

<< code >>

  1. include "mbed.h" DigitalOut myled(LED1); int main() { while(1) { myled = 1; LED is ON wait(1); 200 ms myled = 0; LED is OFF wait(1); 1 sec } } << /code >>

The code above works fine but the following not works. I do not know why.

Can anyone give a hint?

<< code >> From mbed templete

  1. include "mbed.h" PwmOut mypwm(PWM_OUT); DigitalOut myled(LED1); int main() { mypwm.period_ms(10); mypwm.pulsewidth_ms(1); printf("pwm set to %.2f %%\n", mypwm.read() * 100); while(1) { myled = !myled; wait(1); } } << /code >>

Define "not works". It doesn't compile (if so what is the error)? It doesn't run? Do you get anything out of the serial port?

Also please use <<code>> and <</code>> to correctly format any sections of code that you post. The preview button is your friend.

posted by Andy A 01 Sep 2015

2 Answers

8 years, 8 months ago.

Try the following As you do not define which serial port you are writing to or the BAUD rate If you dont use Serial class your code compiles and writes to the USB/Serial on the ST you just need to use PuTTY, teraterm or same to monitor

#include "mbed.h"
PwmOut mypwm(PWM_OUT);// PWM/D3
DigitalOut myled(LED1);
Serial pc(USBTX,USBRX);
int main() {
    pc.baud(115200);
    mypwm.period_ms(10);
    mypwm.pulsewidth_ms(1);
    printf("pwm set to %.2f %%\n", mypwm.read() * 100);
    //or to be more definative pc.printf("pwm set to %.2f %%\n", mypwm.read() * 100);
    while(1) {
        myled = !myled; wait(1);
        }
}

regards

Thanks Andy A, Ok, "Not works" means compiling ok, the led not blinks.

The problem is at "myled = !myled" I think.

posted by liu pf 04 Sep 2015

I think I should learn more of C++.

posted by liu pf 04 Sep 2015
8 years, 7 months ago.

You have not defined a pin for PWM out.

If you are using the LPC1768 - Blue MBED, then the LED's are also PWM capable !

{
    myled = !myled;
    wait (1);
}

is fine, in one or two lines :)

ceri