15 I/O devices Interfacing
Dr K. Vani
In this lecture, the interfacing of 8051 with an output device and an input device are described. The output device considered is LCD display, and the input device is a matrix-type keyboard. The operation modes of LCD are first discussed. Interfacing of the LCD to an 8051 microcontroller using Assembly and C is also discussed. This is followed by the keyboard interface details.
1. LCD (Liquid Crystal Display)interface
LCDs can display numbers, characters, and graphics. To produce a proper display, the information has to be periodically refreshed. This can be done by the CPU or internally by the LCD device itself. Incorporating a refreshing controller into the LCD, relieves the CPU of this task and hence many LCDs have built-in controllers. These controllers also facilitate flexible programming for characters and graphics. Figure 1.1 shows the pin description of an LCD from Optrex.
Vss and VDD provide +5v and ground, V0 is used for controlling LCD contrast. If RS=0, the instruction command register is selected, allowing the user to send a command such as clear display, cursor at home, etc. If RS=1 the data register is selected, allowing the user to send data to be displayed on the LCD. R/W input allows the user to Read/ Write the information to the LCD. The enable pin is used by the LCD to latch information presented to its data pins. The 8-bit data pins are used to send information to LCD. Section 1.1 discusses about command codes for writing the instructions on the LCD register.
Section 1.2 gives an example program for displaying a character on the LCD. Section 1.3 shows the communication of LCD with a busy flag and section 1.4 gives a detailed explanation of LCD data sheet.
1.1 LCD Command Codes
The LCD’s internal controller can accept several commands and modify the display accordingly.
These commands would be things like:
✓ Clear screen
✓ Return home
✓ Decrement/Increment cursor
After writing to the LCD, it takes some time for it to complete its internal operations. During this time, it will not accept any new commands or data. Figure 1.2 shows the command codes of LCD and Figure 1.3 shows the LCD interfacing.
✓ We need to insert a time delay between any two commands or data sent to LCD.
✓ A long delay has to be given between issuing data or commands to the LCD.
1.2 Program to Display characters on LCD
To send any of the commands to the LCD, make pin RS=0. For data, make RS=1. Then send a high-to-low pulse to the E pin to enable the internal latch of the LCD. This is shown in the code below.
;calls a time delay before sending next data/command ;P1.0-P1.7 are connected to LCD data pins D0-D7
;P2.0 is connected to RS pin of LCD
;P2.1 is connected to R/W pin of LCD
;P2.2 is connected to E pin of LCD
ORG 0H
MOV A,#38H ;INIT. LCD 2 LINES, 5X7 MATRIX
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#0EH ;display on, cursor on
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#01 ;clear LCD
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#06H | ;shift cursor right | |
ACALL COMNWRT | ;call command subroutine | |
ACALL DELAY | ;give LCD some time | |
MOV A,#84H | ;cursor at line 1, pos. 4 | |
ACALL COMNWRT | ;call command subroutine | |
ACALL DELAY | ;give LCD some time | |
MOV A,#’N’ | ;display letter N | |
ACALL DATAWRT | ;call display subroutine | |
ACALL DELAY | ;give LCD some time | |
MOV A,#’O’ | ;display letter O | |
ACALL DATAWRT | ;call display subroutine | |
AGAIN: | SJMP AGAIN | ;stay here |
COMNWRT: | ;send command to LCD | |
MOV P1,A | ;copy reg A to port 1 | |
CLR P2.0 | ;RS=0 for command | |
CLR P2.1 | ;R/W=0 for write | |
SETB P2.2 | ;E=1 for high pulse | |
CLR P2.2 | ;E=0 for H-to-L pulse | |
RET | ||
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 | |
CLR P2.2 | ;E=0 for H-to-L pulse | |
RET | ||
DELAY: | MOV R3,#50 | ;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 |
END
1.3 Communicating with LCD using the busy flag The code to communicate with the LCD is given below.
;Check busy flag before sending data, command to LCD ;p1=data pin
;P2.0 connected to RS pin ;init. LCD 2 lines ,5×7 matrix
;P2.1 connected to R/W pin ;issue command
;P2.2 connected to E pin ;LCD on, cursor on
MOV A,#38H ;issue command
CALL COMMAND ;clear LCD command
MOV A,#0EH ;issue command
ACALL COMMAND ;shift cursor right
MOV A,#01H ;issue command
ACALL COMMAND ;cursor: line 1, pos. 6
MOV A,#06H ;command subroutine
ACALL COMMAND ;display letter N
MOV A,#86H
ACALL COMMAND
MOV A,#’N’
ACALL DATA_DISPLAY
MOV A,#’O’ ;display letter O
ACALL DATA_DISPLAY
HERE: SJMP HERE ;STAY HERE
COMMAND:
ACALL READY ;is LCD ready?
MOV P1,A ;issue command code
CLR P2.0 ;RS=0 for command
CLR P2.1 ;R/W=0 to write to LCD
SETB P2.2 ;E=1 for H-to-L pulse
CLR P2.2 ;E=0,latch in
RET
DATA_DISPLAY: ;is LCD ready?
ACALL READY ;issue data
MOV P1,A ;RS=1 for data
SETB P2.0 ;R/W =0 to write to LCD
CLR P2.1 ;E=1 for H-to-L pulse
SETB P2.2 ;E=0,latch in
CLR P2.2
RET
READY:
BACK:
SETB P1.7 ;make P1.7 input port
CLR P2.0 ;RS=0 access command reg
SETB P2.1 ;R/W=1 read command reg
SETB P2.2 ;read command reg and check busy flag
CLR P2.2 ;E=1 for H-to-L pulse
JB P1.7,BACK ;E=0 H-to-L pulse
RET ;stay until busy flag=0
END
1.4 LCD data sheet
1.5 Sending information to LCD with MOVC instruction
The following program shows how to use the MOVC instruction to send data and commands to an LCD
2. Keyboard interface
We will now look at interfacing keyboards to the microcontroller. Keys in a keyboard are arranged in a matrix of rows and columns. The controller access both rows and columns through ports. Using two ports, we can connect to an 8×8 or a 4×4 matrix keyboard. When a key is pressed, a row and column make a contact, otherwise there is no contact. We will look at the details using a 4×4 keyboard.
2.1 4×4 Keyboard
A 4×4 matrix is connected to two ports as shown in Figure 2.1. The rows are connected to an output port and the columns are connected to an input port. Port1 of 8051 is connected to the rows of key matrix, hence it acts as an output port. Port 2 of 8051 is connected to the columns of the key matrix, hence it acts as an input port. A scanning process is used to identify the key that is pressed.
2.2 Key scan
To find out the key pressed , the controller grounds a row by sending a ‘0’ on the corresponding line of the output port. It then reads the data at the columns using the input port. If data from columns is D3-D0=1111, then no key is pressed. If any bit of the column is ‘0’, it indicates that a key is pressed in that column. In this example, the column is identified by the following values:
1110 – key pressed in column 0
1101 – key pressed in column 1
1011 – key pressed in column 2
0111 – key pressed in column 3
This proceeds as follows.
2.2.1 Steps to find out key pressed
Beginning with the row 0, the microcontroller grounds it by providing a low to row D0 only. It then reads the columns(port2). If the data read is all 1s, then no key in that row is activated and the process is moved to the next row. It then grounds the next row, reads the columns, and checks for any zero. This process continues until a row with a zero is identified. After identification of the row in which the key has been pressed, the column to which the pressed key belongs is identified as discussed above – by looking for a zero in the input values read.
Example:
(a) D3 – D0 = 1101 for the row, D3 – D0 = 1011 for the column, indicate row 1 and column 3 are selected. This indicates that key 6 is pressed.
(b) D3 – D0 = 1011 for the row, D3 – D0 = 0111 for the column, indicate row 2 and column 3 are selected. Then key ‘B’ is pressed.
2.3 Program:
The program used for detection and identification of the key activated 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.
● When all columns are found to be high, the program waits for a short amount of time before it goes to the next stage of waiting for a key to be pressed.
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.
● Remember that the output latch is connected to rows, still have their initial zeros (in stage 1), making them grounded.
● After the keypress detection, it waits for 20-ms for the bounce and then scans the columns again.
a) It ensures that the first key press detection was not an erroneous one due to spike noise.
b) After the 20-ms delay, if the key is still pressed, then it goes to the loop (step 3) to detect the actual key pressed.
3. To detect which row the key pressed belongs to, it grounds one row at a time, reading the columns each time.
• If it finds that all columns are high, this means that the key press does not belong to that row. Therefore, it grounds the next row and continues until it finds the row, that the key pressed belongs to.
• Upon finding the row that the key pressed belongs to, it sets up the starting address for the lookup table holding the scan codes for that row.
4. To identify the key pressed, it rotates the column bits, one bit at a time, into the carry flag and checks to see if it is low.
• Upon finding the zero, it pulls out the ASCII code for that key from the look-up table.
• Otherwise, it increments the pointer to point to the next element of the look-up table.
2.4 Keyboard Program
The program used for scanning and identifying the pressed key is shown below. The key press detection is standard for all keyboards but the process for determining which key is pressed varies. The look-up table method is shown in the following program. It can be modified to work with any matrix upto 8 x 8.
(Program Ref:The 8051 Microcontroller and Embedded Systems Using Assembly and C -Second Edition )
3. Summary
LCD display, interfacing of LCD with 8051 and the assembly programming are discussed. The Keyboard, interfacing of Keyboard to 8051 and assembly programming for interfacing are also discussed.
4. References
- Muhammad Ali Mazidi, Janice Gillispie Mazidi, Rolin D. McKinlay, “The 8051 Microcontroller and Embedded Systems Using Assembly and C -Second Edition”, New Delhi (2000).