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

UNIT 3 Microcontrollers

The document provides a comprehensive overview of the 8051 microcontroller architecture, detailing its major components such as the CPU, memory, I/O ports, and timers. It explains the significance of the Program Counter and Data Pointer registers, as well as the functionality of the microcontroller's pins and addressing modes. Additionally, it discusses the structure and functions of the I/O ports, bit-addressable I/O, and includes examples of assembly language instructions.

Uploaded by

Lakshay shonak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

UNIT 3 Microcontrollers

The document provides a comprehensive overview of the 8051 microcontroller architecture, detailing its major components such as the CPU, memory, I/O ports, and timers. It explains the significance of the Program Counter and Data Pointer registers, as well as the functionality of the microcontroller's pins and addressing modes. Additionally, it discusses the structure and functions of the I/O ports, bit-addressable I/O, and includes examples of assembly language instructions.

Uploaded by

Lakshay shonak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

UNIT 3 Microcontrollers

Question 1:

(a) Draw a detailed block diagram of the 8051 microcontroller architecture and explain the function of
each major block.

(b) Briefly describe the significance of the Program Counter (PC) and the Data Pointer (DPTR) registers
in the 8051.

Answer: Block Diagram of the 8051 Microcontroller Architecture

+-----------------+ +-----------------+ +-------------------------+


| CPU |----->| Program Memory |----->| Internal Bus |
|(ALU, Control |<-----|(ROM/EPROM) |<-----|(Address, Data, Control)|
|Unit, Registers)| +-----------------+ +-------------------------+
+-----------------+
^
|
| Control/Data
|
+-----------------+ +-----------------+
| Data Memory |<-----| SFRs |
|(RAM) |<-----|(Special Function|
+-----------------+ | Registers) |
^ +-----------------+
|
| Control/Data

Page 1
|
+-----------------+ +-----------------+ +-----------------+ +-------
----------+
| I/O Ports |<---->| Timer/Counters|----->| Serial Port |<---->|
Interrupt Control|
|(P0, P1, P2, P3)| |(T0, T1) | | (UART) | |
|
+-----------------+ +-----------------+ +-----------------+ +-------
----------+
^
| Control/Timing
|
+-------------------------+
| Oscillator and Timing Unit|
|(External Crystal) |
+-------------------------+

(a) Explanation of Major Blocks:

 CPU (Central Processing Unit): The brain of the microcontroller. It fetches instructions from
the program memory, decodes them, and executes the corresponding operations. It consists of the
Arithmetic Logic Unit (ALU), control unit, registers, and timing and control circuits.
 Program Memory (ROM/EPROM): Stores the program instructions that the microcontroller
will execute. The 8051 can address up to 64KB of external program memory. Some variants
have internal ROM/EPROM.
 Data Memory (RAM): Used for temporary storage of data, variables, and intermediate results
during program execution. The 8051 has 128 bytes of internal RAM, which can be expanded
externally up to 64KB.
 Special Function Registers (SFRs): A block of memory locations within the internal RAM
address space (above 80H) that control and monitor the various peripheral functions of the
microcontroller, such as timers, serial port, and I/O ports.
 I/O Ports (P0-P3): Four 8-bit bidirectional ports used for interfacing with external devices. Each
port has different characteristics and alternate functions.
 Timer/Counters (T0, T1): Two 16-bit timers/counters that can be used for generating time
delays, counting external events, or generating baud rates for serial communication.
 Serial Port: Enables serial communication with other devices using protocols like UART. It
includes transmit and receive circuitry.
 Interrupt Control: Manages the interrupt system, allowing the microcontroller to respond to
external or internal events asynchronously. It prioritizes and handles interrupt requests.
 Oscillator and Timing Unit: Generates the clock signals that synchronize the internal
operations of the microcontroller. An external crystal oscillator is typically connected to this
unit.
 Internal Bus: Consists of address bus, data bus, and control bus, which facilitate the transfer of
data and control signals between the different internal blocks.

(b)

 Program Counter (PC): A 16-bit register that holds the address of the next instruction to be
fetched from the program memory. It is automatically incremented after each instruction is
fetched, ensuring sequential execution of the program. Jump and branch instructions modify the
Page 2
PC to alter the program flow. The PC is crucial for program execution as it dictates the order in
which instructions are executed.
 Data Pointer (DPTR): A 16-bit register used to hold the address of external data memory. It is
