Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
120 views

Interfacing 16×2 LCD With 8051

The document describes interfacing a 16x2 LCD module with an 8051 microcontroller. It discusses: - The LCD module specifications including pin functions - Initialization steps such as sending commands to turn on the display and clear screen - Sending data to the LCD by setting control pin logic and placing data on pins - Interfacing the LCD in 4-bit mode using fewer pins - An example program to initialize and display text on the LCD

Uploaded by

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

Interfacing 16×2 LCD With 8051

The document describes interfacing a 16x2 LCD module with an 8051 microcontroller. It discusses: - The LCD module specifications including pin functions - Initialization steps such as sending commands to turn on the display and clear screen - Sending data to the LCD by setting control pin logic and placing data on pins - Interfacing the LCD in 4-bit mode using fewer pins - An example program to initialize and display text on the LCD

Uploaded by

gunda manasa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

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.

Pin No: Name  Function

1 VSS This pin must be connected to the ground

2 VCC  Positive supply voltage pin (5V DC)

3 VEE Contrast adjustment

4 RS Register selection

5 R/W Read or write

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

15 LED+  Back light LED+

16 LED-  Back light LED-


VEE pin is meant for adjusting the contrast of the LCD display and the contrast can be adjusted by varying the
voltage at this pin. This is done by connecting one end of a POT to the Vcc (5V), other end to the Ground and
connecting the center terminal (wiper) of of the POT to the VEE pin. See the circuit diagram for better
understanding.

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

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.

Command                       Function

0F LCD ON, Cursor ON, Cursor blinking ON

01 Clear screen

02 Return home

04 Decrement cursor

06 Increment cursor

0E Display ON ,Cursor blinking OFF

80 Force cursor to the beginning of  1st line

C0 Force cursor to the beginning of 2nd line


38 Use 2 lines and 5×7 matrix

83 Cursor line 1 position 3

3C Activate second line

08 Display OFF, Cursor OFF

C1 Jump to second line, position1

OC Display ON, Cursor OFF

C1 Jump to second line, position1

C2 Jump to second line, position2

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.

 Send 38H to the 8 bit data line for initialization


 Send 0FH for making LCD ON, cursor ON and cursor blinking ON.
 Send 06H for incrementing cursor position.
 Send 01H for clearing the display and return the cursor.

Sending data to the LCD.

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.

 Make R/W low.


 Make RS=0 if data byte is a command and make RS=1 if the data byte is a data to be displayed.
 Place data byte on the data register.
 Pulse E from high to low.
 Repeat above steps for sending another data.
Circuit diagram.

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.

MOV A,#38H // Use 2 lines and 5x7 matrix

ACALL CMND

MOV A,#0FH // LCD ON, cursor ON, cursor blinking ON

ACALL CMND

MOV A,#01H //Clear screen

ACALL CMND

MOV A,#06H //Increment cursor

ACALL CMND

MOV A,#82H //Cursor line one , position 2

ACALL CMND

MOV A,#3CH //Activate second line

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

MOV A,#0C1H //Jump to second line, position 1

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

HERE: SJMP HERE

CMND: MOV P1,A

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

DELY: CLR P3.3

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.

Program – Interface LCD Module to 8051 – 4 Bit Mode

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 $

SPLITER: MOV L,A

ANL L,#00FH

SWAP A

ANL A,#00FH

MOV U,A

RET

MOVE: ANL PORT,#0F0H

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

DELAY: MOV R7, #10H


L2: MOV R6,#0FH

L1: DJNZ R6, L1

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

TEXT1: DB " CircuitsToday ",0

TEXT2: DB "4bit Using 1Port",0

END

Important aspects of the program


        The programming part is done in assembly language instead of embedded C. As mentioned
earlier the 8 bit data is break into two 4 bit data and send to LCD. A subroutine called “SPLITTER” is
used in the program  for  slicing 8 bit data into nibbles. It uses two memory location ‘U’ and ‘L’  for
storing the upper and lower nibbles. The subroutine named “MOVE” is used to put the upper and
lower nibbles into the data pins of the LCD by making a high to low pulse at the E pin of the LCD
module. If we need to send a command, “LCD_CMD” is used. In this subroutine the RS pin of the LCD
is cleared in order to notify the LCD that the byte at its data pin is a command. And subroutine 
labeled “LCD_DISP” is used for sending data. Here the RS pin is set which will notify the LCD that the
byte arrived is a data for displaying. The necessary commands for the LCD initialization are defined at
the end part of the program. Along with that the text to be displayed is also defined there with a
label “TEXT1”. “LINE1” AND “LINE2” labels contains the commands for selecting the 1 st and 2nd rows of
the LCD respectively.

