eiot u1 learn

 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:

  1. 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:

  1. 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:

  1. 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:

  1. Arithmetic Logic Unit (ALU):

    • Performs math and logical operations.

  2. Control Unit (CU):

    • Controls the flow of data and instructions.

  3. Registers:

    • Small memory units inside the processor to store data temporarily.


๐Ÿงฑ Microprocessor vs Microcontroller:

  1. Microprocessor:

    • Only the CPU.

    • Needs external memory and I/O devices.

  2. Microcontroller:

    • CPU + Memory + I/O all in one chip.

    • Used in small gadgets (e.g., washing machines, microwave ovens).


๐Ÿ•ฐ️ History:

  1. First Microprocessor:

  • Intel 4004, introduced in 1971.

  • It could perform basic operations and was used in calculators.


๐Ÿ” Common Uses of Microprocessors:

  1. Used In:

  • Computers

  • Smartphones

  • ATMs

  • Cars (for engine control)

  • Smart devices (TVs, watches)


๐Ÿง  How It Works (Simple):

  1. Step-by-step:

  2. It gets instructions from memory (called "fetch").

  3. It understands what to do (called "decode").

  4. It does the job (called "execute").

  5. It saves the result back in memory (if needed).


๐Ÿš€ Advantages:

  1. Why it's useful:

  • Very fast

  • Small in size

  • Can handle complex tasks

  • Easy to program


Limitations:

  1. Things to know:

  • Needs external components (memory, I/O)

  • Cannot handle very big tasks alone

it's designed for general-purpose tasks like in computers.

๐Ÿ” What is a Microcontroller?

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

  1. It can read input, process the input, and control devices (like motors, LEDs, sensors).


๐Ÿงฑ Main Parts of a Microcontroller:

  1. 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):

  1. Step-by-step:

    1. Reads input (e.g., from a sensor or button).

    2. Executes program stored in ROM.

    3. Performs required operation (like turn on an LED).

    4. Sends output to device (motor, speaker, etc.).


๐Ÿค– Where Is It Used?

  1. 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:

FeatureMicrocontrollerMicroprocessor
MeaningMini computer in one chipCPU only
ComponentsCPU + RAM + ROM + I/O on one chipOnly CPU (needs external RAM, I/O)
CostCheaperMore expensive
Power ConsumptionLowHigh
Used inSmall devices (embedded systems)PCs, laptops

๐Ÿงช Example Microcontrollers:

  1. 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:

  1. ✅ Low cost
    ✅ Compact size
    ✅ Low power consumption
    ✅ Easy to program
    ✅ Suitable for small and automatic systems


Disadvantages:

  1. ❌ Not suitable for very complex tasks
    ❌ Limited processing speed
    ❌ Limited memory compared to microprocessors


In Simple Words:

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

  1. 8-bit Processor

    • It processes 8 bits of data at a time.

    • Data bus is 8 bits wide.


  1. 4 KB of ROM (Program Memory)

    • Stores the program (code) permanently.

    • It is non-volatile (doesn’t lose data when power is off).


  1. 128 Bytes of RAM (Data Memory)

    • Temporary memory to store variables/data.

    • It is volatile (data is lost when power is off).


  1. 16-bit Timers (2 Timers: Timer 0 & Timer 1)

    • Used for timing operations like delays, counting, generating clock pulses, etc.


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


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


  1. 6 Interrupt Sources

    • The 8051 can respond to events using interrupts (e.g., timer overflow, external signal).

    • Total 5 interrupt sources + 1 reset.


  1. 16-bit Program Counter

    • Keeps track of the next instruction to execute in the program.


  1. Clock Frequency – 12 MHz (typically)

  • Needs an external crystal oscillator (usually 12 MHz) to operate.

  • 1 machine cycle = 12 clock cycles.


  1. Built-in Oscillator and Clock Circuit

  • Supports connection to an external crystal to generate clock signals.


  1. Full Duplex UART

  • Can send and receive data at the same time using serial communication.


  1. Boolean Processor

  • Can perform bit-level operations (like setting/clearing a single bit).


  1. Harvard Architecture

  • Separate memory for program and data.

  • Allows faster and more efficient processing.


  1. 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):

BitNameFunction
7CYCarry flag
6ACAuxiliary carry (for BCD)
5F0User-defined flag
4RS1Register bank select 1
3RS0Register bank select 0
2OVOverflow flag
1Reserved
0PParity 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:

SFRUse
P0–P3I/O ports
TMODTimer mode control
TCONTimer control
SCONSerial control
SBUFSerial buffer
IEInterrupt enable
IPInterrupt priority
PSWProgram status word
SPStack pointer
DPTRData 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:

    1. External INT0

    2. Timer 0

    3. External INT1

    4. Timer 1

    5. 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 NumberCategoryDescription
1–8Port 1 (P1.0 to P1.7)I/O pins
9ResetResets the microcontroller
10–17Port 3 (P3.0 to P3.7)I/O + Special functions
18–19XTAL2, XTAL1Crystal oscillator connections
20GNDGround (0V)
21–28Port 2 (P2.0 to P2.7)I/O or higher address bus (A8–A15)
29PSENProgram Store Enable (external ROM)
30ALE/PROGAddress Latch Enable / Program pulse
31EA/VPPExternal Access (0 = use ext ROM)
32–39Port 0 (P0.0 to P0.7)I/O or multiplexed address/data bus
40VCCPower 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:

