0% found this document useful (0 votes)
19 views48 pages

Module 5

The document discusses peripheral interfacing in embedded systems, focusing on LCDs and keypads. It explains the functionality of a 16x2 LCD, including its registers and programming steps, as well as how to interface a 4x4 matrix keypad with a microcontroller. Additionally, it provides assembly language code examples for displaying messages on the LCD and detecting key presses from the keypad.

Uploaded by

Jo jo
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)
19 views48 pages

Module 5

The document discusses peripheral interfacing in embedded systems, focusing on LCDs and keypads. It explains the functionality of a 16x2 LCD, including its registers and programming steps, as well as how to interface a 4x4 matrix keypad with a microcontroller. Additionally, it provides assembly language code examples for displaying messages on the LCD and detecting key presses from the keypad.

Uploaded by

Jo jo
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

MODULE-6

PERIPHERAL
INTERFACING
LCD

LED

KEYPAD
 Display units are the most important output devices
in embedded projects and electronics products.

 LCD is finding widespread use replacing LEDs


 The declining prices of LCD
 The ability to display numbers, characters and graphics
 Incorporation of a refreshing controller into the LCD,
thereby relieving the CPU of the task of refreshing LCD
 Ease of programming for characters and graphics
ECE3003 - Microcontroller and Application
 16x2 LCD is one of the most used display unit.

 As per the name the 2x16 has 2 lines with 16 characters on


each lines.

 It supports all the ASCII chars and is basically used for


displaying the alpha numeric characters.

 Here each character is displayed in a matrix of 5x7 pixels.

 Apart from alpha numeric chars it also provides the provision


to display the custom characters by creating the pattern.
ECE3003 - Microcontroller and Application
ECE3003 - Microcontroller and Application
CURSOR ADDRESSES FOR SOME LCDS
ECE3003 - Microcontroller and Application
 The 16X2 LCD has two built in registers namely data register
and command register.
 Command Register - stores the command instructions
given to the LCD. A command is an instruction given to
LCD to do a predefined task like initializing, clearing the
screen, setting the cursor position, controlling display
etc.

 Data Register - stores the data to be displayed on the


LCD. The data is the ASCII value of the character to
be displayed on the LCD.
ECE3003 - Microcontroller and Application
 For programming LCD follow these steps:
•STEP1: Initialization of LCD.
•STEP2: Sending command to LCD.
•STEP3: Writing the data to LCD.

Step1: LCD initialization (common for almost all applications)


1. Send 38H to the 8 bit data line for initialization
2. Send 0FH for making LCD ON, cursor ON and cursor
blinking ON.
3. Send 06H for incrementing cursor position.
4. Send 01H for clearing the display and return the cursor.
ECE3003 - Microcontroller and Application
Step2: Sending command to LCD
 Send the command data to command register
 Make R/W low.
 Make RS=0 if data byte is a command
 Pulse E from high to low with some delay.
 Repeat above steps for sending another data.

Step3: Writing the data to LCD


 Place data byte on the data register.
 Make R/W low.
 make RS=1 if the data byte is a data to be displayed.
 Pulse E from high to low with some delay.
 Repeat above steps for sending another data.
ECE3003 - Microcontroller and Application
Write an 8051 assembly language program to display the
message “HELLO” on LCD display using DPTR.
; P1.0-P1.7=D0-D7, P2.0=RS, P2.1=R/W, P2.2=E
ORG 0000H
MOV DPTR, #MYCOM
C1: CLR A
MOVC A,@A+DPTR
ACALL COMNWRT
ACALL DELAY
INC DPTR
JZ SEND_DAT
SJMP C1
ECE3003 - Microcontroller and Application
SEND_DAT:

MOV DPTR, #MYDATA

D1: CLR A

MOVC A,@A+DPTR

ACALL DATAWRT

ACALL DELAY

INC DPTR

JZ AGAIN

SJMP D1

AGAIN: SJMP AGAIN

ECE3003 - Microcontroller and Application


COMNWRT: ; send command to LCD

MOV P1, A ; copy reg A to P1

CLR P2.0 ; RS=0 for command

CLR P2.1 ; R/W=0 for write

SETB P2.2 ; E=1 for high pulse

ACALL DELAY ; give LCD some time

CLR P2.2 ; E=0 for H-to-L pulse

RET

ECE3003 - Microcontroller and Application


DATAWRT: ; write data to LCD

MOV P1, A ; copy reg A to port 1

SETB P2.0 ; RS=1 for data

CLR P2.1 ; R/W=0 for write

SETB P2.2 ; E=1 for high pulse

ACALL DELAY ; give LCD some time

CLR P2.2 ; E=0 for H-to-L pulse

RET

ECE3003 - Microcontroller and Application