often used in conjunction with indirect addressing modes to access data stored in external RAM
or ROM. The DPTR can be manipulated as two 8-bit registers (DPH and DPL) for easier
manipulation of the higher and lower byte of the address. It provides a way to access a larger
range of memory than what is directly addressable by other instructions.

Question 2:

(a) Draw the pin diagram of a standard 40-pin 8051 microcontroller and label all the pins.

(b) Explain the primary function of the following pins: * EA (External Access) * ALE (Address Latch
Enable) * RST (Reset) * XTAL1 and XTAL2

Answer (a)

Page 3
(b)

 EA (External Access) - Pin 31: This is an active-low input pin that determines whether the
microcontroller fetches program code from internal ROM or external memory.
o When EA is connected to V$_{CC}$ (+5V), the 8051 fetches program instructions from
its internal ROM (if present). If the address exceeds the internal ROM capacity, it
automatically switches to external memory.
o When EA is connected to GND (0V), the 8051 fetches all program instructions from
external memory, starting from address 0000H. This is essential when the program code
is larger than the internal ROM or when the variant doesn't have internal ROM. For
devices without internal ROM, this pin must be tied to GND.
 ALE (Address Latch Enable) / PROG - Pin 30: This pin has two functions:
o ALE (Address Latch Enable): During normal operation, ALE is an output pin that
emits a pulse at one-sixth of the oscillator frequency. This pulse is used to demultiplex
the address and data lines of Port 0 (AD0-AD7) when accessing external memory. The
lower byte of the memory address is available on P0 during the ALE high pulse and is
latched into an external latch on the falling edge of ALE.
o PROG (Program Pulse Input): During EPROM programming, this pin serves as the
program pulse input.
 RST (Reset) - Pin 9: This is an active-high input pin that resets the microcontroller to its initial
state. When RST is held high for at least two machine cycles, the 8051 initializes its internal
registers and program counter to 0000H, effectively restarting the program execution from the
beginning. It is typically used at power-up or when a system reset is required.
 XTAL1 and XTAL2 - Pins 19 and 18: These pins are connected to an external crystal oscillator
or a TTL clock source to provide the main clock signal for the microcontroller. The frequency of
the oscillator determines the speed of the microcontroller's operations.
o XTAL1: Input to the inverting oscillator amplifier.
o XTAL2: Output from the inverting oscillator amplifier. A crystal is typically connected
across these two pins along with appropriate load capacitors to ground to form a stable
oscillator circuit.

Question 3:

(a) Describe the structure and functionality of the four I/O ports (P0, P1, P2, P3) of the 8051
microcontroller. Highlight their alternate functions.

(b) Explain the concept of bit-addressable I/O and identify which of the 8051 ports (or parts thereof) are
bit-addressable. Provide an example of how a single bit of an I/O port can be manipulated in 8051
assembly language.

Answer :

Page 4
(a)

The 8051 microcontroller has four 8-bit bidirectional I/O ports: P0, P1, P2, and P3. Each port consists
conceptually of a latch, an output driver, and an input buffer.

Conceptual Diagram of an I/O Port Bit:

+-------+ +---------------+ +---------------+


| Latch | --> | Output Driver | --> | External Pin |
+-------+ +---------------+ +---------------+
^ |
| |
+-------------------------------------+
Input Buffer

 Port 0 (P0 - Pins 39-32):


o Primary Function: Serves as a multiplexed address and data bus during external
memory access. The lower 8 bits of the address (A0-A7) are output through P0 during the
ALE high pulse, and then P0 becomes the data bus (D0-D7) during the rest of the
memory cycle.
o Alternate Functions: None directly on the port pins themselves in standard operation.
However, during external memory access, the pins are used for address and data.
 Port 1 (P1 - Pins 1-8):
o Primary Function: A dedicated general-purpose I/O port. It does not have any alternate
functions related to external memory access. It can be used for both input and output
operations. Internally, it has pull-up resistors.
 Port 2 (P2 - Pins 21-28):
o Primary Function: When accessing external memory (greater than 256 bytes of data
memory or any program memory), Port 2 outputs the higher 8 bits of the 16-bit address
(A8-A15). In the absence of external memory access, it can be used as a general-purpose
I/O port. It also has internal pull-up resistors.
o Alternate Functions: Outputs the high byte of the external memory address.
 Port 3 (P3 - Pins 10-17):
