You are on page 1of 69

Microprocessor and Interfacing

CSE-317
 Microcontroller
 8051

 Embeded system and IoT

 Interfacing
 8259 PIC (Programmable interrupt control)

 8255 PPI (Programmable peripheral interface)

 8254 Programmable interval timer

 8237 DMA
The 8051 Microcontroller and Embedded Systems
Mazidi Mazidi McKinlay Second Edition
8051
Introduction to 8051
The 8051 microcontroller was first introduced by Intel in the year 1981 for
embedded systems
It is an 8 – bit Microcontroller i.e. the data bus of the 8051 Microcontroller
(both internal and external) is 8 – bit wide
It is a CISC (Complex Instruction Set Computer) based Microcontroller with
Harvard Architecture (separate program and data memory). i.e It has
separate memory spaces for instructions and data
Originally, 8051 Microcontrollers were developed using N-MOS Technology
but the use of battery powered devices and their low power consumption
lead to usage of CMOS Technology (which is famous for its low power
consumption)
Introduction to 8051
• Even though Intel developed 8051 Microcontrollers (which is
discontinued in 2007), more than 20 semiconductor manufacturers are
still producing 8051 compatible microcontrollers i.e. processors based
on MSC-51 Architecture.
• Some of the 8051 Microcontrollers produced by different manufacturers
are: Atmel (AT89C51, AT89S51), Phillips (S87C654), STC Micro
(STC89C52), Infineon (SAB-C515, XC800), Siemens (SAB-C501),
Silicon Labs (C8051), NXP (NXP700, NXP900), etc.
• Because of their low power consumption, smaller size and simple
architecture, 8051 IP Cores are used in FPGAs (Field Programmable
Gate Array) and SoCs (System on Chip) instead of Advanced ARM
Architecture based MCUs.
Introduction to 8051

The 8051 microcontrollers might be old in comparison to current era


microcontrollers but it is still a better tool for beginners to learn
basic functionality of MCUs such as serial communication, interrupt,
timers, and so on.
Features of 8051 Microcontroller

• The 8051 Microcontroller Architecture is based on a Harvard


architecture and is equipped with a variety of features such as
• An 8-bit CPU with an instruction set that includes a variety of arithmetic
and logic operations
• 128 bytes of RAM,
• 4KB of ROM (on-chip flash memory)
• It also features a number of I/O ports, timers, and serial communication
interfaces that make it well-suited for a variety of applications.
Features of 8051 Microcontroller

One of the key advantages of the 8051 Microcontroller Architecture is

• Its low power consumption, which makes it an ideal choice for


battery-powered applications
• It is also a versatile microcontroller that is relatively easy to program
and offers a wide range of programming options
• Additionally, the 8051 Microcontroller Architecture is compatible with
a variety of software and hardware tools, which makes it easy to
integrate into a wide range of applications.
Application
• Consumer Appliances (TV Tuners, Remote controls, Computers, Sewing Machines, etc.)
• Home Applications (TVs, VCR, Video Games, Camcorder, Music Instruments, Home Security
Systems, Garage Door Openers, etc.)
• Communication Systems (Mobile Phones, Intercoms, Answering Machines, Paging Devices, etc.)
• Office (Fax Machines, Printers, Copiers, Laser Printers, etc.)
• Automobiles (Air Bags, ABS, Engine Control, Transmission Control, Temperature Control, Keyless
Entry, etc)
• Aeronautical and Space
• Medical Equipment
• Defense Systems
• Robotics
• Industrial Process and Flow Control
• Radio and Networking Equipment
• Remote Sensing
Internal Architecture
Internal Architecture
• The block diagram of the 8051 Microcontroller Architecture is divided into
several functional blocks, each of which plays an important role in the operation
of the microcontroller. Here’s a brief explanation of each block:
• CPU: The CPU block is the core of the 8051 Microcontroller Architecture. ALU &
CU are the main components of CPU. It is responsible for performing all
arithmetic and logical operations, as well as controlling the execution of
instructions
• Memory: The memory block consists of ROM, RAM, The on-chip memory of the
8051 Microcontroller Architecture includes
• 4KB of flash memory i.e ROM which can add external program memory as well as 64k data
memory.
• 128 bytes of RAM.
Internal Architecture
• Oscillator: An on chip crystal oscillator is integrated in 8051 which is
having a clock freq of 12 MHz. This Oscillator generates the clock
signal that is used to synchronize the operation of the microcontroller
• Interrupts: The interrupt block is responsible for handling interrupts,
which are signals that indicate an event that requires immediate
attention by the microcontroller. There are 2 external and 3 internal
interrupts
• I/O Ports: The I/O ports block includes the input/output pins of the
microcontroller, which are used to interface with external devices and
peripherals. There are total 4 ports each having 8 pins available in
8051. I/O ports provide microcontroller a physical connection to
outside world
Internal Architecture

