8051 Microcontroller Unit 4
8051 Microcontroller Unit 4
The 8051 microcontroller was invented in 1980's by Intel. Its foundation is based on Harvard
architecture and this microcontroller was developed principally for bringing it to be used
in Embedded Systems.
At first it was created by using NMOS technology but the use of NMOS consumed more power
to work therefore Intel re-launch the microcontroller 8051 using CMOS technology and new
edition came up with edition of letter 'C' in the title name, therefore the new modified version of
microcontroller is called by name 80C51.
1. Energy Management: In energy management system the measuring device is used for
calculating the energy consumption in industrialized and domestic applications. These systems
are manufactured by integrating the microcontrollers inside their architecture configuration.
2. Automobiles: Microcontroller 8051 is to be used for providing automobile solutions. They are
largely be used in hybrid motor vehicles to control engine variations.
3. Touch screens: The advanced degree of microcontroller integrate the touch sensing ability within
their design .Transportable devices such as cell phones, media players and gaming devices are
some example of microcontroller integrated with touch screens.
4. Medical Devices: Microcontroller is used in various medical devices such as glucose and blood
pressure measurement machine for monitoring and measuring the exact result in real-time
computational environment.
CPU (Central Processing Unit): CPU act as a mind of any processing machine. It synchronizes and
manages all processes that are carried out in microcontroller. User has no power to control the functioning
of CPU. It interprets the program stored in ROM and carries out from storage and then performs it
projected duty. CPU manage the different types of registers available in 8051 microcontroller.
Interrupts: Interrupts is a sub-routine call that given by the microcontroller when some other program
with high priority is request for acquiring the system buses the n interrupts occur in current running
program.
Interrupts provide a method to postpone or delay the current process, performs a sub-routine task and then
restart the standard program again.
Memory: For operation Micro-controller required a program. This program guides the microcontroller to
perform the specific tasks. This program installed in microcontroller required some on chip memory for
the storage of the program.
Microcontroller also required memory for storage of data and operands for the short duration. In
microcontroller 8051 there is code or program memory of 4 KB that is it has 4 KB ROM and it also
comprise of data memory (RAM) of 128 bytes.
Bus : Bus is a group of wires which uses as a communication canal or acts as means of data transfer. The
different bus configuration includes 8, 16 or more cables. Therefore, a bus can bear 8 bits, 16 bits all
together.
Oscillator: As the microcontroller is digital circuit therefore it needs timer for their operation.
To perform timer operation inside microcontroller it required externally connected or on-chip
oscillator. Microcontroller is used inside an embedded system for managing the function of
devices. Therefore, 8051 uses the two 16 bit counters and timers. For the operation of this timers
and counters the oscillator is used inside microcontroller.
8051 Microcontroller Pin Diagram
The 40 pin Dual Inline Package (DIP) of microcontroller integrated circuit with externally
connected power supply and oscillator.
• Pins 1 to 8 − These pins are known as Port 1. This port doesn’t serve any other functions. It is
internally pulled up, bi-directional I/O port.
• Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its initial values.
• Pins 10 to 17 − These pins are known as Port 3. This port serves some functions like interrupts,
timer input, control signals, serial communication signals RxD and TxD, etc.
• Pins 18 & 19 − These pins are used for interfacing an external crystal to get the system clock.
• Pins 21 to 28 − These pins are known as Port 2. It serves as I/O port. Higher order address bus
signals are also multiplexed using this port.
• Pin 29 − This is PSEN pin which stands for Program Store Enable. It is used to read a signal from
the external program memory.
• Pin 30 − This is EA pin which stands for External Access input. It is used to enable/disable the
external memory interfacing.
• Pin 31 − This is ALE pin which stands for Address Latch Enable. It is used to demultiplex the
address-data signal of port.
• Pins 32 to 39 − These pins are known as Port 0. It serves as I/O port. Lower order address and
data bus signals are multiplexed using this port.
TCON Register
TCON register specifies the type of external interrupt to the microcontroller.
8051 Programming in Assembly Language
The assembly language is a low-level programming language used to write program code in terms of
mnemonics. Even though there are many high-level languages that are currently in demand, assembly
programming language is popularly used in many applications. It can be used for direct hardware
manipulations. It is also used to write the 8051 programming code efficiently with less number of clock
cycles by consuming less memory compared to the other high-level languages.
The assembly language is a fully hardware related programming language. The embedded designers must
have sufficient knowledge on hardware of particular processor or controllers before writing the program.
The assembly language is developed by mnemonics; therefore, users cannot understand it easily to
modify the program.
Op-code: The op-code is a single instruction that can be executed by the CPU. Here
the op-code is a MOV instruction.
Operands: The operands are a single piece of data that can be operated by the op-
code. Example, multiplication operation is performed by the operands that are
multiplied by the operand.
• Assembler Directives
• Instruction Set
• Addressing Modes
Assembler Directives:
The assembling directives give the directions to the CPU. The 8051 microcontroller consists of
various kinds of assembly directives to give the direction to the control unit. The most useful
directives are 8051 programming, such as:
• ORG
• DB
• EQU
• END
ORG(origin): This directive indicates the start of the program. This is used to set the register
address during assembly. For example; ORG 0000h tells the compiler all subsequent code
starting at address 0000h.
Syntax: ORG 0000h
DB(define byte): The define byte is used to allow a string of bytes. For example, print the
“EDGEFX” wherein each character is taken by the address and finally prints the “string” by the
DB directly with double quotes.
Syntax:
ORG 0000h
MOV a, #00h
————-
————-
DB”EDGEFX”
EQU (equivalent): The equivalent directive is used to equate address of the variable.
Syntax:
reg equ,09h
—————–
—————–
MOV reg,#2h
END:The END directive is used to indicate the end of the program.
Syntax:
reg equ,09h
—————–
—————–
MOV reg,#2h
END
Addressing Modes:
The way of accessing data is called addressing mode. The CPU can access the data in different
ways by using addressing modes. The 8051 microcontroller consists of five addressing modes
such as:
• Immediate Addressing Mode
• Register Addressing Mode
• Direct Addressing Mode
• Indirect Addressing Mode
• Base Index Addressing Mode
Immediate Addressing Mode:
In this addressing mode, the source must be a value that can be followed by the ‘#’ and
destination must be SFR registers, general purpose registers and address. It is used for
immediately storing the value in the memory registers.
Syntax:
MOV A, #20h //A is an accumulator register, 20 is stored in the A//
MOV R0,#15 // R0 is a general purpose register; 15 is stored in the R0 register//
MOV P0, #07h //P0 is a SFR register;07 is stored in the P0//
MOV 20h,#05h //20h is the address of the register; 05 stored in the 20h//
Ex:
MOV R0, #1
MOV R0, #20 //R0 <—R0[15]+20, the final value is stored in R0//
Register Addressing Mode:
In this addressing mode, the source and destination must be a register, but not general purpose
registers. So the data is not moved within the general purpose bank registers.
Syntax:
MOV A, B; // A is a SFR register, B is a general purpose register//
MOV R0, R1 //Invalid instruction, GPR to GPR not possible//
EX:
MOV R0, #02h
MOV A, #30h
ADD R0, A //R0<—R0+A, the final value is stored in the R0 register//
Syntax:
MOV A,20h // 20h is an address; A is a register//
MOV 00h, 07h // both are addressed of the GPS registers//
Ex:
MOV 07h,#01h
MOV A, #08h
ADD A,07h //A<—A+07h the final value is stored in A//
Indirect Addressing Mode:
In this addressing mode, the source or destination (or destination or source) must be a indirect
address, but not a value. This addressing mode supports the pointer concept. The pointer is a
variable that is used to store the address of the other variable. This pointer concept is only used
for R0 and R1 registers.
Syntax:
MOVR0, #01h //01 value is stored in the R0 register, R0 address is 08h//
MOV R1, #08h//R1 is the pointer variable that stores address (08h) of R0 //
MOV 20h,@R1 //01 value is stored in the 20h address of the GP register//
Instruction Set:
The instruction set is the structure of the controller or processor that provides commands to the
controller to guide the controller for processing data. The instruction set consists of instructions,
native data types, addressing modes, interrupt registers, exceptional handling and memory
architecture. The 8051 microcontroller can follow CISC instructions with Harvard architecture.
In case of the 8051 programming different types of CISC instructions include:
• Data Transfer Instruction set
• Sequential Instruction Set
• Arithmetic Instruction set
• Branching Instruction set
• Loop Instrcution Set
• Conditional Instruction set
• Unconditional Instruction set
• Logical Instruction set
• Boolean Instruction set
• Addition
• Multiplication
• Subtraction
• Division
Addition:
ORG 0000h
MOV R0, #03H // move the value 3 to the register R0//
MOV A, #05H // move the value 5 to accumulator A//
Add A, 00H // addA value with R0 value and stores the result inA//
END
Multiplication:
ORG 0000h
MOV R0, #03H // move the value 3 to the register R0//
MOV A, #05H // move the value 5 to accumulator A//
MUL A, 03H // Multiplied result is stored in the Accumulator A //
END
Subtraction:
ORG 0000h
MOV R0, #03H // move the value 3 to register R0//
MOV A, #05H // move the value 5 to accumulator A//
SUBB A, 03H // Result value is stored in the Accumulator A //
END
Division:
ORG 0000h
MOV R0, #03H // move the value 3 to register R0//
MOV A, #15H // move the value 5 to accumulator A//
DIV A, 03H // final value is stored in the Accumulator A //
END
Conditional Instructions
The CPU executes the instructions based on the condition by checking the single bit status or
byte status. The 8051 microcontroller consists of various conditional instructions such as:
• JB —>Jump below
• JNB —> Jump if not below
• JC —> Jump if Carry
• JNC —>Jump if not Carry
• JZ —>Jump if Zero
• JNZ —> Jump if not Zero
Conditional Instructions
1. Syntax:
JB P1.0, label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
2. Syntax:
JNB P1.0, label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
3. Syntax:
JC, label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
4. Syntax:
JNC, label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
5. Syntax:
JZ, label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
6. Syntax:
JNZ, label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
The call and jump instructions are used to avoid the code replication of the program. When some
specific code used more than once in different places in the program, if we mention specific
name to code then we could use that name anywhere in the program without entering a code for
every time. This reduces the complexity of the program. The 8051 programming consists of call
and jump instructions such as LCALL, SJMP.
• LCALL
• ACALL
• SJMP
• LJMP
1. Syntax:
ORG 0000h
––––––––
––––––––
ACALL, label
––––––––
––––––––
SJMP STOP
Label: – – – – – – – –
––––––––
––––––––
ret
STOP:NOP
2. Syntax:
ORG 0000h
––––––––
––––––––
LCALL, label
––––––––
––––––––
SJMP STOP
Label: – – – – – – – –
––––––––
––––––––
ret
STOP:NOP
Call and Jump Instructions
Loop Instructions:
The loop instructions are used to repeat the block each time while performing the increment and
decrement operations. The 8051 microcontroller consist two types of loop instructions:
• CJNE —> compare and jump if not equal
• DJNZ —> decrement and jump if not zero
1. Syntax:
of CJNE
MOV A, #00H
MOV B, #10H
Label:INC A
––––––
––––––
CJNE A, label
2. Syntax:
of DJNE
MOV R0, #10H
Label:– – – – – –
––––––
DJNE R0, label
––––––
––––––
END
Logical Instruction Set:
The 8051 microcontroller instruction set provides the AND, OR, XOR, TEST, NOT and Boolean
logic instructions for set and clears the bits based on the need in the program.
Logical Instruction Set
1. Syntax:
MOV A, #20H /00100000/
MOV R0, #03H /00000101/
ORL A, R0 //00100000/00000101=00000000//
2. Syntax:
MOV A, #20H /00100000/
MOV R0, #03H /00000101/
ANL A, R0
3. Syntax:
MOV A, #20H /00100000/
MOV R0, #03H /00000101/
XRL A, R0
Shifting Operators
The shift operators are used for sending and receiving the data efficiently. The
8051 microcontroller consist four shift operators:
• RR —> Rotate Right
• RRC —>Rotate Right through carry
• RL —> Rotate Left
• RLC —>Rotate Left through carry
Rotate Right (RR):
In this shifting operation, the MSB becomes LSB and all bits shift towards right side bit-by-bit,
serially.
Syntax:
MOV A, #25h
RR A
Syntax:
MOV A, #25h
RL A
Syntax:
MOV A, #27h
RRC A
Syntax:
MOV A, #27h
RLC A
Basic Embedded C Programs:
The microcontroller programming differs for each type of operating system. There are many
operating systems such as Linux, Windows, RTOS and so on. However, RTOS has several
advantages for embedded system development. Some of the Assembly level programming
examples are given below.
LED blinking using with 8051 microcontroller:
• Number Displaying on 7-segment display using 8051 microcontroller
• Timer/Counter calculations and program using 8051 microcontroller
• Serial Communication calculations and program using 8051 microcontroller
LED programs with 8051 Microcontrller
1. WAP to toggle the PORT1 LEDs
ORG 0000H
TOGLE: MOV P1, #01 //move 00000001 to the p1 register//
CALL DELAY //execute the delay//
MOV A, P1 //move p1 value to the accumulator//
CPL A //complement A value //
MOV P1, A //move 11111110 to the port1 register//
CALL DELAY //execute the delay//
SJMP TOGLE
DELAY: MOV R5, #10H //load register R5 with 10//
TWO: MOV R6, #200 //load register R6 with 200//
ONE: MOV R7, #200 //load register R7 with 200//
DJNZ R7, $ //decrement R7 till it is zero//
DJNZ R6, ONE //decrement R7 till it is zero//
DJNZ R5, TWO //decrement R7 till it is zero//
RET //go back to the main program //
END
Stepper Motor
Stepper motors are used to translate electrical pulses into mechanical movements. In some disk
drives, dot matrix printers, and some other different places the stepper motors are used. The
main advantage of using the stepper motor is the position control. Stepper motors generally have
a permanent magnet shaft (rotor), and it is surrounded by a stator.
Normal motor shafts can move freely but the stepper motor shafts move in fixed repeatable
increments.
1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1
• Full Drive Mode − In this mode, two coils are energized at the same time. This mode
produces more torque. Here the power consumption is also high
The following table is showing the sequence of input states in different windings.
1 1 1 0 0
2 0 1 1 0
Steps Winding Winding Winding Winding
A B C D
3 0 0 1 1
4 1 0 0 1
• Half Drive Mode − In this mode, one and two coils are energized alternately. At first,
one coil is energized then two coils are energized. This is basically a combination of wave
and full drive mode. It increases the angular rotation of the motor
The following table is showing the sequence of input states in different windings.
1 1 0 0 0
2 1 1 0 0
3 0 1 0 0
4 0 1 1 0
5 0 0 1 0
6 0 0 1 1
7 0 0 0 1
8 1 0 0 1
The circuit diagram is like below: We are using the full drive mode.
The circuit diagram of stepper motor interfacing
UNIT - 5
8051 INTERFACING ANDAPPLICATIONS
Interfacing of 8051with: Analog Sensors, Keypad & LCD display, ADC, DAC,
DC motor.
LCD INTERFACING:
➢ We also use RS=0 to check the busy flag bit to see if the LCD is ready to receive
information’s.
➢ The busy flag id D7 and can be read when R/W=1 and RS=0, as follows: if R/W=1,
RS=0.
➢ When D7=1 (busy flag =1), the LCD is busy taking care of internal operations and will
not accept any new information.
LED+ & LED-:
➢ 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.
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.
o Send 38H to the 8 bit data line for initialization
o Send 0FH for making LCD ON, cursor ON and cursor blinking ON.
o Send 06H for incrementing cursor position.
o Send 80H for displaying the character from 1st row and 1st column in LCD
o Send 01H for clearing the display and return the cursor.
ORG 0
MOV A,#38H ;INIT. LCD 2 LINES, 5X7 MATRIX
ACALL COMNWRT ;call command subroutine
MOV A,#0EH ;display on, cursor on
ACALL COMNWRT ;call command subroutine
MOV A,#01 ;clear LCD
ACALL COMNWRT ;call command subroutine
MOV A,#06H ;shift cursor right
ACALL COMNWRT ;call command subroutine
MOV A,#84H ;cursor at line 1, pos. 4
ACALL COMNWRT ;call command subroutine
MOV A,#’N’ ;display letter N
ACALL DATAWRT ;call display subroutine
MOV A,#’O’ ;display letter O
ACALL DATAWRT ;call display subroutine
AGAIN: SJMP AGAIN ;stay here
READY:
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
;read command reg and check busy flag
BACK: SETB P2.2 ;E=1 for H-to-L pulse
CLR P2.2 ;E=0 H-to-L pulse
JB P1.7,BACK ;stay until busy flag=0
RET
END
LCD Interfacing Using MOVC Instruction:
ORG 0
MOV DPTR,#MYCOM
C1: CLR A
MOVC A,@A+DPTR
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
INC DPTR
JZ SEND_DAT
SJMP C1
SEND_DAT:
MOV DPTR,#MYDATA
D1: CLR A
MOVC A,@A+DPTR
ACALL DATAWRT ;call command subroutine
ACALL DELAY ;give LCD some time
INC DPTR
JZ AGAIN
SJMP D1
AGAIN: SJMP AGAIN
ORG 300H
MYCOM: DB 38H,0EH,01,06,84H,0 ; commands and null
MYDATA: DB “HELLO”,0
END
KEYBOARD INTERFACING:
➢ At the lowest level, keyboards are organized in a matrix of rows and columns.
➢ The CPU accesses both rows and columns through ports; therefore, with two 8-bit ports,
an 8 x 8 matrix of keys can be connected to a microprocessor.
➢ When a key is pressed, a row and a column make a contact; otherwise, there is no
connection between rows and columns
Scanning and identifying the key
➢ Figure 5.3 shows a 4 x4 matrix connected to two ports.
➢ The rows are connected to an output port and the columns are connected to an input
port.
➢ If no key has been pressed, reading the input port will yield 1s for all columns since they
are all connected to high (Vcc).
➢ If all the rows are grounded and a key is pressed, one of the columns will have 0 since
the key pressed provides the path to ground.
➢ It is the function of the microcontroller to scan the keyboard continuously to detect and
identify the key pressed, How it is done is explained next.
Figure 5.3 Matrix Keyboard Connection to Ports
Grounding rows and reading the columns
➢ 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 the columns is D3 - D0 =1111, no key has been pressed and the
process continues until a key press is detected.
➢ However, if one of the column bits has a zero, this means that a key press has occurred.
➢ For example, if D3 - D0 = 1101, this means that a key in the D1 column has been
pressed.
➢ After a key press is detected, the microcontroller will go through the process of
identifying the key.
➢ Starting with the top row, the microcontroller grounds it by providing a low to row D0
only; then it reads the columns.
➢ If the data read is all 1s, no key in that row is activated and the process is moved to the
next row.
➢ 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, the next task is to find
out which column the pressed key belongs to.
➢ This should be easy since the microcontroller knows at any time which row and column
are being accessed.
➢ Given keyboard program is the 8051 Assembly language program for detection and
identification of key activation.
➢ In this program, it is assumed that P1 and P2 are initialized as output and input,
respectively.
➢ Program goes through the following four major stages:
o 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.
o 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 latches
connected to rows still have their initial zeros (provided in stage 1), making them
grounded. After the key press detection, the microcontroller waits 20 ms for the
bounce and then scans the columns again. This serves two functions: (a) it
ensures that the first key press detection was not an erroneous one due to a
spike noise, and (b) the 20-ms delay prevents the same key press from being
interpreted as a multiple key press. If after the 20-ms delay the key is still
pressed, it goes to the next stage to detect which row it belongs to; otherwise, it
goes back into the loop to detect a real key press.
o To detect which row the key press belongs to, the microcontroller 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 cannot belong to that row; therefore, it grounds the
next row and continues until it finds the row the key press belongs to. Upon
finding the row that the key press belongs to, it sets up the starting address for
the look-up table holding the scan codes (or the ASCII value) for that row and
goes to the next stage to identify the key.
o To identify the key press, the microcontroller 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. Figure
5.4 flowcharts this process.
➢ While the key press detection is standard for all keyboards, the process for determining
which key is pressed varies.
➢ The look-up table method shown in keyboard Program can be modified to work with any
matrix up t0 8 x 8.
➢ Figure 5.4 provides the flowchart for keyboard interfacing Program for scanning and
identifying the pressed key.
Figure 5.4 Flowchart for Programming Keyboard Interfacing
Program:
;keyboard subroutine. This program sends the ASCII
;code for pressed key to P0.1
;P1.0-P1.3 connected to rows, P2.0-P2.3 to column
MOV P2,#0FFH ;make P2 an input port
K1: MOV P1,#0 ;ground all rows at once
MOV A,P2 ;read all col
;(ensure keys open)
ANL A,00001111B ;masked unused bits
CJNE A,#00001111B,K1 ;till all keys release
K2: ACALL DELAY ;call 20 msec delay
MOV A,P2 ;see if any key is pressed
ANL A,00001111B ;mask unused bits
CJNE A,#00001111B,OVER ;key pressed, find row
SJMP K2 ;check till key pressed
OVER: ACALL DELAY ;wait 20 msec debounce time
MOV A,P2 ;check key closure
ANL A,00001111B ;mask unused bits
CJNE A,#00001111B,OVER1;key pressed, find row
SJMP K2 ;if none, keep polling
OVER1: MOV P1, #11111110B ;ground row 0
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_0 ;key row 0, find col.
MOV P1,#11111101B ;ground row 1
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_1 ;key row 1, find col.
MOV P1,#11111011B ;ground row 2
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_2 ;key row 2, find col.
MOV P1,#11110111B ;ground row 3
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_3 ;key row 3, find col.
LJMP K2 ;if none, false input,
;repeat
ROW_0: MOV DPTR,#KCODE0 ;set DPTR=start of row 0
SJMP FIND ;find col. Key belongs to
ROW_1: MOV DPTR,#KCODE1 ;set DPTR=start of row
SJMP FIND ;find col. Key belongs to
ROW_2: MOV DPTR,#KCODE2 ;set DPTR=start of row 2
SJMP FIND ;find col. Key belongs to
ROW_3: MOV DPTR,#KCODE3 ;set DPTR=start of row 3
FIND: RRC A ;see if any CY bit low
JNC MATCH ;if zero, get ASCII code
INC DPTR ;point to next col. addr
SJMP FIND ;keep searching
MATCH: CLR A ;set A=0 (match is found)
MOVC A,@A+DPTR ;get ASCII from table
MOV P0,A ;display pressed key
LJMP K1
;ASCII LOOK-UP TABLE FOR EACH ROW
ORG 300H
KCODE0: DB ‘0’,’1’,’2’,’3’ ;ROW 0
KCODE1: DB ‘4’,’5’,’6’,’7’ ;ROW 1
KCODE2: DB ‘8’,’9’,’A’,’B’ ;ROW 2
KCODE3: DB ‘C’,’D’,’E’,’F’ ;ROW 3
END
➢ D0-D7:
• D0-D7 are the digital data output pins.
• These are tri-state buffered and the converted data is accessed only when CS =0
and RD is forced low.
• To calculate the output voltage, use the following formula
Figure 5.8 8051 Connection to ADC0804 with Clock from XTAL2 of the 8051
Example:
Write a program to monitor the INTR pin and bring an analog input into register A. Then call a
hex-to ACSII conversion and data display subroutines. Do this continuously.
ADC0808:
➢ While the ADC0804 has only one analog input, this chip has 8 of them.
➢ The ADC0808/0809 chip allows us to monitor up to 8 different analog inputs using only a
single chip.
➢ Notice that the ADC0808/0809 has an 8-bit data output just like the ADC804.
➢ The 8 analog input channels are multiplexed and selected according to Table 5.5 using
three address pins, A, B, and C.
Table 5.5 Channel Selection in ADC0808
➢ In the ADC0808/0809, Vref (+) and Vref.(-) set the reference voltage.
➢ If Vref(-) = Gnd and Vref (+) = 5 V, the step size is 5 V/256 = 19.53 mV.
➢ Therefore, to get a l0 mV step size we need to set Vref (+) = 2.56 V and Vref.(-) = Gnd.
➢ From Figure 5.9, notice the ALE pin.
➢ We use A, B, and C addresses to select.IN0 - IN7, and activate ALE to latch in the
address.
➢ SC is for start conversion.
➢ SC is the same as the WR pin in other ADC chips.
➢ EOC is for end-of-conversion, and OE is for output enable (READ).
➢ The EOC and OE are the same as the INTR and RD pins respectively.
➢ Table 5.6 shows the step size relation to the Vref voltage.
➢ Notice that there is no Vref/2 in the ADC0808/0809 chip.
Figure 5.9 ADC0808/0809
➢ The sensors of the LM34 series are precision integrated-circuit temperature sensors
whose output voltage is linearly proportional to the Fahrenheit temperature.
➢ It also internally calibrated.
➢ It outputs 10mV for each degree Fahrenheit temperature.
Signal Conditioning and Interfacing the LM35 to the 8051
➢ The above figure 5.10 shows the steps involved in acquiring data from analog world.
➢ Signal conditioning is widely used in the world of data acquisition.
➢ The most common transducers produce an output in the form of voltage, current,
charge, capacitance, and resistance.
➢ However, we need to convert these signals to voltage in order to send input to an A-to-D
converter.
➢ This conversion (modification) is commonly called signal conditioning.
➢ Signal conditioning can be a current-to-voltage conversion or a signal amplification.
➢ For example, the thermistor changes resistance with temperature.
➢ The change of resistance must be translated into voltages in order to be of any use to an
ADC.
➢ Look at the case of connecting an LM35 to an ADC0848.
➢ Since the ADC0848 has 8-bit resolution with a maximum of 256 (28) steps and the LM35
(or LM34) produces l0 mV for every degree of temperature change, we can condition Vin
of the ADC0848 to produce a Vout, of 2560 mV (2.56 V) for full-scale output.
➢ Therefore, in order to produce the full-scale Vout of 2.56 V for the ADC0848, we need to
set Vref = 2.56.
➢ This makes Vout, of the ADC0848 correspond directly to the temperature as monitored by
the LM35. Refer the table 5.8
Table 5.8 Temperature vs. Vout for ADC0848
CONVERSION:
MOV B,#10
DIV AB
MOV R7,B
MOV B,#10
DIV AB
MOV R6,B
MOV R5,A
RET
DATA_DISPLAY:
MOV P0,R7
ACALL DELAY
MOV P0,R6
ACALL DELAY
MOV P0,R5
ACALL DELAY
RET
DAC0808:
➢ The digital inputs are converter to current (Iout), and by connecting a resistor to the Iout
pin, we can convert the result to voltage.
➢ The total current provided by the Iout pin is a function of the binary numbers at the D0-D7
inputs of the DAC0808 and the reference current (Iref), and is as follows