0% found this document useful (0 votes)
16 views76 pages

MC&A Unit 3

This document provides an overview of the 8051 instruction set, detailing the syntax, types of operands, and various instruction categories such as data transfer, arithmetic, logical, and branching instructions. It explains the structure of assembly language instructions, including labels, opcodes, operands, and comments, along with addressing modes like immediate, register, direct, indirect, and indexed. Additionally, it includes examples and use cases for each instruction type to illustrate their functionality within the 8051 microcontroller architecture.

Uploaded by

sarthakvshukla6
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)
16 views76 pages

MC&A Unit 3

This document provides an overview of the 8051 instruction set, detailing the syntax, types of operands, and various instruction categories such as data transfer, arithmetic, logical, and branching instructions. It explains the structure of assembly language instructions, including labels, opcodes, operands, and comments, along with addressing modes like immediate, register, direct, indirect, and indexed. Additionally, it includes examples and use cases for each instruction type to illustrate their functionality within the 8051 microcontroller architecture.

Uploaded by

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

UNIT 3

INSTRUCTION SET
8051 Instruction Set
General syntax for 8051 assembly language is as follows.
LABEL: OPCODE OPERAND ; COMMENT
LABEL: (THIS IS NOT NECESSARY UNLESS THAT SPECIFIC LINE HAS TO BE
ADDRESSED). The label is a symbolic address for the instruction. When the program is assembled, the label
will be given specific address in which that instruction is stored. Unless that specific line of instruction is
needed by a branching instruction in the program, it is not necessary to label that line.
OPCODE: Opcode is the symbolic representation of the operation. The assembler converts the opcode to a
unique binary code (machine language).
OPERAND: While opcode specifies what operation to perform, operand specifies where to perform that
action. The operand field generally contains the source and destination of the data. In some cases only
source or destination will be available instead of both. The operand will be either address of the data, or
data itself.
COMMENT: Always comment will begin with ; or // symbol. To improve the program quality,
programmer may always use comments in the program.
Types of Operand
What is an Operand?
The operand is the data on which an instruction operates.
It can be a constant, a register, or a memory address.
MOV A, #30H ; Here, #30H is the operand (immediate value)
MOV A, R1 ; Here, R1 is the operand (register)
MOV A, 40H ; Here, 40H is the operand (direct memory address)
MOV A, @R0 ; Here, @R0 is the operand (indirect memory location)

Operand Type Example Description

Immediate MOV A, #10H Direct constant value

Register MOV A, R2 Data in a register

Memory MOV A, 50H Data stored at a memory address

Indirect MOV A, @R1 Data from an address stored in a register


Instruction
What is an Instruction?
A command given to the microcontroller to perform a
specific task. e.g

Every instruction consists of: MOV A, #50H ; Move immediate value 50H into accumulator A

Mnemonic (operation name) ADD A, R2 ; Add the value of register R2 to A

Operands (values, registers, or memory addresses)

Instruction Type Example Description

Data Transfer MOV A, R1 Move data between registers/memory

Arithmetic ADD A, R3 Perform math operations

Logical ANL A, R2 Perform AND, OR, XOR operations

Branching SJMP LABEL Change program flow (jumps, calls)

Bit Manipulation SETB P1.0 Set/Clear specific bits


Mnemonic
What is a Mnemonic?
A short name for an assembly instruction. Mnemonic Meaning

Makes it easier to write and remember. MOV Move data

ADD Add two values

SUBB Subtract

ANL AND logic operation

ORL OR logic operation

CLR Clear (set to 0)

SETB Set bit to 1

Jump to another
JMP
instruction
Opcode (Operation Code)
What is an Opcode?
The machine code (binary/hex value) of an instruction.
Mnemonics are for humans, but microcontrollers
understand opcodes.

Mnemonic Opcode

MOV A, #30H 74 30

ADD A, R2 28

JMP 1000H 02 10 00
Addressing Modes

Immediate

Addressing Modes Register Direct

Memory Indirect

Indexed
Immediate Addressing Mode
In this mode, the operand (data) is directly given in the instruction.
The data is immediately available and does not need to be fetched from memory or
registers.

MOV destination, #data

MOV → Move instruction


destination → Register where the data is stored
#data → Immediate value (indicated by #)

Use Cases
Setting initial values for loops and counters.
Defining constants in a program.
Performing quick arithmetic operations without using memory.
Register Addressing Mode
The operand is stored in a register.
The instruction refers directly to the register.

MOV destination, source


MOV A, R3 ; Copy contents of Register R3 into Accumulator A
MOV R2, R1 ; Copy contents of Register R1 into Register R2

Use Cases
Fast execution (registers are inside CPU).
Used in loops where values need frequent updates.
Temporary storage for calculations
Direct Addressing Mode
The operand is located in an internal RAM address.
The instruction directly specifies the memory address

MOV A, 40H ; Load the value from RAM address 40H into A
MOV 50H, R2 ; Store the value of R2 into memory address 50H