PinFunctionAlternate Use
P3.0RxDSerial Input
P3.1TxDSerial Output
P3.2INT0External Interrupt 0
P3.3INT1External Interrupt 1
P3.4T0Timer 0 input
P3.5T1Timer 1 input
P3.6WRWrite to ext. memory
P3.7RDRead 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.NameFunction
1–8P1.0–P1.7Port 1 – I/O only
9RSTReset input (active HIGH)
10–17P3.0–P3.7Port 3 – I/O + alternate functions
18–19XTAL1/2Crystal oscillator pins
20GNDGround
21–28P2.0–P2.7Port 2 – I/O / High address bus (A8–A15)
29PSENExternal program memory read control
30ALE/PROGAddress latch / Programming pulse
31EA/VPPInternal/external ROM select
32–39P0.0–P0.7Port 0 – I/O / Address+Data bus (A0–D7)
40VCC+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:

PinSignalAlternate Function
P3.0RXDSerial input (receive data)
P3.1TXDSerial output (transmit data)
P3.2INT0External interrupt 0
P3.3INT1External interrupt 1
P3.4T0Timer 0 external input
P3.5T1Timer 1 external input
P3.6WRWrite signal for ext. memory
P3.7RDRead 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 #NameFunction
1–8P1.0–7Port 1 (General-purpose I/O)
9RSTReset input (active high)
10–17P3.0–7Port 3 (I/O + special functions)
18XTAL2Crystal oscillator output
19XTAL1Crystal oscillator input
20GNDGround
21–28P2.0–7Port 2 (I/O or A8–A15)
29PSENExternal program memory read
30ALEAddress latch enable
31EASelect internal or external ROM
32–39P0.0–7Port 0 (I/O or A0–A7/D0–D7)
40Vcc+5V power supply


๐Ÿ“˜ Internal and External Memories in 8051


๐Ÿ”ท 1. Memory Types in 8051

The 8051 has two main types of memory:

Memory TypeUseLocation
Program MemoryStores program (code)Usually ROM
Data MemoryStores temporary data/variablesUsually 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:

AreaAddress RangeDescription
Register Banks00H – 1FH4 banks × 8 registers each
Bit-addressable area20H – 2FH16 bytes, each bit can be accessed
General-purpose RAM30H – 7FHFor 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

InstructionUsed ForAccesses
MOVInternal data memoryRAM/SFR
MOVCCode memory (ROM)Program memory (read only)
MOVXExternal data memoryExternal RAM

๐Ÿ” Memory Mapping Overview

Program Memory Map:

AddressDescription
0000H–0FFFHInternal ROM (4 KB)
1000H–FFFFHExternal ROM (if used)

Data Memory Map:

AddressDescription
00H–7FHInternal RAM (128 bytes)
80H–FFHSpecial Function Registers (SFRs)
0000H–FFFFHExternal RAM (up to 64 KB)

Quick Comparison: Internal vs External Memory

FeatureInternal MemoryExternal Memory
SpeedFasterSlower (bus required)
Size (default)ROM: 4KB, RAM: 128BUp to 64 KB each
Required for useNoOnly if large program/data needed
UsesSmall programsLarge 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 ValueMeaningProgram Source
EA = 1Use internal ROM (0000H–0FFFH)Internal, then external
EA = 0Ignore internal ROMExternal 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 RangePurpose
00H–1FHRegister banks (Bank 0–3)
20H–2FHBit-addressable area (128 bits)
30H–7FHGeneral-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:

    • MOVX instruction

    • 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

BankAddress RangeRegisters
Bank 000H–07HR0–R7
Bank 108H–0FHR0–R7
Bank 210H–17HR0–R7
Bank 318H–1FHR0–R7

๐Ÿ”ธ Bank Selection Logic: PSW (Program Status Word)

RS1RS0Selected Bank
00Bank 0 (default)
01Bank 1
10Bank 2
11Bank 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

AreaAddressable AsNotes
20H–2FH (bit area)Bit + Byte128 bits = 16 bytes
General-purpose RAMByte only30H–7FH
SFRs (some)Bit + ByteLike P1, TCON, IE
Register BanksByte onlyR0–R7 in each bank

✅ Summary Table: 8051 Memory Map

Memory TypeSizeAddress RangeAccess Method
Internal ROM4 KB0000H–0FFFHDirect (code fetch)
External ROM64 KB0000H–FFFFHPSEN pin, EA = 0
Internal RAM128 B00H–7FHDirect/Indirect
Internal SFRs128 B80H–FFHDirect only
External RAM64 KB0000H–FFFFHMOVX 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:

assembly

PUSH ACC ; Saves Accumulator on the stack POP ACC ; Restores Accumulator from the stack

๐Ÿ”น 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

  • CALL pushes the return address onto the stack

  • RET pops 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:

assembly

MOV SP, #2FH ; Moves stack start address to 30H

๐Ÿ”น 7. Interaction with RAM Sections

RAM AreaAddress RangeConflict Risk?
Register Banks00H–1FH❗Yes
Bit-addressable RAM20H–2FH❗Yes
Scratch-pad RAM30H–7FH✅ Safe to use
SFRs80H–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:

assembly

SP = 07H

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:

mathematica

Stack grows upward in RAM SP increasesPUSH SP decreasesPOP

๐Ÿงพ Summary Table

OperationSP ChangeData Location
PUSHSP = SP + 1Data → [SP]
POPData ← [SP]SP = SP - 1
CALLReturn Addr → SPSP = SP + 2
RETReturn Addr ← SPSP = SP - 2