o Primary Function: Besides being a general-purpose I/O port, Port 3 has several
important alternate functions for various peripherals within the 8051. Each pin of Port 3
has a specific alternate function. It also has internal pull-up resistors.
o Alternate Functions (Diagram):
 Pin | Name | Alternate Function
 ----|---------|-----------------------
 P3.0| RXD | Serial Input
 P3.1| TXD | Serial Output
 P3.2| INT0 | External Interrupt 0 Input (Active Low)
 P3.3| INT1 | External Interrupt 1 Input (Active Low)
 P3.4| T0 | Timer/Counter 0 External Input
 P3.5| T1 | Timer/Counter 1 External Input
 P3.6| WR | External Data Memory Write Strobe (Active Low)
 P3.7| RD | External Data Memory Read Strobe (Active Low)

(b)
Page 5
Bit-Addressable I/O:

Bit-addressable I/O refers to the ability to access and manipulate individual bits within certain memory
locations, rather than having to deal with the entire byte. This is particularly useful for controlling
individual devices or reading the status of single sensors connected to the I/O ports.

Bit-Addressable Ports/Areas:

In the 8051, the following areas are bit-addressable:

 Port 0 (P0): Each of the 8 bits (P0.0 to P0.7) can be individually addressed.
 Port 1 (P1): Each of the 8 bits (P1.0 to P1.7) can be individually addressed.
 Port 2 (P2): Each of the 8 bits (P2.0 to P2.7) can be individually addressed.
 Port 3 (P3): Each of the 8 bits (P3.0 to P3.7) can be individually addressed.
 Special Function Registers (SFRs): A specific range of SFRs (those whose addresses are
multiples of 8, i.e., end in 000B) are also bit-addressable. This includes control registers for
peripherals like timers, serial port, and interrupts.
 Internal RAM (Bytes 20H to 2FH): A 16-byte area of the internal data RAM (addresses 20H to
2FH) is specifically designated as bit-addressable. This provides 128 individual bits that can be
set, cleared, complemented, etc.

Example of Single Bit Manipulation in Assembly Language:

Let's assume we want to set the third bit of Port 1 (P1.2) to high (logic 1).

Code snippet
SETB P1.2 ; Set bit 2 of Port 1

Similarly, to clear the fifth bit of Port 3 (P3.4, which is also Timer 0 input):

Code snippet
CLR P3.4 ; Clear bit 4 of Port 3

To complement the first bit of Port 0 (P0.0):

Code snippet
CPL P0.0 ; Complement bit 0 of Port 0

These instructions directly target and modify a single bit within the specified bit-addressable I/O port or
SFR, without affecting the other bits of the same byte.

Question 4: Explain the different addressing modes of the 8051 microcontroller with suitable
examples for each.

Answer:

Addressing modes refer to the way in which the operand (the data to be operated upon) is specified in an
assembly language instruction. The 8051 microcontroller offers several addressing modes that provide
Page 6
flexibility in accessing data stored in different memory locations (internal RAM, SFRs, external
RAM/ROM). Understanding these modes is crucial for efficient programming.

Here are the key addressing modes supported by the 8051:

1. Immediate Addressing:

 Description: The operand itself is directly included in the instruction as an 8-bit or 16-bit value.
It is preceded by the '#' symbol.
 Location of Operand: Within the instruction itself (program memory).
 Example:

Code snippet

MOV A, #50H ; Move the immediate value 50H into the Accumulator (A)
MOV DPTR, #1234H ; Load the Data Pointer (DPTR) with the immediate value
1234H

 Explanation: In the first instruction, the value 50H is directly loaded into the Accumulator. In
the second, the 16-bit value 1234H is loaded into the DPTR register.

2. Register Addressing:

 Description: The operand is located in one of the 8051's general-purpose registers (R0-R7) of
the currently selected register bank.
 Location of Operand: Internal RAM (within the selected register bank).
 Example:

Code snippet

MOV A, R5 ; Move the content of register R5 into the Accumulator (A)


ADD A, R2 ; Add the content of register R2 to the Accumulator (A)

 Explanation: These instructions access data directly from one of the working registers. The
active register bank is determined by the RS0 and RS1 bits in the Program Status Word (PSW)
register.

