Anda di halaman 1dari 6

Fast and effective embedded systems design:

Applying the ARM mbed, Part 2


Rob Toulson and Tim Wilmshurst - August 27, 2014

Editor's Note: Low-cost, easy-to-use development systems lower the


threshold to entry into embedded systems development for engineers,
regardless of their experience level. Among these compact development
systems, ARM's mbed finds support for a diverse set of ARM-based MCUs
from the leading semiconductor suppliers. Fast and Effective Embedded
Systems Design: Applying the ARM mbed by Rob Toulson and Tim
Wilmshurst, offers a clear, hands-on approach to embedded systems design
principles with step-by-step directions on how to use the mbed development
system to explore each key element of an embedded design. In Part 1 of this
excerpt from the book, the
introduced the mbed and its key components. In this Part 2, the authors offer a tutorial showing how
to get started with the mbed.
Elsevier is offering this and other engineering books at a 25% discount. To
use this discount, click on the coupon to the right and use code PBTY14 (valid
through March 31, 2015) during checkout.
Adapted from "Fast and Effective Embedded Systems Design, 1st Edition,
Applying the ARM mbed" by Rob Toulson and Tim Wilmshurst (Newnes)

2.2 Getting Started with the mbed: A Tutorial


Now comes the big moment when you connect the mbed for the first time, and run a first program.
We will follow the procedure given on the mbed website and use the introductory program on the
compiler, a simple flashing LED example. You will need:

an mbed microcontroller with its USB lead


a computer running Windows (XP, Vista or 7), Mac OS X or GNU/Linux
a web browser, for example Internet Explorer or Firefox.

Now follow the sequence of instructions below. The purpose of this tutorial is to explain the main
steps in getting a program running on the mbed. We will look into the detail of the program in the
next chapter.
Step 1. Connecting the mbed to the PC

Connect the mbed to the PC using the USB lead. The Status light will come on, indicating that the
mbed has power. After a few seconds of activity, the PC will recognize the mbed as a standard
removable drive, and it will appear on the devices linked to the computer, as seen in

Figure 2.4. Locating the mbed: (a) Windows XP example; (b) Mac OS X example
Step 2. Creating an mbed Account
Open the MBED.HTM file found on the mbed in your web browser and click the Create a new mbed
Account link. Follow the instructions to create an mbed Account. This will lead you to the website, as
seen in Figure 2.5. From here you can link to the compiler, libraries and documentation.

Figure 2.5. The mbed home page


Running a Program
Step 3. Running a Program
Open the compiler using the link in the site menu, i.e. at the right of Figure 2.5. By doing this you
enter your allocated personal program workspace. The compiler will open in a new tab or window.
Follow these steps to create a new program:

As seen in Figure 2.6a, right-click (Mac users, Ctrl-click) on My Programs and select New
Program ..
Choose and enter a name for the new program (for example Prog_Ex_2_1) and click OK. Do not
leave spaces in the program name.
Your new program folder will be created under My Programs.

Click on the main.cpp file in your new program to open it in the file editor window, as seen in
Figure 2.6b. This is the main source code file in your program. Whenever you create a new program
it always contains the same simple code. This is shown here as Program Example 2.1. This program
will be examined in the next chapter.

Figure 2.6. Opening a new program: (a) selecting New Program; (b) opening the main
source file
The other item in the program folder is the mbed library. This provides all the functions used to
start up and control the mbed, such as the DigitalOut interface used in this example.

/* Program Example 2.1: Simple LED flashing


*/
#include "mbed.h" DigitalOut myled(LED1); int main() {
while(1) {
myled = 1;
wait(0.2);
myled = 0;
wait(0.2);
}
}
Program Example 2.1 Simple LED flashing

Step 4. Compiling the Program


To compile the program, click the Compile button in the toolbar. This will compile all the source
code files within the program folder to create the binary machine code which will be downloaded to
the mbed. Typically, this is the single program you have written, plus the library calls you have
almost certainly made. After a successful compile, you will get a Success! message in the compiler
output, and a popup will prompt you to download the compiled .bin file to the mbed.
Of course, with this given program, it would be most surprising to find it had an error in it; you will
not be so lucky with future programs! Try inserting a small error into your source code, for example
by removing the semi-colon at the end of a line, and compiling again. Notice how the compiler gives
a useful error message at the bottom of the screen. Correct the error, compile again and proceed.
The type of error you have just inserted is often called a syntax error. This is an error which relates
to the rules of writing lines of C code. When a syntax error is found, the compiler is unable to

proceed with the compilation, as it perceives that the program has stepped outside the rules of the
language, and hence cannot reliably interpret the code that is written.
Downloading the Program Binary Code
Step 5. Downloading the Program Binary Code
After a successful compile, the program code, in binary form, can be downloaded to the mbed. Save
it to the location of the mbed drive. You should see the Status LED (as seen in Figure 2.1) flash as
the program downloads. Once the Status LED has stopped flashing, press the reset button on the
mbed to start your program running. You should now see LED1 flashing on and off every 0.2
seconds.

Step 6. Modifying the Program Code