Interfacing hex keypad to 8051


Hex keypad.

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.

Interfacing hex keypad to 8051.

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 DPTR,#LUT // moves starting address of LUT to DPTR

MOV A,#11111111B // loads A with all 1's


MOV P0,#00000000B // initializes P0 as output port

BACK:MOV P1,#11111111B // loads P1 with all 1's

CLR P1.0 // makes row 1 low

JB P1.4,NEXT1 // checks whether column 1 is low and jumps to NEXT1 if not


low

MOV A,#0D // loads a with 0D if column is low (that means key 1 is


pressed)

ACALL DISPLAY // calls DISPLAY subroutine

NEXT1:JB P1.5,NEXT2 // checks whether column 2 is low and so on...

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

MOV P0,A // puts corresponding digit drive pattern into P0

RET

LUT: DB 01100000B // Look up table starts here

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

About the program.

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.

 The 5V DC power supply must be well regulated and filtered.


 Column scanning is not the only method to identify the key press. You can use row scanning also.
In row scanning a particular column is kept low (other columns are kept high) and the rows are
tested for low using a suitable branching instruction. If a particular row is observed low then that
means that the key connected between that row and the corresponding column (the column that is
kept low) is been pressed. For example if  column C1 is initially kept low and row R1  is observed
low during scanning, that means key 1 is pressed.
 A membrane type hex keypad was used during the testing. Push button switch type and dome
switch type will also work. I haven’t checked other types.
 The display used was a common cathode seven segment LED display with type number
ELK5613A. This is just for information and any general purpose common cathode 7 segment LED
display will work here.
External ROM – For program/data
ROM is a type of non-volatile memory. The data is not lost, even if the power supply to the
IC is taken off. ROM can be classified into:

 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. Program Code Space


2. Data Memory Space

Program Code Space


To access the program space, we require a program counter (PC), it locates and fetches
instructions. We use the MOVC A, @A+DPTR instruction to get data, where C stands for
code. It is 64K bytes in size.
How to access program space?
For storing the program code, we can either use on-chip ROM or off-chip ROM or a
combination of both on-chip and off-chip ROM depending on the status of the EA pin.

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.

Circuit diagram to interface external program ROM with 8051

1. Step 1: Connect EA pin to ground


2. Step 2: Connect the PSEN to the CE and OE.
3. Step 3: Then, Port 2 (P2.0 – P2.7) to A8 – A12 pins of ext. ROM.
4. Step 4: Connect ALE to G of 74LS373 latch to enable it.
5. Step 5: Next, connect the OC of 74LS373 to GND.
6. Step 6: Connect Port 0 (P0.0 – P0.7), which consists of both address and data
multiplexed into Port 0 to 1D – 8D pins of 74LS373 latch to demultiplex it and 1Q –
8Q of the latch to A0 – A7 of ext. ROM.
7. Step 7: Connect Port 0 (P0.0 – P0.7) to D0 – D7 of the ext. ROM.
8. Step 8: VPP of ext. ROM to VCC.

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

MOVC A, @A+DPTR ; Syntax to mode data from ext. ROM to accumulator

MOV @R0, A ; Copy the value of accumulator in location pointed by R0

INC R0 ; Inc R0 to point to next int RAM location

INC DPTR ; Inc DPTR to point to next ext. ROM location

CJNE A, #00H, rep ; Repeat this process until 0 is received from the DPTR

stay:SJMP stay ; Stay here

END

; Let’s say that the data present at 4000H is

ORG 4000H

DB 1H, 2H, 0AH, 0F2H, 30H, 5CH, 2AH, 01H, 00H, FFH, 0

; Here 0 is the stop bit that we’re assuming

Data Memory Space


