Chapter 6 - LCD

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

HCMC University of Technology and Education

Faculty of Electrical & Electronic Engineering


No.1 Vo Van Ngan Street, Thu Duc Dist., HCMC, VN

EMBEDDED SYSTEM
IN INDUSTRY

NGUYEN THANH NGHIA


11/23/2021 11
NGUYEN THANH NGHIA
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
No.1 Vo Van Ngan Street, Thu Duc Dist., HCMC, VN

CHAPTER 6:
LCD

NGUYEN THANH NGHIA


11/23/2021 22
NGUYEN THANH NGHIA
Outline
1. LCD Structure (HD44780 LCD).
2. DDRAM Memory (Display Data
RAM).
3. Important commands.
4. Application.

NGUYEN THANH NGHIA 3


Chapter 6: LCD
1. LCD Structure (HD44780 LCD)
 An alphanumeric low cost LCD Display is very
essential for may small and big projects to
Display various type of information.
 Hitachi HD44780 Chipset based 16x2 char LCD is
Really very cheap and easily available in the local
market.

NGUYEN THANH NGHIA 4


PINChapter 6: LCD
NUM. SYMBOL FUNCTION
1 Vss GND
1. LCD Structure (HD44780 LCD)
2 Vdd + 3V or + 5V
 An 3 alphanumeric
Vo low
Contrast cost LCD Display is very
Adjustment
essential
4 RS for H/L
may small
Register and big projects to
Select Signal
5 R/W H/L Read/Write Signal
Display
6 E
various Htype of information.
→ L Enable Signal
 Hitachi
7 HD44780
DB0 Chipset
H/L Data Bus Linebased 16x2 char LCD is
8
Really DB1 cheap
very H/L Data
andBuseasily
Line available in the local
9 DB2 H/L Data Bus Line
market.
10 DB3 H/L Data Bus Line
11 DB4 H/L Data Bus Line
12 DB5 H/L Data Bus Line
13 DB6 H/L Data Bus Line
14 DB7 H/L Data Bus Line
15 A/Vee + 3.5V for LED/Negative Voltage Output
16 K K Power Supply
NGUYEN for B/L
THANH (OV)
NGHIA 5
Chapter 6: LCD
1. Introduction

NGUYEN THANH NGHIA 6


Chapter 6: LCD
1. Introduction
• DB0 - DB7, the 8 data bus lines, which perform
read/write of data
• Vss, Vdd - Voltage supply pins
• R/W – Pin writing/reading to/from - LCD
• RS - Pin selects registers between Instruction Register
and Data Register
• E - "Enabling" pin; when this pin is set to logical low,
the LCD does not care what is happening with R/W, RS,
and the data bus lines; when this pin is set to logical high,
the - LCD is processing the incoming data
• Vo - Pin for LCD contrast

NGUYEN THANH NGHIA 7


Chapter 6: LCD
1. Introduction

NGUYEN THANH NGHIA 8


Chapter 6: LCD
1. Introduction

NGUYEN THANH NGHIA 9


Chapter 6: LCD
1. Introduction

NGUYEN THANH NGHIA 10


Chapter 6: LCD
1. Introduction
LCD registers
The HD44780U controller has two 8-bit registers:
• an instruction register (IR) - the IR stores instruction codes, such as display clear
and cursor shift, and address information for display data RAM (DDRAM) and
character generator RAM (CGRAM).
• a data register (DR) - the DR temporarily stores data to be written into DDRAM
or CGRAM and temporarily stores data to be read from DDRAM or CGRAM. The
DR is also used for data storage when reading data from DDRAM or CGRAM.
The choice between the two registers is made by the register selector (RS) signal as
detailed the following table:

NGUYEN THANH NGHIA 11


Chapter 6: LCD
2. DDRAM Memory (Display Data RAM)
Display data RAM (DDRAM) stores the information we
send to LCD in ASCII Code. For each letter there is a
special code that represents it: for example, the letter A in
ASCII code, “receives” a value of 65 in base 10 or
01000001 in binary base, or 41 in the base 16. The
memory can contain up to 80 letters.

