0% found this document useful (0 votes)
26 views

8051 Microcontroller Unit 4

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)
26 views

8051 Microcontroller Unit 4

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
You are on page 1/ 49

8051 Microcontroller

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.

The 8051 microcontroller programming is performed in embedded C language using Keil


software.

Features of 8051 Microcontroller:


o It having four register banks
o 64K bytes on-chip programmable memory (ROM)
o 128 bytes on-chip data memory (RAM)
o Address bus is 16-bit unidirectional
o Data bus is 8-bit bidirectional
o 128 user defined flags
o 16 bit timers
o 32 general purpose registers each of 8-bit
o 8051 microcontroller offers a number of special features such as ADC, UARTs, Op-amp,
etc.

Application of 8051 Microcontroller


The 8051 microcontroller applications include a large amount of machines because it is used for
incorporating inside a project or to assemble a machine using it.

Let's see the major applications of 8051 Microcontroller:

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.

8051 Microcontroller Architecture


The internal architecture of 8051 Microcontroller represented in form of block diagram as shown
below:

Basic components present internally inside 8051 Microcontroller architecture are:

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.

Types of interrupt in 8051 Microcontroller:

Let's see the five sources of interrupts in 8051 Microcontroller:

o Timer 0 overflow interrupt - TF0

o Timer 1 overflow interrupt - TF1

o External hardware interrupt - INT0

o External hardware interrupt - INT1

o Serial communication interrupt - RI/TI

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.

Types of buses in 8051 Microcontroller:

The two types of bus used in 8051 microcontroller:

o Address Bus: 8051 microcontrollers is consisting of 16 bit address bus. It is generally be


used for transferring the data from Central Processing Unit to Memory.
o Data bus: 8051 microcontroller is consisting of 8 bits data bus. It is generally be used for
transferring the data from one peripherals position to other peripherals.

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.

• Pin 20 − This pin provides the power supply to the circuit.

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

• Pin 40 − This pin is used to provide power supply to the circuit.

Microcontrollers 8051 Input Output Ports


8051 microcontrollers have 4 I/O ports each of 8-bit, which can be configured as input or output.
Hence, total 32 input/output pins allow the microcontroller to be connected with the peripheral
devices.
• Pin configuration, i.e. the pin can be configured as 1 for input and 0 for output as per the
logic state.
o Input/Output (I/O) pin − All the circuits within the microcontroller must be
connected to one of its pins except P0 port because it does not have pull-up
resistors built-in.
o Input pin − Logic 1 is applied to a bit of the P register. The output FE transistor
is turned off and the other pin remains connected to the power supply voltage over
a pull-up resistor of high resistance.
• Port 0 − The P0 (zero) port is characterized by two functions −
o When the external memory is used then the lower address byte (addresses A0A7)
is applied on it, else all bits of this port are configured as input/output.
o When P0 port is configured as an output then other ports consisting of pins with
built-in pull-up resistor connected by its end to 5V power supply, the pins of this
port have this resistor left out.
Input Configuration
If any pin of this port is configured as an input, then it acts as if it “floats”, i.e. the input has
unlimited input resistance and in-determined potential.
Output Configuration
When the pin is configured as an output, then it acts as an “open drain”. By applying logic 0 to a
port bit, the appropriate pin will be connected to ground (0V), and applying logic 1, the external
output will keep on “floating”.
In order to apply logic 1 (5V) on this output pin, it is necessary to build an external pullup
resistor.
Port 1
P1 is a true I/O port as it doesn’t have any alternative functions as in P0, but this port can be
configured as general I/O only. It has a built-in pull-up resistor and is completely compatible
with TTL circuits.
Port 2
P2 is similar to P0 when the external memory is used. Pins of this port occupy addresses
intended for the external memory chip. This port can be used for higher address byte with
addresses A8-A15. When no memory is added then this port can be used as a general
input/output port similar to Port 1.
Port 3
In this port, functions are similar to other ports except that the logic 1 must be applied to
appropriate bit of the P3 register.

Pins Current Limitations