To access the data memory space,  we use the instruction MOVX A, @DPTR. Connect
the RD pin (PIN 3.7) to the OE of data ROM and give an active low signal to the Chip
enable (CE) pin of data ROM. Here we access the data from the external ROM containing
the data and transferred to internal RAM.

Circuit diagram to interface external data ROM with 8051


 

Code to interface external data ROM with 8051


This is an assembly language program to read 100 bytes of data from external data ROM
located at 1000H and send it to Port 1 of 8051.
ORG 0000H

MYXDATA EQU 1000H ; 1000H, location where data is stored externally

COUNT EQU 100 ; To receive all 100 bytes of data

MOV DPTR, #MYXDATA ; Move DPTR to 1000H location

MOV R0, #COUNT ; Load R0 with 100

rep: MOVX A, @DPTR ; Copy data from location pointed by DPTR to acc

MOV P1, A ; Move contents of acc to P1

INC DPTR ; Inc DPTR to next ROM location

DJNZ R0, rep ; Repeat until all 100 bytes are received
stay: SJMP stay ; Stay here forever

END

External RAM – For data


The RAM memory is called volatile memory since cutting off the power to the IC will result in
the loss of data. An improvement of the default RAM is RAWM (read and write memory),
which in contrast to ROM, to which we cannot write any data.
There are three types of RAM:

 Static RAM (SRAM)


 NV-RAM (non-volatile RAM)
 Dynamic RAM (DRAM)

We’re going to interface an SRAM chip.


SRAM stand for static random access memory, the SRAM does not require refreshing in order to keep
the data because it is made up of flip-flops. But, the problem with the use of flip-flops for storage cell is
that each cell requires at least six transistors to build, and the cell holds only 1-bit of data

How to access data space?


If we want to connect external data memory, i.e. SRAM, we must connect the RD (Pin 3.7)
and WR (Pin 3.6) to the SRAM data memory. In writing data to external data RAM, we use
the instruction.
MOVX @DPTR, A
We can also use NV-RAM, as it is the most efficient type of RAM whose memory remains
even after the power to the IC is cut off. First off what is NV-RAM? It stands for non-volatile
RAM and is the memory element which combines the best features of both RAM and ROM.
Features of NV-RAM:

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

Circuit diagram to interface external RAM with 8051

1. Step 1: Connect RD to OE of ext. RAM.


2. Step 2: Connect WR to WE of ext. RAM.
3. Step 3: Connect active low input of NAND gate to CE of external RAM, where the
input to NAND gate are address lines A15, A14, and A13. We’ve given 0 1 0 to these
lines to access the 8000H location of the external RAM.
Code to interface external RAM with 8051
This is an assembly language program to send 150 bytes of data or status of Port 2 to
external RAM located at 6000H.
First of all, to access 6000H of the data memory, we have to send 0 1 1 to A15 A14 A13
respectively.
ORG 0000H

RAMLOC EQU 6000H ; External RAM location = 6000H

COUNT EQU 150 ; Variable count = 150d

MOV DPTR, #RAMLOC ; Mov DPTR to point to ext. RAM location

MOV R3, #COUNT ; Number of bytes of data

rep: MOV A, P1 ; Copy status of P2 into acc

MOVX @DPTR, A ; Copy the content of acc to the ext. RAM

ACALL DELAY ; call a delay before repeating the process

INC DPTR ; Inc DPTR to point to next location


DJNZ R3, rep ; Repeat this until all the bytes of data are sent

stay: SJMP stay ; Stay here forever

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.

ADC0804 is an 8 bit successive approximation analogue to digital converter from National


semiconductors. The features of ADC0804 are  differential analogue voltage inputs, 0-5V input
voltage range, no zero adjustment, built in clock generator, reference voltage can be externally
adjusted to convert smaller analogue voltage span to 8 bit resolution etc. The pin out diagram of
ADC0804 is shown in the figure below.
The voltage at Vref/2  (pin9)  of ADC0804 can be externally adjusted  to convert smaller input voltage
spans to full 8 bit resolution. Vref/2 (pin9) left open means input voltage span is 0-5V and step size is
5/255=19.6V. Have a look at the table below for different Vref/2 voltages and corresponding
analogue  input voltage spans.

