You are on page 1of 10

Module Syllabus

▪ mbed Digital IO APIs


▪ DigitalIN – DigitalOut – DigitalInOut
▪ BusIn – BusOut – BusInOut
▪ PortIn – PortOut – PortInOut
CSE211s Mbed & CMSIS
Spring 2023 ▪ Digital IO Examples
▪ Using LED
Rapid Development (with GPIO)
▪ Using Infrared Emitter/ Detector
▪ Using 7-Segment Display
▪ Using Hex-Keypad

1 2

Introduction to Mbed Mbed Online Compiler


▪ What is Mbed?

▪ 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

Comparison: High-level vs Low-level MCU Register Example


▪ As shown in previous modules, the GPIO peripheral can be directly accessed by writing/reading
▪ MCU register layer specific memory addresses:
▪ Blinky example by poking registers ▪ Assign a pointer to the address of each register
▪ Registers can be read/ written to using the pointer
▪ Mbed API
▪ Blinky example using Mbed API functions

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

▪ Object oriented API (using C++)

▪ Top-level API used by the majority of the


programs developed on the mbed platform

▪ Defines basic operators to provide intuitive


... using Mbed API
casting to primitive types and assignments code...

▪ A digital IO class is defined as shown in the


code clip. • Note that the Mbed API is programmed using the object-oriented language C++, which originated from C
language with object-oriented features such as classes.

• Deep knowledge of C++ is not necessary to use the Mbed API.

9 10

Mbed API – Digital I/O

The Mbed API provides a number of drivers that provide access to general purpose microcontroller hardware

For digital I/O the key APIs are:


▪ DigitalIn

mbed Digital IO APIs ▪ DigitalOut

▪ 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)

17 © 2019 Arm Limited


18

Digital IO API: DigitalOut FRDM-KL25Z pin names to use in mbed


Use the DigitalOut interface to configure and control a digital output pin by setting the pin to logic level 0 or 1.

Function Description Parameters


Create a DigitalOut connected to
DigitalOut(Pin Name pin) pin: DigitalOut pin to connect to
the specified pin.
Create a DigitalOut connected to
DigitalOut(Pin Name pin, int value) the specified pin, set the initial value value: The initial pin value
of the pin.
value: An integer specifying the pin output
void write(int value) Set the output, specified as 0 or 1 value, 0 for logical 0, non-zero value for
logical 1

int read () Return the output setting, represented as 0 or 1

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

Blinking LED Example Digital IO API: DigitalInOut


Use the DigitalInOut interface as a bidirectional digital pin:
#include "mbed.h" • Read the value of a pin when set as an input().
Blink RED LED at 1Hz
• Write the value to a pin when set as an output().
DigitalOut myled(LED1);
int main() Function Description Parameters
{ Create a DigitalInOut connected to the
DigitalInOut(Pin Name pin) pin: DigitalInOut pin to connect to.
These are custom data types // Check that myled object is initialized and connected to a pin specified pin.
that are included with mbed if(myled.is_connected()) { DigitaiInOut (PinName pin, Create a DigitalInOut connected to the direction: The initial direction of the pin
printf("myled is initialized and connected!\n\r"); PinDirection direction, specified pin, set the initial direction, mode: The initial mode of the pin if input
} PinMode mode, int value) mode, and value of the pin. value: The initial value of the pin if output
void write(int value) Set the output, specified as 0 or 1. Same as in DigitalOut
// Blink LED
while(1) { Return the output setting, represented as 0 or 1 if it is an output or
int read()
myled = 1; // set LED1 pin to high read the input if set as an input
wait(0.5); void output() Set as an output.

myled.write(0); // set LED1 pin to low void input() Set as an input.


wait(0.5); void mode (PinMode pull) Set the input pin mode. Same as in DigitalIn
}
} Int is_connected() Same as DigitalIn and DigitalOut

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"

BusInOut pins(D0, D1, D2);

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.

Usually, the current limiting resistors are


integrated with the device. Common Anode Common Cathode
37 38

7-Segment Display: Digit Encoding 7-Segment Display: Board Connections


Connect digital I/O pins (PTA1, PTA2, …, PTC12,…)
Display
gfedcba abcdefg
a b c d e f g
gfedcba to the 7-segment device pins:
CC CC CA
• 7 pins to 7 segments cathodes (A to G)
0 0x3F 0x7E on on on on on on 0xC0
• 1 pin to DP
1 0x06 0x30 on on 0xF9
• 1 pin to common anode (to be logical “1”)
2 0x5B 0x6D on on on on on 0xA4
3 0x4F 0x79 on on on on on 0xB0 FRDM- 7-seg pin
mbed pin 7-seg pin
4 0x66 0x33 on on on on 0x99 KL25Z pin name
5 0x6D 0x5B on on on on on 0x92 PTC6 19 7 A
6 0x7D 0x5F on on on on on on 0x82
PTC5 20 6 B
7 0x07 0x70 on on on 0xF8
8 0x7F 0x7F on on on on on on on 0x80 PTC16 21 4 C
9 0x6F 0x7B on on on on on on 0x90 PTA16 22 2 D
A 0x77 0x77 on on on on on on 0x88
PTA17 23 1 E
b 0x7C 0x1F on on on on on 0x83
C 0x39 0x4E on on on on 0xC6 PTC10 24 9 F
d 0x5E 0x3D on on on on on 0xA1 PTC11 25 10 G
E 0x79 0x4F on on on on on 0x86
PTC13 26 5 DP
F 0x71 0x47 on on on on 0x8E FRDM-KL25Z Pinouts
PTC17 33 3,8 CA
Encoding for Displaying Digits
39 40 FRDM-KL25Z Possible Connections
7-Segment Display: Example — Modulo-10 Counter
#include “mbed.h”
BusOut Disp1(PTC6,PTC5,PTC16,PTA16,PTA17,PTC10,PTC11,PTC13); //Define many output pins: ABCDEFGdp
DigitalOut vdd(PTC17); //Defune one pin: Common Anode 3.3V
void setvdd(); // to set CA to 3.3V
{ Order is important
vdd=1;
{
int main()
{ setvdd() //Set CA On.
while(1)
{
Digital Input : Hex-Keypad
Disp1=0xC0; wait(1); //Display 0, wait for 1 sec. 0xC0=11000000, so only G dp are OFF.
Disp1=0xF9; wait(1); //Display 1, wait for 1 sec.
Disp1=0xA4; wait(1); //Display 2, wait for 1 sec.
Disp1=0xB0; wait(1); //Display 3, wait for 1 sec.
Disp1=0x99; wait(1); //Display 4, wait for 1 sec.
Disp1=0x92; wait(1); //Display 5, wait for 1 sec.
Disp1=0x82; wait(1); //Display 6, wait for 1 sec.
Disp1=0xF8; wait(1); //Display 7, wait for 1 sec.
Disp1=0x80; wait(1); //Display 8, wait for 1 sec. 0x80=10000000, so only dp is OFF.
Disp1=0x90; wait(1); //Display 9, wait for 1 sec. 0x90=10010000, so only E dp are FF.
}
}
41 42

Digital Input Example: Hex Keypad Basics Hex Keypad: Basics


Operation:
Layout:
All switches located at the row-column intersections

To MCU digital input


➢ 16 switches arranged in a 4x4 matrix, four rows
are normally open.
R1-R4 and four columns C1-C4.
All rows are pulled “High” via equal resistors so
➢ A switch is placed at the intersection of a row
R1R2R3R4 normally reads “1111” in case no switch is
and a column, such that when pressed it
pressed.
connects its row to its column.
If all columns are connected to “High” except for one,
➢ 8 Terminals R1-R4 and C1-C4
connected to “Low”, this low voltage probes all four
switches connected to that column such that if a single
switch is pressed, it connects the low voltage to a single
Layout & row that is connected to the pressed switch.
Terminals
The pressed switch is then identified by the single
column and the single row with “Low” voltage. Schematic
0 1 1 1
Columns are scanned sequentially with a “Low” 1 0 1 1
probing input, and rows are examined to see if there is a 1 1 0 1
low signal on any row indicating a pressed switch at that 1 1 1 0
row-column intersection.
From MCU digital output
43 44
Hex Keypad: Board Connections Hex Keypad: Program //Pul a single column Lo
#include “mbed.h” void setCol1Lo() {
//Create a BusOut connected to the 7 segments col1=0;
BusOut Disp1(PTC6,PTC5,PTC16,PTA16,PTA17,PTC10,PTC11,PTC13); col2=1;
//Pull all columns Hi
col3=1;
void setColsHi() {
//Create a DigitalOut connected to 7-seg anode col4=1;
col1=1;
DigitalOut vdd(PTC17); }
col2=1;
void setCol2Lo() {
FRDM- Keypad col3=1;
mbed pin //Create DigitalIns to probe rows col1=1;
KL25Z pin Terminal col4=1;
DigitalIn row1(PTE5) // row1 Input col2=0;
}
PTB8 1 C4 DigitalIn row2(PTE4) // row2 Input col3=1;
DigitalIn row3(PTE3) // row3 Input col4=1;
PTB9 3 C3 DigitalIn row4(PTE2) // row4 Input }
PTB10 5 C2 void setCol3Lo() {
//Create DigitalOuts to scan columns col1=1;
PTB11 7 C1 DigitalOut col1(PTB11)// col1 output col2=1;
PTE2 9 R4 DigitalOut col2(PTB10)// col2 output col3=0;
DigitalOut col3(PTB9)// col3 output col4=1;
PTE3 11 R3 DigitalOut col4(PTB8)// col4 output }
PTE4 13 R2 void setCol4Lo() {
void setvdd(); col1=1;
PTE5 15 R1 { col2=1;
FRDM-KL25Z Pinouts vdd=1; col3=1;
FRDM-KL25Z Possible Connections
} col4=0;
45 46 }

Hex Keypad: Program // Key scan function wait(0.2)


Hex Keypad: Program
//Display the digit on 7-seg void keyscan() { setCol3Lo();
void key_1() { setColsHi(); wait(0.2)
Disp1=0xF9; wait(0.2); if (row1==0) // main program routine
} setCol1Lo(); key_3(); int main() {
void key_2() { wait(0.2); if (row2==0)
Disp1=0xA4; if (row1==0) key_6(); setvdd();
} key_1(); if (row3==0) while (1) {
void key_3() { void key_7() { if (row2==0) key_9();
Disp1=0x80; key_4();
keyscan();
Disp1=0xF8; if (row4==0)
} } if (row3==0) key_E(); wait(0.2);
void key_4() { void key_8() { key_7(); wait(0.2) }
Disp1=0x99; Disp1=0x80; if (row4==0) setCol4Lo();
} } key_F(); wait(0.2) }
void key_5() { void key_9() { wait(0.2) if (row1==0)
Disp1=0x92; Disp1=0x90; setCol2Lo(); key_A();
} } wait(0.2) if (row2==0)
void key_6() { if (row1==0) key_B();
Disp1=0x82; //Display nothing on 7-seg key_2(); if (row3==0)
} void key_nope() { if (row2==0) key_C();
Disp1=0x3F; key_5(); if (row4==0)
} if (row3==0) key_D();
key_8(); wait(0.2)
if (row4==0) }
47 key_0(); 48

You might also like