DELAY: MOV R3, #250 ; 50 or higher for fast CPUs

HERE2: MOV R4, #255 ; R4 = 255

HERE: DJNZ R4, HERE ; stay until R4 becomes 0

DJNZ R3, HERE2

RET

ORG 300H

MYCOM: DB 38H, 0EH, 01, 06, 84H, 0 ; commands and null

MYDATA: DB “HELLO”, 0

END

ECE3003 - Microcontroller and Application


 Keyboards are organized in a matrix of rows and columns
 The CPU accesses both rows and columns through ports
 When a key is pressed, a row and a column make a contact

 A 4x4 matrix connected to two ports


 The rows are connected to an output port and the columns
are connected to an input port

ECE3003 - Microcontroller and Application


ECE3003 - Microcontroller and Application
ECE3003 - Microcontroller and Application
 It is the function of the microcontroller to scan the keyboard
continuously to detect and identify the key pressed

 To detect a pressed key, the microcontroller grounds all rows


by providing 0 to the output latch, then it reads the columns

 If the data read from columns is D3 –D0 = 1111, no key has


been pressed and the process continues till key press is
detected
 If one of the column bits has a zero, this means that a key
press has occurred
ECE3003 - Microcontroller and Application
 It grounds the next row, reads the columns, and checks for
any zero, this process continues until the row is identified

 After identification of the row in which the key has been


pressed it find out which column the pressed key belongs to.

 Identify the row and column of the pressed key for


(a) D3 – D0 = 1110 for the row, D3 – D0 = 1011 for the column
(b) D3 – D0 = 1101 for the row, D3 – D0 = 0111 for the column

 Answer : (a). 2 (b). 7

ECE3003 - Microcontroller and Application


 It grounds the next row, reads the columns, and checks for
any zero, this process continues until the row is identified

 After identification of the row in which the key has been


pressed it find out which column the pressed key belongs to.

 Identify the row and column of the pressed key for


(a) D3 – D0 = 1110 for the row, D3 – D0 = 1011 for the column
(b) D3 – D0 = 1101 for the row, D3 – D0 = 0111 for the column

 Answer : (a). 2 (b). 7

ECE3003 - Microcontroller and Application


Steps for key press identification

 Initially all switches are assumed to be released. So there is


no connection between the rows and columns.

 When any one of the switches are pressed, the corresponding


row and column are connected (short circuited). This will drive
that column pin (initially high) low.

 Using this logic, the button press can be detected. The colors
red and black is for logic high and low respectively.

ECE3003 - Microcontroller and Application


Steps for key press identification

 Step 1: The first step involved in interfacing the matrix keypad is to


write all logic 0’s to the rows and all logic 1’s to the columns. In the
image, black line symbolizes logic 0 and red line symbolizes logic 1.

ECE3003 - Microcontroller and Application


Steps for key press identification
 Step 2: Now the program has to scan the pins connected to columns of
the keypad. If it detects a logic 0 in any one of the columns, then a
key press was made in that column. This is because the event of the
switch press shorts the C2 line with R2. Hence C2 is driven low.

ECE3003 - Microcontroller and Application


Steps for key press identification

 Step 3: Once the column corresponding to the key pressed is located,


start writing logic 0’s to the rows sequentially (one after the other) and
check if C2 becomes low.

ECE3003 - Microcontroller and Application


Steps for key press identification

 Step 4: The procedure is followed till C2 goes low when logic low is
written to a row. In this case, a logic low to the second row will be
reflected in the second column.

ECE3003 - Microcontroller and Application


 We already know that the key press happened at column 2.
Now we have detected that the key is in row 2. So, the
position of the key in the matrix is (2,2).

 Once this is detected, its up to us to name it or provide it


with a task on the event of the key press.

ECE3003 - Microcontroller and Application


 Program for detection and identification of key activation
goes through the following stages:
1. To make sure that the preceding key has been released, 0s are
output to all rows at once, and the columns are read and checked
repeatedly until all the columns are high
2. To see if any key is pressed, the columns are scanned over and
over in an infinite loop until one of them has a 0 on it
3. To detect which row key press belongs to, it grounds one row at a
time, reading the columns each time
4. To identify the key press, it rotates the column bits, one bit at a
time, into the carry flag and checks to see if it is low

ECE3003 - Microcontroller and Application


ECE3003 - Microcontroller and Application
Keyboard Program
Write an assembly language program for the 8051 to interface the 4x4 matrix
keypad and LCD. Any key pressed on the Keypad must be display in LCD.

ECE3003 - Microcontroller and Application


ORG 000H
SJMP START

ORG 0030H
START: MOV P0,#0FFH ;MAKE P0 AN INPUT PORT
ACALL LCD_INITIALIZE