Use Cases
Storing variables in internal RAM.
Flags and control registers can be accessed directly.
Used in embedded applications for memory-based operations.
Indirect Addressing Mode
A register (R0 or R1) holds the memory address instead of the actual data.
The instruction fetches data from the memory location pointed to by the register.

MOV R0, #40H ; Store address 40H in R0


MOV A, @R0 ; Fetch value from memory address 40H into A
MOV @R0, R3 ; Store the value of R3 into memory address 40H

Use Cases
Used for arrays and buffers (when data location is variable).
Accessing external RAM in 8051 (e.g., via MOVX).
Efficient when processing multiple memory locations dynamically.
Indexed Addressing Mode
Definition
The operand address is calculated using an offset (A) and a base register (DPTR or PC).
Used only for reading from Program Memory (ROM).

MOV DPTR, #2000H ; Load base address 2000H into DPTR


MOV A, #05H ; Load offset value 05H into A
MOVC A, @A+DPTR ; Fetch byte from ROM at (2000H + 05H)

Use Cases
Used for lookup tables in ROM (e.g., fonts, sine wave values).
Fetching pre-stored data from program memory.
Waveform generation, character mapping.
Comparison Table of Addressing Modes

Addressing Mode Example Where Used? Speed Use Cases

Fixed values,
Immediate MOV A, #10H Constants Fast
counters, delays

Temporary values,
Register MOV A, R2 Registers Fastest
quick operations

Variables, flags,
Direct MOV A, 30H Internal RAM Medium
buffers

Internal & External Arrays, flexible


Indirect MOV A, @R0 Medium
RAM memory access

MOVC A, Program Memory Lookup tables,


Indexed Slow
@A+DPTR (ROM) font storage
UNIT 3

DATA TRANSFER
INSTRUCTIONS
Data Transfer (MOV & MOVX)
MOV
Used to move data between registers, memory, and I/O ports.

Fetch the instruction (MOV A, R0) from program


memory.
Instruction Description Read the value inside R0 from the register file.
Copy the value from R0 into A (Accumulator).

MOV A, R0 Copy data from R0 to A Fetch the instruction (MOVX A, @DPTR).


Read the 16-bit address stored in DPTR.
Send the address over the external memory bus.
Load data from external Read the value from the external memory and store it
MOVX A, @DPTR in A.
memory

Example
MOV 50H, A ; Store A’s content into RAM at 50H
Data Transfer (MOV & MOVX)

Instruction Example Initial State Execution Steps Final State

1. Fetch instruction 2.
MOV A, #55H MOV A, #55H A = ?? A = 55H
Load 55H into A

1. Fetch instruction 2.
MOV R0, A MOV R0, A A = 55H, R0 = ?? A = 55H, R0 = 55H
Copy A into R0

1. Fetch instruction 2.
DPTR = 4000H,
Read memory at DPTR
MOVX A, @DPTR MOVX A, @DPTR External RAM at 4000H A = AAH
= 4000H3. Copy value
= AAH
into A

1. Fetch instruction 2.
XCH A, R1 XCH A, R1 A = 30H, R1 = 50H Swap values A = 50H, R1 = 30H
of A and R1
Arithmetic Instructions (ADD, SUBB, MUL, DIV)

Perform math operations inside the ALU (Arithmetic Logic Unit).

Instruction Description Example Execution Steps

1. Fetch A and R1 2.
ADD A, R1 Add register R1 to A. ADD A, R1 Perform A = A + R1 3. Store
result in A 4. Update flags.

1. Fetch A and R3 2.
Subtract R3 from A (with
SUBB A, R3 SUBB A, R3 Perform A = A - R3 3. Store
borrow).
in A 4. Update flags.

1. Fetch A and B 2. Multiply


(A * B) 3. Store result
MUL AB Multiply A and B. MUL AB
in A:B 4. Update overflow
flag.

1. Fetch A and B 2. Divide (A /


DIV AB Divide A by B. DIV AB B) 3. Store quotient in A,
remainder in B.
Arithmetic Instructions (ADD, SUBB, MUL, DIV)

Instruction Example Initial State Execution Steps Final State

1. Fetch instruction 2.
ADD A, R1 ADD A, R1 A = 10H, R1 = 20H Add A + R1 → 10H + A = 30H
20H = 30H3. Store in A

1. Fetch instruction 2.
A = 50H, R2 = 20H, CY =
SUBB A, R2 SUBB A, R2 Subtract A - R2 → 50H - A = 30H
0
20H = 30H3. Store in A

1. Fetch instruction 2.
Multiply A * B → 05H *
MUL AB MUL AB A = 05H, B = 04H A = 14H, B = 00H
04H = 14H3. Store A =
14H, B = 00H

1. Fetch instruction 2.
Divide A / B → 20H /
DIV AB DIV AB A = 20H, B = 04H A = 08H, B = 00H
04H3. Store quotient
in A, remainder in B
Logical Instructions (ANL, ORL, XRL, CPL)