• When pins are configured as an output (i.e. logic 0), then the single port pins can receive
a current of 10mA.
• When these pins are configured as inputs (i.e. logic 1), then built-in pull-up resistors
provide very weak current, but can activate up to 4 TTL inputs of LS series.
• If all 8 bits of a port are active, then the total current must be limited to 15mA (port P0:
26mA).
• If all ports (32 bits) are active, then the total maximum current must be limited to 71mA.

Microcontrollers - 8051 Interrupts


Interrupts are the events that temporarily suspend the main program, pass the control to the
external sources and execute their task. It then passes the control to the main program where it
had left off.
8051 has 5 interrupt signals, i.e. INT0, TFO, INT1, TF1, RI/TI. Each interrupt can be enabled or
disabled by setting bits of the IE register and the whole interrupt system can be disabled by
clearing the EA bit of the same register.

IE (Interrupt Enable) Register


This register is responsible for enabling and disabling the interrupt. EA register is set to one for
enabling interrupts and set to 0 for disabling the interrupts. Its bit sequence and their meanings
are shown in the following figure.
It disables all interrupts. When EA = 0 no interrupt will be
EA IE.7
acknowledged and EA = 1 enables the interrupt individually.

- IE.6 Reserved for future use.

- IE.5 Reserved for future use.

ES IE.4 Enables/disables serial port interrupt.

ET1 IE.3 Enables/disables timer1 overflow interrupt.

EX1 IE.2 Enables/disables external interrupt1.

ET0 IE.1 Enables/disables timer0 overflow interrupt.

EX0 IE.0 Enables/disables external interrupt0.

IP (Interrupt Priority) Register


We can change the priority levels of the interrupts by changing the corresponding bit in the
Interrupt Priority (IP) register as shown in the following figure.
• A low priority interrupt can only be interrupted by the high priority interrupt, but not
interrupted by another low priority interrupt.
• If two interrupts of different priority levels are received simultaneously, the request of
higher priority level is served.
• If the requests of the same priority levels are received simultaneously, then the internal
polling sequence determines which request is to be serviced.

- IP.6 Reserved for future use.


- IP.5 Reserved for future use.

PS IP.4 It defines the serial port interrupt priority level.

PT1 IP.3 It defines the timer interrupt of 1 priority.

PX1 IP.2 It defines the external interrupt priority level.

PT0 IP.1 It defines the timer0 interrupt priority level.

PX0 IP.0 It defines the external interrupt of 0 priority level.

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.

8051 Programming in Assembly Language


Assembly programming language is developed by various compilers and the “keiluvison” is best suitable
for microcontroller programming development. Microcontrollers or processors can understand only
binary language in the form of ‘0s or 1s’; An assembler converts the assembly language to binary
language, and then stores it in the microcontroller memory to perform the specific task.

8051 Microcontroller Programs in Assembly Language


The assembly language is made up of elements which all are used to write the
program in sequential manner. Follow the given rules to write programming in
assembly language.
Rules of Assembly Language
• The assembly code must be written in upper case letters
• The labels must be followed by a colon (label:)
• All symbols and labels must begin with a letter
• All comments are typed in lower case
• The last line of the program must be the END directive
The assembly language mnemonics are in the form of op-code, such as MOV, ADD,
JMP, and so on, which are used to perform the operations.

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.

Syntax: MUL a,b;

The Elements of an Assembly Language Programming:

• 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//

Direct Addressing Mode


In this addressing mode, the source or destination (or both source and destination) must be an
address, but not value.

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//

Indirect Addressing Mode

Base Index Addressing Mode:


This addressing mode is used to read the data from the external memory or ROM memory. All
addressing modes cannot read the data from the code memory. The code must read through the
DPTR register. The DPTR is used to point the data in the code or external memory.
Syntax:
MOVC A, @A+DPTR //C indicates code memory//
MOCX A, @A+DPTR // X indicate external memory//
EX: MOV A, #00H //00H is stored in the A register//
MOV DPTR, #0500H //DPTR points 0500h address in the memory//
MOVC A, @A+DPTR //send the value to the A register//
MOV P0, A //date of A send to the PO registrar//

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

