The 8051 Microcontroler Based Embedded Systems
The 8051 Microcontroler Based Embedded Systems
The 8051 Microcontroler Based Embedded Systems
for
The 8051 Microcontroller
based
Embedded Systems
(First Edition)
Manish K Patel
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
CHAPTER 1
INTRODUCTION TO
MICROCONTROLLERS
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
CHAPTER OUTLINE
COMPUTER SYSTEM
MICROPROCESSOR, MICROCOMPUTER AND
MICROCONTROLLER
CLASSIFICATION OF MICROCONTROLLERS
CHOOSING A MICROCONTROLLER
APPLICATIONS OF MICROCONTROLLERS
OVERVIEW OF THE 8051 FAMILY
EMBEDDED SYSTEMS
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
COMPUTER SYSTEM
Basic components of a computer system.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Central processing unit
The brain of a computer is the central processing unit. It consists
of group of circuits that determine the operations that the
computer can perform.
CPU components
Arithmetic and Logic Unit (ALU)
Registers
Instruction register (IR)
Program counter (PC)
Stack pointer (SP)
Instruction decoder and control unit
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
General Block diagram of a CPU
(Microprocessor)
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Memory
The memory is used to store data and binary instructions. It is
normally organized as several modules (chips), where each
module contains several memory locations. Each location may
contain part or all of the data or instruction. CPU reads (fetches)
the instructions from the memory and performs operations
(indicated by instructions) on data.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
I/O unit
I/O port:
The hardware in a computer that allows information
transfer between external world and computer is called I/O
port.
I/O device (Peripherals):
The device that gives information to computer is called
input device. For example, keyboard, mouse, joystick,
microphone, A/D converters are all input devices.
I/O interfacing circuits:
The circuits that are used to interconnect (interface) I/O
devices with a computer or I/O ports are called I/O
interfacing circuits. For example, buffers, latches and
voltage level converters are all interfacing circuits.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
System bus
Address bus
It is group of wires used by CPU to identify specific
memory location within a memory chip (also to identify
specific memory chip out of many chips present in a
computer system) and to identify I/O devices as well.
Data bus
It transfers data or instructions between CPU and
memory or I/O devices. It is bidirectional because data
can be transferred in both directions i.e. from CPU to
memory (or output devices) or from memory or input
devices to CPU.
Control bus
It is used to enable memory and I/O devices to perform
read or write operations. It regulates all activities on the
bus and specifies timing and direction of the data
transfer. Read (RD), write (WR) and memory/ I/O (M/
I/O) are most common control signals.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Computer block diagram with buses and interfacing circuits
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
MICROPROCESSOR, MICROCOMPUTER
AND MICROCONTROLLER
Microprocessor is a central processing unit (CPU) built
into a single semiconductor chip. The structure of
microprocessor is same as CPU.
Microcomputer is a small computer built using
microprocessor as a central element. It includes all
necessary components required for an application. The
I/O devices and memory (types and amount) of a
microcomputer are chosen as per the specific
application.
Microcontroller is an entire computer built into a single
semiconductor chip. . It includes data and code
memory, various on-chip peripherals like
timers/counters, serial port, A/D converters, D/A
converters etc, interface controllers, and general
purpose I/O ports which allow it to directly interface to
external environment.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Microcomputer system built around microprocessor
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Block diagram of microcontroller
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Comparison between microprocessor and
microcontroller.
Microprocessor Microcontroller
Microprocessor is complete functional CPU
i.e. it contains ALU, registers, stack pointer,
program counter, instruction decode and
control unit and interrupt processing
circuits.
Microcontroller is complete functional
microcomputer i.e. it contains the circuitry of
microprocessor and in addition it has built in
memory (ROM, RAM), I/O circuits and
peripherals necessary for an application.
Microprocessor instruction sets are data
processing intensive, means powerful
addressing modes and many instructions to
move data between memory and CPU to
handle large volumes of data.
Microcontrollers have instruction sets that are
related to the control of inputs and outputs,
means they have many bit handling instructions
along with byte processing instructions.
Microprocessor based products are
primarily designed to interact with humans
and are more flexible to design
Microcontroller based products are primarily
designed to interact with machines; once a
system is designed they are less flexible.
Access times for external memory and I/O
devices are more, resulting in a slower
system.
Access times for on-chip memory and
I/O devices are less, resulting in a faster
system.
Microprocessor based systems require
support devices and are usually bulkier,
costly, less reliable and consume more
power.
Microcontroller based systems require less
external hardware, reducing PCB size and
hence are compact, cheaper, more reliable and
consume less power.
Software protection is not possible because
of the requirement of external code
memory.
Software protection is possible because of on-
chip code memory.
The 8051 microcontroller based Embedded systems, First edition http://www.mhhe.com/patel/mbes
CLASSIFICATION OF MICROCONTROLLERS
Word length:
4 bit Microcontrollers
8 bit Microcontrollers
16 bit Microcontrollers
32 bit Microcontrollers
64 bit Microcontrollers
Memory Architecture:
Von Neumann
Harvard Architectures
Core Architecture:
Microcoded
Hardwired designs
Instruction set architectures:
CISC
RISC
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
von Neumann Architecture:
It has single memory storage to hold both program
instructions and data i.e. common program and data
space. The CPU can either read an instruction or data
from the memory one at a time.
The advantage of Von Neumann architecture is simple
design of microcontroller chip because only one memory
is to be implemented which in turn reduces required
hardware. The disadvantage is slower execution of a
program.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Harvard Architecture:
It has physically separate memory storage to hold
program instructions and data i.e. separate program and
data space. Since it has separate buses to access
program and data memory, it is possible to access
program memory and data memory simultaneously. The
advantage of a Harvard architecture microcontroller is that
it is faster for a given circuit complexity because it offers
greater amount of parallelism. The disadvantage is that it
requires more hardware, because two sets of buses and
memory blocks are required.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Microcoded design
Microcode is a group of instructions used to
implement the instructions of a microcontroller/
processor. It resides in a ROM or a programmable
logic array (PLA) that is part of the microcontroller
chip. The microinstruction is group of bits used to
represent the sequence of control signals to fetch,
decode and execute the instruction i.e. control
signals (in a sequence) for every instruction are
generated using memory.
The 8051 microcontroller based Embedded systems, First edition http://www.mhhe.com/patel/mbes
Hardwired design
A hardwired microcontroller/processor uses the bit
patterns of the instructions to select and activate specific
circuits (may be unique to the each instruction) to execute
the instructions. All control signals (or sequence of steps)
required to fetch, decode and execute the instructions are
generated and controlled by combinatorial logic and state
machine circuitry.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
CISC: Complex Instruction Set
Computer
Complex hardware: complex as well as more addressing
modes, variable instruction size.
Many clock cycles to execute an instruction.
High code density- small program size.
Complex data types.
RISC: Reduced Instruction Set
Computer
Simple hardware: simple and less addressing modes,
fix instruction size.
Single clock cycle execution, uniform instruction format.
Low code density- Larger program size.
Few data types in hardware.
Emphasis is on software: Compiler design is more
complex.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
APPLICATIONS OF MICROCONTROLLERS
Household appliances: Microwave oven, washing
machine,
Office and commercial appliances: Fax machine,
photocopier
Telecommunication: Telephones, phone answering
machines, mobile phones
Entertainment and gaming: Televisions, VCRs, music
players
Automotive industry: Fuel injection, ABS
Industrial automation and manufacturing: Motor control
systems, data acquisition and supervisory systems,
industrial robots,
Electronic measurement instruments: Digital
multimeters, logic analyzers
Biomedical systems: ECG recorder, blood cell analyzers
Computer systems: Keyboard controller, CD drive or
hard disk Military weapons, guidance and positioning
systems.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
OVERVIEW OF THE 8051 FAMILY
Features of the 8051 (MCS 51) family
The key features of 8051 microcontroller are:
8 bit CPU with Boolean processing capabilities.
4K bytes on-chip *program memory.
128 bytes on-chip data memory.
64 Kbytes each program and external data address space.
32 bidirectional I/O lines organized as four 8-bit I/O ports.
serial port Full duplex UART.
2 16-bit timers/counters.
Two-level prioritized interrupt structure.
Direct byte and bit addressability.
Four register banks.
Binary or decimal arithmetic support.
Hardware multiply and divide operations.
12 clock cycles per machine cycle
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
The 8051 block diagram.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Comparison of hardware resources of MCS 51
family.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Feature 8031 8051 8751 8032 8052 8752
Program
memory
None
ROM less
4K ROM
4K
EPROM
None
ROM
less
8K ROM
8K
EPROM
Data memory
128
Bytes
128
Bytes
128 RAM
256
Bytes
256
Bytes
256
Bytes
Timers/
Counters
(16-bit)
2 2 2 3 3 3
I/O pins 32 32 32 32 32 32
Serial port 1 1 1 1 1 1
Interrupt
sources (Reset
not included)
5 5 5 6 6 6
EMBEDDED SYSTEMS
They are designed to perform specific (or limited)
tasks.
They are tightly constrained with respect to power
consumption, size, design, testing and
manufacturing costs.
These constraints are achieved by selecting
microcontroller speed just sufficient to satisfy
computational needs, limited memory, and limited
peripheral resources to achieve design goal.
They guarantee the response to events and
completion of tasks within specified time. This is
more popularly known as real time operation.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
CHAPTER 2
PROGRAMMING MODEL AND
ARCHITECTURE OF THE 8051
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
CHAPTER OUTLINE
THE 8051 ARCHITECTURE
PROGRAMMING MODEL OF THE 8051
ON-CHIP MEMORY ORGANIZATION
EXTERNAL MEMORY ORGANIZATION
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
THE 8051 ARCHITECTURE
ALU
Memory
Peripherals
Timing and control unit
Oscillator
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Block diagram of the 8051 microcontroller
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
ALU
Arithmetic and logic unit performs all arithmetic
(addition, subtraction, multiplication and division) and
logical (AND, OR, NOT, EXCLUSIVE-OR and
rotating) operations on 8 bit data i.e. the 8051 has 8
bit ALU. The ALU also updates information about the
nature of the result in the flag register (PSW).
Memory
The 8051 family has separate on-chip program and
data memory. The program instructions are stored in
a program memory. , 80C51 has 4Kbytes of on-chip
ROM, whereas 80C52 has 8Kbyte (ROM) , 87C51
has 4Kbytes (EPROM) and 87C52 has
8Kbytes(EEPROM) of on-chip program memory.
Data memory can be on-chip or off-chip. Internal data
memory (RAM) in 80C51 is 128 bytes and in 80C52
is 256 bytes.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Peripherals
The 8051 has two 16 bit timers (8052 has three
timers) that are used for timing and counting
applications. It has full duplex serial port (UART) to
handle serial data transmission and reception.
Timing and control unit
This unit generates all timing and control signals
necessary for the execution of instructions and
synchronizes all internal activities with the clock.
Oscillator
The 8051 has an internal (on-chip) oscillator circuit
which generates the clock pulses by which all internal
operations are synchronized. Normally quartz crystal
is used to make oscillator functional. Typically 12
MHz crystal is used.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
PROGRAMMING MODEL OF THE
8051
The programming model of 8051 contains 8 (or 16)
bit registers and memory locations. Each register (or
memory location) has an internal 1byte address with
exception of program counter. Some registers are
byte as well as bit addressable i.e. whole byte of data
stored in a register can be accessed (read/write) at a
time or individual bits can be accessed at a time.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Programming model of the 8051
The 8051 microcontroller based Embedded systems, First edition http://www.mhhe.com/patel/mbes
ON-CHIP MEMORY ORGANIZATION
Special function registers (SFRs)
Internal RAM
Internal ROM
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Special function registers (SFRs):
Math registers: A and B.
Status register: PSW (Program Status Word)
Program counter: PC
Pointer registers: DPTR (Data Pointer) and SP (Stack Pointer)
Input output port latches: P0, P1, P2, and P3.
Peripheral data registers: TL0, TH0, TL1, TH1, and SBUF.
Peripheral control registers: IP, IE, TMOD, TCON, SCON, and PCON
Internal RAM
The 8051 has 128 bytes of internal RAM
Register Banks: Bank 0, Bank 1, Bank 2 and Bank 3 (00H to 1FH)
Bit Addressable RAM: Memory locations from addresses 20H to 2FH.
General Purpose RAM: Memory locations from addresses 30H to 7FH.
Internal ROM
The 8051 has 4Kbytes of internal ROM.
It is used to store program instructions to be executed by the
microcontroller. It may also be used to store permanent data like
constants, passwords and lookup tables.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Special function registers (SFRs)
Accumulator: A
Accumulator is the most useful and versatile register because it is used
in
i) all arithmetic operations like addition, subtraction, multiplication and
division.
ii) Majority of logical operations like logical AND, OR, NOT, EX-OR and
Rotate
iii) all data transfer between the 8051 and any external memory.
B
B is used along with A in multiplication operation to hold one of the
operand (either multiplier or multiplicand) and to store higher order
byte of the result. It is also used in division operation to hold divisor
and to store remainder of the result.
PSW
Program status word is an 8 bit register. It is also referred as flag
register or processor status word.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Program status word structure
The 8051 microcontroller based Embedded systems, First edition http://www.mhhe.com/patel/mbes
PSW.7 PSW.6 PSW.5 PSW.4 PSW.3 PSW.2 PSW.1 PSW.0
CY AC F0 RS1 RS0 OV -- P
Bit Symbol Flag name and description
7 C (or CY) Carry; Used in arithmetic, logic and Boolean operations
6 AC Auxiliary carry ; useful only for BCD arithmetic
5 F0 Flag 0; general purpose user flag
4 RS1 Register bank selection bit 1
3 RS0 Register bank selection bit 0
RS1 RS0
0 0 Bank 0
0 1 Bank 1
1 0 Bank 2
1 1 Bank 3
2 0V Overflow; used in arithmetic operations
1 -- Reserved; may be used as a general purpose flag
0 P
Parity; set to 1 if A has odd number of ones, otherwise reset
to 0
Program counter: PC
Program counter (PC) is a 16-bit register. It always contains the
memory address of the next instruction to be executed i.e. it points to
the instruction that is to be executed next. As the CPU fetches the op-
code (instruction byte) from the program memory, the PC is
incremented automatically point to the next instruction.
Data Pointer: DPTR
DPTR is a 16 bit register. It is used to point to data byte in external data
(RAM) or program (ROM) memory. It can be used as a single 16 bit
register or can also be accessed as two separate 8 bit registers named
DPL and DPH, where DPH means higher byte of the DPTR and DPL is
lower byte of the DPTR.
Stack pointer: SP
Stack pointer always points to the top of the stack and used to access
data from there. It is an 8 bit register. The data is stored on to the stack
using PUSH and CALL instructions and retrieved using POP and RET
instructions.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
I/O port registers (latches): P0, P1, P2 and P3.
The 8051 has four 8 bit ports named as P0, P1 P2 and P3, each can be
used as an input or output or both. All ports are byte as well as bit
addressable.
Peripheral data registers: TL0, TH0, TL1, TH1, and SBUF.
TL0 (timer 0 lower byte) and TH0 (timer 0 higher byte) together represents
a16 bit register for timer 0. They are also used as event counters. Similarly,
TL1 and TH1 are registers for timer 1.
Peripheral control registers: IP, IE, TMOD, TCON, SCON, and PCON.
IP (interrupt priority) is used to assign priorities to different interrupt
sources.
IE (interrupt enable) register is used to enable/disable interrupts.
TMOD (timer mode) is used to control behavior i.e. mode of operation of
timers.
TCON (timer control) is used to start/stop timers.
SCON (serial port control) register is used to control the modes of
operation of the serial port.
PCON (power mode control) register is used to select power saving modes
of operations.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Internal RAM
The 8051 microcontroller has a total of 128 bytes of internal
RAM. These bytes are assigned addresses 00H to 7FH. These
128 bytes are grouped into three different areas.
1. Register banks
First 32 bytes from addresses 00H to 1FH are organized as four
banks. Each bank is made up of eight registers named R0 to
R7.
2. Bit addressable memory
The 8051 has a bit addressable area of 16 bytes from byte
addresses 20H to 2FH in internal RAM, forming a total of 128
(16x8) addressable bits. An addressable bit can be accessed by
its bit addresses from 00H to 7FH.
3. General Purpose RAM
Bytes from memory locations 30H to 7FH are used for general
purpose data storage.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
Internal RAM organization of the 8051
The 8051 microcontroller based Embedded systems, First edition http://www.mhhe.com/patel/mbes
Internal ROM
The 8051 contains 4Kbytes of internal ROM (on-chip). It
occupies address range from 0000H to 0FFFH. Since it is used
to store program instructions (code), it is also called program
memory or code memory.
ROM organization of the 8051.
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes
EXTERNAL MEMORY
ORGANIZATION
There are two parallel 64 Kilobytes address spaces
one for the ROM(program memory).
one for the RAM (data memory).
The data space is accessed using external data movement instructions
(MOVX A, source or MOVX destination, A) and code space is accessed
using external code movement instructions (MOVC A, source).
External program and data memory space for the 8051
The 8051 microcontroller based Embedded systems, First edition
http://www.mhhe.com/patel/mbes