microprocessor-tiny chip 3 main parts alu,cu,register clock -check how fast it computes
What is a Microprocessor ->
-
-
A microprocessor is a small electronic chip that acts like the brain of a computer.
-
It processes data and controls tasks in a computer or electronic device.
-
Main Function:
-
Processes Instructions:
-
It takes input, performs calculations or logical operations, and gives output.
-
All operations (like adding, comparing numbers) happen inside this chip.
-
Works like a Brain:
-
Controls the System:
-
Just like your brain controls your body, the microprocessor controls all the parts of a computer or device.
-
Comes as a Chip:
-
Single Integrated Circuit (IC):
-
A microprocessor is usually a single chip made of millions of transistors.
-
Looks small, but very powerful.
-
๐ ️ Basic Parts of a Microprocessor:
-
Arithmetic Logic Unit (ALU):
-
Performs math and logical operations.
-
-
Control Unit (CU):
-
Controls the flow of data and instructions.
-
-
Registers:
-
Small memory units inside the processor to store data temporarily.
-
๐งฑ Microprocessor vs Microcontroller:
-
Microprocessor:
-
Only the CPU.
-
Needs external memory and I/O devices.
-
-
Microcontroller:
-
CPU + Memory + I/O all in one chip.
-
Used in small gadgets (e.g., washing machines, microwave ovens).
-
๐ฐ️ History:
-
First Microprocessor:
-
Intel 4004, introduced in 1971.
-
It could perform basic operations and was used in calculators.
๐ Common Uses of Microprocessors:
-
Used In:
-
Computers
-
Smartphones
-
ATMs
-
Cars (for engine control)
-
Smart devices (TVs, watches)
๐ง How It Works (Simple):
-
Step-by-step:
-
It gets instructions from memory (called "fetch").
-
It understands what to do (called "decode").
-
It does the job (called "execute").
-
It saves the result back in memory (if needed).
๐ Advantages:
-
Why it's useful:
-
Very fast
-
Small in size
-
Can handle complex tasks
-
Easy to program
❌ Limitations:
-
Things to know:
-
Needs external components (memory, I/O)
-
Cannot handle very big tasks alone
๐ What is a Microcontroller?
-
Definition:
-
A microcontroller is a small computer built into a single chip.
-
It has a processor (CPU), memory (RAM & ROM), and input/output ports (I/O) — all in one chip.
-
๐ง It Acts Like a Mini Computer
-
It can read input, process the input, and control devices (like motors, LEDs, sensors).
๐งฑ Main Parts of a Microcontroller:
-
It usually includes:
-
CPU – processes instructions.
-
RAM – for temporary data storage.
-
ROM/Flash – stores the program (permanent).
-
I/O ports – connect to buttons, sensors, motors, etc.
-
Timers & Counters – for delay and counting tasks.
-
ADC/DAC – convert signals between digital and analog.
-
⚙️ How It Works (Simple Steps):
-
Step-by-step:
-
Reads input (e.g., from a sensor or button).
-
Executes program stored in ROM.
-
Performs required operation (like turn on an LED).
-
Sends output to device (motor, speaker, etc.).
-
๐ค Where Is It Used?
-
Microcontrollers are used in embedded systems, like:
-
Washing machines
-
Microwave ovens
-
Air conditioners
-
Remote controls
-
Cars (automatic doors, engine control)
-
Toys
-
Smart watches
-
๐ Microcontroller vs Microprocessor:
| Feature | Microcontroller | Microprocessor |
|---|---|---|
| Meaning | Mini computer in one chip | CPU only |
| Components | CPU + RAM + ROM + I/O on one chip | Only CPU (needs external RAM, I/O) |
| Cost | Cheaper | More expensive |
| Power Consumption | Low | High |
| Used in | Small devices (embedded systems) | PCs, laptops |
๐งช Example Microcontrollers:
-
Some popular types:
-
8051 (used in learning)
-
Arduino (used in DIY projects)
-
PIC (used in industrial systems)
-
AVR (used in many consumer devices)
-
⚡ Advantages of Microcontrollers:
-
✅ Low cost
✅ Compact size
✅ Low power consumption
✅ Easy to program
✅ Suitable for small and automatic systems
❌ Disadvantages:
-
❌ Not suitable for very complex tasks
❌ Limited processing speed
❌ Limited memory compared to microprocessors
✅ In Simple Words:
-
A microcontroller is like a tiny computer on a chip that:
-
Reads input,
-
Runs a small program,
-
Controls a machine or device.
-
⭐ Features of 8051 Microcontroller
-
8-bit Processor
-
It processes 8 bits of data at a time.
-
Data bus is 8 bits wide.
-
-
4 KB of ROM (Program Memory)
-
Stores the program (code) permanently.
-
It is non-volatile (doesn’t lose data when power is off).
-
-
128 Bytes of RAM (Data Memory)
-
Temporary memory to store variables/data.
-
It is volatile (data is lost when power is off).
-
-
16-bit Timers (2 Timers: Timer 0 & Timer 1)
-
Used for timing operations like delays, counting, generating clock pulses, etc.
-
-
32 Input/Output (I/O) Pins
-
These pins are used to connect to external devices like sensors, LEDs, switches, etc.
-
Arranged in 4 ports (Port 0, Port 1, Port 2, Port 3), each with 8 pins.
-
-
1 Serial Communication Port (UART)
-
Used to send and receive data serially (one bit at a time) to/from other devices like computers or GSM modules.
-
-
4 Parallel I/O Ports
-
Used for parallel communication (multiple bits at a time).
-
These are Port 0, Port 1, Port 2, and Port 3.
-
-
6 Interrupt Sources
-
The 8051 can respond to events using interrupts (e.g., timer overflow, external signal).
-
Total 5 interrupt sources + 1 reset.
-
-
16-bit Program Counter
-
Keeps track of the next instruction to execute in the program.
-
-
Clock Frequency – 12 MHz (typically)
-
Needs an external crystal oscillator (usually 12 MHz) to operate.
-
1 machine cycle = 12 clock cycles.
-
Built-in Oscillator and Clock Circuit
-
Supports connection to an external crystal to generate clock signals.
-
Full Duplex UART
-
Can send and receive data at the same time using serial communication.
-
Boolean Processor
-
Can perform bit-level operations (like setting/clearing a single bit).
-
Harvard Architecture
-
Separate memory for program and data.
-
Allows faster and more efficient processing.
-
On-chip Stack
-
Uses internal RAM to manage function calls and temporary storage.
๐น Introduction to 8051 Architecture
-
The 8051 microcontroller is a powerful, widely used 8-bit microcontroller.
-
It was developed by Intel in 1980 and is used in embedded systems.
-
It has CPU + RAM + ROM + I/O ports + Timers + Serial port, all in one chip.
๐ธ Block Diagram Overview
The 8051 block diagram contains:
-
CPU (Central Processing Unit)
-
RAM (128 Bytes)
-
ROM (4KB)
-
4 I/O Ports (P0–P3)
-
Timers (2)
-
Serial Port
-
Interrupt Control
-
Oscillator & Clock Circuit
-
Internal Bus System
๐ธ Internal Components Overview
1. Central Processing Unit (CPU)
-
The brain of the 8051.
-
Fetches instructions, decodes them, and executes.
2. Arithmetic and Logic Unit (ALU)
-
Performs arithmetic (add, subtract) and logical (AND, OR, NOT) operations.
๐ธ Associated CPU Registers
A. Accumulator (A Register)
-
Most important register.
-
Used in all arithmetic and logic operations.
B. Register B
-
Used with Accumulator during multiplication and division.
C. Program Counter (PC)
-
16-bit register.
-
Holds the address of the next instruction to execute.
D. Data Pointer (DPTR)
-
16-bit register (split into DPH and DPL).
-
Used to access external memory.
E. DPH & DPL
-
DPH: Data Pointer High byte
-
DPL: Data Pointer Low byte
F. Stack Pointer (SP)
-
8-bit register.
-
Points to the top of the stack in RAM.
๐ธ Flag Register and Program Status Word (PSW)
PSW (8 bits total):
| Bit | Name | Function |
|---|---|---|
| 7 | CY | Carry flag |
| 6 | AC | Auxiliary carry (for BCD) |
| 5 | F0 | User-defined flag |
| 4 | RS1 | Register bank select 1 |
| 3 | RS0 | Register bank select 0 |
| 2 | OV | Overflow flag |
| 1 | — | Reserved |
| 0 | P | Parity flag (even=1, odd=0) |
➤ PSW is used to:
-
Store flags
-
Select register banks (there are 4 banks)
๐ธ Register Bank Selection
-
PSW bits RS1 and RS0 are used to choose a register bank:
-
Bank 0: RS1 = 0, RS0 = 0 (default)
-
Bank 1: RS1 = 0, RS0 = 1
-
Bank 2: RS1 = 1, RS0 = 0
-
Bank 3: RS1 = 1, RS0 = 1
-
๐ธ Memory Organization
A. Data Memory (RAM)
-
128 bytes total
-
Divided into:
-
Register banks (0–31)
-
Bit-addressable area (20 bytes)
-
General-purpose RAM
-
B. Program Memory (ROM/EPROM)
-
4KB ROM
-
Stores program code
C. Memory Mapping
-
Internal ROM: 0000H–0FFFH
-
Internal RAM: 00H–7FH
๐ธ I/O Ports
-
Four 8-bit ports:
-
P0, P1, P2, P3
-
-
Used to read inputs or send outputs
-
Each pin can be programmed individually
๐ธ Bit and Byte Addressing
-
Some memory and SFRs allow:
-
Bit-level access (single bits like P1.0)
-
Byte-level access (entire byte like P1 = 0xFF)
-
๐ธ Special Function Registers (SFRs)
A. Definition
-
SFRs control specific functions of the 8051 (timers, serial port, etc.)
B. Common SFRs:
| SFR | Use |
|---|---|
| P0–P3 | I/O ports |
| TMOD | Timer mode control |
| TCON | Timer control |
| SCON | Serial control |
| SBUF | Serial buffer |
| IE | Interrupt enable |
| IP | Interrupt priority |
| PSW | Program status word |
| SP | Stack pointer |
| DPTR | Data pointer |
C. SFR Map
-
Located in addresses 80H–FFH
-
Some are bit-addressable, others are not
๐ธ Timers and Counters
-
8051 has 2 timers/counters: Timer 0 and Timer 1
-
Used for:
-
Delays
-
Event counting
-
Generating square waves
-
Timer SFRs:
-
TMOD: Selects mode (13-bit, 16-bit, auto reload)
-
TCON: Starts/stops timers, interrupt flags
๐ธ Serial Communication
-
8051 has a serial port (UART)
-
Uses TxD (transmit) and RxD (receive) pins
SFRs:
-
SCON: Serial control
-
SBUF: Holds data to send or receive
๐ธ Interrupt System
-
8051 supports 5 interrupt sources:
-
External INT0
-
Timer 0
-
External INT1
-
Timer 1
-
Serial Port
-
Interrupt SFRs:
-
IE (Interrupt Enable): Turns interrupts on/off
-
IP (Interrupt Priority): Sets priority
๐ธ Oscillator and Clock Circuit
-
Needs an external crystal (typically 12 MHz)
-
Can work in range 4 MHz – 30 MHz
-
Controls the speed of instruction execution
๐ธ Bus System
A. Internal Data Bus
-
Carries data inside the 8051 between CPU, RAM, ROM, etc.
B. External Bus
-
Used to connect external memory or devices
-
8051 supports:
-
External program memory up to 64KB
-
External data memory up to 64KB
-
๐ 8051 Microcontroller Pin Description (40 Pins Total)
The 8051 has 40 pins, and each pin has a specific function.
✅ Pin Categories:
| Pin Number | Category | Description |
|---|---|---|
| 1–8 | Port 1 (P1.0 to P1.7) | I/O pins |
| 9 | Reset | Resets the microcontroller |
| 10–17 | Port 3 (P3.0 to P3.7) | I/O + Special functions |
| 18–19 | XTAL2, XTAL1 | Crystal oscillator connections |
| 20 | GND | Ground (0V) |
| 21–28 | Port 2 (P2.0 to P2.7) | I/O or higher address bus (A8–A15) |
| 29 | PSEN | Program Store Enable (external ROM) |
| 30 | ALE/PROG | Address Latch Enable / Program pulse |
| 31 | EA/VPP | External Access (0 = use ext ROM) |
| 32–39 | Port 0 (P0.0 to P0.7) | I/O or multiplexed address/data bus |
| 40 | VCC | Power supply (+5V) |
๐ Detailed Pin Descriptions
๐น 1–8: Port 1 (P1.0 – P1.7)
-
8-bit general-purpose I/O port.
-
No alternate function.
-
Can be used to connect LEDs, switches, sensors, etc.
๐น 9: Reset (RST)
-
Active high input.
-
A high signal (for at least 2 machine cycles) resets the 8051.
๐น 10–17: Port 3 (P3.0 – P3.7)
-
8-bit I/O port with alternate functions:
| Pin | Function | Alternate Use |
|---|---|---|
| P3.0 | RxD | Serial Input |
| P3.1 | TxD | Serial Output |
| P3.2 | INT0 | External Interrupt 0 |
| P3.3 | INT1 | External Interrupt 1 |
| P3.4 | T0 | Timer 0 input |
| P3.5 | T1 | Timer 1 input |
| P3.6 | WR | Write to ext. memory |
| P3.7 | RD | Read from ext. memory |
๐น 18 & 19: XTAL1 & XTAL2
-
Connect to external crystal oscillator (usually 12 MHz).
-
XTAL1: Input to internal oscillator.
-
XTAL2: Output from oscillator.
๐น 20: GND
-
Connect to ground (0V).
๐น 21–28: Port 2 (P2.0 – P2.7)
-
General-purpose I/O.
-
When using external memory, carries high-order address bus (A8–A15).
๐น 29: PSEN (Program Store Enable)
-
Used to read from external program memory (ROM).
-
Active LOW signal.
๐น 30: ALE / PROG
-
ALE (Address Latch Enable):
-
Used to separate address/data signals on Port 0.
-
-
PROG: In programming mode, this pin receives the programming pulse.
๐น 31: EA / VPP
-
EA (External Access):
-
EA = 1 → use internal ROM
-
EA = 0 → use external ROM
-
-
VPP: In programming mode, used as programming voltage input.
๐น 32–39: Port 0 (P0.0 – P0.7)
-
Dual-purpose port:
-
I/O port
-
Multiplexed address/data bus (A0–A7 / D0–D7)
-
-
When using external memory, used to transfer address & data.
๐น 40: VCC
-
Power supply pin.
-
Connect to +5V.
๐ง Summary Table (Grouped)
| Pin No. | Name | Function |
|---|---|---|
| 1–8 | P1.0–P1.7 | Port 1 – I/O only |
| 9 | RST | Reset input (active HIGH) |
| 10–17 | P3.0–P3.7 | Port 3 – I/O + alternate functions |
| 18–19 | XTAL1/2 | Crystal oscillator pins |
| 20 | GND | Ground |
| 21–28 | P2.0–P2.7 | Port 2 – I/O / High address bus (A8–A15) |
| 29 | PSEN | External program memory read control |
| 30 | ALE/PROG | Address latch / Programming pulse |
| 31 | EA/VPP | Internal/external ROM select |
| 32–39 | P0.0–P0.7 | Port 0 – I/O / Address+Data bus (A0–D7) |
| 40 | VCC | +5V Power supply |
๐ท 8051 Microcontroller: Pin Description
The 8051 microcontroller comes in a 40-pin Dual Inline Package (DIP), with several pins having multiple functions depending on the application (e.g., I/O, memory access, serial communication, etc.).
๐น 8051 Pin Overview
-
Total Pins: 40
-
Divided into:
-
4 I/O Ports: Port 0, 1, 2, 3
-
Power supply pins: Vcc, GND
-
Oscillator pins: XTAL1, XTAL2
-
Control pins: RST, ALE, PSEN, EA
-
-
Some pins are multiplexed with alternate functions (like serial, interrupt, timer).
๐จ I/O Ports
1. Port 0 (Pins 32–39)
-
Dual-function port:
-
Acts as general-purpose I/O.
-
Also serves as a multiplexed address/data bus when interfacing with external memory.
-
-
Used for:
-
Lower-order address (A0–A7) during the 1st clock cycle.
-
Data bus (D0–D7) during the 2nd cycle.
-
-
Requires external pull-up resistors when used as I/O.
2. Port 1 (Pins 1–8)
-
General-purpose I/O only.
-
Not multiplexed — no alternate functions.
-
Each pin can be used for digital input or output directly.
3. Port 2 (Pins 21–28)
-
General-purpose I/O, unless external memory is used.
-
When accessing external memory:
-
Carries high-order address (A8–A15).
-
-
Otherwise, works like regular input/output pins.
4. Port 3 (Pins 10–17)
-
Multifunctional port:
-
Can be used as general-purpose I/O.
-
Also supports special alternate functions.
-
๐ธ Port 3 Alternate Functions:
| Pin | Signal | Alternate Function |
|---|---|---|
| P3.0 | RXD | Serial input (receive data) |
| P3.1 | TXD | Serial output (transmit data) |
| P3.2 | INT0 | External interrupt 0 |
| P3.3 | INT1 | External interrupt 1 |
| P3.4 | T0 | Timer 0 external input |
| P3.5 | T1 | Timer 1 external input |
| P3.6 | WR | Write signal for ext. memory |
| P3.7 | RD | Read signal for ext. memory |
๐ฉ Power Supply Pins
๐ธ Vcc (Pin 40)
-
Connect to +5V DC power supply.
๐ธ Vss (Pin 20)
-
Connect to ground (0V).
๐ฆ Oscillator Pins
๐ธ XTAL1 (Pin 19)
-
Input to the internal oscillator circuit.
-
Connects to one side of the crystal oscillator.
๐ธ XTAL2 (Pin 18)
-
Output from the oscillator circuit.
-
Connects to the other side of the crystal oscillator.
✅ A crystal oscillator (commonly 12 MHz) is connected between XTAL1 and XTAL2 along with two small capacitors to ground.
๐ฅ Control and Special Function Pins
๐ธ RST (Pin 9) — Reset Input
-
Active high reset.
-
Must be held high for at least 2 machine cycles to reset the microcontroller.
-
Clears registers, sets PC to 0000H.
๐ธ ALE (Pin 30) — Address Latch Enable
-
Used when accessing external memory.
-
Helps separate address and data from multiplexed Port 0 using a latch (like 74LS373).
๐ธ PSEN (Pin 29) — Program Store Enable
-
Used to read program code from external ROM.
-
Acts as a read signal for external program memory.
๐ธ EA (Pin 31) — External Access
-
Controls whether the microcontroller uses internal or external program memory:
-
EA = 1 → Use internal ROM.
-
EA = 0 → Use external ROM only (fetches program externally).
-
✅ Summary Table: 8051 Pin Functions
| Pin # | Name | Function |
|---|---|---|
| 1–8 | P1.0–7 | Port 1 (General-purpose I/O) |
| 9 | RST | Reset input (active high) |
| 10–17 | P3.0–7 | Port 3 (I/O + special functions) |
| 18 | XTAL2 | Crystal oscillator output |
| 19 | XTAL1 | Crystal oscillator input |
| 20 | GND | Ground |
| 21–28 | P2.0–7 | Port 2 (I/O or A8–A15) |
| 29 | PSEN | External program memory read |
| 30 | ALE | Address latch enable |
| 31 | EA | Select internal or external ROM |
| 32–39 | P0.0–7 | Port 0 (I/O or A0–A7/D0–D7) |
| 40 | Vcc | +5V power supply |
๐ Internal and External Memories in 8051
๐ท 1. Memory Types in 8051
The 8051 has two main types of memory:
| Memory Type | Use | Location |
|---|---|---|
| Program Memory | Stores program (code) | Usually ROM |
| Data Memory | Stores temporary data/variables | Usually RAM |
๐น 2. Internal Memory of 8051
A. Internal ROM (Program Memory)
-
Size: 4 KB (4096 bytes)
-
Stores the user-written program.
-
Read-only memory (ROM) or Flash.
-
If EA (pin 31) = 1, the microcontroller uses internal ROM.
B. Internal RAM (Data Memory)
-
Size: 128 Bytes
-
Divided into 3 areas:
| Area | Address Range | Description |
|---|---|---|
| Register Banks | 00H – 1FH | 4 banks × 8 registers each |
| Bit-addressable area | 20H – 2FH | 16 bytes, each bit can be accessed |
| General-purpose RAM | 30H – 7FH | For variables and stack |
๐ธ 3. External Memory
A. External Program Memory
-
If the program is too large for the 4 KB internal ROM, external ROM (up to 64 KB) can be connected.
-
PSEN (pin 29) is used to read from this memory.
-
If EA = 0, the microcontroller ignores internal ROM and fetches code from external ROM.
B. External Data Memory
-
Up to 64 KB of external RAM can be used for data.
-
Accessed using:
-
MOVX instruction.
-
Port 0 → for address/data (multiplexed)
-
Port 2 → for high address (A8–A15)
-
WR (P3.6) and RD (P3.7) → to write/read
-
๐ง Important Memory Access Instructions
| Instruction | Used For | Accesses |
|---|---|---|
MOV | Internal data memory | RAM/SFR |
MOVC | Code memory (ROM) | Program memory (read only) |
MOVX | External data memory | External RAM |
๐ Memory Mapping Overview
Program Memory Map:
| Address | Description |
|---|---|
| 0000H–0FFFH | Internal ROM (4 KB) |
| 1000H–FFFFH | External ROM (if used) |
Data Memory Map:
| Address | Description |
|---|---|
| 00H–7FH | Internal RAM (128 bytes) |
| 80H–FFH | Special Function Registers (SFRs) |
| 0000H–FFFFH | External RAM (up to 64 KB) |
✅ Quick Comparison: Internal vs External Memory
| Feature | Internal Memory | External Memory |
|---|---|---|
| Speed | Faster | Slower (bus required) |
| Size (default) | ROM: 4KB, RAM: 128B | Up to 64 KB each |
| Required for use | No | Only if large program/data needed |
| Uses | Small programs | Large programs/data |
๐ง 8051 Microcontroller: Memory Overview
๐ท 1. Total Accessible Memory Range
The 8051 can access:
-
Program memory (ROM/EPROM): 64 KB maximum
-
Data memory (RAM): 64 KB maximum
These memories are separate (Harvard architecture).
๐ 2. Program Memory (ROM/EPROM)
A. Internal Program Memory
-
Size: 4 KB
-
Address range: 0000H to 0FFFH
-
Stores the user program (code).
-
If EA pin = 1, 8051 will use internal ROM.
B. External Program Memory
-
Can connect up to 64 KB external ROM.
-
Address range: 0000H to FFFFH
-
If EA pin = 0, internal ROM is disabled, and 8051 fetches code from external ROM only.
๐ถ EA (External Access) Pin Functionality
| EA Pin Value | Meaning | Program Source |
|---|---|---|
| EA = 1 | Use internal ROM (0000H–0FFFH) | Internal, then external |
| EA = 0 | Ignore internal ROM | External only |
๐ 3. Data Memory (RAM)
A. Internal Data Memory
-
Total: 256 bytes
-
Divided into:
-
Lower 128 bytes (00H–7FH) → General RAM
-
Upper 128 bytes (80H–FFH) → SFRs (Special Function Registers)
-
๐ Lower 128 Bytes (00H–7FH)
-
Accessible using direct and indirect addressing.
-
Organized into:
| Address Range | Purpose |
|---|---|
| 00H–1FH | Register banks (Bank 0–3) |
| 20H–2FH | Bit-addressable area (128 bits) |
| 30H–7FH | General-purpose RAM |
๐ Upper 128 Bytes (80H–FFH)
-
Used for SFRs.
-
Only accessible via direct addressing.
-
Some are bit-addressable, like P0, TCON, IE, etc.
B. External Data Memory
-
Up to 64 KB of external RAM can be added.
-
Accessed using:
-
MOVXinstruction -
Port 0 for address/data (multiplexed)
-
Port 2 for high-order address
-
P3.6 (WR) and P3.7 (RD) control signals
-
๐ 4. Internal RAM Organization (Lower 128 bytes: 00H–7FH)
A. Register Banks (00H–1FH)
-
Divided into 4 banks (each with 8 registers: R0–R7)
-
4 banks = 32 bytes total
| Bank | Address Range | Registers |
|---|---|---|
| Bank 0 | 00H–07H | R0–R7 |
| Bank 1 | 08H–0FH | R0–R7 |
| Bank 2 | 10H–17H | R0–R7 |
| Bank 3 | 18H–1FH | R0–R7 |
๐ธ Bank Selection Logic: PSW (Program Status Word)
| RS1 | RS0 | Selected Bank |
|---|---|---|
| 0 | 0 | Bank 0 (default) |
| 0 | 1 | Bank 1 |
| 1 | 0 | Bank 2 |
| 1 | 1 | Bank 3 |
B. Bit Addressable Area (20H–2FH)
-
16 bytes = 128 bits
-
Bit addresses: 00H to 7FH
-
You can access individual bits directly
-
Example:
SETB 21H.3→ Set bit 3 of byte at 21H
-
C. General Purpose RAM (30H–7FH)
-
Used to store:
-
Variables
-
Temporary data
-
Stack
-
-
Fully byte-addressable
๐งพ 5. Special Function Registers (SFRs)
A. Address Range: 80H to FFH
B. Used for controlling:
-
Timers
-
Serial port
-
I/O ports
-
Interrupts
-
CPU status
C. Addressability:
-
All SFRs are byte-addressable
-
Some are bit-addressable (e.g., P0, IE, TCON)
๐ฉ 6. Bit vs Byte Addressability
| Area | Addressable As | Notes |
|---|---|---|
| 20H–2FH (bit area) | Bit + Byte | 128 bits = 16 bytes |
| General-purpose RAM | Byte only | 30H–7FH |
| SFRs (some) | Bit + Byte | Like P1, TCON, IE |
| Register Banks | Byte only | R0–R7 in each bank |
✅ Summary Table: 8051 Memory Map
| Memory Type | Size | Address Range | Access Method |
|---|---|---|---|
| Internal ROM | 4 KB | 0000H–0FFFH | Direct (code fetch) |
| External ROM | 64 KB | 0000H–FFFFH | PSEN pin, EA = 0 |
| Internal RAM | 128 B | 00H–7FH | Direct/Indirect |
| Internal SFRs | 128 B | 80H–FFH | Direct only |
| External RAM | 64 KB | 0000H–FFFFH | MOVX instruction |
๐ Stack in 8051 Microcontroller
๐น 1. Definition and Purpose
-
The stack is a small memory area used for temporary data storage.
-
It follows LIFO (Last In, First Out) — last data stored is the first to be retrieved.
๐ Purpose:
-
To store return addresses, register values, or temporary data during:
-
Function calls (
CALL) -
Interrupt handling
-
Nested operations
-
Temporary variable storage (
PUSH,POP)
-
๐น 2. Role of the Stack in 8051
-
Enables:
-
Fast data storage and retrieval
-
Return address saving during subroutine or interrupt calls
-
Temporary storage for register values during operations
-
Example:
๐น 3. Stack Pointer (SP) Register
-
SP is an 8-bit register (can address 0x00–0xFF)
-
It always holds the address of the top of the stack
-
Increments before storing (PUSH), decrements after retrieving (POP)
๐น 4. Stack Operations
✅ PUSH
-
Stores data onto the stack
-
SP is incremented first, then data is stored at the new address
✅ POP
-
Retrieves data from the stack
-
Data is read from the current SP address, then SP is decremented
✅ CALL / RET
-
CALLpushes the return address onto the stack -
RETpops the return address and resumes execution
๐น 5. Stack Initialization
-
After reset, SP = 07H
-
So, first PUSH goes to 08H
๐น 6. Stack Location in RAM
Default Location
-
Starts from address 08H (just after Register Bank 0)
Can Be Relocated
-
You can change the stack location by assigning a different value to SP
-
Recommended to move stack above 30H to avoid conflicts with:
-
Register banks (00H–1FH)
-
Bit-addressable RAM (20H–2FH)
-
๐ธ Example:
๐น 7. Interaction with RAM Sections
| RAM Area | Address Range | Conflict Risk? |
|---|---|---|
| Register Banks | 00H–1FH | ❗Yes |
| Bit-addressable RAM | 20H–2FH | ❗Yes |
| Scratch-pad RAM | 30H–7FH | ✅ Safe to use |
| SFRs | 80H–FFH | ❌ Cannot use |
๐น 8. Precautions for Stack Positioning
-
✅ Avoid low RAM (00H–2FH) — used by registers and bit RAM
-
✅ Place stack in general RAM (30H–7FH)
-
❌ Do not exceed 7FH — upper RAM is for SFRs, not usable for stack
๐น 9. Illustration / Example
Stack Growth Example:
Initial:
After PUSH ACC:
-
SP becomes 08H
-
ACC content is stored at 08H
After another PUSH B:
-
SP becomes 09H
-
B register content stored at 09H
After POP B:
-
Data from 09H is restored to B
-
SP becomes 08H
Stack growth direction:
๐งพ Summary Table
| Operation | SP Change | Data Location |
|---|---|---|
| PUSH | SP = SP + 1 | Data → [SP] |
| POP | Data ← [SP] | SP = SP - 1 |
| CALL | Return Addr → SP | SP = SP + 2 |
| RET | Return Addr ← SP | SP = SP - 2 |