• Timers/Counters: The timers/counters block includes two 16-bit


timers/counters that can be used for a variety of tasks such as timing events,
generating waveforms, and measuring frequency.
• Serial Communication: The serial communication block includes UART, SPI,
and I2C interfaces that can be used for serial communication with other
devices.
• Bus Control: There are two buses in 8051 microcontroller. The system
bus consist of 8 bit bidirectional data bus and 16 bit address bus and bus
control signal.
• Register A (Accumulator): It is an 8 bit register, it holds a source operand and
store/receive the result of the arithmatic operations (add, sub, mul, div).
• Register B: It is actually an extension of accumaulator A. It is used during
mul and div op which can be performed only upon numbers stored in the register A
& B. All other instructions in the program can use this register as spare of
accumulator A
• Stack pointer: STACK is the reserved memory area which store temporary
data. Stack pointer holds the address of the stack top.
• DPTR: Data pointer register (DPTR) which has two separate register DPH and
DPL. DPTR usually used for storing data and immediate result.
• Control Register: 8051 has got few SFR (special function register) and control
registers such as SCON, PCON,TCON,TMOD, IP & IE
• 16 bit PC: It is used to hold the address of memory location from which the next
instruction is to be fetched.
PIN DIAGRAM OF 8051
Pin Diagram
P0
XTL1
P1

XTL2
Internal ROM
P2
4KB
(Prog Memory) P3
RESET
ALE
P3.0 RxD
EA 8051 P3.1 TxD
PSEN P3.2 INT0

Internal P3.3 INT1


vcc RAM P3.4 T2
128B P3.5 T1
GRD
(Data
P3.6 WR
Memory)
P3.7 RD
Microprocessor Microcontroller

• Microprocessors act as the heart of the • Microcontrollers act as the heart of the
computer system embedded system

• Does not have internal memory to • The microcontroller has inbuilt


store temporary data or source code memory to store temporary data
(RAM) as well as for code (ROM).
• The microprocessor does not have • The microcontroller has inbuilt
input/output ports. So microprocessors input/output ports. So microcontroller
can not read input data as well as not can read input data as well as able to
able to send data to an output device send data to an output device
Microprocessor Microcontroller

• The clock frequency is higher in • The clock frequency is lower in


microprocessor. So, it is faster. microcontroller. So, it is
comparatively slower.
• Timers are inbuilt and available
• Timers are not available in the in the microcontroller; so, can
microprocessor so, it can no generate precise delay .
generate precise delay.

• Microcontroller consumes less


• Microprocessor consumes more power.
power due to complex design .
PSW (Program Status Word)
PSW (Program Status Word)

CY AC F0 RS1 RS0 OV - P
D7/PSW7 D6/PSW6 D5/PSW5 D4/PSW4 D3/PSW3 D2/PSW2 D1/PSW1 D0/PSW0

The program status word (PSW) is an 8-bit register which is also referred as the
flag register

 The main function of this register is to show the status of the program/flag after
any arithmetic operations

