Go back to the examples list

Blink

This is the Hello World of microcontroller programming : turning an LED on and off. It demonstrates efficiently how the microcontroller is initialized, how to control an output pin, and how to make people smile.

If you need help to understand this program, there is a line-by-line explaination in the Getting started tutorial.

Two versions of the same program are presented below : a version using the helper module specifically for the Carbide board, and a generic version using only the main library functions which can be used on any board.

It might surprise you that setHigh() turns the LED off and setLow() turns the LED on. This is because the other side of the LED is connected to Vcc and the pin is used as a current sink, so it needs to be pulled to GND in order to let the current flow : this is called negative logic. The reason for doing this is that the microcontroller is not designed to supply lots of currents on its pins, however, it can tolerate to let a bit more current pass to ground. Anyway, if you plan to drive more powerful devices than a LED (e.g. a motor), remember to use a dedicated circuit for this (such as based on a transistor for example).

Carbide

blink.cpp [download]
wget http://libtungsten.io/static/code/examples/blink_carbide/blink.cpp

#include <core.h>
#include <carbide.h>

int main() {
    // Init the board, including defining the
    // red LED pin as output
    Carbide::init(true);

    while (true) {
        // Turn the LED on
        Carbide::setLedR(true);

        // Wait for half a second
        Core::sleep(500);

        // Turn the LED off
        Carbide::setLedR(false);

        // Wait for half a second
        Core::sleep(500);
    }
}

Makefile [download]
wget http://libtungsten.io/static/code/examples/blink_carbide/Makefile

NAME=blink

BOOTLOADER=true
CARBIDE=true

# Available modules : adc dac eic gloc i2c spi tc trng usart
# Some modules such as gpio and flash are already compiled by default
# and must not be added here.
MODULES=

# The toolchain's bin/ path, don't forget to customize it
# If this directory is already in your PATH, comment this line.
TOOLCHAIN_PATH=/opt/arm-none-eabi/bin/

# Include the main lib makefile
include libtungsten/Makefile

Generic

blink.cpp [download]
wget http://libtungsten.io/static/code/examples/blink/blink.cpp

#include <core.h>
#include <gpio.h>

int main() {
    // Init the microcontroller
    Core::init();

    // Define the pin on which the LED is connected
    // On Carbide, this is the red LED
    const GPIO::Pin PIN_LED = GPIO::PA00;

    // Enable this pin in output to a HIGH state
    GPIO::enableOutput(PIN_LED, GPIO::HIGH);

    while (true) {
        // Turn the LED on
        GPIO::setLow(PIN_LED);

        // Wait for half a second
        Core::sleep(500);

        // Turn the LED off
        GPIO::setHigh(PIN_LED);

        // Wait for half a second
        Core::sleep(500);
    }
}

Makefile [download]
wget http://libtungsten.io/static/code/examples/blink/Makefile

NAME=blink

BOOTLOADER=true

# Available modules : adc dac eic gloc i2c spi tc trng usart
# Some modules such as gpio and flash are already compiled by default
# and must not be added here.
MODULES=

# The toolchain's bin/ path, don't forget to customize it
# If this directory is already in your PATH, comment this line.
TOOLCHAIN_PATH=/opt/arm-none-eabi/bin/

# Include the main lib makefile
include libtungsten/Makefile