Professional Documents
Culture Documents
1 2
▪ A platform used for the easy prototyping and development of applications and systems based on Arm
Cortex-M-based microcontrollers, typically for use in the world of the Internet of Things (IoT)
▪ A lightweight online C/C++ IDE that enables
quick writing of programs, compilation, and
▪ The Mbed platform provides: running on a microcontroller
▪ Open software libraries
▪ Open hardware designs ▪ No prior-installation or set-up required to
▪ Open online tools for professional rapid prototyping of products based on Arm-based microcontrollers work with Mbed
▪ The Mbed platform includes: ▪ Includes full code editor, version control, and
library management
▪ Mbed Operating System (Mbed OS)
▪ Libraries, RTOS core, HAL, API, and more
▪ A microcontroller Hardware Development Kit (HDK) and supported development boards
▪ Integrated Development Environment (IDE), including an online compiler and online developer collaboration
tools
3 4
Mbed Enabled Platforms High-level vs. Low-level Programming
▪ The Arm® Mbed Enabled™ program outlines a set of functionality and requirements that must be met in order to
become “Mbed Enabled”. This can cover development boards, modules, components, and interfaces High-level Low-level
▪ This benefits developers as they are assured that the platforms they choose to work with, can perform certain • Advantages: • Advantages:
functions/provide certain performance • Higher productivity (less development time) • More optimized code and memory efficient
• Portability across devices • Less translation time for source to machine code
▪ It is also beneficial to the vendors as it allows their products more exposure when certified, and enables their • Resulting code that is easier code to read and • Directly talk to hardware
product to become more familiar with developers in the Mbed eco-system maintain
• Allows reuse of code
• Rapid prototyping of applications
▪ Some example boards include:
• Disadvantages: • Disadvantages:
• Less optimized code • Less portability from one device to another
• Additional translation time for source to machine • Resulting code is more difficult for others to read,
code reuse, and maintain
• Another level of abstraction to deal with • Low productivity
NUCLEO-F401RE DISCO-F413ZH DISCO-F413ZH Nordic nRF51-DK
5 6
High-level
Control GPIO using Mbed API
GPIO Registers
Low-level
7 8
Directly access MCU registers
Mbed API Example Mbed API Example
▪ The Mbed API provides the actual user-friendly ▪ With the support of the Mbed API, the same blinky example can be programmed in a much
object-oriented API to the final user simpler and more intuitive way:
▪ More friendly functions/APIs
9 10
The Mbed API provides a number of drivers that provide access to general purpose microcontroller hardware
▪ BusIn
▪ BusOut
We will take a look at some examples and see how they compare to the low-level equivalent
15 16
DigitalIn/DigitalOut Digital IO API: DigitalIn
Use the DigitalIn interface to read the value of a digital input pin, either 1 or 0.
▪ The DigitalIn interface is used to read the value of a digital input pin
▪ The logic level is either 1 or 0 Function Description Parameters
Create a DigitalIn connected to the
▪ The DigitalOut interface is used to configure and control a digital output pin by setting the pin to 0 or 1. DigitalIn(Pin Name pin) pin: DigitalIn pin to connect to
specified pin.
mode: The initial mode of the pin
▪ Any number of Arm Mbed pins can be used as a DigitalIn or DigitalOut. Create a DigitalIn connected to the
DigitalIn(Pin Name pin, mode) (PullUp, PullDown, PullNone,
specified pin, set the initial mode of the pin.
OpenDrain)
▪ There is also the DigitalInOut interface, which is a bidirectional digital pin, we can use this interface to read
int read() Read the input pin, represented as 0 or 1
the value of a digital pin when set as an input(), as well as write the value when set as an output().
pull: (PullUp, PullDown, PullNone,
void mode (PinMode pull) Set the input pin mode.
OpenDrain)
Returns nonzero value if pin is connected to MCU GPIO, 0 if GPIO object was
int is_connected()
initialized with NC (no-connection)
Returns nonzero value if pin is connected to MCU GPIO, 0 if GPIO object was Arduino pin
int is_connected() names
initialized with NC (no-connection)
FRDM-KL-25Z
Pin functions pin names
20 22
FRDM-KL25Z pin names to use in mbed DigitalIn & DigitalOut Example
LED (RGB) Arduino Headers USB Pins I2C pins TSI electrodes READ two switches, #include "mbed.h”
LED_RED = PTB18 D0 = PTA1 A0 = PTB0 USBTX = PTA2 I2C_SCL = D15 TSI_ELEC0 = PTB16
variables “a” and “b” , and DigitalIn a(D0);
LED_GREEN = PTB19 D1 = PTA2 A1 = PTB1 USBRX = PTA1 I2C_SDA = D14 TSI_ELEC1 = PTB17
DigitalIn b(D1);
LED_BLUE = PTD1 D2 = PTD4 A2 = PTB2 SET four LEDs,
D3 = PTA12 A3 = PTB3 DigitalOut z_not(LED1);
variables z_not z_and z_or z_xor
mbed original LED naming D4 = PTA4 A4 = PTC2 DigitalOut z_and(LED2);
D5 = PTA5
representing four basic logical operations on
LED1 = LED_RED A5 = PTC1 DigitalOut z_or(LED3);
LED2 = LED_GREEN D6 = PTC8 “a” and “b” .
DigitalOut z_xor(LED4);
LED3 = LED_BLUE D7 = PTC9
LOOP FOREVER int main() {
LED4 = LED_BLUE D8 = PTA13
D9 = PTD5 while(1) {
D10 = PTD0 z_not = !a;
D11 = PTD2
z_and = a && b;
D12 = PTD3
D13 = PTD1
z_or = a || b;
D14 = PTE0 z_xor = a ^ b;
D15 = PTE1 }
Link to the board KL25Z pin names to use in mbed: }
23 https://os.mbed.com/teams/Freescale/wiki/frdm-kl25z-pinnames 24
25 26
DigitalInOut Example #include "mbed.h"
Digital IO API: BusIn
➢ Combines a number of DigitalIn pins to read them all at once.
DigitalInOut mypin(LED1); /create DigitalInOut connected to RED LED ➢ Useful for checking multiple inputs together as single interface instead of individual pins.
Blink RED LED at 1Hz, Can have up to 16 pins in a Bus.
int main() ➢
read its status every 0.5 sec., { ➢ The order of pins in the constructor is the reverse order of the pins in the byte order.
write its status, to the standard output. // check that mypin object is initialized and connected to a pin Function Description Parameters
if(mypin.is_connected()) {
printf("mypin is initialized and connected!\n\r"); BusIn (PinName p0, PinName It is only required to specify as many
Create a BusIn, connected to the specified
p1=NC,…, PinName pin variables as is required for the bus;
} pins.
p15=NC) the rest will default to NC
// Optional: set mode as PullUp/PullDown/PullNone/OpenDrain
mypin.mode(PullNone); Create an BusIn, connected to the specified pins: An array of pins to connect to
BusIn (PinName pins[16])
while(1) { pins. bus bit
mypin.output(); // write to pin as output
led = button; // Equivalent to
mypin = !mypin; // toggle output int read() Read the value of the input bus.
led.write(button.read())
wait(0.5);
mypin.input(); // read from pin as input void mode (PinMode pull) Same as DigitalIn
printf("mypin.read() = %d \n\r",mypin.read());
wait(0.5); Return binary mask of connected pins. If bus pin is in NC state, mask corresponding
int mask()
} bit will be 0, else mask bit will be 1.
}
27 29
#include "mbed.h"
BusIn Example BusIn nibble(D0, D1, D2, D3); // Change these pins to buttons on your board.
int main() {
Digital IO API: BusOut
Use the BusOut interface to combine a number of DigitalOut pins to write them all at once
nibble.mode(PullNone); // Optional: set mode as PullUp/PullDown/PullNone/OpenDrain Useful for writing to multiple pins together as single interface instead of individual pins.
while(1) { Can have up to 16 pins in a Bus.
// check bits set in nibble The order of pins in the constructor is the reverse order of the pins in the byte order.
switch(nibble & nibble.mask()) { // read the bus and mask out bits not being used
Function Description Parameters
case 0x0: printf("0b0000, D3,D2,D1,D0 are low \n\r");break;
case 0x1: printf("0b0001, D0 is high \n\r");break; BusOut (PinName p0, Create a BusOut, connected to the It is only required to specify as many pin
case 0x2: printf("0b0010, D1 is high \n\r");break; PinName p1=NC,…, specified pins. variables as is required for the bus; the rest
case 0x3: printf("0b0011, D1,D0 are high \n\r");break; PinName p15=NC) will default to NC
case 0x4: printf("0b0100, D2 is high \n\r");break;
Create a BusOut, connected to the pins: An array of pins to connect to bus bit
case 0x5: printf("0b0101, D2 ,D0 are high \n\r");break; BusOut (PinName pins[16])
specified pins.
case 0x6: printf("0b0110, D2,D1 are high \n\r");break;
case 0x7: printf("0b0111, D2,D1,D0 are high \n\r");break; Write the value to the output bus. value: An integer specifying a bit to write
void write(int value)
case 0x8: printf("0b1000, D3 is high \n\r");break; for every corresponding DigitalOut pin
// ... Read the value currently output on the bus.
case 0xF: printf("0b1111, D3,D2,D1,D0 are high \n\r");break; int read() Returns an integer with each bit corresponding to associated DigitalOut pin setting.
}
wait(1); Return binary mask of connected pins.
} int mask() If bus pin is in NC state, mask corresponding bit will be 0, else mask bit will be 1.
}
30 31
BusOut Example Digital IO API: BusInOut
Use the BusInOut interface as a 16-DigitalInOut-pin bidirectional bus that you can read and write as one value.
// 4-bit binary counter The order of pins in the constructor is the reverse order of the pins in the byte order.
#include "mbed.h"
Function Description Parameters
BusOut myleds(LED1, LED2, LED3, LED4); BusInOut (PinName p0,
Create a BusInOut, connected to the It is only required to specify as many pin variables as
PinName p1=NC,…, PinName
specified pins. is required for the bus; the rest will default to NC
p15=NC)
int main() {
while(1) { value: An integer specifying a bit to write for every
void write(int value) Write the value to the output bus.
corresponding DigitalInOut pin
for(int i=0; i<16; i++) {
myleds = i; int read() Returns an integer with each bit corresponding to associated DigitalInOut pin.
wait(0.25);
void output() Set all the pins in bus as output.
}
} void input() Set all the pins in bus as input.
}
void mode (PinMode pull) Set the input pin mode for ALL the pins in bus.
Return binary mask of connected pins.
int mask()
If bus pin is in NC state, mask corresponding bit will be 0, else mask bit will be 1.
32 33
BusInOut Example
#include "mbed.h"
int main() {
while(1) {
pins.output();
pins = 0x3;
wait(1);
Extra Readings
pins.input();
wait(1);
if(pins == 0x6) {
printf("Hello!\n");
}
}
}
34 36
Digital Output Example: 7-Segment Display Basics
There are two types of LED 7-segment
display:
1. Common Cathode (CC)
All the anode connections of the LED
segments are joined together to “High”.
Individual segments are illuminated by
applying a “Low” signal via a suitable
Digital IO Examples current limiting resistor to the cathode
of the segment. Segments are labeled (A B C D E F G, DP ).
2. Common Anode (CA).
All the cathode connections of the LED
segments are joined together to “Low”.
Individual segments are illuminated by
applying a “High” signal via a suitable
current limiting resistor to the cathode
of the segment.