Out of 8 bit only 6-bits of it are used by 8051

The two unused bits are user definable


PSW (Program Status Word)

CY AC F0 RS1 RS0 OV - P
D7/PSW7 D6/PSW6 D5/PSW5 D4/PSW4 D3/PSW3 D2/PSW2 D1/PSW1 D0/PSW0

Out of 6, Four of the flags are called conditional flags, meaning that they
indicate some conditions that results after an instruction is executed, these
four are:
 CY(carry)
 AC(auxiliary carry)
 P(parity) and
 OV(overflow)
 The bit 3 and bit 4 are designated as RS0 and RS1 which are
register selection bit and are used to select the bank register
PSW (Program Status Word)

CY AC F0 RS1 RS0 OV - P
D7/PSW7 D6/PSW6 D5/PSW5 D4/PSW4 D3/PSW3 D2/PSW2 D1/PSW1 D0/PSW0

PSW.7 = CY= Carry bit


PSW.6 = AC= Auxiliary Carry
PSW.5 = F0= User defined general purpose
PSW.4 & PSW.3 = RS1 and RS0 for selecting register bank
PSW.2= OV = Over flow bit
PSW.1=-= user defined
PSW.0 =P = Parity bit
PSW
CY (carry flag)
• This flag is set whenever there is a carry out from the D.7/PSW.7 bit
• This flag bit is affected after 8 bit addition or subtraction

1000 0000 1000 0000 CY AC F0 RS1 RS0 OV - P


1000 0001 0001 0001 D7 D6 D5 D4 D3 D2 D1 D0
10000 0001 1001 0001

• It can also be set to 1 or 0 directly by an instruction such as

• " SET BC" which means set bit carry CY=1


• "CLR C” Which means clear carry CY=0
AC(Auxiliary flag)
• If there is a carry from lower nibble to higher nibble i.e D3 to D4
during ADD or SUB operation this bit is set (1) otherwise it is cleared
(0)
• Example:

1001 1001 1000 0010


0000 1010 0110 1001
1010 0011 1110 1011

CY AC F0 RS1 RS0 OV - P
D7 D6 D5 D4 D3 D2 D1 D0
P (parity flag)
The parity flag reflects the number of 1's in the A (accumulator) register
If the A register contains an odd number of 1's then Parity flag will be 1 and
it will be 0 if there is even numbers of 1’s

1011 1111 CY AC F0 RS1 RS0 OV - P


0010 1010 D7 D6 D5 D4 D3 D2 D1 D0
1110 1001

In this case after addition total no of 1 is odd,


So, here the parity flag will be 1
OV (overflow flag)
• This flag is set whenever the result of a signed number operation is too
large causing the higher order bit to overflow into the sign bit
• The carry flag is used to detect error in unsigned arithmetic operations
• The overflow flag is only used to detect error in signed arithmetic
operations
1011 1111 0110 0101
0010 1010 0111 0101
1110 1001 1101 1010

Here, in first case no carry generated from D6/PSW6 to D7/PSW7.


So, Overflow bit will be 0.
In the second case carry generated from D6/PSW6 to D7/PSW7.
So, in this case overflow bit will be 1
• Sample Problem
1. Show the status of CY, AC, OV and P flag after execution of the
following instruction:

MOV A, #9CH
ADD A, #64H

2. Show the PSW after execution of the following code.

MOV A, #BFH
ADD A, #2AH
MEMORY ORGANIZATION OF 8051
MEMORY ORGANIZATION OF 8051
 The 8051 Microcontroller Memory is
divided into
 Program Memory (ROM)
 Data Memory (RAM)
 The internal ROM of 8051 is 4K and the
internal RAM is 128B
 The Program Memory of the 8051
Microcontroller is used for storing the
program to be executed i.e.,
instructions
 The Data Memory on the other hand, is  If the internal memory is inadequate,
used for storing temporary variable you can add external memory using
data and intermediate results suitable circuits
Program Memory (ROM)

