MC&A Unit 3
MC&A 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)
Every instruction consists of: MOV A, #50H ; Move immediate value 50H into accumulator A
SUBB Subtract
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
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.
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.
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.
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).
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
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
DATA TRANSFER
INSTRUCTIONS
Data Transfer (MOV & MOVX)
MOV
Used to move data between registers, memory, and I/O ports.
Example
MOV 50H, A ; Store A’s content into RAM at 50H
Data Transfer (MOV & MOVX)
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)
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 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)
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 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.
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
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)
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.).
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
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)
6 — Reserved
7 — Reserved
6 — Reserved
TCON.7 TF1 Timer 1 Overflow Flag (Set when Timer 1 overflows, cleared in ISR)
TCON.5 TF0 Timer 0 Overflow Flag (Set when Timer 0 overflows, cleared in ISR)
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 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.
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.
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)
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)
void main() {
while (1) {
PWM_PIN = 1; // Turn ON PWM output
delay(50); // High time (TON)
ORG 0000H
SETB P1.0 ; PWM output pin
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.
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.