Embedded Systems Lab05
Embedded Systems Lab05
LAB SESSION 05
OBJECTIVE:
To interface an LCD (Liquid Crystal Display) screen with ATmega328P by sending required commands
and data.
LAB OUTCOMES:
By the end of this lab, you would be able to:
1) Identify the pins and commands for controlling a 16x2 LCD
2) Interface a 16x2 LCD screen with ATmega328P and display different messages
“I don't care what it is, when it has an LCD screen, it makes it better.” — Kevin Rose
INTRODUCTION:
Display units like LEDs, 7-segment LED displays, LCD screens etc. play an important part in establishing
a good communication between the users and machines, and therefore, are vital for embedded systems.
Through display screens, the user gets a feeling of knowing the system’s working status. Consider the
examples of ATM machine, automatic washing machine or microwave ovens. They allow us to give input
through keypad or knobs or touch screens, and display useful messages on screens which guide us or show
the status of process. LCD screens are now seen everywhere due to their declining prices, ease of
programming due to an internal controller, and ability to display characters and graphics.
For learning purpose, we are interfacing our ATmega32P microcontroller with a standard 16x2 LCD screen.
16×2 LCD is named so because; it has 16 Columns and 2 Rows. Such dot-matrix LCDs are available in
different packages like 8x1, 8x2, 16x1, and 20x4.
LCD Pinout
Figure 1 shows the 16 pins of a 16x2 LCD and their names. Most of the LCDs have these 16 pins that are
used for connection according to their functionality. Let’s discuss the function of each pin one-by-one.
In this lab, we are interfacing the LCD with ATmega328P directly (parallel interface) and sending data
in 8-bit mode. Note that the 8-bit data interfacing is easier to program but uses 4 more pins.
LCD Commands
The following table hex code for the commands that are sent to LCD instruction register for the specified
functions.
Table 1: Hex code for Commands
Hex Code for
Function
Command to LCD
0E Display on, cursor on
01 Clear display screen
02 Return home
04 Decrement cursor (shift cursor to left
06 Increment cursor (shift cursor to right)
05 Shift display right
07 Shift display left
0F Display on, cursor blinking
80 Force cursor to beginning of first line
C0 Force cursor to beginning of 2nd line
38 Function Set: 2 lines and 5 × 8 matrix (D0–D7, 8-bit mode)
08 Display off, cursor off
18 Shift the entire display to the left
1C Shift the entire display to the right
Interfacing LCD with ATmega328P and C-Programming
The Figure 2 shows required connections for LCD 16 pins with ATmega328P in 8-bit data mode. The
R/W pin can be directly connected to ground instead of utilizing an I/O pin (as we are performing write
operation only).
For controlling the LCD and sending commands and data, the following steps are needed. Remember, the
digital I/O pins connected with the LCD must be configured as output pins using DDRx registers as per
your connections.
To initialize the LCD for 2-line and 8-bit operation, the following sequence of commands should be sent
to the LCD. Next we will show how to send a command to the LCD. After power-up you should wait
about 15ms before sending initializing commands to the LCD. If the LCD initializer function is not the
first function in your code you can omit this delay.
Based on the steps described earlier, 4 useful subroutines are defined before the main function.
1) lcdCommand: It takes hex code of command as input.
4) lcd_print: This takes a complete string to be printed and passes one character at a time by lcdData.
Figure 4: Sample Code for Example 1 - LCD Interfacing (8-bit Mode) with ATmega328P
LAB TASKS
TASK 1: To test Example 1 using ATmega328P and 16x2 LCD
Program ATmega328P with the given code. Make connections to interface the LCD and test the results.
TASK 2: To test different commands for modifying LCD display
Modify the code and test different commands to make the display interesting.
On line 1, display: <Your Name> (You can have more than 16 characters in the string).
On line 2, display: <Roll # …… >
Make the text moving (scrolling) continuously from left to right by using shift display commands.
6
Embedded Systems Lab Lab 05 LCD Interfacing
NED University of Engineering & Technology Electrical Engineering Department
Go-to Subroutine
Following is an interesting subroutine that allows you to move cursor at any specified location (y, x).
Where x is the line number (1 or 2) and y is character position (1 to 16).
7
NED University of Engineering & Technology
Department of Electrical Engineering
Page 1 of 2
Psychomotor Domain Assessment Rubric for Laboratory (Level P3)
Extent of Achievement
Skill(s) to be assessed
0 1 2 3 4
Programming the Incorrect Correct selection Correct selection Correct selection Correct selection
Controller for given selection and of programming and use of and use of and use of
Embedded System use of constructs and programming programming programming
Problem: programming instructions but constructs and constructs and constructs and
Imitate and practice given constructs and their use is instructions with instructions with instructions with
embedded C instructions instructions incorrect many syntax/logical little to no no syntax/logical
for implementing specific errors syntax/logical errors
control strategy and store errors
required variables
15% 0 15 30 45 60
Software Menu Unable to Little ability and Moderate ability Reasonable Demonstrates
Identification and Usage: understand and understanding of and understanding understanding of command over
Ability to operate use software software menu of software menu software menu software menu
software environment menu operation, makes operation, makes operation, makes usage with
under supervision, using many mistake lesser mistakes no major mistakes occasional use of
menus, shortcuts, advance menu
instructions etc. options
10% 0 10 20 30 40
Detecting and Removing Unable to check Able to find error Able to find error Able to find error Able to find error
Errors/Exceptions in and detect messages in messages in messages in messages in
Hardware and Software: error messages software but no software and software and software and
Detect Errors/Exceptions in software and sense of recognise them on recognise them recognise them on
and manipulate, under hardware hardware error hardware. Still on hardware. hardware.
supervision, to rectify the identification unable to Moderately able Reasonably able in
embedded C program understand the in understanding understanding
error type and error type and error type and
possible causes possible causes possible causes
10% 0 10 20 30 40
Visualisation, Unable to Ability to Ability to Ability to Ability to
Comparison and analysis understand and understand and understand and understand and understand and
of results: utilise utilise utilise visualisation utilise utilise
Copy or enter results in visualisation, visualisation and and plotting visualisation and visualisation and
analysis software to plotting and plotting instructions with plotting plotting
visualise and compare analysis instructions with occasional errors. instructions with instructions
them with inputs. Use software errors. Unable to Able to partially no errors. Able to without errors.
analysis tools to compute compute compute standard partially compute Able to compute
standard indices from standard indices indices standard indices standard indices
result completely
10% 0 10 20 30 40
Page 2 of 2