In 8051 Microcontroller, the code or instructions to be executed are stored


in the Program Memory, which is also called as the ROM of the
Microcontroller. The original 8051 Microcontroller by Intel has 4KB of
internal ROM.
Some variants of 8051 like the 8031 and 8032 series doesn’t have any
internal ROM (Program Memory) and must be interfaced with external
Program Memory with instructions loaded in it.
Almost all modern 8051 Microcontrollers, like 8052 Series, have 8KB of
Internal Program Memory (ROM) in the form of Flash Memory (ROM) and
provide the option of reprogramming the memory.
Program Memory (ROM)
 In case of 4KB of Internal ROM, the
address space is 0000H to 0FFFH.
 If the address space i.e., the program
addresses exceed this value, then the CPU FFFFH External
+5V Program
will automatically fetch the code from the
Memory
external Program memory. For this, the EA 64K
External Access Pin (EA Pin) must be pulled (ROM)
HIGH i.e. 1000H

 When the EA Pin is high, the CPU first 0FFFH


Internal
fetches instructions from the Internal Program
Program Memory in the address range of Memory
0000H to 0FFFH 0000H
4K (ROM)

 If the memory addresses exceed the limit,


then the instructions are fetched from the
external ROM in the address range of 1000H
to FFFFH
Program Memory (ROM)

• There is another way to fetch


the instructions: ignore the
FFFFH External
Internal ROM and fetch all the Program
instructions only from the EA Memory
External Program Memory 64K (ROM)
(External ROM) GRD 0000H

0FFFH
Internal
• For this scenario, the EA Pin Program
Memory
must be connected to GND. In 4K (ROM)
this case, the memory 0000H
addresses of the external ROM
will be from 0000H to FFFFH.
Data Memory (RAM) of 8051 Microcontroller
The Data Memory or RAM of the 8051
Microcontroller stores temporary data and
intermediate results that are generated and used
during the operation of the microcontroller. Original
Intel’s 8051 Microcontroller had 128B of internal
RAM.

But almost all modern variants of 8051


Microcontroller have 256B of RAM. In this 256B, the
first 128B i.e., memory addresses from 00H to 7FH is
divided in to

• Working Registers (organized as Register Banks),


• Bit – Addressable Area and
• General Purpose RAM (also known as
Scratchpad area).
Data Memory (RAM) of 8051 Microcontroller

 In the first 128B of RAM (from 00H to


7FH), the first 32B i.e., memory
addresses from 00H to 1FH consists of
32 Working Registers that are organized
as four banks

 The 4 banks are named as Bank0,


Bank1, Bank2 and Bank3.

 Each Bank consists of 8 registers named


as R0 – R7.

 Each Register can be addressed in two


ways: either by name or by address.
Data Memory (RAM) of 8051 Microcontroller
To address the register by name,
first the corresponding Bank must be
selected. In order to select the bank,
we have to use the RS0 and RS1
bits of the Program Status Word
(PSW) Register (RS0 and RS1 are
3rd and 4th bits in the PSW
Register).

When addressing the Register


using its address i.e., 12H for
example, the corresponding Bank
may or may not be selected. (12H
corresponds to R2 in Bank2).
Data Memory (RAM) of 8051 Microcontroller

The next 16B of the RAM i.e., from


20H to 2FH are Bit – Addressable
memory locations.

There are totally 128 bits that can be


addressed individually using 00H to
7FH or the entire byte can be
addressed as 20H to 2FH.

For example 32H is the bit 2 of the


internal RAM location 26H.
Data Memory (RAM) of 8051 Microcontroller

The final 80B of the internal RAM i.e.,


addresses from 30H to 7FH, is the
general purpose RAM area which are
byte addressable.
Data Memory (RAM) of 8051 Microcontroller

In some microcontrollers,
there is an additional 128B of
RAM, which share the
memory address with SFRs
i.e., 80H to FFH. But, this
additional RAM block is only
accessed by indirect
addressing
Data Memory (RAM) of 8051 Microcontroller

