Getting Started Help

Hello,
I have been trying to back in to programming a LPC 1768 after 4years of being away, and I have noticed how much the API has changed. One of the example programs I have been playing with to try and get back up to speed is the blinky example, and when I run it in its initial form LED1 will come on, on the board but does not ever turn off ie blink.
Previously, I would just use "led = 1; wait(xx); led = 0; wait();, but ive seen how wait has been depreciated…
Here is the example code, could someone please help me prod through this to help me understand?

//
#include “mbed.h”

// Blinking rate in milliseconds

#define BLINKING_RATE 500ms

int main()

{

// Initialise the digital pin LED1 as an output

DigitalOut led(LED1);

while (true) {

    led = !led;

    ThisThread::sleep_for(BLINKING_RATE);

}

}

Oh this is a somewhat common issue, on LPC1768 you need to update the dev board firmware for Mbed 6 to work properly. AFAIK new-from-the-factory boards are still being shipped with broken firmware -_-

2 Likes

Hello Dylan,

You can still have

led = 1;
ThisThread::sleep_for(500ms);
led = 0;
ThisThread::sleep_for(500ms);

The line:

led = !led;

is just a shorthand code avoiding delaration of temporary variales.
It’s equivalent to:

bool currentLedStatus = led.read();    // read current led status
bool newLedStatus = !currentLedStatus; // calculate new status as opposit to the current one
led = newLedStatus;                    // set led status to the calculated one

Although it was written for Mbed OS 2, I’d recomment to read Andy Kirkham’s excellent article Regarding interrupts, their use and blocking. It will explain why should be the wait function avoided as much as possible.
The Mbed OS 6 features threads. One can think of them as additional endless while loops running in parallel with the one located in the main function. To overcome the inefficiency of the wait function (as explained in Andy’s article) it was replaced with a more efficien one called ThisThread::sleep_for. This function allows the other threads (endless while loops) to run while this while loop is waiting (sleeping) for the time passed to the function. For more info read the RTOS overview.

Best regards, Zoltan

1 Like