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

Embedded Systems Lab05

This document describes how to interface a 16x2 LCD screen with an ATmega328P microcontroller. It discusses the pins and commands needed to control the LCD. The objective is to send commands and data to display messages on the LCD screen. It provides details on initializing the LCD, including sending the function set, display on, and clear display commands. It also describes how to send commands and data to the LCD by setting the register select pin low for commands and sending the 8-bit data over the parallel data pins.

Uploaded by

Aiman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Embedded Systems Lab05

This document describes how to interface a 16x2 LCD screen with an ATmega328P microcontroller. It discusses the pins and commands needed to control the LCD. The objective is to send commands and data to display messages on the LCD screen. It provides details on initializing the LCD, including sending the function set, display on, and clear display commands. It also describes how to send commands and data to the LCD by setting the register select pin low for commands and sending the 8-bit data over the parallel data pins.

Uploaded by

Aiman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Embedded Systems Lab Lab 05 LCD Interfacing

NED University of Engineering & Technology Electrical Engineering Department

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.

16x2 LCD (Liquid Crystal Display)


Features
 The operating voltage of this LCD is 4.7V-5.3V
 It includes two rows where each row can produce 16-characters.
 The utilization of current is 1mA with no backlight
 Every character can be built with a 5×8 pixel box
 It can display alphabets, numbers and a few custom generated characters
 It can work on two modes (4-bit and 8-bit): In 4-bit mode we send the 4 bits (out of the total 8-
bits) at a time and in the 8-bit mode, we can send all 8-bits in one stroke.
 These are obtainable in Blue & Green Backlight
The LCD can display 32 characters in total and each character will be made of 5*8 (40) pixel dots. The
standard LCDs have HD44780 dot-matrix liquid crystal display controller / driver that is mounted on LCD
module itself. The function of this interface IC is to get the commands and data (sent over parallel data
lines) from the MCU and process them to display meaningful information onto our LCD Screen . Hence,
the MCU doesn’t directly have to deal with the 1280 pixels of LCD and their positions.
[Datasheet HD44780.]
1
Embedded Systems Lab Lab 05 LCD Interfacing
NED University of Engineering & Technology Electrical Engineering Department

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.

Figure 1: 16x2 LCD Pinout


Pin
# Description / Function Type Connection
Name
1 Connected to the ground of the MCU/
VSS Pin for LCD ground.
Source Power source.
2 Pin Connected to the supply pin of power
VDD Pin for LCD supply voltage.
source / +5V of MCU.
3 Contrast Control: Adjusts the Connected to a variable potentiometer
V0
contrast of the LCD. that can source 0-5V.
4 Connected to a digital output pin of
Register Select: Selects either MCU.
RS
command register or data register 0: Command Mode
1: Data Mode
5 Read/Write: Toggles the LCD Connected to a digital output pin of
between Read/Write operations. Control MCU.
RW
Read operation is rarely needed for Pins 0: Write Operation
information like cursor position etc. 1: Read Operation
6 The enable pin is used by the LCD to
latch information presented to its data
Enable: Must be held high to pins. When data is supplied to data pins,
E
perform Read/Write Operation a high-to-low pulse must be applied to
this pin in order for the LCD to latch in
the data present at the data pins.
7- In 4-bit mode, only 4 pins (D0-D3) are
14 Data/ connected to MCU digital output pins.
D0 to Data Bits (0-7): Pins used to send
Comman In 8-bit mode, all 8 pins (D0-D7) are
D7 command or data bits to the LCD.
d Pins connected to MCU digital output pins.
15 LED + Anode of backlight LED LED+ and LED- are connected to 5V
A
is given positive voltage. and Ground pins of MCU with a current
Backlight
16 LED - Cathode is connected to limiting resistor in series.
LED Pins
K ground to illuminate backlight
LED.
2
Embedded Systems Lab Lab 05 LCD Interfacing
NED University of Engineering & Technology Electrical Engineering Department

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

Figure 2: LCD Connections with ATmega328P for 8-bit Mode


3
Embedded Systems Lab Lab 05 LCD Interfacing
NED University of Engineering & Technology Electrical Engineering Department

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.

Initializing or Configuring the LCD:

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.

1) Function Set: 2 lines and 5 × 8 matrix (D0–D7, 8-bit mode) - 0x38


2) Display on, cursor blinking - 0x0E
3) Clear display screen - 0x01
After initialization, wait for 2msec.
Sending Command:

To send any of the commands from Table 1 to the LCD,


1) Make pin RS low for selecting command register. (R/W should be made low if not already grounded).
2) Put the command number on the data pins (D0–D7) i.e., use relevant PORTx register.
3) Send a high-to-low pulse to the E pin to enable the internal latch of the LCD.
Notice that after each command you should wait for some time (100us generally or for 2msec in some
cases like clear screen and return home) to let the LCD module run the command.
Sending Data:

To send data to the LCD


1) Make pins RS = 1 (for data register) and R/W = 0.
2) Put the data on the data pins (D0–D7) i.e., use relevant PORTx register.
3) Send a high-to-low pulse to the E pin to enable the internal latch of the LCD.
Notice that after sending data you should wait about 100 μs to let the LCD module write the data on the
screen.

Example Code and Subroutines