NGUYEN THANH NGHIA 12


Chapter 6: LCD
2. DDRAM Memory (Display Data RAM)
Some of the addresses represent the lines of LCD (0x00-
0x0F- first line; 0x40-0x4F – second line). The rest of the
addresses represent the “non-visible” memory of the
DRAM, which can be also used as a general memory. The
DDRAM address is the position of the cursor on the
display LCD (the received information will be written at
the place where the cursor is).

NGUYEN THANH NGHIA 13


Chapter 6: LCD
3. Important commands
COMMAND DESCRIPTION
0x38 Used 8 bit mode, enable display on 2 lines,
dot matrix is 5x7
0x0C Turn on display, turn off cursor
0X06 Cursor auto shift right
0x01 Clear display
0x80 Start address of line 1
0xC0 Start address of line 2

NGUYEN THANH NGHIA 14


Chapter 6: LCD
3. Important commands
STEPS TO WRITE LCD
1. Clear pin RW
2. Control pin RS base on what you want to write
 Command : RS = 0
 Data : RS = 1
3. Output what you want to write to data lines (D0-
D7)
4. Output one pulse from pin E
5. Delay to wait for LCD handling

NGUYEN THANH NGHIA 15


Chapter 6: LCD
3. Important commands
LCD instruction set
Clear Display:
This command clears the display and returns the cursor to the
home position (address 0) and sets I/D to 1 in order to
increment the cursor. Its line settings are as follows:

Home Cursor:
This returns the cursor to the home position, returns a shifted
display to the correct position, and sets the display data (DD)
RAM address to 0. Its line settings are as follows:

NGUYEN THANH NGHIA 16


Chapter 6: LCD
3. Important commands
LCD instruction set
Entry Mode Set:
This command sets the cursor move direction and specifies
whether toshift the display or not. These operations are
performed during the data write/read of the CG or DD RAM.
Its line settings are as follows:

I/D=0 means the cursor position is decremented (moves right to left).


I/D=1 means the cursor position is incremented (moves left to right).
S=0 means normal operation, the display remains still, and the cursor
moves.
S=1 means the display moves with the cursor.

NGUYEN THANH NGHIA 17


Chapter 6: LCD
3. Important commands
LCD instruction set
Display On/Off Control:
This command sets the ON/OFF display as well as the cursor
and blinking capabilities (0 equals OFF; 1 equals ON). D
controls whether the display is ON or OFF, C controls whether
the cursor is ON or OFF, B controls whether the blinking is
ON or OFF. The line settings are as follows:

NGUYEN THANH NGHIA 18


Chapter 6: LCD
3. Important commands
LCD instruction set
Cursor or Display Shift:
This moves the cursor and shifts the display without changing
DD RAM contents. The line settings are as follows:

S/C=0 means move the cursor.


S/C=1 means shift display.
R/L= 0 means shift to the left.
R/L= 1 means shift to the right.

NGUYEN THANH NGHIA 19


Chapter 6: LCD
3. Important commands
LCD instruction set
Function Set:
This sets the interface data length (DL), the number of display
lines (N), and character font (F). The line settings are as
follows:

DL=0 means 4 bits are being used (the standard)


DL=1 means a full 8 bits being utilized
N=0 means 1 line
N=1 means 2 lines or more
F=0 means that 5x7 dot characters are used (which is how 99% of all LCDs are
set up)
F=1 means 5x10 dot characters are used
NGUYEN THANH NGHIA 20
Chapter 6: LCD
4. Application
4.1 Display information on LCD
 Create a program to display information on two line of
LCD with the content as follows:

NGUYEN THANH NGHIA 21


Chapter 6: LCD
4. Application
4.1 Display information on LCD
 Schematic:

NGUYEN THANH NGHIA 22


Chapter 6: LCD
4. Application
4.1 Display information on LCD
 Flowchart:

NGUYEN THANH NGHIA 23


Chapter 6: LCD
4. Application
4.1 Display information on LCD
 Code: import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