Perform bitwise operations inside the ALU.

Instruction Description Example Execution Steps

1. Fetch A and R2 2. Perform A


ANL A, R2 AND A with R2. ANL A, R2
= A AND R2 3. Store in A.

1. Fetch A 2. Perform A = A OR
ORL A, #0FH OR A with immediate value. ORL A, #0FH
0FH 3. Store in A.

1. Fetch A and R3 2. Perform A


XRL A, R3 XOR A with R3. XRL A, R3
= A XOR R3 3. Store in A.

1. Fetch A 2. Invert all bits 3.


CPL A Complement A. CPL A
Store in A.
Logical Instructions (ANL, ORL, XRL, CPL)
Instruction Example Initial State Execution Steps Final State

1. Fetch instruction 2.
Perform A AND
A = 0CH (0000
ANL A, #0FH ANL A, #0FH A = 3CH (0011 1100B) 0FH → 0011 1100B AND
1100B)
0000 1111B3. Store result
in A

1. Fetch instruction 2.
Perform A OR
ORL A, #F0H ORL A, #F0H A = 12H (0001 0010B) A = F2H (1111 0010B)
F0H → 0001 0010B OR 1111
0000B3. Store result in A

1. Fetch instruction 2.
Perform A XOR
XRL A, #55H XRL A, #55H A = 3CH (0011 1100B) 55H → 0011 1100B XOR A = 6EH (0110 1110B)
0101 0101B3. Store result
in A

1. Fetch instruction 2.
Perform bitwise
CPL A CPL A A = 3CH (0011 1100B) A = C3H (1100 0011B)
complement of A3. Store
result in A
Rotate and Shift Instructions (RL, RR, RLC, RRC)
Used for data shifting, encryption, and serial communication.

Instruction Description Example Execution Steps

1. Fetch A 2. Rotate bits


RL A Rotate A left. RL A
left 3. Store in A.

1. Fetch A 2. Shift bits


Rotate A right through
RRC A RRC A right 3. Last bit moves
carry.
into carry.
Rotate and Shift Instructions (RL, RR, RLC, RRC)

Instruction Example Initial State Execution Steps Final State

1. Fetch
instruction 2.
A = 35H (0011 A = 6AH (0110
RL A RL A Rotate left
0101B) 1010B)
→ 0110 1010B3.
Store in A

1. Fetch
instruction 2.
A = 35H (0011 A = 9AH (1001
RR A RR A Rotate right
0101B) 1010B)
→ 1001 1010B3.
Store in A
Branching Instructions (JMP, CJNE, CALL, RET)
Used for looping and conditional execution

Instruction Description Example Execution Steps

1. Fetch instruction 2. Compute new address


SJMP LABEL Short jump to LABEL. SJMP LABEL
3. Change PC.

CJNE A, #30H, Compare A with 30H, 1. Compare A with 30H 2. If not equal, jump
CJNE A, #30H, LABEL
LABEL jump if not equal. to LABEL.
Branching Instructions (JMP, CJNE, CALL, RET)

Instruction Example Initial State Execution Steps Final State

1. Fetch
instruction 2.
SJMP LABEL SJMP NEXT PC = 0010H Compute new PC = NEXT
address 3.
Change PC

1. Fetch
instruction 2.
CJNE A, #30H, CJNE A, #30H,
A = 20H Compare A with 30 PC = NEXT
LABEL NEXT
H3. If not equal,
jump to NEXT
Bit Manipulation Instructions (SETB, CLR, CPL)
Used for controlling digital outputs (LEDs, Motors, etc.).

Instruction Description Example Execution Steps

Set bit P1.0 (Turn ON


SETB P1.0 SETB P1.0 1. Fetch instruction 2. Set P1.0 to 1.
LED).

Clear bit P1.0 (Turn OFF


CLR P1.0 CLR P1.0 1. Fetch instruction 2. Set P1.0 to 0.
LED).
Bit Manipulation Instructions (SETB, CLR, CPL)

Instruction Example Initial State Execution Steps Final State

1. Fetch
SETB P1.0 SETB P1.0 P1.0 = 0 instruction 2. P1.0 = 1
Set P1.0 = 1

1. Fetch
CLR P1.0 CLR P1.0 P1.0 = 1 instruction 2. P1.0 = 0
Clear P1.0 = 0

1. Fetch
instruction 2.
CPL P1.0 CPL P1.0 P1.0 = 1 P1.0 = 0
Complement P1.
0=0
Interrupts

Definition: An interrupt is a mechanism that allows


the microcontroller to pause the current program
execution and respond to an event (external or
internal) immediately.
Why are interrupts important?
Instead of continuously polling for an event, the
microcontroller can focus on executing the main
program and only respond when an event occurs.
This improves efficiency and responsiveness in
embedded systems.
Interrupt Mechanism in 8051

1. Interrupt Occurs – A hardware or software event triggers an interrupt.