Arithmetic Instruction Set:

The arithmetic instructions perform the basic operations such as:

• 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

Call and Jump Instructions:

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

Rotate Left (RL):


In this shifting operation, the MSB becomes LSB and all bits shift towards Left side bit-by-bit,
serially.

Syntax:
MOV A, #25h
RL A

RRC Rotate Right through Carry:


In this shifting operation, the LSB moves to carry and the carry becomes MSB, and all the bits
are shift towards right side bit by bit position.

Syntax:
MOV A, #27h
RRC A

RLC Rotate Left through Carry:


In this shifting operation, the MSB moves to carry and the carry becomes LSB and all the bits
shift towards left side in a bit-by-bit position.

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

Timer/Counter Calculations and Program using 8051 Microcontroller:


The delay is the one of the important factors in the application software development. The timers
and counters are hardware components of the microcontroller, that are used in many applications
to provide the accurate time delay with count pulses. Both the tasks are implemented by the
software technique.
1. WAP to calculate the 500us time delay.
MOV TMOD, #10H //select the timer mode by the registers//
MOV TH1, #0FEH // store the delay time in higher bit//
MOV TL1, #32H // store the delay time in low bit//
JNB TF1, $ //decrement the value of the timer till it is zero//
CLR TF1 //clear the timer flag bit//
CLR TR1 //OFF the timer//
2. WAP to toggle the LEDs withthe 5 sec time delay
ORG 0000H
RETURN: MOV PO, #00H
ACALL DELAY
MOV P0, #0FFH
ACALL DELAY
SJUMP RETURN
DELAY: MOV R5, #50H //load register R5 with 50//
DELAY1: MOV R6, #200 //load register R6 with 200//
DELAY2: MOV R7, #229 //load register R7 with 200//
DJNZ R7, $ //decrement R7 till it is zero//
DJNZ R6, DELAY2//decrement R6 till it is zero//
DJNZ R5, DELAY1//decrement R5 till it is zero//
RET //go back to the main program //
END

3. WAP to count the 250 pulses using mode0 count0


Syntax:
ORG 0000H
MOV TMOD, #50H //select the counter//
MOV TH0, #15 //move the counting pulses higher bit//
MOV TH1, #9FH //move the counting pulses, lower bit//
SET TR0 //ON the timer//
JNB $ //decrement the count value till zero//
CLR TF0 //clear the counter, flag bit//
CLR TR0 //stop the timer//
END

Serial Communication Programming Using 8051 Microcontroller:


Serial communication is commonly used for transmitting and receiving the data. The
8051 microcontroller consist of UART/USART serial communication and the signals are
transmitted and received by Tx and Rx pins. The UART communication transfers the data bit-
by-bit serially. The UART is a half-duplex protocol that transfers and receives the data, but not at
the same time.
1. WAP to transmit the characters to the Hyper Terminal
MOV SCON, #50H //set the serial communication//
MOV TMOD, #20H //select the timer mode//
MOV TH1, #-3 //set the baud rate//
SET TR1 //ON the timer//
MOV SBUF, #’S’ //transmit S to the serial window //
JNB TI, $ //decrement value of the timer till it is zero//
CLR RI // clear receive interrupt //
CLR TR1 //clear timer//

2. WAP to transmit the Receive the character by the Hyper Terminal


MOV SCON, #50H //set the serial communication//
MOV TMOD, #20H //select the timer mode//
MOV TH1, #-6 //set the baud rate//
SET TR1 //on the timer//
MOV SBUF, #’S’ //transmit S to the serial window //
JNB RI, $ //decrement value of timer till it is zero//
CLR RI // clear receive interrupt //
MOV P0, SBUF //send the SBUF register value to the port0//
CLR TR1 //clear timer//

Interfacing Stepper Motor with 8051Microcontroller

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.

Some parameters of stepper motors −


