Getting started

What is a microcontroller?

Watch this short video (https://www.youtube.com/embed/jKT4H0bstH8) if you are not sure what a micro-controller is.



ARM & mbed microcontrollers

Unboxing and testing

  • Remove the micro-controller from its packaging, and connect the micro-USB cable to USB PWR slot. Connect the other end to your computer. The microcontroller is powered on.
  • The micro-controller is loaded at the first start with a default program that blinks on of the LEDs. The device has 3 LEDs accessible to the user. Press the blue button at the bottom left corner to select another the LED. It should also blink at a different frequency. This is your first interaction with your new microcontroller!

Tip

Keep the plastic packaging to store and transport the microcontroller.

Working with ARM’s mbed development environment

This video (https://www.youtube.com/embed/BAzKg3vcB88) will explain to you the gist of how to program ARM based microcontrollers using their mbed development environment.

Register an account on the mbed development platform

  • Open an account. Visit http://os.mbed.com/
  • Click login/sign-up. Create a new account if you don’t have one already.
  • Once you are logged in, click on ``Compiler’’ (top horizontal menu) to access the development environment.
  • If this is the first time you use the board with the online mbed environment: at the top right of the screen, click ‘no device selected’, then ‘add platform’ and find device ‘NUCLEO-F746ZG’.

You are ready to start coding!


A first project

Creation of a new project

  • Create new project by clicking on “New”,
  • Make sure that the platform (i.e. the micro-controller model) is correct, and select the template “mbed OS Blinky LED Hello World”,
  • Open the “main.cpp” file. The code should look like this:
/* mbed Microcontroller Library
 * Copyright (c) 2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "platform/mbed_thread.h"


// Blinking rate in milliseconds
#define BLINKING_RATE_MS          500


int main()
{
    // Initialise the digital pin LED1 as an output
    DigitalOut led(LED1);

    while (true) {
        led = !led;
        thread_sleep_for(BLINKING_RATE_MS);
    }
}

To get us started, we will use the code below as a first example. Please delete the sample code above and replace it with the code below.

#include "mbed.h"

DigitalOut myled(LED1);

int main() {
        while(true) {
                myled = 1; // LED is ON
                wait(0.2); // 200 ms
                myled = 0; // LED is OFF
                wait(1.0); // 1 sec
        }
}
  • Press the compile button. If there is no error in your code, a file is then downloaded on your computer, ready to be installed on your microcontroller.

You will notice a number of warning messages related to the use of the function “wait”. As you will see in this lab, this function is ineffective as it keeps the processor busy doing nothing. The original template code did a better job. You can read more about this in the documentation if you want, but for now we will continue to use the wait function.

Dissecting the sample code

Basic knowledge of C/C++ programming is now assumed.

Do not hesitate to consult online documentation about C/C++ when appropriate. There are so many good sources available to you! See for instance:

Basic Introduction: https://www.geeksforgeeks.org/c-language-set-1-introduction/

Good set of tutorials: http://www.tutorialspoint.com/cprogramming/

Here are a few comments that may be helpful at this point:

  • main() is the function that is executed when the microcontroller starts.
  • In C/C++, a line of code is terminated with ;, and a block is delimited by curly brackets {...}. This is different from python where line returns and indentation provide such information. Python style indentation is however good practice for the readability of your code.
  • The main program contains a single “while” loop. The term between parentheses after while should be 0 or false for the loop to end, so this loops never ends.
  • The variable myled controls the state of LED1. Although it is manipulated as an integer, it is an instance of the class DigitalOut. The pin number is specified when the object is declared, and remains attached to it. LED1 is a shortcut for the pin number associated with the user LED1. These associations are board specific, and defined in the “mbed.h” header file - so we don’t need to worry about them.
  • The variable myled is defined at the top of the code, outside of any function. It is a global variable that will be available to all functions.

Installing the code on your micro-controller

  • Connect the micro-controller to your computer using a micro-USB cable. The board should be visible as a USB drive on the computer. If it isn’t, you may need to install specific drivers; consult this page to get support. If you are using Windows on versions older than Win 10, try ignoring warnings such as “Driver not installed correctly”; it may work well enough already.
  • Drag and drop the .bin file obtained at the previous step on the board
  • LED at top right corner should be temporarily flashing to indicate that the transfer is happening. The program starts automatically after that.
  • You should see a LED1 blinking!

Task

Explore different blinking frequencies and try the other LEDs, LED2 and LED3.