2. Current Instruction Completes – The microcontroller completes the
execution of the current instruction.
3. PC (Program Counter) is Saved – The microcontroller stores the
current program address in the stack.
4. Jumps to Interrupt Vector Address – The microcontroller switches
execution to a predefined memory location where the Interrupt
Service Routine (ISR) is stored.
5. ISR Executes – The interrupt-specific task is performed.
6. RETI Instruction – The microcontroller executes the RETI (Return
from Interrupt) instruction, which restores the PC and resumes the
original program execution.
Types of 8051 Interrupts

Interrupt Source Vector Address Function

Triggered by a signal
IE0 External Interrupt 0 0003H
at INT0 (P3.2)

Triggered by a signal
IE1 External Interrupt 1 0013H
at INT1 (P3.3)

Triggered when Timer


TF0 Timer 0 Overflow 000BH
0 overflows

Triggered when Timer 1


TF1 Timer 1 Overflow 001BH
overflows

Triggered when Serial


RI/TI Serial Interrupt 0023H Port receives or
transmits data
Interrupt Enable Register (IE)
The IE (Interrupt Enable) Register is an 8-bit register that enables or disables specific
interrupts.

Bit Name Function

7 EA Enable All Interrupts (1 = ON, 0 = OFF)

6 — Reserved

5 ET2 Enable Timer 2 Interrupt

4 ES Enable Serial Port Interrupt

3 ET1 Enable Timer 1 Interrupt

2 EX1 Enable External Interrupt 1

1 ET0 Enable Timer 0 Interrupt

0 EX0 Enable External Interrupt 0


Interrupt Priority Register (IP)
The Interrupt Priority Register (IP) allows prioritization of interrupts.
Interrupt priority is useful when multiple interrupts occur simultaneously.
Each bit assigns high (1) or low (0) priority.
IP Register Bit Structure:

Bit Name Priority

7 — Reserved

6 — Reserved

5 PT2 Priority of Timer 2 Interrupt

4 PS Priority of Serial Port Interrupt

3 PT1 Priority of Timer 1 Interrupt

2 PX1 Priority of External Interrupt 1

1 PT0 Priority of Timer 0 Interrupt

0 PX0 Priority of External Interrupt 0


Interrupt Execution Process in 8051

1. Interrupt occurs (e.g., Timer 0 Overflow)


2. Current instruction completes
3. Program Counter (PC) is pushed to Stack
4. 8051 jumps to the Interrupt Vector Address
5. Interrupt Service Routine (ISR) executes
6. Execution returns to the main program using RETI instruction
Interrupt Execution Process in 8051
The decision between edge-triggered and level-triggered interrupts in the 8051 microcontroller is determined by
hardware design and the TCON (Timer Control) register.

Trigger Type How It Works? Use Case Example

The interrupt occurs only when a


Button Press Detection (Detect when
Edge-Triggered transition happens (e.g., from HIGH
pressed, ignore if held)
to LOW)

The interrupt remains active as long


Holding a Button Down (Interrupt
Level-Triggered as the signal is at a certain level
stays active while button is held)
(LOW or HIGH)
TCON
Bit Bit Name Function

TCON.7 TF1 Timer 1 Overflow Flag (Set when Timer 1 overflows, cleared in ISR)

TCON.6 TR1 Timer 1 Run Control (1 = Start Timer 1, 0 = Stop Timer 1)

TCON.5 TF0 Timer 0 Overflow Flag (Set when Timer 0 overflows, cleared in ISR)

TCON.4 TR0 Timer 0 Run Control (1 = Start Timer 0, 0 = Stop Timer 0)

External Interrupt 1 Flag (Set when INT1 is triggered, cleared in


TCON.3 IE1
ISR)

TCON.2 IT1 Interrupt 1 Trigger Mode (1 = Edge-Triggered, 0 = Level-Triggered)

External Interrupt 0 Flag (Set when INT0 is triggered, cleared in


TCON.1 IE0
ISR)

TCON.0 IT0 Interrupt 0 Trigger Mode (1 = Edge-Triggered, 0 = Level-Triggered)


Interrupt Execution Process in 8051
In 8051, external interrupts (INT0 and INT1) can be either edge-triggered or level-triggered, controlled by bits in
the TCON register.

Bit Name Function

1 = Edge-Triggered (Falling Edge), 0 =


IT0 (Bit 0) Interrupt Type for INT0
Level-Triggered (Active Low)

1 = Edge-Triggered (Falling Edge), 0 =


IT1 (Bit 2) Interrupt Type for INT1
Level-Triggered (Active Low)
Interrupt Prioritization in 8051
In 8051, each interrupt can have a priority set using the Interrupt Priority (IP) Register.
If two interrupts have different priorities, the one with the higher priority executes first.
If two or more interrupts have the same priority, the 8051 resolves conflicts based on a fixed internal polling
sequence.

Interrupt Priority Order

External Interrupt 0 (INT0) Highest