• Step Angle − The step angle is the angle in which the rotor moves when one pulse is
applied as an input of the stator. This parameter is used to determine the positioning of a
stepper motor.
• Steps per Revolution − This is the number of step angles required for a complete
revolution. So the formula is 360° /Step Angle.
• Steps per Second − This parameter is used to measure a number of steps covered in
each second.
• RPM − The RPM is the Revolution Per Minute. It measures the frequency of rotation.
By this parameter, we can measure the number of rotations in one minute.
The relation between RPM, steps per revolution, and steps per second is like below:
Steps per Second = rpm x steps per revolution / 60

Interfacing Stepper Motor with 8051 Microcontroller


We are using Port P0 of 8051 for connecting the stepper motor. Here ULN2003 is used. This is
basically a high voltage, high current Darlington transistor array. Each ULN2003 has seven
NPN Darlington pairs. It can provide high voltage output with common cathode clamp diodes
for switching inductive loads.
The Unipolar stepper motor works in three modes.
• Wave Drive Mode − In this mode, one coil is energized at a time. So all four coils are
energized one after another. This mode produces less torque than full step drive mode.
The following table is showing the sequence of input states in different windings.

Steps Winding Winding Winding Winding


A B C D

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.

Steps Winding Winding Winding Winding


A B C D

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.

Steps Winding Winding Winding Winding


A B C D

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:

Figure 5.1 16X2 LCD Module


➢ 16×2 LCD module is a very common type of LCD module.
➢ It consists of 16 rows and 2 columns of 5×7 or 5×8 LCD dot matrices.
➢ 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 5.1.
Table 5.1 LCD Pin Description
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
VCC,VSS & VEE Pin:
➢ VCC and VSS provide +5V and Ground
➢ 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. (Refer Figure
5.2)
RS:
➢ LCD 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.
➢ 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:
➢ 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.
Enable (E):
➢ E pin is for enabling the module.
➢ The enable pin is used by the LCD to latch information presented to its data pins.
➢ When data is supplied to data pins, a high to low pulse must be applied to this pin in
order for the LCD to latch in the data present at the data pins.
➢ This pulse must be a minimum of 450ns wide.
Data Pin:
➢ The 8-bit data pins, DB0 to DB7 are used to send information to the LCD or read the
contents of the LCD’s internal register.
➢ To display letters and numbers, send ASCII codes for the letters A-Z; a-z and numbers
0-9 to these pins while making RS=1.
➢ There are also instruction command codes that can be sent to the LCD to clear the
display or force the cursor to the home position or blink the cursor.
➢ Table 5.2 Lists the instructions command codes.

Table 5.2 LCD Command Codes


CODE(Hexa Decimal) COMMAND
01 Clear display screen
02 Return Home
04 Decrement cursor (shift cursor to left)
05 Increment cursor (shift cursor to right)
06 shift display right
07 shift display left
08 Display off, cursor off
0A Display off, cursor on
0C Display on, cursor off
0E Display on, cursor blinking
0F Display on, cursor blinking
10 Shift cursor position to left
14 Shift cursor position to right
18 Shift the entire display to the left
1C Shift the entire display to the right
80 Force cursor to the beginning of 1st line
C0 Force cursor to the beginning of 2nd line
38 2 lines and 5 x 7 matrix

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

Figure 5.2 LCD Interfacing With 8051

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.

Sending data to the LCD.


➢ The steps for sending data to the LCD module is given below.
➢ It is the logic state of the pins (RS, R/W and E) that make the module to determine
whether a given data input is a command or data to be displayed.
o Make R/W low.
o Make RS=0 if data byte is a command and make RS=1 if the data byte is a data
to be displayed.
o Place data byte on the data register.
o Pulse E from high to low.
o Repeat above steps for sending another data
Program:
;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 0
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
ACALL DELAY
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
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
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

Check Busy Flag:


➢ The above code showed how to send command to the LCD without checking the busy
flag.
➢ Notice that we put a long delay between issuing data or commands to the LCD.
➢ However a much better way is to monitor the busy flag before issuing a command or
data to the LCD. This is shown in below program

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

COMNWRT: ACALL READY ;send command to LCD if LCD is ready


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
ACALL DELAY
CLR P2.2 ;E=0 for H-to-L pulse
RET