Vref/2 (pin9)  (volts) Input voltage span (volts) Step size (mV)

Left open 0–5 5/255 = 19.6

2 0–4 4/255 = 15.69

1.5 0–3 3/255 = 11.76

1.28 0 – 2.56 2.56/255 = 10.04

1.0 0–2 2/255 = 7.84

0.5 0–1 1/255 = 3.92


Steps for converting the analogue input   and reading the output from ADC0804.

 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

MOV P1,#11111111B // initiates P1 as the input port

MAIN: CLR P3.7 // makes CS=0

SETB P3.6 // makes RD high

CLR P3.5 // makes WR low

SETB P3.5 // low to high pulse to WR for starting conversion

WAIT: JB P3.4,WAIT // polls until INTR=0

CLR P3.7 // ensures CS=0

CLR P3.6 // high to low pulse to RD for reading the data from ADC

MOV A,P1 // moves the digital data to accumulator

CPL A // complements the digital data (*see the notes)

MOV P0,A // outputs the data to P0 for the LEDs

SJMP MAIN // jumps back to the MAIN program

END
Notes.

 The entire circuit can be powered from 5V DC.


  ADC 0804 has active low outputs and the instruction CPL A  complements it t0 have a straight
forward display. For example, if input is 5V then the output will be 11111111 and if CPL A was not used
it would have been 00000000 which is rather awkward to see.

Interfacing DAC with 8051


Microcontroller
The Digital to Analog converter (DAC) is a device, that is widely used for converting digital pulses to
analog signals. There are two methods of converting digital signals to analog signals. These two methods
are binary weighted method and R/2R ladder method. In this article we will use the MC1408 (DAC0808)
Digital to Analog Converter. This chip uses R/2R ladder method. This method can achieve a much higher
degree of precision. DACs are judged by its resolution. The resolution is a function of the number of
binary inputs. The most common input counts are 8, 10, 12 etc. Number of data inputs decides the
resolution of DAC. So if there are n digital input pin, there are 2 n analog levels. So 8 input DAC has 256
discrete voltage levels.

The MC1408 DAC (or DAC0808)


In this chip the digital inputs are converted to current. The output current is known as Iout by connecting a
resistor to the output to convert into voltage. The total current provided by the  Iout pin is basically a function
of the binary numbers at the input pins D 0 - D7 (D0 is the LSB and  D7 is the MSB) of DAC0808 and the
reference current Iref. The following formula is showing the function of Iout

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.

Generating Sinewave using DAC and 8051 Microcontroller


For generating sinewave, at first we need a look-up table to represent the magnitude of the sine value of
angles between 0° to 360°. The sine function varies from -1 to +1. In the table only integer values are
applicable for DAC input. In this example we will consider 30° increments and calculate the values from
degree to DAC input. We are assuming full-scale voltage of 10V for DAC output. We can follow this
formula to get the voltage ranges.

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

30 0.5 7.5 192

60 0.866 9.33 238

90 1.0 10 255

120 0.866 9.33 238

150 0.5 7.5 192

180 0 5 128

210 -0.5 2.5 64

240 -0.866 0.669 17

270 -1.0 0 0

300 -0.866 0.669 17

330 -0.5 2.5 64

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

repeat: call squarwave ; generate square wave

call triwave ; generate triangular wave

call stairwave ; generate staircase wave

jmp repeat

squarwave: mov P1,#FFH

call delay2sec

mov P1,#00H

call delay2sec

ret

triwave: mov R7,#00H

triwave1: mov P1,R7

inc R7

cjne R7,#FFH,triwave1

mov R7,#FFH

triwave2: mov P1,R7

djnz R7,triwave2

ret

stairwave: mov P1,#00H

call delay2sec

mov P1,#20H

call delay2sec

mov P1,#40H

call delay2sec

mov P1,#80H
call delay2sec

ret

delay1sec: mov r0,#10

del2: mov r1,#250

del1: mov r2,#250

djnz r2,$

djnz r1,del1

djnz r0,del2

ret

delay2sec: mov r0,#20

del22: mov r1,#250

del21: mov r2,#250

djnz r2,$

djnz r1,del21

djnz r0,del22

ret

END
Serial Communication and Bus Interface

You might also like