8051 Microcontroller Basics and Comparisons
8051 Microcontroller Basics and Comparisons
MODULE–I
8051 MICROCONTROLLER BASICS
Criteria for Choosing a Microcontroller
Meeting the computing needs of the task at hand efficiently and cost effectively
o Speed
o Packaging
o Power consumption
o The amount of RAM and ROM on chip
o The number of I/O pins and the timer on chip
o How easy to upgrade to higher performance or lower power-consumption versions
o Cost per unit
Availability of software development tools, such as compilers, assemblers, and debuggers
Wide availability and reliable sources of the microcontroller
The8051familyhasthelargestnumberofdiversified(multiplesource)suppliers
o Intel(original)
o Atmel
o Philips/Signetics
o AMD
o Infineon (formerly Siemens)
o Matra
o Dallas Semiconductor/Maxim
Difference between Microprocessor and Microcontroller
Microprocessor Microcontroller
Micro Controller is the heart of an embedded
Microprocessor is the heart of Computer system.
system.
It is only a processor, so memory and I/O Micro Controller has a processor along with
components need to be connected externally internal memory and I/O components.
Memory and I/O has to be connected externally, so Memory and I/O are already present, and the
the circuit becomes large. internal circuit is small.
You can’t use it in compact systems You can use it in compact systems.
As external components are low, total power
Due to external components, the total power
consumption is less. So it can be used with
consumption is high. Therefore, it is not ideal for the
devices running on stored power like
devices running on stored power like batteries.
batteries.
Most of the microprocessors do not have power Most of the microcontrollers offer power-
saving features. saving mode.
Microprocessor Microcontroller
It is used mainly in a washing machine, MP3
It is mainly used in personal computers.
players, and embedded systems.
Microprocessor has a smaller number of registers, so Microcontroller has more register. Hence the
more operations are memory-based. programs are easier to write.
Micro controllers are based on Harvard
Microprocessors are based on Von Neumann model
architecture
It is a byproduct of the development of
It is a central processing unit on a single silicon-based
microprocessors with a CPU along with other
integrated chip.
peripherals.
It has no RAM, ROM, Input-Output units, timers, and It has a CPU along with RAM, ROM, and other
other peripherals on the chip. peripherals embedded on a single chip.
It uses an external bus to interface to RAM, ROM, and
It uses an internal controlling bus.
other peripherals.
Microcontroller based systems run up to
Microprocessor-based systems can run at a very high
200MHz or more depending on the
speed because of the technology involved.
architecture.
It’s used for general purpose applications that allow
It’s used for application-specific systems.
you to handle loads of data.
It’s complex and expensive, with a large number of It’s simple and inexpensive with less number
instructions to process. of instructions to process.
It stands for Reduced Instruction Set It stands for Complex Instruction Set
1.
Computer. Computer.
It is a microprocessor architecture
This offers hundreds of instructions of
2. that uses small instruction set of
different sizes to the users.
uniform length.
Registers are used for procedure The stack is used for procedure arguments
6.
arguments and return addresses. and return addresses.
Simple addressing formats are The instructions interact with memory using
9.
supported. complex addressing modes.
Block
diagram
Physical It uses one single physical address for It uses two separate physical addresses
Address accessing and storing both data and for storing and accessing both
instructions. instructions and data.
Buses (Signal One common signal path (bus) helps It uses separate buses for the transfer
Paths) in the transfer of both instruction and of both data and instructions.
data.
Number of It requires two clock cycles for It executes any instruction using only
Cycles executing a single instruction. one single cycle.
Uses This method comes to play in the case This architecture is best for signal
of small computers and personal processing as well as microcontrollers.
computers.
Usage of This architecture does not waste any This type of architecture can result in
Space space. It is because the instruction space wastage. It is because the
memory can utilize the left space of instruction memory cannot utilize the
the data memory leftover space in the data memory.
Execution The speed of execution of the Von The overall speed of execution of
Speed Neumann Architecture is Harvard Architecture is comparatively
comparatively slower. It is because it faster. It is because the processor, in
is not capable of fetching the this case, is capable of fetching both
instructions and data both at the same instructions and data at the very same
time. time.
8051 Architecture
8051 has 4 KBytes of internal ROM. The address space is from 0000 to 0FFFh. If the program
size is more than 4 KBytes, 8051 will fetch the code automatically from external memory.
Accumulator is an 8-bit register widely used for all arithmetic and logical operations.
Accumulator is also used to transfer data between external memory. B register is used along
with Accumulator for multiplication and division. A and B registers together are also called
MATH registers.
PSW (Program Status Word). This is an 8-bit register which contains the arithmetic status of
ALU and the bank select bits of register banks.
The heart of the 8051 is the circuitry that generates the clock pulses by which all
internal operations are synchronized.
Pins XTAL1 and XTAL2 are provided for connecting a resonant network to form an
oscillator.
The 8051 requires an external oscillator circuit. The oscillator circuit usually runs
around 12 MHz. The crystal generates 12 million pulses in one second. The pulse is used
to synchronize the system operation in a controlled pace
A machine cycle is the minimum amount of time a simplest machine instruction must
take
An 8051 machine cycle consists of 12 crystal pulses (ticks).
Instructions with a memory operand need multiple memory accesses (machine cycles).
The other 32 are arranged as part of internal RAM in four banks, B0–B3, of
eight registers each, named R0 to R7.
The A (accumulator) register is used for many operations, including addition,
subtraction, integer multiplication and division, and Boolean bit
manipulations.
The A register is also used for all data transfers between the 8051 and any
external memory.
The B register is used with the A register for multiplication and division
operations.
Program Counter (PC) and Data Pointer (DPTR)
Program instruction bytes are fetched from locations in memory that are
addressed by the PC.
The PC is automatically incremented after every instruction byte is fetched
and may also be altered by certain instructions.
The PC is the only register that does not have an internal address.
The DPTR register is made up of two 8-bit registers, named DPH and DPL.
They are used to furnish memory addresses for internal and external code
access and external data access.
The DPTR is under the control of program instructions.
It can be specified by its 16-bit name, DPTR, or by each individual byte name,
DPH and DPL.
DPTR does not have a single internal address; DPH and DPL are each assigned
an address.
Note that all of the flags can be set and cleared by the programmer.
7 6 5 4 3 2 1 0
CY AC F0 RS1 RS0 OV - P
The Stack and the Stack Pointer
• The stack refers to an area of internal RAM that is used in conjunction with certain opcodes to
store and retrieve data quickly.
• The 8-bit Stack Pointer (SP) register is used by the 8051 to hold an internal RAM address that
is called the "top of the stack."
• The top of the stack is the location in internal RAM where the last byte of data was stored by a
stack operation.
• The SP increments before storing data on the stack so that the stack grows upward as data is
stored.
• As data is retrieved from the stack, the byte is read from the stack, and then the SP decrements
to point to the next available byte of stored data.
• The SP is set to 07h when the 8051 is reset and can be changed to any internal RAM address by
the programmer.
Pin Diagram
Pin out Description
Pins 1–8: PORT1
Each of these pins can be configured as an input or an output.
Pin 9: RESET
A logic one on this pin disables the microcontroller and clears the contents of most registers. In
other words, the positive voltage on this pin resets the microcontroller. By applying logic zero to
this pin, the program starts execution from the beginning.
Pin 31: EA
By applying logic zero to this pin, P2 and P3 are used for data and address transmission with no
regard to whether there is internal memory or not. It means that even if there is a program
written to the microcontroller, it will not be executed. Instead, the program written to external
ROM will be executed. By applying logic one to the EA pin, the microcontroller will use both
memories — first internal, then external (if it exists).
Address output (A0–A7) when the ALE pin is driven high (1), or
Data output (Data Bus) when the ALE pin is driven low (0).
Internal ROM
The 8051 is designed such that data memory and program code memory are in
separate physical spaces but have the same address range.
A block of internal ROM holds program code at addresses 0000h to 0FFFh.
The Program Counter (PC) typically addresses program bytes in the 0000h–
0FFFh range.
If the program exceeds 0FFFh, the 8051 automatically fetches code from
external program memory.
If the EA pin (Pin 31) is connected to ground, the entire code is fetched from
external memory (0000h to FFFFh).
The PC doesn’t care where the code resides; it’s up to the circuit designer to:
o Use only internal ROM
o Use only external ROM
o Or use a combination of both
Connection Summary:
Signal Purpose Connected To
Port 0 Multiplexed A0–A7 / D0–D7 Data Bus + Latch
Port 2 Higher address lines (A8–A15) Connected directly to memory
Controls latch for lower address
ALE Latch enable pin
byte
/RD Read signal for data memory (RAM) RAM's Read Enable
/WR Write signal for data memory (RAM) RAM's Write Enable
Read signal for code memory
/PSEN EPROM's Output Enable
(EPROM)
Ground (GND) if only external memory is
EA (pin 31) External Access pin (active low)
used
EA (External Access) Pin Behavior:
EA = 0 (connected to GND):
o Microcontroller uses only external memory for program code.
EA = 1 (connected to Vcc):
o Microcontroller uses internal ROM first.
o If program exceeds internal ROM size, it switches to external ROM
automatically.
2. Memory Organization
A memory chip is organized into a number of locations (or words).
Each location can store:
o 1 bit, 4 bits, 8 bits, 16 bits, etc., depending on the chip design.
Example:
A chip with 14 address pins and 8 data pins:
o Number of locations = 2¹⁴ = 16,384 (16K)
o Data per location = 8 bits (1 byte)
o Chip capacity = 2¹⁴ × 8 = 131,072 bits = 16 KB
Example:
MOV A, #40H ; Loads 40H (hex) into the accumulator
MOV R3, #0FFH ; Loads FFH into Register R3
2. Register Addressing Mode
Operands are registers (R0-R7, A, B, etc.).
Example:
MOV A, R5 ; Copies value from R5 to A
ADD A, R2 ; Adds R2 to A
3. Direct Addressing Mode
Operand is a memory location (internal RAM or SFR).
Example:
MOV A, 30H ; Copies data from RAM location 30H to A
MOV 40H, A ; Stores A’s value in RAM location 40H
4. Register Indirect Addressing Mode
A register (R0 or R1) holds the address of the operand.
Example:
MOV @R0, A ; Stores A’s value at the address in R0
MOV A, @R1 ; Loads A from the address in R1
5. Indexed Addressing Mode
Used with lookup tables in code memory (ROM).
Example:
MOVC A, @A+DPTR ; Reads from ROM at (A + DPTR)
MOVC A, @A+PC ; Reads from ROM at (A + PC)