DATAWRT: ACALL READY ;write data to LCD if LCD is ready


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
ACALL DELAY
CLR P2.2 ;E=0 for H-to-L pulse
RET

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

ANALOG-TO-DIGITAL CONVERTER (ADC) INTERFACING:


➢ ADCs (analog-to-digital converters)are among the most widely used devices for data
acquisition.
➢ A physical quantity, like temperature, pressure, humidity, and velocity, etc., is converted
to electrical (voltage, current)signals using a device called a transducer or sensor
➢ We need an analog-to-digital converter to translate the analog signals to digital numbers,
so microcontroller can read and process them.
➢ An ADC has n-bit resolution where n can be 8, 10, 12, 16 or even 24 bits.
➢ The higher-resolution ADC provides a smaller step size, where step size is the smallest
change that can be discerned by an ADC. This is shown in table 5.3
Table 5.3 Resolution Vs Step Size for ADC

➢ In addition to resolution, conversion time is another major factor in judging an ADC.


➢ Conversion time is defined as the time it takes the ADC to convert the analog input to a
digital (binary) number.
➢ The ADC chips are either parallel or serial.
➢ In parallel ADC, we have 8 of more pins dedicated to bringing out the binary data, but in
serial ADC we have only one pin for data out.
ADC804 chip:
➢ ADC804 IC is an 8-bit parallel analog-to-digital converter.
➢ It works with +5 volts and has a resolution of 8bits.
➢ In ADC804 conversion time varies depending on the clocking signals applied to the CLK
R and CLK IN pins, but it cannot be faster than 110μs.
➢ Figure 5.5 Pin out of ADC0804 in free running mode.
➢ The following is the ADC0804 pin description.

Figure 5.5 ADC0804 Chip (Testing ADC0804 in Free Running Mode)


➢ CLK IN and CLK R:
• CLK IN is an input pin connected to an external clock source when an external clock
is used for timing.
• However, the 0804 has an internal clock generator.
• To use the internal clock generator (also called self-clocking), CLK IN and CLK R
pins are connected to a capacitor and a resistor and the clock frequency is
determined by:

• Typical values are R = 10K ohms and C =150pF.


• By substituting, we get f = 606 kHz and the conversion time is 110μs.
➢ Vref/2: (Pin 9)
• It is used for the reference voltage.
• If this pin is open (not connected), the analog input voltage is in the range of 0 to 5
volts (the same as the Vcc pin).
• If the analog input range needs to be 0 to 4 volts, Vref/2 is connected to 2 volts.
• Table 5.4 shows the Vin range for various Vref/2 inputs.
Table 5.4 Vref/2 Relation to Vin Range (ADC0804)

➢ 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

o Dout = digital data output (in decimal),


o Vin = analog voltage, and
o Step size (resolution) is the smallest change, which is (2 * Vref/2)/256 for ADC
0804
➢ Analog ground and digital ground:
• Analog ground is connected to the ground of the analog Vin and digital ground is
connected to the ground of the Vcc pin.
• The reason that to have ground pin is to isolate the analog Vin signal from transient
voltages caused by digital switching of the output D0 – D7. This contributes to the
accuracy of the digital data output.
➢ Vin(+) & Vin(-):
• Differential analog inputs where Vin= Vin (+) – Vin (-).
• Vin (-) is connected to ground and Vin(+) is used as the analog input to be
converted.
➢ RD:
• This is an input signal and is active low.
• The ADC converts the analog input to its binary equivalent and holds it in an internal
register.
• RD is, used to get the converted data out of the ADC0804 chip.
• Is “output enable” a high-to-low RD pulse is used to get the 8-bit converted data out
of ADC804.
➢ INTR:
• This is an output pin and is active low.
• It is “end of conversion” When the conversion is finished, it goes low to signal the
CPU that the converted data is ready to be picked up.
➢ WR:
• This is an active low input
• It is “start conversion” When WR makes a low-to-high transition, ADC804 starts
converting the analog input value of Vin to an 8-bit digital number.
• When the data conversion is complete, the INTR pin is forced low by the ADC0804.
➢ CS:
• It is an active low input used to activate ADC804.
➢ Steps to Be followed For Data Conversion:
• The following steps must be followed for data conversion by the ADC804 chip:
o Make CS= 0 and send a L-to-H pulse to pin WR to start conversion.
o Monitor the INTR pin, if high keep polling but if low, conversion is complete, go to
next step.
o Make CS= 0 and send a H-to-L pulse to pin RD to get the data out.
• Figure 5.6 shows the timing diagram for ADC process.