K1: MOV P1,#0 ;GROUND ALL ROWS AT ONCE


MOV A,P0 ;READ ALL COL.
ANL A,#00001111B ;MASKED UNUSED BIT
CJNE A,#00001111B,K1 ;CHECK ALL KEYS RELEASED
ECE3003 - Microcontroller and Application
K2: ACALL DELAY ;CALL 20 MS DELAY
MOV A,P0 ;SEE IF ANY KEY IS PRESSED
ANL A,#00001111B ;MASKED UNUSED BIT
CJNE A,#00001111B,OVER ;KEY PRESSED, WAIT
SJMP K2 ;CHECK TILL KEY PRESSED

OVER: ACALL DELAY ;WAIT 20 ms Key DEBOUNCE TIME

MOV A,P0 ;CHECK KEY CLOSURE


ANL A,#00001111B ;MASKED UNUSED BIT
CJNE A,#00001111B,OVER1 ;KEY PRESSED, FIND ROW
SJMP K2 ;IF NONE, KEEP POLLING
ECE3003 - Microcontroller and Application
OVER1: MOV P1,#1111110B ;GROUND ROW 0

MOV A,P0 ;READ ALL COLUMNS

ANL A,#00001111B ;MASKED UNUSED BIT

CJNE A,#00001111B,ROW_0 ;ROW0, FIND COL

MOV P1,#11111101B ;GROUND ROW 1

MOV A,P0 ;READ ALL COL.

ANL A,#00001111B ;MASKED UNUSED BIT

CJNE A,#00001111B,ROW_1 ;ROW 1, FIND THE COL

ECE3003 - Microcontroller and Application


MOV P1,#11111011B ;GROUND ROW 2

MOV A,P0 ;READ ALL COL.

ANL A,#00001111B ; MASKED UNUSED BIT

CJNE A,#00001111B,ROW_2 ;ROW 2, FIND COL

MOV P1,#11110111B ;GROUND ROW 3

MOV A,P0 ;READ ALL COL.

ANL A,#00001111B ;MASKED UNUSED BIT

CJNE A,#00001111B,ROW_3 ;ROW 3, FIND COL

LJMP K2 ;IF NONE, FALSE INPUT, REPEAT


ECE3003 - Microcontroller and Application
ROW_0: MOV DPTR, #KCODE0 ;SET DPTR=START OR ROW 0

SJMP FIND ;FIND COLUMN BELONGS TO

ROW_1: MOV DPTR, #KCODE1 ;SET DPTR=START OR ROW 1

SJMP FIND ;FIND COLUMN BELONGS TO

ROW_2: MOV DPTR, #KCODE2 ;SET DPTR=START OR ROW 2

SJMP FIND ;FIND COLUMN BELONGS TO

ROW_3: MOV DPTR, #KCODE3 ;SET DPTR=START OR ROW 3

ECE3003 - Microcontroller and Application


FIND: RRC A ;SEE IF ANY CY BIT LOW

JNC MATCH ;IF ZERO GET ASCII CODE

INC DPTR ;POINT TO NEXT COLUMN

SJMP FIND ;KEEP SEARCHING

MATCH: CLR A

MOVC A,@A+DPTR

ACALL LCD_DATA ;GET CODE FROM LOOK-UP TABLE

LJMP K1 ;LOOP

ECE3003 - Microcontroller and Application


DELAY: MOV R4,#40
REPEAT: MOV R5,#230 20ms DELAY
REPEATT: DJNZ R5,REPEATT PROGRAM
DJNZ R4,REPEAT
RET
ORG 300H
KCODE3: DB 'C','D','E','F‘ ;ROW 3
LOOK-UP TABLE
KCODE2: DB '8','9','A','B‘ ;ROW 2 FOR KEYPAD
KCODE1: DB '4','5','6','7‘ ;ROW 1 (Modify based on key
KCODE0: DB '0','1','2','3‘ ;ROW 0 position on keypad)
END
ECE3003 - Microcontroller and Application
LCD_INITIALIZE: MOV A,#38H
ACALL LCD_COMMAND
MOV A,#0EH
ACALL LCD_COMMAND
MOV A,#01H
ACALL LCD_COMMAND
MOV A,#06H
ACALL LCD_COMMAND
MOV A,#80H
ACALL LCD_COMMAND
RET
ECE3003 - Microcontroller and Application
LCD_COMMAND: MOV P2,A

CLR P3.7

CLR P3.6

SETB P3.5

ACALL DELAY

CLR P3.5

RET

ECE3003 - Microcontroller and Application


LCD_DATA: MOV P2,A

SETB P3.7

CLR P3.6

SETB P3.5

ACALL DELAY

CLR P3.5

RET

ECE3003 - Microcontroller and Application

You might also like