Getting Started With C/C++ On The Micro:bit
Getting Started With C/C++ On The Micro:bit
Written by Harry Fairhead   
Thursday, 21 April 2016
Article Index
Getting Started With C/C++ On The Micro:bit
A simple example project

Anyone who wants to use the BBC micro:bit to its full potential as an IoT device needs to look outside the coding environments provided by its own website. As an mbed device, however, the micro:bit  is capable of being programmed in C/C++. Here we look at how to use the mbed online compiler for a simple demo program.

Important note:

This article and the rest of the book have moved to our sister site:

http://www.iot-programmer.com/

Where you will find any updates and more chapters.

 

 

The fact that the micro:bit is an mbed device brings with it both good and not so good news. The good news is that you can program the micro:bit in C/C++ because it is an mbed system. The bad news is that the documentation only gives you a hint as to how to do it. Here we explore how to use the online compiler. In a follow-on tutorial we'll find out how to do the same job offline using NetBeans. While I'm specifically looking at the Micro:bit, much of this will apply to any mbed device.

If you want to get started withC/C++ on the micro:bit quickly and don't mind signing up to an online service then it is all very easy. On the other hand, if you have a preference for a desktop IDE or just a command line interface things are a bit more difficult. The reason is that mbed comes with a brand new make replacement called yotta. It's not that yotta is bad, it is just another thing to learn. It is also surprising how complicated embedded programming has - become so much so that it actually need something this complicated. We seem to be a long way from the C programming running on almost bare metal. Once upon a time the hardware was the tough part - now it's the software as well.

So if you want an easy life go for the online compiler. 

If you want more control, but perhaps more complexity, go for the offline approach. 

I personally prefer the off line approach using NetBeans and suggest that after you have tried things out with the online compiler you spend a few hours getting things set up for NetBeans development, which is described in the next article.

Why C/C++?

Before you spend time finding out how to program in C/C++ there is the good question of why bother? After all, you have the choice of Touch Develop, JavaScript and MicroPython to program the micro:bit and they are all fairly easy to use. 

The answer to the question of why C/C++ is that it gets you closest to the hardware. Not all of the features and facilities of the micro:bit are accessible from the higher level languages. For example, at the moment you can't get access to the micro:bit's radio. Using C/C++ and the microbit library you can use the BlueTooth radio as a simple point to point radio connection between micro:bits. This makes it possible to build radio control robots and similar gadgets - something much harder if all of the connections have to go through the BlueTooth stack. 

Another example is the LED control. In MicroPython you have nine brightness levels. In the micro:bit's other supplied languages the LEDs are either on or off. In C/C++ you can set any of 255 levels. 

Going beyond the standard hardware you can also implement missing hardware facilities. For example, there is no SPI bus available under any of the supplied languages, only an I2C bus. Using C/C++ you can access a standard mbed SPI driver and work with SPI peripherals. You can even use your own code to implement protocols that are not supported such as the one-wire bus and the DHT11/22 temperature humidity sensor. 

The reason you can implement these additional protocols is that C/C++ is about as fast as you can get on any machine. You might be able to get some more speed out of an assembly language version of a program, but it would only be a few percent. Using C/C++ lets your programs run tens of times faster. For example, the blinky program given below runs about 20 times faster than the equivalent MicroPython program. When you are writing embedded programs often speed really does matter. You might not be able to write it in MicroPython and have to move to C/C++. If you can't write it in C/C++ then there is a good chance it can't be done using the hardware you have. 

C/C++ isn't significantly harder to work with than other languages. C is particularly simple and easy to use. The micro:bit library is written in C++, but for most of the time you can make use of it in a dialect of C which is quite good enough for the task.

The online compiler

The online compiler for the micro:bit is the best way to get started. 

To use the online compiler you have to sign up with mbed.

Go to:

https://www.mbed.com/

and sign up for an account. At the time of writing, to find the online compiler a you have to go to the mbed Classic Developer site, but this could change. 

Once you have an account and have signed in, your first task is to get the compiler set up with the micro:bit as its target. You can either go to Platforms and select the vendor as BBC to see the one and only product in that category, i.e. the micro:bit

compiler1

 

Alternatively you can go to the compiler and select the target device icon in the top right, which initially shows as "No device Selected". Click the Add Platform button and you are back to the Platform selection page. Select the BBC micro:bit and you are back in the same place.

The micro:bit page contains a lot of information but if you just want to work with it in the compiler simply select the Add to your mbed Compiler at the right of the screen:

 

 

add

 

Following this when you go back to the compiler you will see the BBC micro:bit as the target platform in the top right hand corner:

 

bbccompiler

Hello Blinky

You are now ready to start work on a project but at the moment there isn't a good starter project template. However Lancaster University has provided a template called Blinky to flash an LED - the equivalent of a Hello World program for the device.

There are two way to work with the micro:bit as a pure mbed device or using the specific library that Lancaster University have built. As long as you don't want to change to a different device the Lancaster library is the one to use. The mbed library makes it easy to write a single program that will work on different hardware and this is its big advantage. You can use the mbed library with the micro:bit but unless you really do want to move to another piece of hardware it is better to use the micro:bit library.

To get a starter project you have to select the samples template because the blinky templates use the mbed library.

 

new

It takes some time to build the project but eventually you will see the finished project ready for you to edit. 

The C/C++ programs are all in the source directory as usual but the difference is that you are using the microbit library rather than the usual mbed library. 

 

samplestructure

 

If you want to you can compile (ignore the compiler warnings) and run the sample without modification just to check everything works. If you select the compile button the program is compiled and then downloaded to your machine. You then have to download the file - microbit-samples_NRF51_MICROBIT.hex - the the micro:bit via the usb drive in the usual way. When you run the program you will discover that by default you will see a Hello World message scroll across the screen. 

If you want to see any of the other demos in action simply open the MicroBitSamples.h file and uncomment the line corresponding to the one you want to run.

 



Last Updated ( Sunday, 10 July 2016 )
 
 

   
Banner
RSS feed of all content
I Programmer - full contents
Copyright © 2017 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.