0% found this document useful (0 votes)
12 views

Mbed Course Notes - Parallel Comms

This document discusses interfacing a parallel LCD display with an mbed microcontroller. It covers connecting the LCD hardware, initializing the display with control signals, and sending display data. Modular code is used to define LCD functions in separate files and call them from the main program.

Uploaded by

upspeed5050
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Mbed Course Notes - Parallel Comms

This document discusses interfacing a parallel LCD display with an mbed microcontroller. It covers connecting the LCD hardware, initializing the display with control signals, and sending display data. Modular code is used to define LCD functions in separate files and call them from the main program.

Uploaded by

upspeed5050
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Embedded Systems Design Course

Applying the mbed microcontroller

Parallel data and communication

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.

• Buses can be parallel, carrying data on multiple wires, or serial, carrying


data sequentially on a single wire.

• 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 is used to create a number of parallel DigitalOut pins


that can be written as one numeric value.

• 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

• On the mbed, the four on-board LEDs can be used to create a


BusOut interface and have been specially configured to operate with
no need for extra wires or connections.

• The mbed also has 26 digital IO


pins (pins 5-30) which can be
configured as a digital bus for
use with the BusOut and BusIn
interfaces.

4
Using parallel digital outputs with BusOut

The BusOut library functions are shown in the table below

A digital output bus, used for setting the state of a


BusOut
collection of pins
BusOut Create a BusOut object, connected to the specified pins
write Write the value to the output bus
read Read the value currently output on the bus
operator= A shorthand for write
operator int() A shorthand for read

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