3. Direct Addressing:

 Description: The operand's address in the internal data RAM (00H to 7FH) or the Special
Function Register (SFR) space (80H to FFH) is directly specified as an 8-bit address within the
instruction.
 Location of Operand: Internal RAM (00H-7FH) or SFRs (80H-FFH).
 Example:

Code snippet

Page 7
MOV A, 30H ; Move the content of internal RAM location 30H into the
Accumulator (A)
MOV P1, A ; Move the content of the Accumulator (A) to Port 1 (SFR
address 90H)
MOV TH1, #0FDH ; Move the immediate value 0FDH to the Timer/Counter 1 High
Byte (TH1, SFR address 8DH)

 Explanation: The first instruction directly accesses the data byte at RAM address 30H. The
second instruction writes the value in the Accumulator to the SFR associated with Port 1. The
third instruction directly loads a value into the TH1 SFR.

4. Indirect Addressing:

 Description: The instruction specifies a register (R0 or R1 for internal RAM, or DPTR for
external RAM/ROM) that contains the address of the operand. The address register is prefixed
with the '@' symbol.
 Location of Operand: Internal RAM (addressed by R0 or R1) or External RAM/ROM
(addressed by DPTR).
 Example (Internal RAM):

Code snippet

MOV R0, #40H ; Load R0 with the address 40H


MOV A, @R0 ; Move the content of the memory location whose address is in
R0 into A (i.e., move content of 40H to A)
MOV @R1, B ; Move the content of the B register to the memory location
whose address is in R1

 Example (External RAM):

Code snippet

MOV DPTR, #1000H ; Load DPTR with the external RAM address 1000H
MOVX A, @DPTR ; Move the content of external RAM location 1000H into A
MOVX @DPTR, A ; Move the content of A to external RAM location 1000H

 Explanation: Indirect addressing allows for more flexible memory access, especially when
dealing with data structures or when the memory address needs to be calculated or changed
during program execution. The MOVX instruction is used for accessing external data memory.

5. Indexed Addressing:

 Description: The actual address of the operand is calculated by adding the content of the Data
Pointer (DPTR) or the Program Counter (PC) to the content of the Accumulator (A). This mode
is primarily used for accessing data tables or look-up tables stored in program memory
(ROM/EPROM).
 Location of Operand: Program Memory (ROM/EPROM).
 Example (using DPTR):

Code snippet
Page 8
MOV DPTR, #TABLE_ADDR ; Load DPTR with the starting address of the table in
ROM
MOV A, #05H ; Assume we want to access the 6th element (index 5)
MOVC A, @A+DPTR ; Move the byte from ROM at address (DPTR + A) into A
; Assume TABLE_ADDR contains the address of a table in ROM
; The instruction reads the byte at (TABLE_ADDR + 5) and loads it into A.

 Example (using PC):

Code snippet

MOV A, #03H
MOVC A, @A+PC ; Move code byte relative to PC to A
; ... (some instructions) ...
ORG $+1 ; Skip the next byte (the table starts after this)
TABLE:
DB 10H, 20H, 30H, 40H, 50H ; Define a table of bytes in ROM

 Explanation: Indexed addressing is powerful for accessing fixed data stored in program
memory. The MOVC instruction is specifically used for reading code memory. When using the PC
as the base register, the effective address is relative to the address of the MOVC instruction itself.
The ORG $+1 in the PC-relative example ensures the table starts at the correct offset after the
MOVC instruction.

6. Bit Addressing:

 Description: This mode allows direct access and manipulation of individual bits within the bit-
addressable area of the internal RAM (20H-2FH) and certain Special Function Registers (SFRs)
whose addresses are multiples of 8 (e.g., P0, P1, P2, P3, PSW, etc.).
 Location of Operand: Bit-addressable area of Internal RAM (20H-2FH) or bit-addressable
SFRs.
 Example:

Code snippet

SETB 07H ; Set bit 7 of the bit-addressable RAM (address 27H in byte
addressing) to 1
CLR P1.3 ; Clear the third bit of Port 1 to 0
JNB P3.2, LABEL ; Jump to LABEL if bit 2 of Port 3 is 0 (Not Bit)
MOV C, P3.0 ; Move the value of bit 0 of Port 3 to the Carry flag

 Explanation: Bit addressing simplifies the control of individual I/O lines, flags, and other