See the sample code for Example 1. It is written in in terms of LCD_DPRT, LCD_DDDR, LCD_DPIN.
These will be replaced by the PORT, DDR and PIN register of the port with which D0-D7 are connected.
In our example, it is port D. This is done to make the code more generalize, and to achieve this we have
used #define directive (#define causes the compiler to substitute token-string for each occurrence of
identifier in the source file). Similarly, LCD_CPRT, LCD_CDDR, LCD_CPIN are used to show the
relevant registers of port with which we have connected control pins (RS and E). Here, it is port B. For the
position of these pins is indicated by LCD_RS and LCD_E which is 0 and 1. If you change the hardware
connections, you have to update the relevant registers only once (i.e., at the start with the #define directive)
without changing the rest of the code or subroutines.
4
Embedded Systems Lab Lab 05 LCD Interfacing
NED University of Engineering & Technology Electrical Engineering Department

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.

2) lcdData: It takes the data character to be displayed.

3) lcd_init: This performs initialization steps.

4) lcd_print: This takes a complete string to be printed and passes one character at a time by lcdData.

Figure 3: Connections for LCD Interfacing (8-bit Mode) with ATmega328P

Note that RW is connected with GND for writing.


5
Embedded Systems Lab Lab 05 LCD Interfacing
NED University of Engineering & Technology Electrical Engineering Department

#include <avr/io.h> void lcd_init()


#include <util/delay.h> {
#define LCD_DPRT PORTD //LCD DATA PORT LCD_DDDR = 0xFF;//making data port (output)
#define LCD_DDDR DDRD //LCD DATA DDR LCD_CDDR |= (1<<LCD_RS)|(1<<LCD_EN);
#define LCD_DPIN PIND //LCD DATA PIN //making control pins output pins
#define LCD_CPRT PORTB //PORT for LCD Control Pins LCD_CPRT &=~(1<<LCD_EN); //LCD_EN = 0
#define LCD_CDDR DDRB //DDR for LCD Control Pins _delay_us(2000); //wait for init.
#define LCD_CPIN PINB //PIN Reg for LCD Control Pins lcdCommand(0x38); //init. LCD 2-line, 8-bit mode
#define LCD_RS 0 //LCD RS (RS is connected at PB0) lcdCommand(0x0F); //display on blinking
#define LCD_EN 1 //LCD EN (EN is connected at PB1) lcdCommand(0x01); //clear LCD
_delay_us(2000); //wait
lcdCommand(0x06); //shift cursor right
void lcdCommand( unsigned char cmnd ) }
{
LCD_DPRT = cmnd; //send cmnd to data port void lcd_print( char * str )
LCD_CPRT &= ~ (1<<LCD_RS); //RS = 0 for command {
LCD_CPRT |= (1<<LCD_EN); //EN = 1 for H-to-L pulse unsigned char i = 0;
_delay_us(1); //wait to make enable wide while(str[i]!=0)
LCD_CPRT &= ~ (1<<LCD_EN); //EN = 0 for H-to-L pulse {
_delay_us(100); //wait to make enable wide lcdData(str[i]);
} i++ ;
// _delay_ms(100); //for typing effect
}

void lcdData( unsigned char data ) }


{
LCD_DPRT = data; //send data to data port int main(void)
LCD_CPRT |= (1<<LCD_RS); //RS = 1 for data {
LCD_CPRT |= (1<<LCD_EN); //EN = 1 for H-to-L pulse lcd_init(); //initialize
_delay_us(1); //wait to make enable wide _delay_ms(1000);
LCD_CPRT &= ~ (1<<LCD_EN); lcd_print("Congratulations!!");
//EN = 0 for H-to-L pulse lcdCommand(0xC0); //Cursor at the start of 2nd line
_delay_us(100); //wait to make enable wide lcd_print(" LCD works ");
} return 0;
}

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

void lcd_gotoxy(unsigned char x, unsigned char y)


{
char firstCharAdr[]={0x80,0xC0,0x94,0xD4}; //Table
lcdCommand(firstCharAdr[y-1] + x - 1);
_delay_us(100);
}

If you are interested, you can try this too!

7
NED University of Engineering & Technology
Department of Electrical Engineering

Course Code: EE-354 Course Title: Embedded Systems


Laboratory Session No.: ________________________ Date: ______________________________
Psychomotor Domain Assessment Rubric for Laboratory (Level P3)
Extent of Achievement
Skill(s) to be assessed
0 1 2 3 4
Software Initialisation Completely Able to recognise Able to recognise Able to recognise Able to recognise
and Configuration: unable to initialisation but initialisation but initialisation and initialisation and
Set up and recognise recognise could not configuration is configuration configuration with
software initialisation and initialisation configure erroneous with minimal complete success
configuration steps and errors
configuration
10% 0 10 20 30 40
Equipment Identification Completely Ability to identify Ability to identify
and Handling: unable to equipment but equipment and
Sensory skill to identify identify makes mistakes in recognises all
equipment and its equipment and recognising components,
components along with components __ components, __ practices careful
adherence to safe and no regard demonstrates and safe handling
handling to safe handling decent equipment
handling capacity
15% 0 30 60
Establish and Verify Unable to Able to verify Able to verify
Hardware-Software perform hardware hardware
Connection: hardware and connection but connection and
Recognise interface software unable to establish successfully
between computer and connection __ software __ establishes
hardware kit and establish verification connection software
connectivity with verification connection
software verification
15% 0 30 60
Following step-by-step Inability to Able to recognise Able to recognise Able to recognise Able to recognise
procedure to complete recognise and given lab given lab given lab given lab
lab work: perform given procedures and procedures and procedures and procedures and
Observe, imitate and lab procedures perform them but perform them by perform them by perform them by
operate hardware in could not follow following following following
conjunction with software the prescribed prescribed order of prescribed order prescribed order
to complete the provided order of steps steps, with of steps, with of steps, with no
sequence of steps frequent mistakes occasional mistakes
mistakes
15% 0 15 30 45 60

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

Total Points (out of 400)


Weighted CLO (Psychomotor Score) (Points/4)
Remarks
Instructor’s Signature with Date

Page 2 of 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy