Mbed Course Notes - Parallel Comms
Mbed Course Notes - Parallel Comms
These course notes are written by R.Toulson (Anglia Ruskin University) and T.Wilmshurst
(University of Derby). (c) ARM 2012
These course notes accompany the textbook “Fast and effective embedded system design :
Applying the ARM mbed”
1
Parallel data and communication
• Using parallel digital outputs with the BusOut object
• Working with a parallel LCD display
• Hardware integration
• Using modular coding to interface the LCD display
• Initialising the LCD display
• Sending parallel display data to the LCD
• Adding data to a specified location
• Using the mbed TextLCD library
• Displaying variable data on the LCD
2
Using parallel digital outputs with BusOut
• A digital bus is a system to transfer data between components, or
between controllers.
• Conventional PCI, Parallel ATA and PCMCIA are examples of parallel buses.
• Examples of serial interfaces include Ethernet, FireWire, USB, I2C, and SPI.
• The BusOut interface can be used to set the state of the output pins, and
also read back the current output state.
3
Using parallel digital outputs with BusOut
4
Using parallel digital outputs with BusOut
5
• We can use digital outputs #include "mbed.h"
DigitalOut led1(LED1);
to switch the on-board DigitalOut led2(LED2);
DigitalOut led3(LED3);
LEDs in a specific order. DigitalOut led4(LED4);
6
Using parallel digital outputs with BusOut
• Exercise 1: Using digital outputs, create a program to produce a
“Knightrider” LED sweep effect with the on-board LEDs.
#include "mbed.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
int main() {
while(1) {
led1 = 1; led2 = 0; led3 = 0; led4 = 0;
wait(0.25);
led1 = 0; led2 = 1; led3 = 0; led4 = 0;
wait(0.25);
led1 = 0; led2 = 0; led3 = 1; led4 = 0;
wait(0.25);
led1 = 0; led2 = 0; led3 = 0; led4 = 1;
wait(0.25);
led1 = 0; led2 = 0; led3 = 1; led4 = 0;
wait(0.25);
led1 = 0; led2 = 1; led3 = 0; led4 = 0;
wait(0.25);
}
}
7
Using parallel digital outputs with BusOut
• Exercise 2: Using the BusOut object, create a program to produce a “Knightrider”
sweep effect with the on-board LEDs.
• Verify that this program behaves the same as the previous exercise.
#include "mbed.h"
• Note: Shift operators, << and >>, are used to multiply and divide by two.
8
Working with a parallel LCD display
• We will use the 2x16 character Powertip PC1602F LCD, though a number
of similar LCD displays can be found with the same hardware configuration
and functionality.
9
Hardware integration
• The PC1602F display
is a 2x16 character
display with an on
board data controller
chip and an
integrated backlight.
10
Hardware integration
• The Powertip PC1602F datasheet is available from here:
http://www.rapidonline.com/netalogue/specs/57-0911.pdf
11
Hardware integration
• We obviously need a digital mbed pin to attach to each of the LCD data
pins. We need 4 digital outputs to send the 4-bit instruction and display
data and 3 digital outputs to manipulate the RS, R/W and E control flags.
12
Hardware integration
• Note that the PC1602F has a non-conventional pin layout which reads
from left to right:
13
Using modular coding to interface the LCD
– An LCD feature file (LCD.cpp) which will include all the functions for
initialising and sending data to the LCD.
– An LCD header file (LCD.h) which will be used to declare data and function
prototypes.
14
Using the LCD display
• We will declare the following functions in our LCD header file
toggle_enable - function to toggle the enable bit
LCD_init - function to initialise the LCD
display_to_LCD - function to display characters on the LCD
// LCD.h file
#ifndef LCD_H
#define LCD_H
#include "mbed.h"
#endif
15
Initialising the LCD display
• In LCD.cpp:
– We define the digital IO classes for the // define mbed objects
DigitalOut RS(p19);
mbed. We need one digital output for each DigitalOut E(p20);
BusOut data(p21, p22, p23, p24);
of RS and E and we will use the mbed
BusOut class for the 4-bit data.
– We send a number of 4-bit data packets to
the LCD in order to initialise it and to display
alphanumeric characters on the display.
– After each data packet has been sent, the /**** toggle enable function
void toggle_enable(void){
LCD requires the Enable bit to be toggled E=1;
wait(0.001);
(i.e. sent high and then low with a pause in E=0;
between). This is done by the toggle_enable wait(0.001);
}
function.
16
Initialising the LCD display
• A specific initialisation procedure must be followed in order for the PC1602F
display to operate correctly. Please refer to the PC1602F datasheet for more
specific configuration details.
• We will code the initialisation routine using the LCD_init function:
– In order to initialise we first need to wait a short period (approximately 20ms),
set the RS and E lines to zero and then send a number of configuration messages
to set up the LCD functionality.
– To set the LCD function mode we send a binary value of 0010 1000 (0x28 hex) to
the LCD data pins, we define 4-bit mode, 2 line display and 5x7 dot characters.
17
Initialising the LCD display
value 0x01.
18
Sending parallel display data to the LCD
19
Sending parallel display data to the LCD
• If we wish to display the word “HELLO”, for example, the hexadecimal ascii
values required are as follows: 0x48, 0x45, 0x4C, 0x4C and 0x4F.
• Other ascii values can be found in the table below:
20
Digital IO and LCD functions are therefore defined in LCD.cpp as below
// LCD.cpp file
#include “LCD.h"
//... LCD.cpp continued...
DigitalOut RS(p19);
//display mode
DigitalOut E(p20);
data=0x0;
BusOut data(p21, p22, p23, p24);
toggle_enable();
data=0xF;
void toggle_enable(void){
toggle_enable();
E=1;
wait(0.001);
//clear display
E=0;
data=0x0;
wait(0.001);
toggle_enable();
}
data=0x1;
toggle_enable();
//initialise LCD function
}
void LCD_init(void){
wait(0.02);
//display function
RS=0;
void display_to_LCD(char value ){
E=0;
RS=1;
data=value>>4;
//function mode
toggle_enable();
data=0x2;
data=value&0x0F;
toggle_enable();
toggle_enable();
data=0x8;
}
toggle_enable();
// continued...
21
Using the LCD display
• Exercise 3: Connect the LCD to an mbed and construct a new program with
the files main.cpp, LCD.cpp and LCD.h ,as described in the previous slides.
Add the following code to your main.cpp file and compile and run on the
mbed:
#include “LCD.h"
int main() {
LCD_init();
display_to_LCD(0x48); // ‘H’
display_to_LCD(0x45); // ‘E’
display_to_LCD(0x4C); // ‘L’
display_to_LCD(0x4C); // ‘L’
display_to_LCD(0x4F); // ‘O’
}
Verify that the word ‘HELLO’ is correctly displayed with a flashing cursor.
22
Using the mbed TextLCD library
• The mbed TextLCD library is more advanced than the simple functions we
have created.
– The TextLCD library performs the laborious LCD setup routine for us
– The TextLCD definition also tells the LCD object which pins are used for which functions
• We need to ensure that our pins are defined in the same order. For our
hardware setup this will be:
TextLCD lcd(p19, p20, p21, p22, p23, p24);
23
Using the mbed TextLCD library
• Exercise 4: Compile a “Hello World” example using the mbed library, which
makes use of an alphanumeric LCD much simpler and quicker to program.
#include "mbed.h"
#include "TextLCD.h"
TextLCD lcd(p19, p20, p21, p22, p23, p24); //rs,e,d0,d1,d2,d3
int main() {
lcd.printf("Hello World!");
}
• Import the mbed TextLCD.h library file to your project (right click and
select ‘import library’).
• This library is effectively a file full of specific LCD functions already written
for you. Use the following link for the library file:
http://mbed.org/users/simon/libraries/TextLCD/livod0
24
Using the mbed TextLCD library
• The cursor can be moved to a chosen position to allow you to choose
where to display data, for example
lcd.locate(3,1);
• The display is laid out as 2 rows (0-1) of 16 columns (0-15). The locate
function defines the column first followed by the row.
– The above example moves the cursor to the 4th column and 2nd line
– Any printf statements after the locate command will be printed at the new cursor
location.
25
Using the mbed TextLCD library
• We display data on the screen using the standard printf statement too. If
we want to display the value of an integer to the screen, we need to:
– declare the variable
– give the variable a value
– display the variable to the screen by using the printf statement,
x = 1028
lcd.printf("%i",x);
26
Displaying variables on the LCD
• Exercise 5: display a continuous count variable on the LCD display by
implementing the following code:
#include "mbed.h“
#include "TextLCD.h“
TextLCD lcd(p19, p20, p21, p22, p23, p24); // rs, e, d0, d1, d2, d3
int x=0;
int main() {
lcd.printf("LCD Counter");
while (1) {
lcd.locate(5,1);
lcd.printf("%i",x);
wait(1);
x++;
}
}
• Increase the speed of the counter and investigate how the cursor position
changes as the count value increases.
27
Displaying analog input data on the LCD
• Exercise 6: Display the analog value to the screen.
28
Displaying analog input data on the LCD
• Add the following to your main.cpp . Your code should now
compile and run:
#include "mbed.h"
#include "TextLCD.h"
int main() {
while(1){
percentage=Ain*100;
lcd.printf("%i",percentage);
wait(0.002);
lcd.cls();
}
}
29
Extended task
• Exercise 7: Create a program to make the mbed and display act like a
standard voltmeter. Potential difference should be measured between 0 -
3.3 V and display this to the screen similar to that shown below:
30
Summary
• Using parallel digital outputs with the BusOut object
• Working with a parallel LCD display
• Hardware integration
• Using modular coding to interface the LCD display
• Initialising the LCD display
• Sending parallel display data to the LCD
• Adding data to a specified location
• Using the mbed TextLCD library
• Displaying variable data on the LCD
31