The mbed Compiler is a C/C++ compiler, and the mbed Libraries and code you develop are written in C/C++.
There are lots of the resources on the web explaining C/C++ programming. However, be careful, as we are programming for a microcontroller rather than a PC! Even though we are programming in C++, it is recommended you use C programming style, references and tutorials in most cases. We are effectively programming in C, using some of the really useful features of C++ (like classes to enable object-oriented abstractions of hardware) and ignoring many others (like the huge Standard Template Library); think of it as "C with Objects".
Programming is a tricky business, especially if you are new to it, but the following instructions will help you get a feel for how it works before you dive in to the details. This will demonstrate some basic changes to Program Source Code to produce different Program Binaries.
1. Create a new program
Create a new program (e.g. called "test2") which will form the basis for our experiments. We'll modify the default program in different ways to introduce some very basic programming concepts.
2. Modify the program to make LED3 flash every second
#include "mbed.h"
DigitalOut myled(LED3);
int main() {
while(1) {
myled = 1;
wait(0.1);
myled = 0;
wait(0.9);
}
}
Edit the source code in the mbed Compiler to be the same as this example. Then compile and download the program binary to the mbed Microcontroller to check it works!
2. Make LED3 flash 3 times, then stop
#include "mbed.h"
DigitalOut myled(LED3);
int main() {
for(int i=0; i<3; i++) {
myled = 1;
wait(0.1);
myled = 0;
wait(0.9);
}
}
3. Control LED3 dependent on an analog input
This example will turn on LED3 if the voltage on pin 20 gets above 1.65v. You can usually cause this just by touching your finger on pin 20, making it flicker on and off.
#include "mbed.h"
AnalogIn myinput(p20);
DigitalOut myled(LED3);
int main() {
while(1) {
if(myinput > 0.5) {
myled = 1;
} else {
myled = 0;
}
}
}
The best way to learn is through experiemtation and trying examples, so look around the Handbook, Cookbook and Forum.
What Next
A first introduction to C programming (until we have our own...)
A C Library reference