Figure 5.6 Read and Write Timing for ADC08804

Clock source for ADC0804:


➢ The speed at which an analog input is converted to the digital output depends on the
speed of the CLK input.
➢ According to the ADC0804 datasheets, the typical operating frequency is approximately
640kHz at 5 volts.
➢ Figures 5.7 and 5.8 show two ways of providing clock to the ADC0804.
➢ In Figure 5.8, notice that the clock in for the ADC0804 is coming from the crystal of the
microcontroller.
➢ Since this frequency is too high, we use D flip-flops (74LS74) to divide the frequency.
➢ A single D flip-flop divides the frequency by 2 if we connect its 𝑄̅ to the D input.
➢ For a higher-frequency crystal, you can use 4 flip-flops
Figure 5.7 8051 Connection to ADC0804 with Self-Clocking

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.

;p2.6=WR (start conversion needs to L-to-H pulse)


;p2.7 When low, end-of-conversion)
;p2.5=RD (a H-to-L will read the data from ADC chip)
;p1.0 – P1.7= D0 - D7 of the ADC804
;
MOV P1,#0FFH ;make P1 = input
BACK: CLR P2.6 ;WR = 0
SETB P2.6 ;WR = 1 L-to-H to start conversion
HERE: JB P2.7,HERE ;wait for end of conversion
CLR P2.5 ;conversion finished, enable RD
MOV A,P1 ;read the data
ACALL CONVERSION ;hex-to-ASCII conversion
ACALL DATA_DISPLAY ;display the data
SETB P2.5 ;make RD=1 for next round
SJMP BACK

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

Table 5.6 ADC0808/0809 Analog Channel Selection

Steps to program the ADC0808/0809


➢ The following are steps ro get data from an ADC0808/0809.
o Select an analog channel by providing bits to A, B, and C addresses according to
Table 5.6.
o Activate the ALE (address latch enable) pin. It needs an L-to-H pulse to latch in
the address.
o Activate SC (start conversion) by an L-to-H pulse to initiate conversion.
o Monitor EOC (end of conversion) to see whether conversion is finished. H-to- L
output indicates that the data is converted and is ready to be picked up. If we do
not use EOC, we can read the converted digital data after a brief time delay. The
delay size depends on the speed of the external clock we connect to the CLK
pin. Notice that the EOC is the same as the INTR pin in other ADC chips.
o Activate OE (output enable) to read data out of the ADC chip. An L-to H pulse to
the OE pin will bring digital data out of the chip. Also notice that the OE is "the
same as the RD pin in other ADC chips.
➢ The speed of conversion depends on the frequency of the clock connected to the CLK
pin, it cannot be faster than 100 microseconds
SENSOR INTERFACING:
LM35 Temperature sensors:
➢ The LM35 series sensors are precision integrated-circuit temperature sensors whose
output voltage is linearly proportional to the celsius (centigrade) temperature.
➢ The LM35 requires no external calibration since it is internally calibrated.
➢ It outputs 10mV for each degree of centigrade temperature.
➢ Table 5.7 is the selection guide for the LM35
Table 5.7 LM35 Temperature Sensor Series Selection Guide

➢ 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

Figure 5.10 Getting Data from Analog World

➢ 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

➢ Figure 5.11 shows the connection of a temperature sensor to the ADC0848.


➢ The LM336-2.5 zener diode to fix the voltage across the 10K pot at 2.5V.
➢ The use of the LM336-2.5 should overcome any fluctuations in the power supply.

Figure 5.11 8051 Connection to ADC0848 and Temperature sensor


