|Getting Started With C/C++ On The Micro:bit|
|Written by Harry Fairhead|
|Thursday, 21 April 2016|
Page 2 of 2
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:
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.
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?
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:
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.
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.
or email your comment to: email@example.com
|Last Updated ( Sunday, 10 July 2016 )|