single-bit entities within the microcontroller. Instructions like SETB (set bit), CLR (clear bit), CPL
(complement bit), JNB (jump if not bit), JB (jump if bit), MOV C, bit, and MOV bit, C are used
for bit manipulation.

Assembly Language Programming of the 8051 Microcontroller


Assembly language programming for the 8051 involves writing instructions using mnemonics that
directly correspond to the microcontroller's machine code. While higher-level languages like C are now
Page 9
commonly used, understanding assembly provides a crucial insight into the 8051's architecture and
allows for fine-grained control over its hardware.

Here's a comprehensive explanation of 8051 assembly language programming, covering key aspects
relevant from a B.Tech exam perspective:

1. Instruction Set Architecture (ISA):

The 8051 has a relatively simple yet powerful instruction set. Instructions can be broadly categorized
into the following types:

 Data Transfer Instructions: These instructions move data between different memory locations
(internal RAM, SFRs, external RAM/ROM), registers, and the Accumulator. Examples include
MOV, MOVX, MOVC, PUSH, POP.
 Arithmetic Instructions: These perform arithmetic operations like addition, subtraction,
multiplication, division, increment, decrement. Examples include ADD, ADDC, SUBB, MUL, DIV,
INC, DEC.
 Logical Instructions: These perform bitwise logical operations like AND, OR, XOR,
complement, rotate, shift. Examples include ANL, ORL, XRL, CPL, RR, RL, RRC, RLC.
 Boolean (Bit Manipulation) Instructions: These operate on individual bits in the bit-
addressable area and the Carry flag. Examples include SETB, CLR, CPL, JNB, JB, MOV C, bit, MOV
bit, C.
 Control Transfer Instructions: These control the flow of program execution by altering the
Program Counter (PC). Examples include JMP, SJMP, LJMP, AJMP, JZ, JNZ, CJNE, DJNZ, CALL,
LCALL, ACALL, RET, RETI.

2. Assembly Language Syntax:

A typical 8051 assembly language program line has the following structure (not all fields are always
present):

Code snippet
[Label:] Mnemonic [Operand1] [, Operand2] [; Comment]

 Label (Optional): A symbolic name assigned to a memory address. It allows you to refer to
locations in the code or data by name instead of absolute addresses, making the code more
readable and maintainable. Labels end with a colon ( :).
 Mnemonic (Mandatory): A short abbreviation that represents a specific machine instruction
(e.g., MOV, ADD, JMP).
 Operand(s) (Often Mandatory): Specifies the data or memory locations that the instruction
will operate on. The number and type of operands depend on the specific instruction. Addressing
modes are used to specify these operands.
 Comment (Optional): Explanatory text that is ignored by the assembler. Comments usually
start with a semicolon (;).

3. Directives (Assembler Directives or Pseudo-Instructions):

Page 10
These are instructions for the assembler itself, not translated into machine code. They control the
assembly process, define data, reserve memory, and organize the program. Common 8051 assembler
directives include:

 ORG (Origin): Specifies the starting address in memory where the subsequent code or data will
be placed.

Code snippet

ORG 0000H ; Start of program memory

 END (End): Marks the end of the assembly source file. The assembler stops processing after
encountering this directive.

Code snippet

END

 DB (Define Byte): Defines one or more byte values in memory.

Code snippet

TABLE: DB 10H, 25H, 0AH, 'A' ; Define a table of bytes

 DW (Define Word): Defines one or more 16-bit word values in memory (LSB first).

Code snippet

ADDRESS: DW 1234H ; Define a 16-bit address

 DS (Define Space): Reserves a block of memory for data storage.

Code snippet

BUFFER: DS 10 ; Reserve 10 bytes of memory for a buffer

 EQU (Equate): Assigns a symbolic name to a constant value or address.

Code snippet

COUNT EQU 10 ; Define COUNT as the value 10


LED_PORT EQU P1 ; Define LED_PORT as the SFR address of Port 1

 SEGMENT and ENDS (For modular programming in some assemblers): Define logical segments
of code or data.
 EXTRN (External): Declares symbols that are defined in other assembly modules.
 PUBLIC (Public): Declares symbols that can be accessed by other assembly modules.

Page 11
4. Program Structure:

A typical 8051 assembly program might have the following structure:

 Initialization Section: Sets up the microcontroller's peripherals, initializes variables, and


