ELEC/XJEL 3662 – Embedded Systems
Embedded Systems
ELEC/XJEL 3662 – Embedded Systems
1 Overview
For the mini-project, you are required to interface the TM4C123GH6PM microcontroller with an external
16 x 2 Liquid Crystal Display (LCD) and 4x4 keypad to design a simple calculator. The goal of the
project is to use the LCD and the keypad to perform some simple calculations. The Keypad will be used
as input and the LCD will output the result of the input calculations. This will entail building the schematic
in TinaCloud for interfacing these components. You will then write a set of C functions, using the Code
Composer Studio (CCS) IDE, to read input from the keypad and send commands and data to the LCD.
This document will give only an outline of what is required to successfully complete the mini-project. At
this stage of your studies you should be proficient at using datasheets and application notes etc. to
solve an engineering problem. The relevant datasheets are on Minerva in the same area as this
handout. Until you read them, some of the following material will not make sense.
This is an individual mini-project: you will work by yourself, not with a lab partner.
1.1 Notes for ELEC3662 students
This mini-project has changed significantly since last year. If you talk to previous students about it, be
aware that their information may be out of date.
This module is being taught identically to students at Leeds and at South-West Jiaotong University in
Chengdu, south-west China. The SWJTU modules have XJEL module codes.
2 Hardware
2.1 Task 1 – Setting up the Microcontroller
The goal of the mini project is to interface the TM4C123GH6PM microcontroller with an external LCD
and a Keypad. You will do this in TinaCloud.
Consult the TM4C123GH6PM data sheet (section 10.5) to check the GPIOs voltage range and
tolerance for correct LCD and Keypad interfacing.
2.2 Task 2 – Connecting the Keypad
Consult the datasheet for the keypad to get the pin assignments. The keypad is a 4x4 matrix (4 rows
and 4 columns). Your specification includes the following:
• Keypad rows: input from PORTE [0:3].
• Keypad columns: output to PORTD [0:3].
Note that the row inputs will need pull-down (or pull-up depending on your design) resistors. You will
need to use external physical resistors because the microcontroller internal pull-downs resistors are not
supported in TinaCloud.
2.3 Task 3 – Connecting the LCD
Consult the datasheet for the LCD to get the pin assignments. Connect the relevant power pins to 5V
and GND. The following is not supported in TinaCloud but is part of the configuration process when
using an external LCD:
ELEC3662&XJEL3662 – Dr L. Mhamdi, Dr C. Trayner, & Dr R. Solis – Mini-project handout – 2020/21 – V2.0
(2020nov12)
Page 4 of 11
ELEC/XJEL 3662 – Embedded Systems
Mini-Project
• Use a 10 kΩ potentiometer with the middle wire connected to the contrast pin (connect the other
potentiometer pins to GND and 5V).
• Use a small valued resistor in series with the LED backlight cathode to limit the current.
You will be using the LCD in 4-bit mode. This means you will only use 4 pins of the microcontroller to
send a byte of information. Therefore, you will need to send two nibbles (4 bit fields), one after the other.
The LCD interface can run on 3.3V and therefore there is no need for voltage conversion to shift signals
between the microcontroller and the LCD. The specification you must follow includes the following port
and pin assignments:
• PORTB to the LCD DB pins
• PA2 to EN
• PA3 to RS
The LCD’s R/W pin can be connected to GND, which fixes it at Write, as you will not be reading data
from the LCD.
3 Software
When you write software professionally, you will be given a statement of the requirements your software
must fulfil. It is often called a functional specification – it specifies what functions your software must
perform, but not how it performs them. There could be a little or a lot of detail, anything from a simple
statement of what the software must do down to specifying variable names. Your software might
become part of a software library and would have to conform to some standard.
For this project you will specify the modules your software must be divided into, the names of most of
the #define constants, and details of most of the functions.
3.1 The keypad
You will build a standard 16-key keypad as shown on the right. The
keys are labelled with the ten digits and A, B, C, D, * and #.
Obviously, the ten digits are used for themselves. The others will be
used for things like plus and equals.
In fact, this immediately presents us with a problem. The minimum
requirement for the keys is the ten digits and plus, minus, multiply,
divide, decimal point, clear (=rubout) and equals (=make the
calculation). This lists 17 keys!
There are several solutions to this problem. The most common one
is to use one of the keys as a shift key, as on many commercial
calculators. This loses one key (there are now only five plus the
digits) but doubles up the use of these five. It gives you what you
need with a few spares.
Table I describes an example on how to configure each key, but you are free to propose your own.
ELEC3662&XJEL3662 – Dr L. Mhamdi, Dr C. Trayner, & Dr R. Solis – Mini-project handout – 2020/21 – V2.0
(2020nov12)
Page 5 of 11
ELEC/XJEL 3662 – Embedded Systems
Mini-Project
Table I Recommended functionality of each key
Marking on
keypad
When not shifted When shifted Notes
Use Display
character [1]
Use Display
character [1]
A Plus + Times x [2,3]
B Minus - Divide / [2]
C Decimal point . Times ten to
the power
E [4,5]
D Shift [None] Cancel shift [None] [6,7]
* End Input [None] End Input [None]
# Rubout last
character
[None] Delete entire
entry
[None] [4]
Notes:
[1] I.e. on the LCD display.
[2] Implementing this shifted function is essential.
[3] Times must be displayed on the LCD as lower-case x, not upper-case X or the asterisk (*).
[4] Implementing this shifted function is optional but will gain extra marks.
[5] For instance 1.2E3 means 1.2x103.
[6] This works like most calculators: you press shift, then release it, then press (e.g.) A for times. It is
not like computer keyboards where you press both at once.
[7] Pressing shift a second time cancels it.
3.2 How to start the software part of this project.
3.2.1 Create Project
The first stage is to create a new CCS project for the Texas Instruments TM4C123GH6PM
microcontroller as target device. As part of this you will create the standard main.c file.
3.2.2 Starting to write your program
In practice, you will probably find that your programming work is of two types, with different styles of
thinking. You can do them in either order, and you may prefer to alternate between them.
• Writing the program code
You are now into serious program writing and it would be wise to follow the divide-and-rule
technique: it is easier to write small functions, even if there are more of them, rather than a few
enormous ones.
• Writing all the #define statements that specify port addresses and the initial contents of
registers. Many of these can be copied from previous work, though some will need changing.
3.2.3 A comment on clocks
There will be many time delays, so you will need the PLL and SysTick to generate them accurately. To
ease calculations a setting of 80 MHz is suggested, though you may want to make your own decision.
ELEC3662&XJEL3662 – Dr L. Mhamdi, Dr C. Trayner, & Dr R. Solis – Mini-project handout – 2020/21 – V2.0
(2020nov12)
Page 6 of 11
ELEC/XJEL 3662 – Embedded Systems
Mini-Project
Clocks control many things (especially LCD timings), so it is probably worth getting them working early.
3.2.4 A comment on the LCD
This is complicated to interface to. There is a lengthy description in Appendix C of this handout.
3.3 Project management tactics
Here are some hints to help you succeed.
3.3.1 What should I do first?
Do the essential things first. Leave the nice refinements for later.
3.3.2 I changed it and it broke!
Once you have a program that works, add things a bit at a time. At each step make sure it still works: if
it doesn’t, you can undo the last change. That way you always have something working.
3.3.3 My program is mis-behaving – how do I see what it’s doing?
A good first step is to use the MCU Debugger in TinaCloud to place breakpoints and examine variables.
If that doesn’t help, you can print debugging messages to the LCD. Thus, it might be wise to get the
LCD software working first.
3.3.4 The KISS motto
“Keep It Simple, Stupid!” It’s tempting to invent very complicated algorithms and code. Good program
code is simple – simple enough to be understood by anyone, or by you on a Monday morning when
you’re not really awake.
3.4 How to start the hardware part of this project.
You will use TinaCloud to simulate the required circuit and to interface the TM4C123GH6PM
microcontroller with an external LCD (2 lines) and a hexadecimal keypad (16 keys). You will have to
design your own keypad based on individual pushbuttons/switches, do not use the prebuilt components
available.
3.5 Extra marks
The followings are considered to be additional task for which extra credit will be given:
• Adding a password to access the keypad/calculator, with the option for the user to change the
password.
• Using the flash memory of the microcontroller, e.g. to store the password.
• Display graphics on the LCD.
• Any additional tasks that you find useful (get the module leader approval first).
These may require use of extra shift keys (e.g. shift-equals).
ELEC3662&XJEL3662 – Dr L. Mhamdi, Dr C. Trayner, & Dr R. Solis – Mini-project handout – 2020/21 – V2.0
(2020nov12)
Page 7 of 11
ELEC/XJEL 3662 – Embedded Systems
Mini-Project
4 Appendix A - Making software device-independent
4.1 The problem
Suppose your TM4C123GH6PM microcontroller has an LED connected to an output bit of a port –
consider the code to turn it on or off. This might include a declaration like
#define LED (*((volatile unsigned long *) 0x12345678))
and a command something like
LED = 0x04;
to turn it on. This is fine when the program is implemented on the same microcontroller. (Actually, it's
not. If someone has to modify the code later, they might wonder why the LED gets the value 0x04, not
just 1 or 0. And what if they set it to 0x01 by mistake - what would happen?).
But suppose you later want to port the software to another microcontroller, i.e. implement it on a different
one. This might be because your firm has moved to a better microcontroller manufacturer, or to a newer
microcontroller by the same manufacturer. This porting will involve two steps:
1. Understanding why the address was 0x12345678 and the output value was 0x04. Then working out
the new ones. This work is inevitable.
2. Going through all the program code looking for anything that refers to LED and changing 0x04 to
whatever the new value is. This is where it is very easy to miss things and make mistakes. It is also
extra work.
With a simple example like this one LED it would not be too hard, but realistic programs have many of
addresses (possibly dozens), all with their strange values to be sent to them.
The problem here is that much of the code is device-dependent – it depends on the specific device
(here the TM4C123GH6PM microcontroller). It is far better if your program is as device-independent as
possible.
4.2 The solution
... is to put all the device-dependent code in one place, in its own module. This module makes the
device-dependent code available in a device-independent way, e.g.
void WriteLed( int value )
{
if (value) // Any non-zero value will turn it on.
LED = 0x04;
else LED = 0x00;
}
This appears merely to replace one command by another - to say WriteLed( 1 ); is no shorter than LED
= 0x04; – but the advantage is that it is device-independent.
In this example any non-zero parameter turns the LED on. In more complicated cases you might want
to include code to check that the parameter has a valid value.
There are possible objections to this practice: the extra function calls might reduce the program’s
efficiency. For comments on this, see Appendix B. Nevertheless, it is better to have a program which
does the right thing slowly than one which does the wrong thing fast.
ELEC3662&XJEL3662 – Dr L. Mhamdi, Dr C. Trayner, & Dr R. Solis – Mini-project handout – 2020/21 – V2.0
(2020nov12)
Page 8 of 11
ELEC/XJEL 3662 – Embedded Systems
Mini-Project
5 Appendix B – Efficiency concerns and coding style
You often find that a better (e.g. clearer) way of writing your code looks less efficient. It might take more
CPU time and/or occupy more program memory space. Do these matter? In both cases the answer is
“it depends” – for some programs it might, but for most it probably doesn't.
5.1 CPU time
Your processor runs at a clock speed of many megahertz. Even without checking, one would guess
that the time to make an extra function call would be of the order of a microsecond or less. If the call is
made 100,000 times a second it might matter. But consider writing to the LCD display or reading from
the keyboard – would you notice an extra microsecond? Even if the job required a thousand accesses,
would you notice a millisecond?
5.2 Program memory space
The function calls will increase the program size (but see below), but probably only by a few bytes per
call. Processors are bought with memory sizes increasing in large steps (e.g. 32k, 64k, 96k, ...). If the
extra code happens to push the size over one of these boundaries then the cost will increase, but the
probability of this is slight. If you did find that your code was just over the size boundary then you could
look into reducing it. (Actually, your first step would be to read up on your compiler's optimisation
options.)
5.3 Avoiding these inefficiencies
The way you write your program controls what the C program is like, which is not necessarily what the
machine code is like. That also depends on what the compiler does. If you define a function as inline
the compiler will consider replacing the call with the program lines inside the function. For details, see
your favourite C/C++ programming textbook.
Inlining avoids the time to call the function. As for program memory space, the contents of the function
are repeated every time it would have been called. With a large function (including with parameter
checks) this would increase memory space. If the function just contains a hardware I/O command (such
as the LED = 0x04; above) then it would be about the same.
Also, modern compilers are very good at optimising code. Compare the following two examples to print
a string. But before you look at the right-hand one, can you work out what the left-hand one does?
char *c = string;
while (*c)
putc( *(c++) );
int i = 0;
while ( string[i] != ‘\0’ ) {
putc( string[i] );
i ++;
}
A good compiler would probably generate much the same machine code from both. But which is easier
to understand? Which is less likely to generate bugs when someone changes the code? Or when it is
first written?
With the left-hand one, for instance, what would have happened if the programmer had used ++c
instead of c++? Or if they had omitted the inner brackets in the putc? Or if they had used the ++ in the
while, not the putc? If you’re not certain, it’s probably an obscure feature of C/C++ and best avoided –
it invites mistakes.
ELEC3662&XJEL3662 – Dr L. Mhamdi, Dr C. Trayner, & Dr R. Solis – Mini-project handout – 2020/21 – V2.0
(2020nov12)
Page 9 of 11
ELEC/XJEL 3662 – Embedded Systems
Mini-Project
The current perception (actually, it’s been around since the 1980s or earlier) is that programs should
be written in simple language, easy to understand. Even if the compiler doesn’t optimise well, it’s more
important to avoid bugs.
In the nineteenth century, Charles Dickens wrote novels which showed off his ability to handle
complicated English grammar. In the earlier days of computing, programmers wrote programs which
showed off their ability to handle complicated ways of describing algorithms. Both are now regarded as
bad practice.
6 Appendix C – Hints on handling the LCD
This appendix provides an overview of the functions involved in handling an external LCD. Of course,
you will need to define your own. One of these, InitDisplayPort(), must be called first. The mechanism
by which these functions send information to the LCD is slightly complicated, and can be defined into
another function called SendDisplayByte(). This can be designed to handle almost all information sent
to the LCD. The only exception is the start of InitDisplayPort(), which is unusual and needs direct 4-bit
access to the LCD port. The initialization process is described on page 45 of the LCD datasheet. You
will soon realize that you will communicate with the LCD by sending two nibbles (4 bits) rather than
sending one byte. Therefore, another function can be defined called SendDisplayNibble().
6.1 SendDisplayNibble()
This sends a nibble (4 bits, i.e. a small byte) to the LCD. It uses two ports of the microcontroller:
• RS on bit 3 of Port A
• EN on bit 2 of Port A
• DB7-DB4 on the bits you choose of Port B.
This function has to do three things:
1. Set up the RS bit appropriately: 0 for instructions or 1 for data.
2. Send the nibble to the bits of the port.
3. Pulse the EN line for 450 ns.
The EN pulse needs extra comments. From the datasheet (Bus Timing Characteristics / Write
Operation, page 491) you will see you need a pulse width of at least 450 ns. Due to the amount of delays
required for controlling the LCD, it would be a good idea to define a function that creates time delays.
6.2 SendDisplayByte()
This function has the job of sending an 8-bit quantity to the LCD. The eight bits must be send four at a
time using SendDisplayNibble() twice.. Figure 9 (page 22) of the PDF shows that the upper four bits
are sent first.
After sending both nibbles, there must be another delay of 37 µs for the display to act on what it has
received.