Timer 0 Overflow (TF0)

External Interrupt 1 (INT1)

Timer 1 Overflow (TF1)

Serial Interrupt (RI/TI) Lowest


Interrupt Prioritization in 8051
Example: If INT0 and Timer 0 Interrupt Have the Same Priority
INT0 (External Interrupt 0) and TF0 (Timer 0 Interrupt) both occur at the same time.
8051 will service INT0 first, because External Interrupt 0 has a higher polling priority than Timer 0.
After INT0 ISR completes, the 8051 will check if Timer 0’s interrupt flag (TF0) is still set and then
execute its ISR.
Special Case: What If an Interrupt is Triggered While Another is Running?
INT0 (External Interrupt 0) and TF0 (Timer 0 Interrupt) both occur at the same time.
If an interrupt is already executing, any other interrupt (even with the same priority) must wait until
the current ISR completes.
8051 does not support nested interrupts automatically.
If a higher-priority interrupt occurs during a lower-priority ISR, the microcontroller will pause the
current ISR and execute the higher-priority ISR first.
How to Handle Two Interrupts Efficiently?
1. Use different priority levels using the IP register to ensure the more critical
interrupt runs first.
2. Keep ISR execution short to avoid delaying other interrupts.
3. Manually enable nested interrupts if necessary

Summary
If two interrupts have the same priority, 8051 executes them based on this order:
1. External Interrupt 0 (INT0)
2. Timer 0 Overflow (TF0)
3. External Interrupt 1 (INT1)
4. Timer 1 Overflow (TF1)
5. Serial Interrupt (RI/TI)
If an interrupt is already running, the next interrupt must wait until ISR
completion.
Use the IP register to manually set priority levels if needed
Explanation of the Diagram:
1. Main Program Running
The 8051 executes its main program until an
interrupt occurs.
2. INT0 and Timer 0 Interrupts Triggered at the
Same Time
Both INT0 (External Interrupt 0) and Timer 0
Overflow (TF0) occur simultaneously.
8051 decides which one to execute first
based on the priority order:
INT0 is executed first (since it has a
higher fixed priority).
3. Executing INT0 ISR
The 8051 jumps to INT0 ISR, handling the
interrupt.
Meanwhile, Timer 0 is still waiting to be
executed.
4. Returning to Timer 0 ISR
Once the INT0 ISR completes, the 8051
checks for any pending interrupts.
Since TF0 is still set, the Timer 0 ISR
executes next.
5. Returning to Main Program
After both ISRs complete, the 8051 resumes
execution of the main program.
Operating Modes of 8051
The 8051 microcontroller has four operating modes, which define how timers
function for different applications like timing delays, event counting, and serial
communication. These modes are configured in the TMOD (Timer Mode)
Register.

Mode Function Description

Mode 0 13-bit Timer Mode Used for low-resolution timing (rarely used).

Mode 1 16-bit Timer Mode Most commonly used mode for precise timing delays.

Best for continuous time-based operations like baud


Mode 2 8-bit Auto-Reload Mode
rate generation.

Mode 3 Split Timer Mode Allows Timer 0 to act as two separate 8-bit timers.
Mode 0: 13-bit Shift Register Mode
Only the lower 13 bits of the 16-bit timer are used.
Timer range: 0x0000 to 0x1FFF (8192 counts).
Timer overflows when it reaches 8192 (instead of 65536 in Mode 1).
Rarely used because it has a limited range and low resolution.
Why is it rarely used
It has low resolution compared to Mode 1 (16-bit mode).
Mode 1 is preferred for general timing operations.
Mode 1: 16-bit Timer Mode (Most Common)
Uses all 16 bits of the timer register (THx and TLx).
Timer range: 0x0000 to 0xFFFF (0 to 65535 counts).
Overflow occurs at 65536 counts, setting the Timer Overflow Flag (TF0/TF1).
Best suited for time delays and event counting.

Best for generating accurate delays.


Most commonly used timer mode in 8051.
Mode 2: 8-bit Auto-Reload Mode
Uses only 8-bit register (TLx), while THx holds a constant reload value.
When TLx overflows, it is automatically reloaded from THx.
Useful for periodic tasks like baud rate generation in serial communication.

Perfect for repetitive timing applications


Used for serial communication (UART) baud rate control.
Mode 3: Split Timer Mode
Only Timer 0 can use Mode 3.
Timer 0 is split into two independent 8-bit timers (TL0 & TH0).
Timer 1 operates normally.
Useful when two independent timing operations are needed.
Allows two independent 8-bit timers using Timer 0.
Rarely used in modern applications.
Summary

Mode Bit Size Best Use Remarks

Mode 0 13-bit Low-resolution timing Rarely used

Mode 1 16-bit General-purpose timing Most common mode

Repetitive tasks, baud rate


Mode 2 8-bit Auto-Reload Reloads TLx automatically
generation

Two 8-bit timers using Timer