configures I/O ports.
 Main Program Loop: Contains the core logic of the application, which usually runs
continuously.
 Subroutines (Functions): Reusable blocks of code that perform specific tasks. They are called
using CALL instructions and return using RET.
 Interrupt Service Routines (ISRs): Special subroutines that are executed automatically when a
specific interrupt event occurs. They must end with RETI (Return from Interrupt).
 Data Definitions: Sections where constants, variables, and tables are defined using assembler
directives.

5. Example Program (Blinking an LED connected to P1.0):

Code snippet
; Define the LED port
LED_PIN EQU P1.0

; Define a delay subroutine


DELAY:
MOV R7, #250
DELAY_LOOP1:
MOV R6, #250
DELAY_LOOP2:
DJNZ R6, DELAY_LOOP2 ; Decrement R6 and jump if not zero
DJNZ R7, DELAY_LOOP1 ; Decrement R7 and jump if not zero
RET ; Return from subroutine

; Main program
ORG 0000H ; Start at program memory address 0

MAIN:
MOV P1, #0FFH ; Initialize Port 1 (all pins high)

LOOP:
CLR LED_PIN ; Turn LED ON (assuming active low)
ACALL DELAY ; Call the delay subroutine
SETB LED_PIN ; Turn LED OFF
ACALL DELAY ; Call the delay subroutine
SJMP LOOP ; Jump back to the beginning of the loop

END ; End of the program

Explanation of the Example:

 LED_PIN EQU P1.0: Defines a symbolic name LED_PIN for the first bit of Port 1.
 DELAY subroutine: Creates a simple software delay loop using registers R6 and R7.
 ORG 0000H: Specifies that the program starts at memory address 0000H, which is the reset
vector.
Page 12
 MAIN: The label for the main program section.
 MOV P1, #0FFH: Initializes all pins of Port 1 to high.
 LOOP: The main program loop.
 CLR LED_PIN: Clears the LED_PIN (P1.0), turning the LED ON (assuming it's active low).
 ACALL DELAY: Calls the DELAY subroutine. ACALL is an absolute call within the same 2KB page.
 SETB LED_PIN: Sets the LED_PIN (P1.0) high, turning the LED OFF.
 SJMP LOOP: Short jump back to the LOOP label, creating an infinite blinking loop.
 END: Marks the end of the assembly program.

6. Assembler and Simulator/Emulator:

To work with 8051 assembly language, you need:

 Assembler: A program that translates the assembly language source code into machine code
(hexadecimal or binary format) that the 8051 can understand. Popular assemblers include SDCC
(Small Device C Compiler, which also includes an assembler), Keil C51 (which has a powerful
assembler), and various open-source assemblers.
 Simulator/Emulator: A software tool that simulates the behavior of the 8051 microcontroller on
your computer. This allows you to test and debug your assembly code without needing physical
hardware. Examples include Keil µVision simulator, Sim8051, and various online simulators.
Some emulators can also interact with hardware programmers.
 Hardware Programmer (Optional): If you want to run your code on a physical 8051
microcontroller, you'll need a hardware programmer to transfer the compiled machine code to the
microcontroller's memory (ROM/Flash).

7. Advantages of Assembly Language Programming for 8051:

 Direct Hardware Control: Provides the most direct control over the 8051's peripherals and
internal resources.
 Memory Efficiency: Allows for writing very compact code, which is crucial in memory-
constrained embedded systems.
 Speed and Performance: Can result in faster execution times compared to higher-level
languages if optimized carefully.
 Understanding the Architecture: Forces a deep understanding of the 8051's architecture and
how it works.

8. Disadvantages of Assembly Language Programming for 8051:

 Development Time: Writing and debugging assembly code can be significantly more time-
consuming than using higher-level languages.
 Complexity: Assembly code can become complex and difficult to maintain for larger projects.
 Portability: Assembly code is specific to the 8051 architecture and is not easily portable to other
microcontrollers.

Conclusion:

Page 13
Assembly language programming for the 8051, while less common for large-scale projects today,
remains a valuable skill for embedded systems engineers. It provides a fundamental understanding of the
microcontroller's operation, allows for highly optimized code in specific scenarios, and is essential for
tasks like low-level hardware interfacing and bootloader development. For a B.Tech student, grasping
the concepts of the instruction set, addressing modes, assembler directives, and basic program structure
in 8051 assembly is crucial for a solid foundation in microcontroller systems.

INTERFACING AND APPLICATIONS


Interfacing of 8051with: Analog Sensors, Keypad & LCD display, ADC, DAC,
DC motor.

LCD INTERFACING:

Figure 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 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)
Page 14
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

