Micro First Chapter
Micro First Chapter
Introduction to 8051
Microcontroller and its
Architecture
1
In summary, a microcontroller can be thought of as a single-chip computer
optimized for controlling devices, whereas a microprocessor is a powerful CPU
chip that forms the brain of a computer and needs additional support chips.
For instance, an 8051 microcontroller can be used to automatically control
appliances like a washing machine or microwave (specific task), while an
Intel Pentium microprocessor would be used in a PC for general computing
needs23.
2
largely 8/16/32-bit RISC Harvard-architecture MCUs with higher perfor-
mance and more peripherals on chip.
3
1.4 Embedded Systems Overview and Clas-
sification
An **embedded system** is a specialized computing system that performs
dedicated functions within a larger mechanical or electronic system. It typi-
cally consists of a microcontroller or microprocessor that runs firmware (soft-
ware) to control the device’s operation. Unlike a general-purpose computer,
an embedded system is designed for a specific task. For example, a digital
camera’s embedded system manages capturing images, storing them, and
interfacing with the user controls.
Definition: Embedded System. An embedded system is an electronic
system that integrates hardware and software (and perhaps mechanical
parts) to perform a specific dedicated function or set of functions. It is
“embedded” as part of a larger system or product and is not typically
directly visible as a standalone computer.
Embedded systems can be classified based on functionality and perfor-
mance requirements:
4
• Mobile Embedded Systems: Portable devices powered by micro-
controllers, designed with emphasis on battery efficiency and compact-
ness. Examples: smartphones (which have multiple embedded con-
trollers for different subsystems), fitness wearables, portable medical
devices. Mobility imposes constraints like low power consumption and
lightweight design.
5
• Special Function Registers (SFRs) – various control registers mapped
in upper memory (e.g. timers, I/O ports, serial port, interrupt regis-
ters).
• Four parallel I/O ports (Port 0,1,2,3, each 8-bit) providing 32 I/O
pins.
6
Program Status Word (PSW) – Flags: The PSW is an 8-bit register
that contains condition flags and status bits reflecting the outcome of oper-
ations and the current state of the processor. Only 6 of the 8 bits are used;
the remaining 2 are user-definable for general use78. The PSW bits are:
The flags CY, AC, and OV are affected by arithmetic operations (useful
for conditional branching or multi-byte math), while P provides a simple
even-parity check of the accumulator. The RS1–RS0 bits are critical for
switching register banks:
RS1 RS0 (PSW.4, PSW.3) Register Bank Address Range for R0–R7
002 Bank 0 (default) 0x00–0x07
012 Bank 1 0x08–0x0F
102 Bank 2 0x10–0x17
112 Bank 3 0x18–0x1F
7
service routines. It operates in a last-in-first-out (LIFO) manner. The Stack
Pointer (SP) is an 8-bit register that holds the address of the top of the
stack11. On the 8051, the stack grows upward (towards higher memory
addresses).
When data is PUSHed onto the stack, the SP is first incremented by
1, then the value is stored at the new top address. Conversely, on a POP,
the value is read from the address pointed by SP, then SP is decremented
by 1. Because SP is 8-bit, it can point anywhere in the 00h–FFh address
range of internal RAM; however not all of that is actual RAM (beyond 7Fh
is SFR space). In practice, the stack should be kept in the on-chip RAM
area (00h–7Fh, or up to 0xFF if the device has 256 bytes RAM).
By default, after a reset, SP = 0x0712. This means the stack will start
at address 0x08 for the first push (since SP is incremented then used). The
default location (0x08) is right after the register bank 0 area. This default
is chosen so that the registers R0–R7 (which occupy 0x00–0x07 in bank 0)
are not overwritten by the stack. The programmer can reinitialize SP to a
different address if needed (for example, to start the stack at a higher address,
leaving more room for global/variable storage below it).
Definition: Stack. A stack is a section of memory used for temporary
storage in a LIFO manner (Last In, First Out). In 8051, it is typically
used to store return addresses during function calls or interrupts, and to
save registers or data temporarily. The Stack Pointer (SP) register keeps
track of the top of this stack. Each PUSH adds an item to the stack and
increments SP, and each POP removes an item and decrements SP.
2. Data Memory: This is the RAM used for variable storage, stack,
and SFRs. The 8051 has 128 bytes of on-chip internal RAM (00h–7Fh
addresses) plus a separate 128-byte area for SFRs (80h–FFh addresses,
8
not all locations used). Additionally, the 8051 can address up to 64
KB of external data memory (using DPTR and MOVX instructions) if
needed.
9
0x7F
0x80 (SFRs)
General
Purpose
RAM
0x30-0x7F
Bit-
addressable 0x30
RAM
Register
(128 bits) 0x20
Banks
0x20-0x2F
(Bank
Default Stack Start (0x08)
0-3) 0x00
0x00-0x1F
Figure 1.1: Internal RAM layout of 8051 (128 bytes). The lower 32 bytes are
divided into 4 register banks (each bank has registers R0–R7). The next 16
bytes (0x20–0x2F) are bit-addressable memory. The addresses 0x30–0x7F
are general purpose RAM. By default, the stack pointer (SP) is initialized to
0x07, so the first available stack address is 0x08 (indicated by the arrow).
Some SFRs are bit-addressable (those ending in 0x0 or 0x8 typically). For
instance, PSW at 0xD0 is bit-addressable (so CY is D7h in bit-address space,
etc.), P0–P3 are bit-addressable (each port pin can be accessed as a bit).
It’s important to note that internal RAM (00h–7Fh) and SFRs (80h–FFh)
share the same address range numerically but are separate in how they are ac-
cessed: instructions that reference addresses above 7Fh automatically direct
to SFR memory space. If an 8051 variant has 256 bytes of RAM, the “upper”
128 bytes (0x80–0xFF) are accessed differently (via indirect addressing) to
avoid collision with SFR addresses.
10
Port 2 to form a 16-bit address bus for external memory (Port 0 serves
as multiplexed low-address/data bus AD0–AD7, Port 2 provides high
address A8–A15). The signal PSEN (Program Store Enable) is an
active-low output that the 8051 uses to read bytes from external pro-
gram memory. The pin EA (External Access) must be held low to
force all fetches to external memory; if EA is high, addresses in range
0000h–0FFFh are fetched from internal ROM, and above that from
external.
11
40 - VCC
39 - P0.0 (AD0)
38 - P0.1 (AD1)
P1.0 - 1
37 - P0.2 (AD2)
P1.1 - 2
36 - P0.3 (AD3)
P1.2 - 3
35 - P0.4 (AD4)
P1.3 - 4
34 - P0.5 (AD5)
P1.4 - 5
33 - P0.6 (AD6)
P1.5 - 6
32 - P0.7 (AD7)
P1.6 - 7
P1.7 - 8 31 - EA
RST - 9 30 - ALE
8051 29 - PSEN
P3.0 (RXD) - 10
P3.1 (TXD) - 11 28 - P2.7 (A15)
P3.2 (/INT0) - 12 27 - P2.6 (A14)
P3.3 (/INT1) - 13 26 - P2.5 (A13)
P3.4 (T0) - 14 25 - P2.4 (A12)
P3.5 (T1) - 15 24 - P2.3 (A11)
P3.6 (/WR) - 16 23 - P2.2 (A10)
P3.7 (/RD) - 17 22 - P2.1 (A9)
21 - P2.0 (A8)
XTAL2 - 18
XTAL1 - 19
GND - 20
Figure 1.2: 8051 Pin Configuration (40-pin DIP). Port pins are labeled with
their port designations and alternate functions (for Port 0, Port 2, Port 3).
12
address range remain available as I/O. - **Port 3 (P3.0–P3.7)**: Pins 10–17.
Port 3 also has internal pull-ups and is an 8-bit bidirectional port, but each
pin of Port 3 has an alternate function (mostly related to interrupts and
serial I/O): - P3.0 (pin 10) – RXD: Serial input (receive data) for UART. -
P3.1 (pin 11) – TXD: Serial output (transmit data) for UART. - P3.2 (pin
12) – INT0: External Interrupt 0 input. - P3.3 (pin 13) – INT1: External
Interrupt 1 input. - P3.4 (pin 14) – T0: Timer 0 external input (counter
trigger). - P3.5 (pin 15) – T1: Timer 1 external input. - P3.6 (pin 16) –
/WR: External Data Memory Write strobe (active low). - P3.7 (pin 17) –
/RD: External Data Memory Read strobe (active low).
When these functions are not needed, Port 3 pins can be used as gen-
eral I/O. But designers must be careful, e.g., if external interrupts are en-
abled, P3.2/P3.3 will be sampled for interrupt signals. - **XTAL1 (pin 19)
and XTAL2 (pin 18)**: These are connections for the external crystal or
resonator that provides the clock to the 8051. XTAL1 is the input to an
inverting oscillator amplifier, and XTAL2 is the output. Typically a quartz
crystal (such as 11.0592 MHz or 12 MHz) is connected across these pins with
small capacitors (20pF–40pF range) to ground (forming a Pierce oscillator).
Alternatively, an external clock source can be fed into XTAL1 while leaving
XTAL2 unconnected. - **RST (Reset, pin 9)**: This input is an active-high
reset signal. A high on this pin (for a minimum duration of 2 machine cycles)
will reset the microcontroller, terminating all activity and clearing registers
to default states. On reset, the program counter is set to 0000h (beginning
of program memory). Typically, a circuit (as described in the next section)
ensures that RST is held high momentarily at power-up. - **ALE (Address
Latch Enable, pin 30)**: This output pin provides a latch pulse for external
address demultiplexing. It emits a periodic square wave at 1/6th the oscilla-
tor frequency when the 8051 is accessing external memory. During external
memory operations, ALE is high during the byte where Port 0 has address
A0–A7, and falling edge of ALE signals that Port 0 will switch to data for
the next byte. ALE is usually connected to the enable of an external latch
(74HC373 or 74HC573) which holds the low address. If no external memory
is used, ALE still toggles at a constant rate (which can be used for timing
or as a general-purpose clock output in some cases). There is also a setting
in some 8051 variants to turn off the ALE toggling if not needed (to reduce
noise). - **PSEN (Program Store Enable, pin 29)**: This is an active-low
output control signal used to read external program memory. When the
8051 fetches code from external ROM, it activates PSEN (low) to enable the
ROM’s output. PSEN is essentially the read strobe for external program
memory. It is generally connected to the OE (Output Enable) of an external
EPROM or ROM. PSEN is not used for internal code fetches (when running
13
from internal ROM). - **EA (External Access, pin 31)**: EA is an active-
low input that determines the source of program code. If EA is held high
(logic 1) and the address is within the on-chip ROM range (0x0000–0x0FFF
for 4KB), the 8051 will fetch instructions from internal ROM. If the address
exceeds internal memory range or if EA is low, the 8051 will fetch from ex-
ternal program memory. In typical usage, EA is tied high when an on-chip
program is used exclusively. To use all code from external memory, EA is
tied low (in which case the internal ROM is ignored). - **VCC (pin 40) and
GND (pin 20)**: These are the power supply pins. The 8051 requires a +5V
DC supply (VCC = +5V, GND = 0V reference). Some modern variants
might run at lower voltages, but the classical 8051 is 5V.
In summary, the pins of the 8051 either serve as I/O lines or have ded-
icated roles in external memory interfacing and system control. An under-
standing of the pin functions is crucial when designing hardware with the
8051, to ensure proper connections for clock, reset, and any needed external
memory or peripherals.
14
To connect a crystal, one end of the crystal is connected to XTAL1 and
the other to XTAL2. Additionally, two small capacitors (typically 20pF
to 33pF) are connected from each XTAL pin to ground. These capacitors
stabilize the oscillation. If a precise external clock source is available, it
can be injected into XTAL1, with XTAL2 left unconnected. Alternatively, a
ceramic resonator can be used instead of a quartz crystal for the oscillator.
Some systems opt for an external oscillator module. In such cases, the
module’s output can drive XTAL1 directly. The oscillator circuit is crucial
because a stable clock ensures the microcontroller’s proper timing.
15
The designer should ensure the reset circuit is reliable. If RST is not given
a proper pulse at startup, the 8051 might start executing from an undefined
state or not execute the program reliably. Hence the emphasis on a proper
POR circuit or supervisor chip to manage reset.
VCC (+5V)
RST
10 µF
8.2 kΩ
GND
Figure 1.3: Typical reset circuit for 8051 (power-on and manual reset). On
power-up, the capacitor C charges, briefly pulling RST high. The resistor R
then pulls RST low allowing normal operation. The push-button provides a
manual reset by connecting RST to VCC when pressed.
In summary, the clock and reset are fundamental parts of the micro-
controller system design. The clock frequency chosen affects the instruction
timing and peripheral operation (e.g., timer tick rates, serial baud base). The
reset circuit ensures the 8051 starts cleanly. Once the clock is running and
the chip is out of reset, the 8051 will continuously fetch, decode, and execute
instructions from program memory, thus bringing the embedded system to
life.
1.9 Summary
In this unit, we have covered the foundational aspects of microcontrollers
using the 8051 as a representative example:
• Microcontrollers integrate CPU, memory, and I/O peripherals on one
chip, making them ideal for dedicated embedded applications. In con-
trast, microprocessors are standalone CPUs intended for general com-
puting, requiring external resources.
16
16-bit, 32-bit, etc.), and they are used in a vast array of applications
from home appliances to industrial systems.
• The Intel 8051 microcontroller has an 8-bit CPU and Harvard architec-
ture. It provides on-chip program ROM (4KB) and data RAM (128B),
along with special function registers for controlling built-in peripherals
like timers, serial port, and interrupt logic.
• Key CPU elements of the 8051 include the accumulator (for arith-
metic/logic operations), B register (for multiply/divide), general reg-
isters R0–R7 (which are bank-selectable), and the PSW register con-
taining status flags (carry, auxiliary carry, overflow, parity) and bank
select bits.
• The 8051 uses a stack in internal RAM for temporary storage, with
the stack pointer initialized to 0x07 on reset (stack starts at 0x08 by
default). The program counter (16-bit) tracks instruction addresses,
enabling up to 64KB of code space. The DPTR is a 16-bit index
register mainly for external memory addressing.
• The pin configuration of 8051 includes four 8-bit I/O ports. Port 0 and
Port 2 serve a dual role for external memory bus. Port 3 pins have
alternate functions (two external interrupts, two timer inputs, serial
RXD/TXD, and external memory read/write control). Proper use of
these pins is crucial when interfacing the 8051 with other hardware.
17
Overall, the 8051 architecture demonstrates the essential features of a mi-
crocontroller: a self-contained computing unit that can read sensors, make
decisions (based on its program logic), and control outputs in a timely man-
ner. Understanding these fundamentals will aid in learning to program the
8051 and interface it with external devices, which are covered in subsequent
units.
(a) 4 KB
(b) 128 bytes
(c) 256 bytes
(d) 4 MB
3. In the 8051, the register that holds the address of the next instruction
to execute is:
(a) Port 0
(b) Port 1
(c) Port 2
(d) Port 3
18
5. Upon reset, the 8051’s Stack Pointer is set to 07h. What will be the
address of the first value pushed onto the stack after reset (assuming
Bank 0 is in use)?
(a) 07h
(b) 08h
(c) 06h
(d) 0Fh
4. Discuss the 8051 I/O port structure and pin functions. How
do the alternate functions of Port 3 pins differ from Ports 0, 1, and 2?
What are the roles of ALE, PSEN, EA, XTAL1/2, and RST pins in an
8051 system?
1.12 Exercises
1. Find at least two everyday devices in your home or surroundings that
likely contain a microcontroller. Identify the tasks the microcontroller
performs in those devices.
2. The 8051 is running with a 11.0592 MHz crystal. Calculate the approx-
imate time for one machine cycle and determine how many machine
cycles occur in one second.
19
3. If the PSW register in an 8051 has the value 0x50 (in hex), identify
which register bank is currently in use and the status of the CY and
AC flags. (Hint: 0x50 in binary is 0101 00002 .)
4. Write the direct memory addresses (in hex) for the following 8051 SFRs:
Accumulator, B register, Stack Pointer, and Port 1. Why can’t these
SFR addresses be used for general RAM access?
20
Table 1.1: Differences between a Microcontroller and a Microprocessor
Aspect Microcontroller (µC) Microprocessor (µP)
Integration CPU, memory (RAM/ROM), CPU is standalone; requires
I/O ports, etc. all on a single external memory, I/O ports,
chip (self-sufficient system) and peripherals (dependent on
external chips)
Application Designed for specific control Designed for general-purpose
tasks in embedded systems computing tasks (can run large
operating systems)
Memory Includes on-chip program No on-chip program memory;
ROM/Flash and data RAM uses external memory (which
(small, e.g. a few KB) can be large in size)
I/O Multiple built-in I/O ports Limited or no built-in I/O
and interfaces for direct sen- ports; must interface via exter-
sor/actuator connection nal circuits for I/O
Power Consumption Generally low power consump- Higher power consumption
tion (suitable for battery- (due to higher clock speeds
powered devices) and external interfacing)
Performance Typically runs at lower clock Capable of high clock speeds
speeds (e.g. a few MHz) for (hundreds of MHz to GHz) for
simple control tasks complex computations
Architecture Often Harvard architecture Often Von Neumann architec-
(separate program and data ture (shared memory) and can
memory) and many are RISC- be CISC or superscalar designs
based
Cost Overall system cost is low Overall system cost is higher
(many functions on one chip) (requires multiple compo-
nents)
Examples 8051, AVR ATmega, Intel 8086, Intel Pentium,
PIC16F877, ARM Cortex- ARM Cortex-A72 (as part of
M series smartphone SoC)
21
Bit Name Description
PSW.7 CY (Carry) Carry flag (set if an arithmetic operation results in a carry out or bo
PSW.6 AC (Auxiliary Carry) Auxiliary carry for BCD (binary-coded decimal) arithmetic (carry fr
PSW.5 F0 User flag 0 (available for general purpose use).
PSW.4 RS1 Register bank select bit 1 (with RS0, selects active R0–R7 bank).
PSW.3 RS0 Register bank select bit 0.
PSW.2 OV (Overflow) Overflow flag (set if result of signed arithmetic is out of range).
PSW.1 – (User-definable flag, general purpose).
PSW.0 P (Parity) Parity flag. Set/cleared by hardware after each instruction to indica
Table 1.2: Program Status Word (PSW) Register Bits in 8051. Two bits
(PSW.5 and PSW.1) are available as user-defined flags9.
22