Mode 3 Split Mode Only Timer 0 supports this
0

For delays & event counting: Mode 1 (16-bit mode)


For continuous timing (e.g., UART baud rate): Mode 2 (Auto-Reload)
For two separate 8-bit timers: Mode 3 (Split Mode)
For very low-resolution timing: Mode 0 (13-bit mode) (rarely used).
UNIT 3

ANALOG AND DIGITAL


INTERFACES
Analog and Digital Signals

Analog vs Digital Signals


Analog signals are continuous and can take infinite values (e.g., temperature,
voltage).
Digital signals are discrete and take only binary values (0 or 1).
Why Analog Input is Required?
Sensors like temperature, pressure, light, and sound produce analog signals.
Microcontrollers work with digital data, so Analog-to-Digital Conversion (ADC)
is needed.
Why PWM?
Pulse Width Modulation (PWM) is a technique to control analog devices with
digital signals.
Used in motor speed control, LED brightness adjustment, and signal
generation.
Analog Signals
The 8051 microcontroller lacks an inbuilt ADC (Analog-to-Digital Converter)
An external ADC (e.g., ADC0804, ADC0808) is needed to read analog signals
in case it has to be connected to a digital pin
How does it work
Since 8051 does not have an inbuilt ADC, an external ADC0804 is
used to convert the analog signal into a digital value that the
microcontroller can process.
Step-by-Step Working:
A sensor (like a temperature or light sensor) generates an analog
voltage.
This voltage varies continuously and needs to be converted to
digital form.
ADC Conversion Process
The ADC0804 samples the analog voltage and converts it into an 8-
bit digital value (0 to 255).
The resolution of ADC0804 is: Step Size=Vref/256Step
If Vref = 5V, then each step represents 5V/256 = 19.53mV.
How does it work
Interfacing ADC0804 with 8051
The ADC0804 is connected to Port 1 (P1.0 – P1.7) of 8051 for digital
output.
Control signals are connected as:
WR (Write): Starts ADC conversion.
INTR (Interrupt): Becomes LOW when conversion is complete.
RD (Read): Reads the digital output.
How does it work
How does it work
Control Flow of ADC and 8051
1. Start ADC Conversion:
8051 sets WR = 0, then WR = 1 to start conversion.
2. Wait for Conversion:
8051 waits until INTR = 0, which indicates that the ADC has completed
the conversion.
3. Read the Digital Output:
8051 sets RD = 0 and reads the 8-bit digital value from the ADC output.
The higher the analog voltage, the higher the digital output (0-255).
4. Process the Data:
The digital value is used for further applications like displaying on LCD,
controlling motors, or transmitting over a serial interface.
How does it work
Example: Mapping ADC Output to Voltage
If ADC output is 128, the input voltage is:Vin=(128/256)×5V=2.5V
If ADC output is 255, the input voltage is 5V.
8051 Assembly Program
Circuit Connections
ADC0804 → 8051 Microcontroller
CS (Chip Select) → GND (Always enabled)
RD (Read) → P3.0
WR (Write) → P3.1
INTR (Interrupt) → P3.2
Data Output (D0 - D7) → P1.0 - P1.7 (Port 1)
8051 Assembly Program
ORG 0000H ; Start program from address 0

ADC_PORT EQU P1 ; Port 1 connected to ADC data output


RD_PIN EQU P3.0 ; Read control signal
WR_PIN EQU P3.1 ; Write control signal
INTR_PIN EQU P3.2 ; Interrupt pin from ADC

START:
CLR WR_PIN ; Start ADC conversion (WR = 0)
SETB WR_PIN ; WR = 1 to start conversion
WAIT_ADC:
JB INTR_PIN, WAIT_ADC ; Wait for ADC conversion to complete (INTR = 0)

CLR RD_PIN ; Enable ADC output (RD = 0)


MOV A, ADC_PORT ; Read ADC digital value into Accumulator A
SETB RD_PIN ; Disable ADC output (RD = 1)

SJMP START ; Repeat process continuously