Page 15
 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 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

Page 16
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 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.

Page 17
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


Page 18
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

Page 19
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:

Page 20
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.

Page 21
Figure 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:

Page 22
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
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 4 provides the flowchart for keyboard interfacing Program for scanning and
identifying the pressed key.

Page 23
Figure 4 Flowchart for Programming Keyboard Interfacing

Page 24
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

Page 25
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 3
Table 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:

Page 26
 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 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.

Page 27
Table 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.

Page 28
 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 6 shows the timing diagram for ADC process.

Figure 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 7 and 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

Page 29
Figure 7 8051 Connection to ADC0804 with Self-Clocking

Figure 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
Page 30
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 using
three address pins, A, B, and C.
Table 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.

Page 31
Figure 9 ADC0808/0809

Table 6 ADC0808/0809 Analog Channel Selection

Steps to program the ADC0808/0809


 The following are steps to get data from an ADC0808/0809.
o Select an analog channel by providing bits to A, B, and C addresses according to
Table 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

Page 32
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 7 is the selection guide for the LM35
Table 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 10 Getting Data from Analog World

 The above figure 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.

Page 33
 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 (2 8) steps and the LM35
(or LM34) produces l0 mV for every degree of temperature change, we can condition V in
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 8
Table 8 Temperature vs. Vout for ADC0848

 Figure 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 11 8051 Connection to ADC0848 and Temperature sensor

Page 34
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.

Page 35
 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 2 n, 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.

Page 36
Figure 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

Page 37
Generating a sine wave
 To generate a sine wave, we first need a table whose values represent the magnitude of
the sine of angles between 0 and 360 degrees.
 The values for the sine function vary from -1.0 to +1.0 for 0- to 360-degree angles.
 Therefore, the table values are integer numbers representing the voltage magnitude for
the sine of theta.
 This method ensures that only integer numbers are output to the DAC by the 805l
microcontroller.
 Table 9 shows the angles, the sine values, the voltage magnitudes, and the integer
values representing the voltage magnitude for each angle (with 30-degree increments).
 To generate Table 9,we assumed the full-scale voltage of 10 V for DAC output (as
designed in Example 4 Figure).
 Full-scale output of the DAC is achieved when all the data inputs of the DAc are high.
 Therefore, to achieve the full-scale 10 V output, we use the following equation
Vout= 5V(1+sinθ)
 Vout of DAC for various angles is calculated and shown in Table 9. See Example 3 for
verification of the calculations
Table 9 Angle Vs Voltage Magnitude for Sine Wave

Example 3:
Verify the values given for the following angles: (a) 30º (b) 60º
Solution:
(a) Vout = 5V+(5V * sin30) =7.5V
DAC input values = 7.5V * 25.6 = 192 (Decimal)
(b) Vout = 5V+(5V * sin60) =9.33V
DAC input values = 9.33V * 25.6 = 238 (Decimal)

Page 38
 To find the values sent to the DAC for various angles, we simply multiply Vout voltage by
25.6 because there are 256 steps and full scale Vout is 10 volts.
256 steps/10V = 25.6 steps per volt
 The following examples 9, 10 and 11 will show the generation of waveforms using
DAC0808.

Example 4:
Write an ALP to generate a sine waveform.
Vout= 5V(1+sinθ)
Solution:
Calculate the decimal values for every 10 degree of the sine wave. These values can be
maintained in a table and simply the values can be sent to port P1. The sine wave can be
observed on the CRO.

Example 5:
Write an ALP to generate a triangular waveform.

Page 39
DC MOTOR INTERFACING:
 DC motor is a device that translates electrical pulses into mechanical movement.
 The DC motor has + and – leads
 Connecting them to a DC voltage source moves the motor in one direction and by
reversing the polarity, the DC motor will move in opposite direction.
Unidirectional Control:
 The following figure 13 shows the DC motor rotation for clockwise (CW) and
counterclockwise (CCW) rotations.

Figure 13 DC Motor Rotation (Permanent Magnet Field)


Bidirectional Control:
 With the help of relays or some specially designed chips we can change the direction of