# Define GPIO to LCD mapping


LCD_RS = 29
LCD_E = 31
LCD_D0 = 7
LCD_D1 = 11
LCD_D2 = 12
LCD_D3 = 13
LCD_D4 = 15
LCD_D5 = 16
LCD_D6 = 18
LCD_D7 = 22

NGUYEN THANH NGHIA 24


Chapter 6: LCD
4. Application
4.1 Display information on LCD
 Code: # Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
delay = 1

#set GPIO.out
GPIO.setup(LCD_E, GPIO.OUT) # E
GPIO.setup(LCD_RS, GPIO.OUT) # RS
GPIO.setup(LCD_D0, GPIO.OUT) # DB0
GPIO.setup(LCD_D1, GPIO.OUT) # DB1
GPIO.setup(LCD_D2, GPIO.OUT) # DB2
GPIO.setup(LCD_D3, GPIO.OUT) # DB3
GPIO.setup(LCD_D4, GPIO.OUT) # DB4
GPIO.setup(LCD_D5, GPIO.OUT) # DB5
GPIO.setup(LCD_D6, GPIO.OUT) # DB6
GPIO.setup(LCD_D7, GPIO.OUT) # DB7

NGUYEN THANH NGHIA 25


Chapter 6: LCD
4. Application
4.1 Display information on LCD
 Code:
# Define some device constants
LCD_WIDTH = 16 # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
def lcd_init(): # this function is used to initialized lcd by
sending the different commands
# Initialise display
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
lcd_byte(0x38,LCD_CMD) # 101000 Data length, number of lines, font
size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)

NGUYEN THANH NGHIA 26


Chapter 6: LCD
4. Application
4.1 Display information on LCD
if bits&0x01==0x01:
 Code: GPIO.output(LCD_D0, True)
def lcd_byte(bits, mode): if bits&0x02==0x02:
# Send byte to data pins GPIO.output(LCD_D1, True)
# bits = data if bits&0x04==0x04:
# mode = True for character GPIO.output(LCD_D2, True)
# False for command if bits&0x08==0x08:
GPIO.output(LCD_RS, mode) # RS GPIO.output(LCD_D3, True)
# High bits if bits&0x10==0x10:
GPIO.output(LCD_D0, False) GPIO.output(LCD_D4, True)
GPIO.output(LCD_D1, False) if bits&0x20==0x20:
GPIO.output(LCD_D2, False) GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D3, False)
GPIO.output(LCD_D6, True)
GPIO.output(LCD_D4, False)
if bits&0x80==0x80:
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D7, True)
GPIO.output(LCD_D6, False) # Toggle 'Enable' pin
GPIO.output(LCD_D7, False) lcd_toggle_enable()
NGUYEN THANH NGHIA 27
Chapter 6: LCD
4. Application
4.1 Display information on LCD
def lcd_toggle_enable():
 Code: # Toggle enable
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)

'''
Function Name :lcd_string(message,line)
Function Description :print the data on lcd
'''
def lcd_string(message):
# Send string to display
message = message.ljust(LCD_WIDTH," ")
for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)
NGUYEN THANH NGHIA 28
Chapter 6: LCD
4. Application
4.1 Display information on LCD
 Code:
def lcd_goto_xy(x,y): # dua con tro den toa do (x,y)
(x:0-1, y:0-15)
dc=(0x80,0xC0)
lcd_byte(dc[x]+y,LCD_CMD)
time.sleep(0.0005)

lcd_init() #khoi tao lcd


while 1:
# Print out results
lcd_goto_xy(0,2)
lcd_string("Welcome to")
lcd_goto_xy(1,1)
lcd_string("DH SPKT TP HCM")
time.sleep(1)

NGUYEN THANH NGHIA 29


Chapter 6: LCD
4. Application
4.2 Clock counter and display on LCD
 Create a program to display the message on two lines
of LCD as in Figure below. In particular, two characters
in the end of line 1 represent the second value of counter.

NGUYEN THANH NGHIA 30