Program:
RD BIT P2.5 ;RD
WR BIT P2.6 ;WR
INTR BIT P2.7 ; END OF CONVERSION
MYDATA EQU P1 ; P1.0-P1.7 = D0-D7 OF THE ADC0848
MOV P1,#0FFH ;make P1 = input
SETB INTR
BACK: CLR WR ;WR = 0
SETB WR ;WR = 1 L-to-H to start conversion
HERE: JB INTR,HERE ;wait for end of conversion
CLR RD ;conversion finished, enable RD
MOV A,MYDATA ;read the data
ACALL CONVERSION ;hex-to-ASCII conversion
ACALL DATA_DISPLAY ;display the data
SETB RD ;make RD=1 for next round
SJMP BACK

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

DIGITAL-TO-ANALOG (DAC) CONVERTER:


➢ The DAC is a device widely used to convert digital pulses to analog signals.
➢ In this section we will discuss the basics of interfacing a DAC to 8051.
➢ The two method of creating a DAC is binary weighted and R/2R ladder.
➢ The Binary Weighted DAC, which contains one resistor or current source for each bit of
the DAC connected to a summing point.
➢ These precise voltages or currents sum to the correct output value.
➢ This is one of the fastest conversion methods but suffers from poor accuracy because of
the high precision required for each individual voltage or current.
➢ Such high-precision resistors and current-sources are expensive, so this type of
converter is usually limited to 8-bit resolution or less.
➢ The R-2R ladder DAC, which is a binary weighted DAC that uses a repeating cascaded
structure of resistor values R and 2R.
➢ This improves the precision due to the relative ease of producing equal valued matched
resistors (or current sources).
➢ However, wide converters perform slowly due to increasingly large RC-constants for
each added R-2R link.
➢ The first criterion for judging a DAC is its resolution, which is a function of the number of
binary inputs.
➢ The common ones are 8, 10, and 12 bits.
➢ The number of data bit inputs decides the resolution of the DAC since the number of
analog output levels is equal to 2n, where n is the number of data bit inputs.
➢ Therefore, an 8-input DAC such as the DAC0808 provides 256 discrete voltage (or
current) levels of output.
➢ Similarly, the 12-bit DAC provides 4096 discrete voltage levels.
➢ There also 16-bit DACs, but they are more expensive.

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

➢ Usually reference current is 2mA.


➢ Ideally we connect the output pin to a resistor, convert this current to voltage, and
monitor the output on the scope.
➢ But this can cause inaccuracy; hence an opamp is used to convert the output current to
voltage.
➢ The 8051 connection to DAC0808is as shown in the below figure 5.12.
➢ Now assuming that Iref = 2mA, if all the inputs to the DAC are high, the maximum output
current is 1.99mA.
Figure 5.12 8051 Connection to DAC808
Example 1:
Assuming that R=5K and Iref=2mA, calculate Vout for the following binary inputs:
(a) 10011001B
(b) 11001000B
Solution:
(a) Iout = 2mA(153/256) = 1.195mA and Vout = 1.195mA * 5K =5.975V
(b) Iout = 2mA(200/256) = 1.562mA and Vout = 1.562mA * 5K =7.8125V

Converting Iout to voltage in DAC0808:


➢ Ideally we connect the output pin lout, to a resistor, convert this current to voltage, and
monitor the output on the scope.
➢ In real life, however, this can cause inaccuracy since the input resistance of the load
where it is connected will also affect the output voltage.
➢ For this reason, the lref current output is isolated by connecting it to an op-amp such as
the 741 with Rf = 5K ohms for the feedback resistor.
➢ Assuming that R= 5K ohms, by changing the binary input, the output voltage changes as
shown in Example 2.
Example 2:
Inorder to generate a stair-step ramp, set up the circuit in figure 5.12 and connect the
output to an oscilloscope. Then write a program to send data to the DAC to generate a
stair-step ramp.
Solution:
CLR A
AGAIN: MOV P1,A ; SEND DATA TO DAC
INC A ; COUNT FROM 0 TO FFH
ACALL DELAY ; LET DAC RECOVER
SJMP AGAIN

You might also like