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

Your First Program

At the moment the samples provide the best starting template for you own projects. Create a new project based on the samples template and call it pulsetest

Next delete all of the source files in the Source directory. The quickest way to do this is to delete the Source directory and then add it back as a new directory. Create a new file called main.cpp - you can call it anything you like but the name main gives you the clue that it is the start of the program. 

Enter the following code:

#include "MicroBit.h"

MicroBit uBit;

int main() {
 uBit.init();
 uBit.display.scroll("NEW PROGRAM");
 while (1) {
  uBit.io.P0.setDigitalValue(1);
  uBit.io.P0.setDigitalValue(0);
 }
 release_fiber();
}

 

compiler2

 

 

Compile the project, download the program and if you connect a logic analyser or oscilloscope to P0 you will see the fastest pulse that the micro:bit can produce.  

 

pulsetrain

 

So programming in C/C++ gives you a 3.5 microsecond pulse or a 133kHz square wave. There is also a 0.1ms pause every 6ms, probably the system timer interrupting as the documentation gives the granularity of the system timer as 6ms.

This should be compared with the same program in MicroPython that produces a 72 microsecond pulse, i.e around 20 times slower. 

Is it possible to do better than 3.5 microseconds?

Update

The answer to the question is yes, quite a lot better. With only little additional work you can shorten the pulse width to around 0.5 microseonds. If anyone wants to know how - just ask. 

 

Finally if you want a standard "blinky" test program - which as mentioned is the real hello world of mbed programming why not try:

while(1)
{ 
 uBit.display.image.setPixelValue(2,2,255);
 uBit.sleep(100);
 uBit.display.image.setPixelValue(2,2,0);
 uBit.sleep(100);
}

The display function makes use of the display buffer implemented by the micro:bit library. There are ways of getting at the raw LEDs without using the display buffer but this is slightly more complicated. 

Conclusion

You now know how to create and run a C/C++ program for the micro:bit. At this point you could start to code anything you want. If you want to work offline however then you will need to do a little more work. Exactly how to work off line and how to use NetBeans as your development environment is described in the next article

microbiticon

More Information

mbed

http://lancaster-university.github.io/microbit-docs/

Related Articles

Offline C/C++ Development With The Micro:bit

Commando Jump Game For The Micro:bit In Python 

The BBC Micro:bit Is An Mbed Device In C/C++ 

BBC Micro To micro:bit 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin

 

Banner


Augmented Reality - A Primer On Coursera
10/07/2018

A short course from Google on the Coursera platform introduces augmented reality (AR) and Google's ARCore. Available for free, it starts on July 16th and lasts four weeks.



Open Source GraphQL Engine Launched
11/07/2018

An open source GraphQL Engine has been launched that can be used with applications based on Postgres without the need for backend GraphQL processing code.


More News

 

justjsquare

 



 

Comments




or email your comment to: comments@i-programmer.info

 



Last Updated ( Sunday, 10 July 2016 )
 
 

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