In the main.cpp file, simply change the DigitalOut statement to read:
DigitalOut myled(LED4);
Now compile and download your code to the mbed. You should now see that LED4 flashes instead of
LED1. You can also change the pause between flashes by modifying the values bracketed in the
wait() command.

2.3 The Development Environment


As Section 1.3 suggests, there are many different approaches to development in embedded systems.
With the mbed there is no software to install, and no extra development hardware needed for
program download. All software tools are placed online, so that you can compile and download
wherever you have access to the Internet. Notably, there is a C compiler and an extensive set of
software libraries, used to drive the peripherals. Thus, there is no need to write code to configure
peripherals, which in some systems can be very time consuming.
2.3.1 The mbed Compiler and API
The mbed development environment uses the ARM RVDS (RealView Development Suite) compiler,
currently Version 4.1. All features of this compiler relevant to the mbed are available through the
mbed portal.
One thing that makes the mbed special is that it comes with an application programming interface
(API). In brief, this is the set of programming building blocks, appearing as C utilities, which allow
programs to be devised quickly and reliably. Therefore, we will be writing code in C or C, but
drawing on the features of the API. You will meet most of the features of this as you work through
the book. Note that you can see all components of the API by opening the mbed handbook, linked
from the mbed home page.
2.3.2 Using C/C++
As just mentioned, the mbed development environment uses a C compiler. That means that all
files will carry the .cpp (C plus plus) extension. C, however, is a subset of C, and is simpler to
learn and apply. This is because it does not use the more advanced object-oriented aspects of C.

In general, C code will compile on a C compiler, but not the other way round.
Using C
C is usually the language of choice for any embedded program of low or medium complexity, so will
suit us well in this book. For simplicity, therefore, we aim to use only C in the programs we develop.
It should be recognized, however, that the mbed API is written in C and uses the features of that
language to the full. We will aim to outline any essential features when we come to them.
This book does not assume that you have any knowledge of C or C, although you
have an advantage if you do. We aim to introduce all new features of C as they come
up, flagging this by using the symbol alongside. If you see that symbol and you are a C
expert, then it means you can probably skim through that section. If you are not an
expert, you will need to read the section with care and refer to Appendix B, which summarizes all
the C features used. Even if you are a C expert, you may not have used it in an embedded context. As
you work through the book you will see a number of tricks and techniques that are used to optimize
the language for this particular environment.
Chapter Review

The mbed is a compact, microcontroller-based hardware platform, accompanied by a program


development environment.
Communication to the mbed from a host computer is by USB cable; power can also be supplied
through this link.
The mbed has 40 pins, which can be used to connect to an external circuit. On the board it has four
user-programmable LEDs, so very simple programs can be run with no external connection to its
pins.
The mbed uses the LPC1768 microcontroller, which contains an ARM Cortex-M3 core. Most mbed
connections link directly to the microcontroller pins, and many of the mbed characteristics derive
directly from the microcontroller.
The mbed development environment is hosted on the web. Program development is undertaken
while online, and programs are stored on the mbed server.

Quiz
1.
2.
3.
4.
5.
6.
7.

What do ADC, DAC and SRAM stand for?


What do UART, CAN, I2C and SPI stand for, and what do these mbed features have in common?
How many digital inputs are available on the mbed?
Which mbed pins can be used for analog input and output?
How many microcontrollers are on the mbed PCB and what specifically are they?
What is unique about the mbed compiler software?
An mbed is part of a circuit which is to be powered from a 9 V battery. After programming the
mbed is disconnected from the USB. One part of the circuit external to the mbed needs to be
supplied from 9 V, and another part from 3.3 V. No other battery or power supply is to be used.
Draw a diagram which shows how these power connections should be made.
8. An mbed is connected to a system, and needs to connect with three analog inputs, one SPI
connection, one analog output and two PWM outputs. Draw a sketch showing how these
connections can be made, and indicate mbed pin number.
9. A friend enters the code shown below into the mbed compiler, but when compiling a number of
errors are flagged. Find and correct the faults.

10. By not connecting all the LPC1768 microcontroller pins to the mbed external pins, a number of
microcontroller peripherals are lost for use. Identify which ones these are, for ADC, UART,
CAN, I2C, SPI and DAC.

References
2.1. The mbed home site. http://mbed.org/
2.2. MBED Circuit Diagrams. 26/08/2010. http://mbed.org/media/uploads/chris/mbed-005.1.pdf
2.3. NXP B.V. LPC1768/66/65/64 32-bit ARM Cortex-M3 microcontroller. Objective data sheet. Rev.
6.0. August 2010. http://www.nxp.com/
2.4. NXP B.V. LPC17xx User Manual. Rev. 02. August 2010. http://www.nxp.com/

Be sure to continue to next chapter in this except: ARM mbed analog-to-digital conversion.
Copyright 2014 Elsevier, Inc. All rights reserved.
Printed with permission from Newnes, a division of Elsevier. Copyright 2014. For more
information on this title and other similar books, please visit www.newnespress.com.

Anda mungkin juga menyukai