Interfacing 16×2 LCD With 8051
Interfacing 16×2 LCD With 8051
16×2 LCD module is a very common type of LCD module that is used in 8051 based embedded
projects. It consists of 16 rows and 2 columns of 5×7 or 5×8 LCD dot matrices. The module were are
talking about here is type number JHD162A which is a very popular one . It is available in a 16 pin
package with back light ,contrast adjustment function and each dot matrix has 5×8 dot resolution.
The pin numbers, their name and corresponding functions are shown in the table below.
4 RS Register selection
6 E Enable
7 DB0 Data
8 DB1 Data
9 DB2 Data
10 DB3 Data
11 DB4 Data
12 DB5 Data
13 DB6 Data
14 DB7 Data
The JHD162A has two built in registers namely data register and command register. Data register is
for placing the data to be displayed , and the command register is to place the commands. The 16×2
LCD module has a set of commands each meant for doing a particular job with the display. We will
discuss in detail about the commands later. High logic at the RS pin will select the data register and
Low logic at the RS pin will select the command register. If we make the RS pin high and the put a
data in the 8 bit data line (DB0 to DB7) , the LCD module will recognize it as a data to be displayed .
If we make RS pin low and put a data on the data line, the module will recognize it as a command.
R/W pin is meant for selecting between read and write modes. High level at this pin enables read
mode and low level at this pin enables write mode.
E pin is for enabling the module. A high to low transition at this pin will enable the module.
DB0 to DB7 are the data pins. The data to be displayed and the command instructions are placed
on these pins.
LED+ is the anode of the back light LED and this pin must be connected to Vcc through a suitable
series current limiting resistor. LED- is the cathode of the back light LED and this pin must be
connected to ground.
16×2 LCD module has a set of preset command instructions. Each command will make the module to
do a particular task. The commonly used commands and their function are given in the table below.
01 Clear screen
02 Return home
04 Decrement cursor
06 Increment cursor
LCD initialization.
The steps that has to be done for initializing the LCD display is given below and these steps are
common for almost all applications.
The steps for sending data to the LCD module is given below. I have already said that the LCD
module has pins namely RS, R/W and E. It is the logic state of these pins that make the module to
determine whether a given data input is a command or data to be displayed.
The circuit diagram given above shows how to interface a 16×2 LCD module with AT89S1
microcontroller. Capacitor C3, resistor R3 and push button switch S1 forms the reset circuitry.
Ceramic capacitors C1,C2 and crystal X1 is related to the clock circuitry which produces the system
clock frequency. P1.0 to P1.7 pins of the microcontroller is connected to the DB0 to DB7 pins of the
module respectively and through this route the data goes to the LCD module. P3.3, P3.4 and P3.5
are connected to the E, R/W, RS pins of the microcontroller and through this route the control
signals are transffered to the LCD module. Resistor R1 limits the current through the back light LED
and so do the back light intensity. POT R2 is used for adjusting the contrast of the display. Program
for interfacing LCD to 8051 microcontroller is shown below.
Program.
ACALL CMND
ACALL CMND
ACALL CMND
ACALL CMND
ACALL CMND
ACALL CMND
MOV A,#49D
ACALL DISP
MOV A,#54D
ACALL DISP
MOV A,#88D
ACALL DISP
MOV A,#50D
ACALL DISP
MOV A,#32D
ACALL DISP
MOV A,#76D
ACALL DISP
MOV A,#67D
ACALL DISP
MOV A,#68D
ACALL DISP
ACALL CMND
MOV A,#67D
ACALL DISP
MOV A,#73D
ACALL DISP
MOV A,#82D
ACALL DISP
MOV A,#67D
ACALL DISP
MOV A,#85D
ACALL DISP
MOV A,#73D
ACALL DISP
MOV A,#84D
ACALL DISP
MOV A,#83D
ACALL DISP
MOV A,#84D
ACALL DISP
MOV A,#79D
ACALL DISP
MOV A,#68D
ACALL DISP
MOV A,#65D
ACALL DISP
MOV A,#89D
ACALL DISP
CLR P3.5
CLR P3.4
SETB P3.3
CLR P3.3
ACALL DELY
RET
DISP:MOV P1,A
SETB P3.5
CLR P3.4
SETB P3.3
CLR P3.3
ACALL DELY
RET
CLR P3.5
SETB P3.4
MOV P1,#0FFh
SETB P3.3
MOV A,P1
JB ACC.7,DELY
CLR P3.3
CLR P3.4
RET
END
Subroutine CMND sets the logic of the RS, R/W, E pins of the LCD module so that the module
recognizes the input data ( given to DB0 to DB7) as a command.
Subroutine DISP sets the logic of the RS, R/W, E pins of the module so that the module recognizes
the input data as a data to be displayed .
Interfacing LCD Module to 8051 in 4 Bit Mode (using only 4 pins of a port)
The microcontroller like 8051 has only limited number of GPIO pins (GPIO – general purpose input
output). So to design complex projects we need sufficient number of I/O pins . An LCD module can
be interfaced with a microcontroller either in 8 bit mode (as seen above) or in 4 bit mode. 8 bit mode
is the conventional mode which uses 8 data lines and RS, R/W, E pins for functioning. However 4 bit
mode uses only 4 data lines along with the control pins. This will saves the number of GPIO pins
needed for other purpose.
Objectives
Interface an LCD with 8051 in 4 bit mode
Use a single port of the microcontroller for both data and control lines of the LCD.
As shown in the circuit diagram, port 0 of the controller is used for interfacing it with LCD module. In
4 bit mode only 4 lines D4-D7, along with RS, R/W and E pins are used. This will save us 4 pins of our
controller which we might employ it for other purpose. Here we only need to write to the LCD
module. So the R/W pin can be ground it as shown in the schematic diagram. In this way the total
number of pins can be reduced to 6. In 4 Bit mode the data bytes are split into two four bits and are
transferred in the form of a nibble. The data transmission to a LCD is performed by assigning logic
states to the control pins RS and E. The reset circuit, oscillator circuit and power supply need to be
provided for the proper working of the circuit.
RS EQU P0.4
EN EQU P0.5
PORT EQU P0
U EQU 30H
L EQU 31H
ORG 000H
MOV DPTR,#INIT_COMMANDS
ACALL LCD_CMD
MOV DPTR,#LINE1
ACALL LCD_CMD
MOV DPTR,#TEXT1
ACALL LCD_DISP
MOV DPTR,#LINE2
ACALL LCD_CMD
MOV DPTR,#TEXT2
ACALL LCD_DISP
SJMP $
ANL L,#00FH
SWAP A
ANL A,#00FH
MOV U,A
RET
ORL PORT,A
SETB EN
ACALL DELAY
CLR EN
ACALL DELAY
RET
LCD_CMD: CLR A
MOVC A,@A+DPTR
JZ EXIT2
INC DPTR
CLR RS
ACALL SPLITER
MOV A,U
ACALL MOVE
MOV A,L
ACALL MOVE
SJMP LCD_CMD
EXIT2: RET
LCD_DATA: SETB RS
ACALL SPLITER
MOV A,U
ACALL MOVE
MOV A,L
ACALL MOVE
RET
LCD_DISP: CLR A
MOVC A,@A+DPTR
JZ EXIT1
INC DPTR
ACALL LCD_DATA
SJMP LCD_DISP
EXIT1: RET
DJNZ R7, L2
RET
INIT_COMMANDS: DB 20H,28H,0CH,01H,06H,80H,0
LINE1: DB 01H,06H,06H,80H,0
LINE2: DB 0C0H,0
CLEAR: DB 01H,0
END
Hex key pad is essentially a collection of 16 keys arranged in the form of a 4×4 matrix. Hex key pad
usually have keys representing numerics 0 to 9 and characters A to F. The simplified diagram of a
typical hex key pad is shown in the figure below.
The hex keypad has 8 communication lines namely R1, R2, R3, R4, C1, C2, C3 and C4. R1 to R4
represents the four rows and C1 to C4 represents the four columns. When a particular key is
pressed the corresponding row and column to which the terminals of the key are connected
gets shorted. For example if key 1 is pressed row R1 and column C1 gets shorted and so on. The
program identifies which key is pressed by a method known as column scanning. In this method
a particular row is kept low (other rows are kept high) and the columns are checked for low. If a
particular column is found low then that means that the key connected between that column
and the corresponding row (the row that is kept low) is been pressed. For example if row R1 is
initially kept low and column C1 is found low during scanning, that means key 1 is pressed.
The circuit diagram for demonstrating interfacing hex keypad to 8051 is shown below.Like previous
8051 projects, AT89S51 is the microcontroller used here. The circuit will display the
character/numeric pressed on a seven segment LED display. The circuit is very simple and it uses only
two ports of the microcontroller, one for the hex keypad and the other for the seven segment LED
display.
The hex keypad is interfaced to port 1 and seven segment LED display is interfaced to port 0 of the
microcontroller. Resistors R1 to R8 limits the current through the corresponding segments of the LED
display. Capacitors C1, C2 and crystal X1 completes the clock circuitry for the microcontroller.
Capacitor C3, resistor R9 and push button switch S1 forms a debouncing reset mechanism.
Program.
ORG 00H
MOV A,#1D
ACALL DISPLAY
NEXT2:JB P1.6,NEXT3
MOV A,#2D
ACALL DISPLAY
NEXT3:JB P1.7,NEXT4
MOV A,#3D
ACALL DISPLAY
NEXT4:SETB P1.0
CLR P1.1
JB P1.4,NEXT5
MOV A,#4D
ACALL DISPLAY
NEXT5:JB P1.5,NEXT6
MOV A,#5D
ACALL DISPLAY
NEXT6:JB P1.6,NEXT7
MOV A,#6D
ACALL DISPLAY
NEXT7:JB P1.7,NEXT8
MOV A,#7D
ACALL DISPLAY
NEXT8:SETB P1.1
CLR P1.2
JB P1.4,NEXT9
MOV A,#8D
ACALL DISPLAY
NEXT9:JB P1.5,NEXT10
MOV A,#9D
ACALL DISPLAY
NEXT10:JB P1.6,NEXT11
MOV A,#10D
ACALL DISPLAY
NEXT11:JB P1.7,NEXT12
MOV A,#11D
ACALL DISPLAY
NEXT12:SETB P1.2
CLR P1.3
JB P1.4,NEXT13
MOV A,#12D
ACALL DISPLAY
NEXT13:JB P1.5,NEXT14
MOV A,#13D
ACALL DISPLAY
NEXT14:JB P1.6,NEXT15
MOV A,#14D
ACALL DISPLAY
NEXT15:JB P1.7,BACK
MOV A,#15D
ACALL DISPLAY
LJMP BACK
DISPLAY:MOVC A,@A+DPTR // gets digit drive pattern for the current key from LUT
RET
DB 11011010B
DB 11110010B
DB 11101110B
DB 01100110B
DB 10110110B
DB 10111110B
DB 00111110B
DB 11100000B
DB 11111110B
DB 11110110B
DB 10011100B
DB 10011110B
DB 11111100B
DB 10001110B
DB 01111010B
END
Firstly the program initializes port 0 as an output port by writing all 0’s to it and port 1 as an
input port by writing all 1’s to it. Then the program makes row 1 low by clearing P1.0 and scans
the columns one by one for low using JB instruction.If column C1 is found low, that means 1 is
pressed and accumulator is loaded by zero and DISPLAY subroutine is called. The display
subroutine adds the content in A with the starting address of LUT stored in DPTR and loads A
with the data to which the resultant address points (using instruction MOVC A,@A+DPTR). The
present data in A will be the digit drive pattern for the current key press and this pattern is put
to Port 0 for display. This way the program scans for each key one by one and puts it on the
display if it is found to be pressed.
Notes.
PROM
EPROM
EEPROM
Flash EPROM
Mask ROM
External addressable ROM for the 8051 is of 128KB of address space which is be divided
into two parts:
1. Internal Program Memory (4KB) i.e. from 0000H to 0FFFH + External Program
Memory (60KB) i.e. from 1000H to FFFFH. We can select this mode by making EA =
1. (Refer to fig.1).
2. Total External Program Memory (64KB), i.e., over the entire range of 0000H to
FFFFH. We can select this mode by making EA = 0. (Refer to fig.2)
The external storage is addressed and accessed via I/O ports P0 & P2
In 8051,the PSEN =1(is active) when reading a byte from external program memory(ROM)
The command used to access external memory is
MOVC A,@A+DPTR
1. When we connect PSEN to the ground, then the 8051 microcontroller fetches the
opcode from the external ROM.
2. But, when we connect PSEN to VCC, the status of the PSEN is ‘not activated’ since
it is an active low pin. Hence, the program memory is saved in the internal ROM of
8051 itself.
Here 8K*8 means that the program ROM is organized in a structure that has an 8k word space and the *8
then means that each word is 8-bits.This means that 32K*8 would be a ROM which has a 32K word
space,at 8 bits per word.In other words it means that there are 32,000 locations that are 8-bit word.
The program & data memory can be a size of 1k*8, 2k*8 , 4k*8 , 8k*8 , 16k*8 , 32k*8 & 64k*8.
Also,multiple chips of smaller sizes cascades together to form a chip of larger size. i.e,we can connect
two 16K*8 data RAM chips to form one 32K*8 data RAM. If we take 32k*8,then 2^15=35K,which
implies 15 address lines and *8 implies 8 data lines.
Code to interface external program ROM with 8051
Let’s say we want to move data stored from locations 4000H onwards in the external ROM
to the location 40H in the internal RAM.
ORG 0000H
MOV DPTR, #4000H ; Load DPTR with the location where data is stored
MOV R0, #40H ; Load R0 with the int RAM loc where you want to save the data
rep: MOV A, #00H ; Clear accumulator
CJNE A, #00H, rep ; Repeat this process until 0 is received from the DPTR
END
ORG 4000H
DB 1H, 2H, 0AH, 0F2H, 30H, 5CH, 2AH, 01H, 00H, FFH, 0
rep: MOVX A, @DPTR ; Copy data from location pointed by DPTR to acc
DJNZ R0, rep ; Repeat until all 100 bytes are received
stay: SJMP stay ; Stay here forever
END
The read and write ability of RAM, plus the nonvolatility of ROM
Extremely power-efficient SRAM cells built out of CMOS
Consists of an internal lithium battery as a backup energy source
Consists of intelligent control circuitry.
END
Interfacing external program ROM, data ROM and external RAM with the
8051
Next, let’s interface both program ROM and data RAM to 8051, Let’s say we want to
interface 16KB data RAM, 16KB program ROM, and 16KB of data RAM, then we’ll have to
follow the following steps:
1. Step 1: Calculate the number of address lines required to access 16KB of data, that
is 214 = 16KB. Here, we require 14 address lines A0 – A13.
2. Step 2: Decide the location of RAM and ROM, here we are going to interface
program ROM from 0000H and data RAM from 8000H.
3. Step 3: Select the decoder circuit, here we’re going to select 74LS138 decoder.
4. Step 4: We do not need a decoder circuit for program ROM, but we have to connect
the 74LS138 decoder to data ROM and data RAM.
5. Step 5: Connect G1 to VCC, G2A, and G2B to ground.
6. Step 6: Connect input A and B to P2.6 and P2.7 respectively, and the input C to
ground.
7. Step 7: We connect external program and data ROM, for that we can use an AND
gate with its input being signal from RD (to access external data space)
and PSEN (to access external program space) and output to OE of external ROM.
8. Step 8: To interface the external RAM, we connect
both RD and WR to WE and OE respectively of external RAM.
Interfacing ADC to 8051
ADC (Analog to digital converter) forms a very essential part in many embedded projects and this
article is about interfacing an ADC to 8051 embedded controller. ADC 0804 is the ADC used here and
before going through the interfacing procedure, we must neatly understand how the ADC 0804
works.
ADC 0804.
Vref/2 (pin9) (volts) Input voltage span (volts) Step size (mV)
Make CS=0 and send a low to high pulse to WR pin to start the conversion.
Now keep checking the INTR pin. INTR will be 1 if conversion is not finished and INTR will be 0 if
conversion is finished.
If conversion is not finished (INTR=1) , poll until it is finished.
If conversion is finished (INTR=0), go to the next step.
Make CS=0 and send a high to low pulse to RD pin to read the data from the ADC.
Circuit diagram.
The figure above shows the schematic for interfacing ADC0804 to 8051. The circuit initiates the ADC
to convert a given analogue input , then accepts the corresponding digital data and displays it on the
LED array connected at P0. For example, if the analogue input voltage Vin is 5V then all LEDs will
glow indicating 11111111 in binary which is the equivalent of 255 in decimal. AT89s51 is the
microcontroller used here. Data out pins (D0 to D7) of the ADC0804 are connected to the port pins
P1.0 to P1.7 respectively. LEDs D1 to D8 are connected to the port pins P0.0 to P0.7 respectively.
Resistors R1 to R8 are current limiting resistors. In simple words P1 of the microcontroller is the input
port and P0 is the output port. Control signals for the ADC (INTR, WR, RD and CS) are available at
port pins P3.4 to P3.7 respectively. Resistor R9 and capacitor C1 are associated with the internal clock
circuitry of the ADC. Preset resistor R10 forms a voltage divider which can be used to apply a
particular input analogue voltage to the ADC. Push button S1, resistor R11 and capacitor C4 forms a
debouncing reset mechanism. Crystal X1 and capacitors C2,C3 are associated with the clock circuitry
of the microcontroller.
Program.
ORG 00H
CLR P3.6 // high to low pulse to RD for reading the data from ADC
END
Notes.
IOut=Iref⟮D72+D64+D58+D416+D332+D264+D1128+D0256⟯IOut=Iref⟮D72+D64+D58+D416+D
332+D264+D1128+D0256⟯
The Iref is the input current. This must be provided into the pin 14. Generally 2.0mA is used as I ref
We connect the Iout pin to the resistor to convert the current to voltage. But in real life it may cause
inaccuracy since the input resistance of the load will also affect the output voltage. So practically
Iref current input is isolated by connecting it to an Op-Amp with R f = 5KΩ as feedback resistor. The
feedback resistor value can be changed as per requirement.
Vout = 5V + (5 ×sinθ)
Let us see the lookup table according to the angle and other parameters for DAC.
Angle(in θ ) sinθ Vout (Voltage Magnitude) Values sent to DAC
0 0 5 128
90 1.0 10 255
180 0 5 128
270 -1.0 0 0
360 0 5 128
Basically DAC converts the Digital data received on its input pins to the respective Analog
output.
ORG 0000h
mov P1,#00H
jmp repeat
call delay2sec
mov P1,#00H
call delay2sec
ret
inc R7
cjne R7,#FFH,triwave1
mov R7,#FFH
djnz R7,triwave2
ret
call delay2sec
mov P1,#20H
call delay2sec
mov P1,#40H
call delay2sec
mov P1,#80H
call delay2sec
ret
djnz r2,$
djnz r1,del1
djnz r0,del2
ret
djnz r2,$
djnz r1,del21
djnz r0,del22
ret
END
Serial Communication and Bus Interface