• For example, we can use int main() {


while(1) {
led1 = 1;
the code shown to led2 = 0;
led3 = 0;
produce a light that moves led4 = 0;
wait(0.25);
horizontally across the 4 led1 = 0;
led2 = 1;
on-board LEDs. led3 = 0;
led4 = 0;
• The code is very simple, wait(0.25);
led1 = 0;
but it could become quite led2 = 0;
led3 = 1;
complex if we require led4 = 0;
wait(0.25);
more outputs, or to led1 = 0;
led2 = 0;
perform more on/off led3 = 0;
led4 = 1;
configurations of the LEDs. }
wait(0.25);

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"

BusOut myleds(LED4, LED3, LED2, LED1);


char x=1;
int main() {
while(1) {
for(int i=0; i<3; i++) { // x = a << b then x = a*2^b;
x = x << 1; // x=1,2,4,8 or x=0001,0010,0100,1000
myleds=x; // sweep left
wait(0.2);
}
for(int i=0; i<3; i++) { // x = a >> b then x = a/2^b;
x = x >> 1; // x=8,4,2,1 or x=1000,0100,0010,0001
myleds=x; // sweep right
wait(0.2);
}
}
}

• 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.

• The following must be achieved in order to interface the LCD:


– Hardware integration: we will need to connect the LCD to the correct mbed pins.
– Modular coding: as there are many processes that need to be completed, it makes sense
to define LCD functions in modular files.
– Initialising the LCD: a specific sequence of control signals must be sent to the LCD in
order to initialise it.
– Outputting data: we will need to understand how the LCD converts control data into
legible display data.

9
Hardware integration
• The PC1602F display
is a 2x16 character
display with an on
board data controller
chip and an
integrated backlight.

• The LCD display has


16 connections as
shown here.

10
Hardware integration
• The Powertip PC1602F datasheet is available from here:
http://www.rapidonline.com/netalogue/specs/57-0911.pdf

• Operation and interfacing the LCD is summarised as follows:


– The display is initialised by sending control instructions to the relevant configuration
registers in the LCD. This is done by setting RS, R/W and E all low, then sending the
correct data through bits DB0-DB7.
– We will use the LCD in 4-bit mode which means that we only need to use the final 4-bits
of the data bus (DB4-DB7). This means we can control the LCD with only 7 digital lines,
rather than 11 lines which are required for 8-bit mode.
– After each data byte has been sent, the Enable bit must be toggled on then off again,
this tells the LCD that data is ready and should be processed.
– Once the LCD has been initialised, display data can be sent by setting the RS bit. Again,
after each byte of display data has been sent, the Enable bit should be toggled to
process the data.

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.

• We can connect the


PC1602F to the mbed
using the following
interface configuration:

• Note: in general, we only


use the LCD in write
mode, so we tie R/W
permanently to ground
(mbed pin 1).

12
Hardware integration
• Note that the PC1602F has a non-conventional pin layout which reads
from left to right:

The hardware setup


is as shown.

13
Using modular coding to interface the LCD

• We will use three files for the LCD application:


– A main code file (main.cpp) which can call functions defined in the LCD
feature file.

– 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

• Our LCD.h file should therefore be as follows:

// LCD.h file

#ifndef LCD_H
#define LCD_H

#include "mbed.h"

void display_to_LCD(char value); //function to display characters on the LCD


void toggle_enable(void); //function to toggle the enable bit
void LCD_init(void); //function to initialise the LCD

#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.

– Note that as we are using 4-bit mode,


// Function Mode
we need to send two pieces of 4-bit data=0x2;
data for each instruction; effectively toggle_enable();
data=0x8;
setting one 8-bit register by sending toggle_enable();
two 4-bit packets of data.

17
Initialising the LCD display

• The LCD display mode control register must


also be set during initialisation.
// Display Mode
data=0x0;
• Here we need to send a command to switch toggle_enable();
data=0xF;
the display on, and to determine the cursor toggle_enable();

function. Value 0x0F will switch the display on


with a blinking cursor.

• Before data can be written to the display, the


display must first be cleared and the cursor // Clear display
data=0x0;
reset to the first character in the first row. This toggle_enable();
data=0x1;
is done by the clear display command with a toggle_enable();

value 0x01.

18
Sending parallel display data to the LCD

• Display data is sent to the LCD screen by


the display_to_LCD function. This function
performs the following tasks:
– Setting the RS flag to 1 (data setting). /**** display ****/
void display_to_LCD(char value ){
– Sending a data byte describing the ascii RS=1;
character to be displayed. //***** display character

– Toggle the enable flag. data=value>>4; // upper 4 bits


toggle_enable();
data=value&0x0F; // lower 4 bits
– The character displayed to the LCD is toggle_enable();
described by an 8-bit hexadecimal ascii }
value. The complete ascii table is
included with the LCD datasheet.

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

• The pin definition is defined in the following manner:


TextLCD lcd(int rs, int e, int d0, int d1, int d2, int d3);

• 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);

• We use printf statements to display characters on the LCD screen.

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.

• We can also clear the screen with the following command:


lcd.cls();

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

• Note that the “%i” command is used to indicate that an integer is to be


output, and the integer name follows.

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++;
}
}

• Don’t forget to import the TextLCD library!

• 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.

– You will need to use a potentiometer to


provide an analog input.

– The analog input variable has a floating point


value between 0 and 1, where 0 is 0V and 1
represents 3.3V.

– We will multiply the analog input value by 100


to make it a percentage between 0-100%.

– An infinite loop is used so that the screen


continuously updates automatically. To do this
it is necessary to clear the screen and add a
delay to set the update frequency.

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"

TextLCD lcd(p19, p20, p21, p22, p23, p24); //rs,e,d0, d1,d2,d3


AnalogIn Ain(p17);
int percentage;

int main() {
while(1){
percentage=Ain*100;
lcd.printf("%i",percentage);
wait(0.002);
lcd.cls();
}
}

• The analog value should change as you move the position of


the potentiometer.

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:

• Note of the following:


– You will need to convert the 0.0 - 1.0 analog input value to a value which
represents 0 - 3.3 Volts.
– You will need to use an infinite loop to allow the voltage value to continuously
update as the potentiometer position changes.
– Check your display with the reading from an actual voltmeter – is it accurate?

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

You might also like