The upper 128B of the RAM i.e., memory


addresses from 80H to FFH is allocated
for Special Function Registers (SFRs).
SFRs control specific functions of the
8051 Microcontroller. Some of the SFRs
are I/O Port Registers (P0, P1, P2 and
P3), SCON, TMOD etc.

SRFs Memory addresses are only direct


addressable. Even though some of the
addresses between 80H and FFH are not
assigned to any SFR, they cannot be
used as additional RAM area
Addressing Modes of 8051
Addressing Modes of 8051
 Addressing is a method to access or address the operand. The operand is
the data we are manipulating such as immediate data, register names, or
direct address of memory location

 The CPU can access data in various way. The data could be

In a Register or
In a Memory location or
Be provided as an immediate value
 This various way of addressing data is called addressing modes
Addressing Modes of 8051

 In 8051 Microcontroller there are five/ six addressing modes.

 Immediate Addressing Mode


 Register Addressing Mode
 Direct Addressing Mode
 Indirect Addressing Mode
 Indexed Addressing Mode
 Implied Addressing Mode
Immediate Addressing Mode

 In immediate addressing mode,

The source data is a constant, immediate data and it can be 8-bit or16 bit
The operant comes immediately after the opcode
The operant (source data)must be preceeded by pound symbol “#”
This addressing mode is used to load information into any of the
registers including DPTR
Format: MOV, # Data
Immediate Addressing Mode
 Some of the example of immediate addressing modes are

MOV A,#55H
MOV R1,#10H
MOV P1,#FFH
MOV DPTR,#4567H

 Although DPTR is 16 bit register, but it can also be accessed by two 8 bit
register as DPH and DPL

MOV DPTR, #2520H; is same as

MOV DPL, #20H


MOV DPH, #25H
Immediate Addressing Mode

• It can also be used to send data at port of 8051. Example:

MOV P1, #26H is a valid instruction

• However, if the data is larger than register size then that will be
an invalid instruction, and will show as an error.
• For example

MOV A, #345H or
MOV DPTR, #32875H
Register Addressing Mode

• Register addressing mode involves the use of register to hold the


data to be manipulated. In register Addressing mode, the source
data should be available on the register such as R0 to R7, A, B,
DPH or DPL, and so on. Some examples are provided below.

MOV A, R0; copy data of R0 register to accumulator A


MOV R1, A; copy data of A register to R1
ADD A, R5; Add contents of R5 with contents of A
MOV R7, DPL; Copy data of DPL to register R7
Register Addressing Mode
It should be noted that the size of the source and destination register must
have to be matched,

MOV DPTR, #3546H


MOV R7, DPL

 Otherwise it wll show an error


MOV DPTR, A
MOV DPL,#2341H

 NOTICE THAT, we can move data between accumulator and Rn (n=0 to 7)


registers but we can not move data between Rn registers

For example,
MOV R1, R5; this is an invalid instruction
Direct Addressing Mode

This mode is used to access the internal memory of the microcontroller 8051.
In this mode address of the data is directly given in the instruction
 Using this mode any register can be accessed such as general purpose,
SFRs, I/O ports etc

Examples of Direct Addressing Mode

MOV A,30H
MOV 80H,B
MOV 45H,50H

Only this addressing mode is allowed for PUSHing and POPing the
data on stack
Indirect Addressing Mode

• This mode is used to access an internal and external memory


location.
• In the Register indirect addressing mode, the source or destination
address is provided in the register.
• Using this addressing mode 8-bit or 16-bit address locations can be
accessed.
• To locate 8-bit addresses R0 or R1 registers (R2 to R7 are not used)
are used and for 16-bit addresses, the DPTR register is used.
• To transfer data to external memory symbol “X” must include in the
instruction set.
• Examples of Register Indirect Addressing Mode (Internal
Memory)
• MOV A,@R0
• MOV @R1,B