END
8051 Assembly Program
Explanation of the Code
1. Initialization
ORG 0000H → Program starts from address 0.
Defines ADC_PORT (P1) as the input port.
Defines control signals (RD, WR, INTR) at P3.0, P3.1, P3.2.
2. Starting ADC Conversion
CLR WR_PIN → Set WR = 0 to start conversion.
SETB WR_PIN → WR = 1, ADC starts conversion.
3. Waiting for ADC Conversion
JB INTR_PIN, WAIT_ADC → The program waits until INTR = 0, indicating the conversion is complete.
4. Reading ADC Data
CLR RD_PIN → Enables ADC output (RD = 0).
MOV A, ADC_PORT → Reads the 8-bit digital value into Accumulator (A).
SETB RD_PIN → Disables ADC output (RD = 1).
5. Repeating the Process
SJMP START → Goes back to the start and repeats continuously.
8051 C program
Circuit Connections
ADC0804 → 8051 Microcontroller
CS (Chip Select) → GND (Always enabled)
RD (Read) → P3.0
WR (Write) → P3.1
INTR (Interrupt) → P3.2
Data Output (D0 - D7) → P1.0 - P1.7 (Port 1)
8051 C program
void main() {
unsigned char adc_value;
#include <reg51.h>
while (1) {
sbit RD = P3^0; // Read signal (Active ADC_Start(); // Start ADC conversion
adc_value = Read_ADC(); // Read ADC value
Low)
sbit WR = P3^1; // Write signal (Active P2 = adc_value; // Display ADC value on Port 2 (for testing)
Low) delay(500); // Small delay before next read
}
sbit INTR = P3^2; // ADC Interrupt
}
(Active Low)
/* Function to Start ADC Conversion */
void ADC_Start() {
void ADC_Start(); // Function to start
WR = 0; // Set WR low to start conversion
ADC conversion WR = 1; // Set WR high to trigger conversion
unsigned char Read_ADC(); // while (INTR == 1); // Wait until conversion is complete (INTR goes low)
}
Function to read ADC data
/* Function to Read ADC Data */
void delay(unsigned int time) { unsigned char Read_ADC() {
unsigned char adc_data;
unsigned int i, j;
for (i = 0; i < time; i++) { RD = 0; // Set RD low to enable ADC output
for (j = 0; j < 100; j++); adc_data = P1; // Read ADC digital value from Port 1
RD = 1; // Set RD high to disable ADC output
}
} return adc_data;
}
8051 C program
Step-by-Step Explanation
1. Initialize and Define Control Signals
RD = P3^0, WR = P3^1, INTR = P3^2 are assigned 8051 I/O pins.
ADC0804 is connected to Port 1, where digital data will be read.
2. ADC_Start() - Start ADC Conversion
WR = 0; WR = 1; → This triggers ADC0804 to start conversion.
The program waits using while (INTR == 1); until INTR goes low (conversion
complete).
3. Read_ADC() - Read Digital Output from ADC
RD = 0; → Enables ADC output.
adc_data = P1; → Reads 8-bit digital value from ADC.
RD = 1; → Disables ADC output after reading.
4. Display ADC Value on Port 2
P2 = adc_value; → For testing, output is sent to Port 2.
PWM (Pulse Width Modulation)
What is PWM?
PWM is a method to control power delivery by switching a signal ON and OFF
at a high frequency.
The average voltage output depends on the duty cycle.
Used for motor speed control, LED dimming, audio signal generation, and
power regulation.
PWM (Pulse Width Modulation)
Duty Cycle in PWM
Duty Cycle (%) = TON/[TON+TOFF]×100
TON = Time the signal is HIGH (ON).
TOFF = Time the signal is LOW (OFF).
A higher duty cycle means higher power.
Example of PWM Duty Cycles:

ON OFF
Duty Cycle (%) Time Time Effect
(TON) (TOFF)

10% Short Long Dim LED / Slow Motor

50% Equal Equal Medium Brightness / Speed

90% Long Short Full Brightness / High Speed


PWM (Pulse Width Modulation)
Generating PWM in 8051
The 8051 does NOT have a dedicated PWM module, but we can generate PWM
using Timers and GPIOs.
Methods to Generate PWM in 8051
1. Using Timers (Best for precise control)
2. Using Software Delays (Simple, but not efficient)
Generating PWM in 8051
The 8051 does NOT have a dedicated PWM module, but we can generate PWM
using Timers and GPIOs.
Methods to Generate PWM in 8051
1. Using Timers (Best for precise control)
2. Using Software Delays (Simple, but not efficient)
PWM (Pulse Width Modulation)
8051 PWM Using Timers
Timers in 8051 help generate precise PWM signals by controlling ON/OFF
durations.
#include <reg51.h>

sbit PWM_PIN = P1^0; // PWM output pin

void delay(unsigned int time) {


unsigned int i, j;
for (i = 0; i < time; i++) {
for (j = 0; j < 100; j++);
}
}

void main() {
while (1) {
PWM_PIN = 1; // Turn ON PWM output
delay(50); // High time (TON)

PWM_PIN = 0; // Turn OFF PWM output


delay(50); // Low time (TOFF)
}
}
PWM (Pulse Width Modulation)
PWM Using Timer Interrupts (More Efficient)
Instead of software delays, we can use Timer Interrupts for a stable PWM output.

ORG 0000H
SETB P1.0 ; PWM output pin

MOV TMOD, #01H ; Timer 0 in Mode 1 (16-bit timer)


MOV TH0, #0FDH ; Load high byte for 1ms delay
MOV TL0, #0A8H ; Load low byte for 1ms delay
SETB TR0 ; Start Timer 0

PWM_LOOP:
CPL P1.0 ; Toggle PWM output pin
JNB TF0, PWM_LOOP ; Wait until Timer overflows
CLR TF0 ; Clear Timer 0 overflow flag
MOV TH0, #0FDH ; Reload Timer for next cycle
MOV TL0, #0A8H
SJMP PWM_LOOP ; Repeat process