Chapter 6: LCD
4. Application
4.2 Clock counter and display on LCD
 Flowchart:

NGUYEN THANH NGHIA 31


Chapter 6: LCD
4. Application
4.2 Clock counter and display on LCD
 Code: import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

# Define GPIO to LCD mapping


LCD_RS = 29
LCD_E = 31
LCD_D0 = 7
LCD_D1 = 11
LCD_D2 = 12
LCD_D3 = 13
LCD_D4 = 15
LCD_D5 = 16
LCD_D6 = 18
LCD_D7 = 22

NGUYEN THANH NGHIA 32


Chapter 6: LCD
4. Application
4.2 Clock counter and display on LCD
 Code: # Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
delay = 1

#set GPIO.out
GPIO.setup(LCD_E, GPIO.OUT) # E
GPIO.setup(LCD_RS, GPIO.OUT) # RS
GPIO.setup(LCD_D0, GPIO.OUT) # DB0
GPIO.setup(LCD_D1, GPIO.OUT) # DB1
GPIO.setup(LCD_D2, GPIO.OUT) # DB2
GPIO.setup(LCD_D3, GPIO.OUT) # DB3
GPIO.setup(LCD_D4, GPIO.OUT) # DB4
GPIO.setup(LCD_D5, GPIO.OUT) # DB5
GPIO.setup(LCD_D6, GPIO.OUT) # DB6
GPIO.setup(LCD_D7, GPIO.OUT) # DB7

NGUYEN THANH NGHIA 33


Chapter 6: LCD
4. Application
4.2 Clock counter and display on LCD
 Code:
# Define some device constants
LCD_WIDTH = 16 # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
def lcd_init(): # this function is used to initialized lcd by
sending the different commands
# Initialise display
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
lcd_byte(0x38,LCD_CMD) # 101000 Data length, number of lines, font
size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)

NGUYEN THANH NGHIA 34


Chapter 6: LCD
4. Application
4.2 Clock counter and display on LCD
if bits&0x01==0x01:
 Code: GPIO.output(LCD_D0, True)
def lcd_byte(bits, mode): if bits&0x02==0x02:
# Send byte to data pins GPIO.output(LCD_D1, True)
# bits = data if bits&0x04==0x04:
# mode = True for character GPIO.output(LCD_D2, True)
# False for command if bits&0x08==0x08:
GPIO.output(LCD_RS, mode) # RS GPIO.output(LCD_D3, True)
# High bits if bits&0x10==0x10:
GPIO.output(LCD_D0, False) GPIO.output(LCD_D4, True)
GPIO.output(LCD_D1, False) if bits&0x20==0x20:
GPIO.output(LCD_D2, False) GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D3, False)
GPIO.output(LCD_D6, True)
GPIO.output(LCD_D4, False)
if bits&0x80==0x80:
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D7, True)
GPIO.output(LCD_D6, False) # Toggle 'Enable' pin
GPIO.output(LCD_D7, False) lcd_toggle_enable()
NGUYEN THANH NGHIA 35
Chapter 6: LCD
4. Application
4.2 Clock counter and display on LCD
def lcd_toggle_enable():
 Code: # Toggle enable
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)

'''
Function Name :lcd_string(message,line)
Function Description :print the data on lcd
'''
def lcd_string(message):
# Send string to display
message = message.ljust(LCD_WIDTH," ")
for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)
NGUYEN THANH NGHIA 36
Chapter 6: LCD
4. Application
4.2 Clock counter and display on LCD
 Code:
def lcd_goto_xy(x,y): # dua con tro den toa do (x,y)
(x:0-1, y:0-15)
dc=(0x80,0xC0)
lcd_byte(dc[x]+y,LCD_CMD)
time.sleep(0.0005)

lcd_init() #khoi tao lcd


while 1:
for i in range(0,59):
lcd_goto_xy(0,14)
lcd_string(str(i))
time.sleep(1)

NGUYEN THANH NGHIA 37


Chapter 6: LCD

The end!

NGUYEN THANH NGHIA 38

You might also like