• Examples of Register Indirect Addressing Mode (External


Memory)
• MOVX A,@R0
• MOVX @R0,B
Indexed Addressing Mode

• In indexed addressing mode, we fetch source data from code


memory or program memory
• The destination register is only the Accumulator register (A).
• The other operand register can be the Program counter (PC) or Data
Pointer (DPTR).
• The symbol “C” indicates the code memory instruction set.
• Examples of Indexed Addressing Mode

• MOVC A, @A+DPTR
• MOVC A, @A+PC
Implied Addressing Mode

• In this addressing mode, only one operand specifies in the


instruction. A specific register assigned for such instructions.

• Examples of Implied Addressing Mode

• SWAP
• RLA
• RR
Instruction set of 8051
Instructions are command which are used to perform any specific task.
A group of instruction is called instruction set and
A set of instructions is called program
Instructions written in a program instruct microprocessor/microcontroller
which operation is to be performed.
The syntax of an instruction is

OPCODE DESTINATION, SOURCE

MOV A, R1
ADD A, #32H
Instruction set of 8051

The instruction set of the 8051 Microcontroller consists of a collection


of different instructions that can be executed by the microcontroller.
These instructions are used for various tasks such as

• Data transfer,
• Arithmetic and logical operations,
• Program control, and I/O operations.
Instruction set of 8051
The instruction set of the 8051 Microcontroller includes a range of
instructions, including load and store instructions, arithmetic and
logical instructions, jump and call instructions, and I/O instructions.

These instructions are executed by the microcontroller’s central


processing unit (CPU) in response to the instructions written in the
program memory.

Load and store instructions are used for transferring data between the
microcontroller’s internal registers and external memory
Instruction set of 8051

Arithmetic and logical instructions are used for performing arithmetic


and logical operations on data stored in registers or memory.

Jump and call instructions are used for controlling program flow by
jumping to specific locations in the program memory.

I/O instructions are used for controlling input and output operations to
and from the microcontroller.
Types of Instruction in 8051

• Data Transfer Instruction


• Arithmetic Instruction
• Logical Instruction
• Branching Instruction
Data Transfer Instruction

This type of instruction is used only to transfer/mov data from one location
to other location

Using this instruction we do not perform add/ sub/ mod of any data

There are three types of data transfer instruction

MOV MOV A, Rn; MOV A,@R1; MOV A, #34H


PUSH OR POP PUSH Direct; POP Direct
XCHG XCHG A, Rn
Arithmetic Instruction

• This group of instruction perform arithmatic operation like addition,


subtraction, multiplication, division etc
• Arithmatic operation affects PSW flag of 8051
• Different arithmatic instructions are
• ADD (addition) ADD A, Source
• SUB (Subtraction) SUBB A, Rn
• MUL MUL AB
• DIV
• INC
• DEC
Logical instruction

Logical instruction performs logical operations in 8051 microcontroller.

It performs bitwise operation between accumiulator and data stored in


memory location/ register or immediate data and the result stored in
accumulator

There are total 25 instruction in logical instruction

AND, OR, XOR, NOR, NOT,CLEAR, COMPLEMENT, SWAP, ROTATE etc


Branching Instruction
• A microcontroller sequentially executes instructions but in some cases,
transferring this control to another block of code becomes essential.
• The branching instructions in the 8051 microcontroller are responsible for
performing this operation.
• Tasks like looping, calling delays, and conditional execution of code can be
performed using these branching instructions.

• JUMP
• CALL
• RETURN
Branching Instruction
• JUMP instruction permenently change the program counter or PC loaded with new
address of memory location and execute the subroutine and it don’t come back again

• CALL instruction temporarily change the content of PC or PC is loaded with new


memory location temporarily and execute the sub routine; after execution of the sub
routine it come back in that main program again.

• The call instruction is used to transfer the control from the presently executing program
code to the subroutine,

• RETURN instruction is used to return the control to the program code.

You might also like