END
PWM (Pulse Width Modulation)
Pulse Width Modulation (PWM) is a crucial technique used in microcontrollers
like 8051 for controlling devices that require varying power levels.
Duty Cycle
Definition: The percentage of time the signal stays HIGH (ON) in one complete
cycle.
Example:
25% Duty Cycle: LED glows dimly.
50% Duty Cycle: LED glows at medium brightness.
75% Duty Cycle: LED glows brightly.
Frequency of PWM
Frequency (Hz) = Number of ON/OFF cycles per second.
A higher frequency means smoother output, reducing flicker in LEDs and
vibrations in motors.
For 8051, PWM frequency is controlled using Timers.
PWM (Pulse Width Modulation)
Applications of PWM
Motor Speed Control – Adjusts DC motor speed by changing duty cycle, used in
fans and automation.
LED Dimming – Controls brightness by varying ON/OFF time, used in displays
and lighting.
Servo Motor Control – Positions servo motors by adjusting pulse width, used in
robotics and CNC machines.
Power Supply Regulation – Used in SMPS, DC-DC converters, and inverters for
efficient power management.
Audio Signal Generation – Generates sound waves for buzzers and speakers.
Communication Systems – Used in IR remote controls and RF transmissions for
wireless communication.
Digital Inputs-Outputs
8051 microcontroller has four ports (P0, P1, P2, P3) for digital I/O.
Each port pin can function as input or output.
Used for switches, LEDs, sensors, relays, motors, and communication.

Digital input detects HIGH (1) or LOW (0) signals.


Switches, buttons, sensors provide digital input.
Pull-up resistors may be needed for proper signal detection.

if (P1_0 == 0) { // If switch is pressed (active low)


P2_0 = 1; // Turn on LED
}
Digital Inputs-Outputs
Digital output controls LEDs, motors, and actuators.
Output is HIGH (1) or LOW (0) to turn devices ON or OFF.

P1_0 = 1; // Turn LED ON


delay(1000);
P1_0 = 0; // Turn LED OFF
Digital Inputs-Outputs
Configuring Ports as Input or Output
By default, all ports act as inputs.
Writing ‘1’ to a port pin makes it an input.
Writing ‘0’ to a port pin makes it an output.
P1 = 0xFF; // Set P1 as input
P2 = 0x00; // Set P2 as output
Digital Inputs-Outputs
Applications of Digital I/O
Digital Inputs – Switches, buttons, IR sensors, motion detectors.
Digital Outputs – LEDs, buzzers, relays, motors.
Communication – UART, SPI, I2C use digital signals.
Industrial Automation – Sensors detect input, relays control machinery.
UNIT 3

MEMORY MAPPING
Memory Mapping of 8051 (Internal & External)
1. Internal Memory Mapping of 8051
The 8051 has on-chip memory, including both ROM and RAM:
a) Internal ROM (Program Memory)
Size: 4 KB (0000H – 0FFFH)
Stores: Program code (firmware).
Accessed via: Program Counter (PC).
Execution: The 8051 fetches instructions from internal ROM
unless EA (External Access) pin is low.
Memory Mapping of 8051 (Internal & External)
b) Internal RAM (Data Memory)
Size: 128 bytes (00H – 7FH)
Divided into:
Register Banks (00H – 1FH)
32 registers (R0 – R7) arranged in four banks.
Bit Addressable Memory (20H – 2FH)
16 bytes (128 bits) that can be addressed bit-wise.
General-Purpose RAM (30H – 7FH)
Used for variables and stack storage.
Memory Mapping of 8051 (Internal & External)
Special Function Registers (SFR)
Address Range: 80H – FFH
Purpose: Controls timers, serial ports, I/O ports, interrupts, etc.
Examples:
P0 (Port 0) – Address 80H
P1 (Port 1) – Address 90H
P2 (Port 2) – Address A0H
P3 (Port 3) – Address B0H
TMOD (Timer Mode Register) – Address 89H
PCON (Power Control Register) – Address 87H
Memory Mapping of 8051 (Internal & External)
External Memory Mapping of 8051
The 8051 supports external memory expansion, including both
external ROM and external RAM.
a) External Program Memory (ROM)
Size: Up to 64 KB (0000H – FFFFH).
Required when: Code exceeds 4 KB internal ROM.
Access via: PSEN (Program Store Enable) pin.
EA (External Access) Pin:
EA = 1 (Vcc) → Uses internal ROM.
EA = 0 (GND) → Uses external ROM.
Memory Mapping of 8051 (Internal & External)
External Data Memory (RAM)
Size: Up to 64 KB (0000H – FFFFH).
Access via: MOVX instruction.
Controlled by: RD (Read) and WR (Write) pins.
Access Methods:
Indirect Addressing (via DPTR or R0/R1)
MOVX instruction for external RAM access.

You might also like