the DC motor rotation.
 Figure 5.14 through 5.17 shows the basic concepts of H-Bridge control of DC motors.

Page 40
Figure 14 H-Bridge Motor Configuration
 Figure 2 shows the connection of an H-Bridge using simple switches.
 All the switches are open, which does not allow the motor to turn.

Figure 15 H-Bridge Motor Clockwise Configuration


 Figure 3 shows the switch configuration for turning the motor in one direction.
 When switches 1 and 4 are closed, current is allowed to pass through the motor.

Page 41
Figure 16 H-Bridge Motor Counterclockwise Configuration
 Figure 3 shows the switch configuration for turning the motor in the opposite direction
from the configuration of Figure 3
 When switches 2 and 3 are closed, current is allowed to pass through the motor.

Figure 17 H-Bridge in an invalid configuration.


 Figure 4 shows an invalid configuration.
 Current flows directly to ground, creating a short circuit.
 The same effect occurs when switches 1 and 3 are closed or switches 2 and 4 are
closed.
 Table 10 shows some of the logic configurations for the H-Bridge design.

Page 42
Table 10 H-Bridge Logic Configurations
Motor Operation SW1 SW2 SW3 SW4
OFF Open Open Open Open
Clockwise Closed Open Open Closed
Counter Clockwise Open Closed Closed Open
Invalid Closed Closed Closed Closed
 H-Bridge control can be created using relays, transistors, or a single IC Solution such as
the L293.
 When using relays and transistors, must ensure that invalid configuration do not occur.
 Example:
A switch is connected to pin P2.7. Write a program to monitor the status of SW and
perform the following:
(a) If SW=0, the DC motor moves clockwise
(b) If SW=1, the DC motor moves counterclockwise
Solution:
ORG 0H
MAIN: CLR P1.0 ; Switch 1
CLR P1.1 ; Switch 2
CLR P1.2 ; Switch 3
CLR P1.3 ; Switch 4
SETB P2.7
MONITOR: JNB P2.7, CLOCKWISE
SETB P1.0 ; Switch 1
CLR P1.1 ; Switch 2
CLR P1.2 ; Switch 3
SETB P1.3 ; Switch 4
SJMP MONITOR
CLOCKWISE: CLR P1.0 ; Switch 1
SETB P1.1 ; Switch 2
SETB P1.2 ; Switch 3
CLR P1.3 ; Switch 4
SJMP MONITOR
END

Page 43
Motor Control Using L293

Figure 18 Bidirectional Motor Control Using L293 Chip


 Figure 18 shows the connection of L293 to an 8051.
 Example:
A switch is connected to pin P2.7. Write a program to monitor the status of SW and
perform the following:
(a) If SW=0, the DC motor moves clockwise
(b) If SW=1, the DC motor moves counterclockwise
Solution:
ORG 0H
MAIN: CLR P1.0
CLR P1.1
CLR P1.2
SETB P2.7
MONITOR: SETB P1.0 ; Enable the Chip
JNB P2.7, CLOCKWISE
CLR P1.1 ; Turn Motor counterclockwise
SETB P1.2
SJMP MONITOR

CLOCKWISE: SETB P1.1


CLR P1.2 ; Turn Motor clockwise
SJMP MONITOR
END
PWM:

 The speed of the motor depends on three factors


o Load
o Voltage
o Current
 For a given fixed load we can maintain a steady speed by using a method called Pulse

Page 44
Width Modulation(PWM)

Page 45
 By changing (modulating) the width of the pulse applied to the DC motor we
can increase or decrease the amount of power provided to the motor, thereby
increasing or decreasing the motor speed.
 Notice that although the voltage has a fixed amplitude, it has a variable duty cycle
 That means the wider the pulse, the higher the speed.
 PWM is do widely used in DC motor control that some microcontrollers come
with the PWM circuitry embedded in the chip.

Figure 19 Pulse Width Modulation Comparison


Optoisolator:
 An optoisolator (also known as optical coupler, optocoupler and opto-
isolator) is a semiconductor device that uses a short optical transmission path
to transfer an electrical signal between circuits or elements of a circuit, while
keeping them electrically isolated from each other.
 Advantage: Their high electrical isolation between the input and output
terminals allowing relatively small digital signals to control much large AC
voltages, currents and power.

Page 46

You might also like