Architecture of 8051
Architecture of 8051
8051
Y Aruna Suhasini
1
Associate Professor
Need for Microprocessors / controllers?
Y Aruna Suhasini
10
Associate Professor
FEATURES OF 8051
• The Intel 8051 is a very popular general purpose
microcontroller widely used for small scale Embedded systems.
• The 8051 is an 8-bit microcontroller with 8 bit data bus and
16-bit address bus.
• The 16 bit address bus can address a 64Kbyte code memory
space and a separate 64K byte of data memory space.
• The 8051 has 4K on-chip read only code memory which is
used for permanent data storage and 128 bytes of internal
Random Access Memory (RAM)
• Microcontroller has 256 byte RAM in which 128 byte is used
for user space which is normally register banks and stack. But
other 128 byte RAM consists of SFRs(Special Function
Registers). Y Aruna Suhasini
11
Associate Professor
FEATURES OF 8051
• Besides internal RAM, the 8051 has various Special
Function Registers(SFR) such as the Accumulator,
the B register, and many other control registers.
• It has 34 8-bit general purpose registers in total.
• The ALU performs one 8-bit operation at a time.
• It has two 16 bit counter/timers
• There are 3 internal interrupts (1 serial, 2 external
interrupts).
• It consists of 4 8-bit I/O ports (3 of them are dual purpose).
one of them is used for serial port.
• Some 8051 chips come with UART for serial communication
and ADC for analog to digital conversion.
Y Aruna Suhasini
12
Associate Professor
BLOCK DIAGRAM
External interrupts
On-chip Timer/Counter
CPU
Bus Serial
4 I/O Ports
OSC Control Port
P0 P1 P2 P3 TxD RxD
Address/Data
Y Aruna Suhasini
Associate Professor 13
Y Aruna Suhasini
14
Associate Professor
REGISTERS
A
R0
DPTR DPH DPL
R1
R2 PC PC
R3
R5
R6
R7
Y Aruna Suhasini
Associate Professor 15
REGISTER ORGANIZATION OF 8051
Y Aruna Suhasini 16
Associate Professor
RAM ORGANIZATION
17
Y Aruna Suhasini
Associate Professor
REGISTER BANKS OF 8051
Y Aruna Suhasini
Associate Professor
18
PSW BANK SELECTION
Y Aruna Suhasini 19
Associate Professor
Y Aruna Suhasini
20
Associate Professor
8051 INTERNAL ARCHITECTURE
• The CPU has many important registers. The Program Counter (PC)
always holds the code memory location of next instruction.
• The CPU fetches instructions from the code memory into the
instruction Register (IR), analyses the opcode of the
instruction, updates the PC to the location of next instruction,
fetches the operand from the data memory if necessary, and
finally performs the operation in the Arithmetic-Logic Unit
(ALU) within the CPU.
• The B register is a register just for multiplication and division
operations which require more register spaces for the product of
multiplication and the quotient and the remainder for the
division.
• The immediate result is stored in the accumulator register (Acc)
for next operation and the Program Status Word (PSW) is
updated depending on the status of the operation result 21
Y Aruna Suhasini
Associate Professor
DATA and ADDRESS BUS
• A bus is group of wires using which data transfer takes place from
one location to another within a system.
• Buses reduce the number of paths or cables needed to set up
connection between components.
• There are mainly two kinds of buses - Data Bus and Address Bus
26
Y Aruna Suhasini
Associate Professor
STACK OPERATION
Stack in 8051
• Stack is used to store the data temporarily
• RAM locations from 08H to 1FH can be used as stack..
• Stack is last in first out (LIFO)
• Stack pointer (SP) is 8bit register, it indicates current
RAM address available for stack or it points the top of
stack.
• After PUSH instruction SP is decremented.
• After each POP instruction the SP is decremented.
Y Aruna Suhasini
Associate Professor
27
PSW (PROGRAM STATUS WORD)
• PSW register has 3 fields namely instruction address field, condition code
field and error status field.
The 4 Math flags are: The 3 General purpose flags or User flags are:
• Carry (c) • FO - Flag 0 is available for general purpose
• Auxiliary carry (AC) • RSO – Register Bank Selector bit 0
• Overflow (OV) • RS1 – Register Bank Selector bit 1
• Parity (P) Y Aruna Suhasini
Associate Professor
28
Y Aruna Suhasini
29
Associate Professor
SPECIAL FUNCTION REGISTERS (SFRS).
Y Aruna Suhasini
Associate Professor
DATA MEMORY ORGANIZATION
• 8051 has 128 bytes of internal data memory and it allows
interfacing external data memory of maximum size up to 64K.
• So the total size of data memory in 8051 can be up to 64K
(external) + 128 bytes (internal).
• There are 3 separations/divisions of the data memory:
1) Register banks
2) Bit addressable area
3) Scratch pad area.
Y Aruna Suhasini
34
Associate Professor
INTERNAL RAM AND ROM
ROM
• A code of 4K memory is incorporated as on-chip ROM in
8051.
• The 8051 ROM is a non-volatile memory meaning that its
contents cannot be altered i.e, they can address program
memory as well as a 64K separate block of data memory.
RAM
• The 8051 microcontroller is composed of 128 bytes of internal
RAM.
• This is a volatile memory since its contents will be lost if
power is switched off.
• These 128 bytes of internal RAM are divided into 32 working
registers which in turn constitute 4 register banks (Bank 0-
Bank 3) with each bank consisting of 8 registers (R0 - R7).
• There are 128 addressable bits in the internal RAM. 35
Y Aruna Suhasini
Associate Professor
Y Aruna Suhasini 36
Associate Professor
▪ The 8051 contains 210 bit addressable
locations, 128 from address 20H to
address 2FH and the rest are special
function registers
▪ 128 general-purpose locations are
accessed as bytes or as bits
▪ The bottom 32 locations of internal
memory contain the register banks
▪ The 8051 instruction set supports 8
registers, R0-R7
Y Aruna Suhasini
37
Associate Professor
• PSW Program Status Word at address
D0H
• Carry Flag (C or CY) is dual-purpose,
during add or subtract
• Auxiliary Carry Flag AC when adding
binary-coded-decimal BCD
• Flag 0 (F0) is a general-purpose flag bit
available for user applications
• Register Bank Select Bits, RS0 and
RS1, determine the active register bank
• Overflow flag (OV), after add or sub,
set when arithmetic overflow
• Parity Bit (P), check for parity after
reception
• B register, or accumulator B, address
F0H, for MUL and DIV
Y Aruna Suhasini
Associate Professor 38
PIN DESCRIPTION of 8051
39
Y Aruna Suhasini
Associate Professor
PIN DESCRIPTION
• 32 of the 8051’s 40 pins function as I/O port lines
• 24 of these lines are dual-purpose (26 on the 8032/8052), each
can operate as I/O or as a control line or the part of the address
or data bus.
• VCC → 5V supply- Pin 40
• VSS → GND - Pin 20
• XTAL2/XTALI are for oscillator input –Pins 18 &19
• Port 0 –P0.0 to P0.7 and AD0/AD7 - Pins 32 to 39
• Port 1 –P1.0 to P1.7 - Pins 1to 8
• Port 2 –P2.0 to P2.7 and A 8 to A15 – Pins 21 to 28
• Port 3 –P3.0 to P3.7 – Pins 10 to 17
Y Aruna Suhasini 40
Associate Professor
PIN DESCRIPTION
• P 3.0 – RXD – Serial data input – SBUF
• P 3.1 – TXD – Serial data output – SBUF
• P 3.2 – INT0 – External interrupt 0 – TCON 0.1
• P 3.3 – INT1 – External interrupt 1 – TCON 0.3
• P 3.4 – T0 – External timer 0 input – TMOD
• P 3.5 – T1 – External timer 1 input – TMOD
• P 3.6 – WR bar – External timer 0 input – TMOD
• P 3.7 – RD bar– External timer 0 input – TMOD
• RST - Pin 9
• The RST input on pin 9 is the master reset for the 8051.
• When brought high for at least two machine cycles, the 8051
internal registers are loaded with appropriate values for an
orderly system start-up. Y Aruna Suhasini
Associate Professor
43
FOUR GENERAL PURPOSE PARALLEL INPUT/OUTPUT
PORTS
The 8051 microcontroller has four 8-bit input/output ports:P0, P1, P2 and P3. All of
them are dual purpose ports except P1 which is only used for I/O.These are:
• PORT P0: When there is no external memory present, this port acts as a general
purpose input/output port. In the presence of external memory, it functions as a
multiplexed address and data bus. It performs a dual role.
• PORT P1: This port is used for various interfacing activities. This 8-bit port is a
normal I/O port i.e. it does not perform dual functions.
• PORT P2: Similar to PORT P0, this port can be used as a general purpose port
when there is no external memory but when external memory is present it works
in conjunction with PORT PO as an address bus. This is an 8-bit port and
performs dual functions.
Y Aruna Suhasini
Associate Professor
PORTS OF 8051
PORT 0 PORT 2
▪ Dual-purpose port on pins 32-39 • Pins 21-28
▪ In minimum-component designs • Dual purpose port
general purpose I/O port • General I/O port or as the high
▪ For larger designs with external byte of the address bus with
memory multiplexed address and external code memory
data bus • Port 2 is at A0H
▪ Port 0 is at address 80H
PORT 1 PORT 3
• Dedicated I/O port on pins 1-8 • Dual-purpose port on pins 10-17
• Used solely for interfacing to • Multifunctional
External devices
• Port 1 is at 90H • General-purpose I/O
• Port 3 is at B0H
45
Y Aruna Suhasini
Associate Professor
Port 0 with Pull-Up Resistors
Vcc
10 K
P0.0
Port
DS5000 P0.1
P0.2
8751 P0.3
8951 P0.4 0
P0.5
P0.6
P0.7
Y Aruna Suhasini 46
Associate Professor
Port 0(p0.0 to p0.7):
It is 8-bit bi-directional I/O port. It is bit/ byte addressable. During external memory
access, it functions as multiplexed data and low-order address bus AD0-AD7.
Port 1 (p1.0 to p1.7):
It is 8-bit bi-directional I/O port. It is bit/ byte addressable. When logic '1' is written
into port latch then it works as input mode. It functions as simply I/O port and it does
not have any alternative function.
Port 2 (p2.0 to p2.7):
It is 8-bit bi-directional I/O port. It is bit/ byte addressable. During external memory
access it functions as higher order address bus (A8-A15).
Port 3(p3.0 to port 3.7):
It is 8-bit I/O port. In an alternating function each pins can be used as a special
function I/O pin.
P3.0-RxD:
It is an Input signal. Through this I/P signal microcontroller receives serial data of
serial communication circuit.
P3.1-TxD:
It is O/P signal of serial port. Through this signal data is transmitted.
P3.2- (INT0):
It is external hardware interrupt I/P signal. Through this user, programmer or
peripheral interrupts to microcontroller.
47
Y Aruna Suhasini
Associate Professor
P3.3-(INT1):
It is external hardware interrupt I/P signal. Through this user, programmer or peripheral
interrupt the microcontroller.
P3.4- T0:
It is I/P signal to internal timer-0 circuit. External clock pulses can connect to timer-0
through this I/P signal.
P3.5-T1:
It is I/P signal to internal timer-1 circuit. External clock pulses can connect to timer-1
through this I/P signal.
P3.6-[WR(bar)]:
It is active low write O/P control signal. During External RAM (Data memory) access it is
generated by microcontroller. when [WR(bar)]=0, it performs write operation.
P3.7-[RD(bar)]:
It is active low read O/P control signal. During External RAM (Data memory) access it is
generated by microcontroller. when [RD(bar)]=0, it performs read operation from external
RAM.
Y Aruna Suhasini 48
Associate Professor
INTERRUPT CONTROL
• An event which is used to suspend or halt the normal program execution for a
temporary period of time in order to serve the request of another program
or hardware device is called an interrupt.
50
Y Aruna Suhasini
Associate Professor
INTERRUPTS OF 8051
5 Sources of interrupts are:
Y Aruna Suhasini 53
Associate Professor
PCON ,TCON
PCON (Power Control, Addresses 87h): The Power Control SFR is
used to control the 8051's power control modes. "sleep" mode
which requires much less power are controlled through PCON.
Additionally, one of the bits in PCON is used to double the
effective baud rate of the 8051's serial port.
56
Y Aruna Suhasini
Associate Professor
POWER-ON RESET CIRCUIT
Vcc
+
10 uF
31
EA/VPP
30 pF X1
19
11.0592 MHz
8.2 K
X2
18
30 pF
9 RST
RESET:
It is active high I/P signal. It should be maintained high for
at least two machine cycle while oscillator is running for
8051 microcontroller to reset. 57
Y Aruna Suhasini
Associate Professor
8051 System Clock
Synchronization among internal operations can be achieved with
the help of clock circuits.
• Quartz crystal is used to generate periodic clock pulses.
• XTAL1 and XTAL2 are provided with two pins which are used
for connecting a resonant network in 8051 microcontroller
device. In addition to this, circuit also consists of four more
pins. They are,
Y Aruna Suhasini
4. RST: Reset. Associate Professor
8051 DRIVEN BY A TTL OSCILLATOR
• Quartz crystal is used to make the
clock circuit.
• A resonant network as quartz
crystal is connected between these
two pins 18 & 19.
• The nominal crystal frequency is
12 MHz, up to 16 MHz
• The first 6 crystal pulses (clock
cycle) is used to fetch the opcode
and the second 6 pulses are used to
perform the operation on the
operands in the ALU. XTAL1 and XTAL2 are two I/P
line for on-chip oscillator and clock
• This gives an effective machine generator circuit.
cycle rate at 1MIPS (Million
Instructions Per Second). Y Aruna Suhasini
Associate Professor
59
8051 System Clock
• One complete oscillation of the
clock source is called a pulse.
• Two pulses form a state and six
states form one machine cycle.
• Two pulses of ALE are available
for 1 machine cycle.
If 8051 microcontroller is
driven from external clock,
then XTAL2 is used to
drive 8051 from external
clock and XTAL1 should be
grounded.
60
Y Aruna Suhasini
8051 machine cycle waveform Associate Professor
TIMER
TR1 –Timer 1 run control bit. Set = Timer ON and Clear = Timer OFF
TF1 – Timer flag which is set when the Timer 1 rolls over from FFFFH to 0000H
Cleared the bit in software !.
TR0 – Timer 0 run control bit. Set = Timer ON and Clear = Timer OFF
TF0 – Timer flag which is set when Timer 0 rolls over from FFFFH to 0000H.
Cleared the bit in software !
IE1 and IT1 – Set the trigger mode of external interrupt 1 63
IE0 and IT0 – Set the trigger mode of external interrupt 0 Y Aruna Suhasini
Associate Professor
TIMER PROGRAMMING
3.Timer Mode Register (TMOD)
• It is used to set the various timer operation modes.
• It is a 8-bit register and bit-addressable.
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
GATE – Gating control when set. The timer/counter is enable only while the
INTx pin is high and the TRx control pin is set. When cleared, the timer is
enabled whenever the TRx control bit is set.
C/T – Timer or counter selected. Cleared for timer operation (input from
internal system clock) and Set for counter operation (input from Tx input pin)
M1 & M0 – Mode bits Y Aruna Suhasini 64
Associate Professor
TIMER PROGRAMMING
Y Aruna Suhasini
67
Associate Professor
Timer / Counter Mode 1 : 16 Bit Counter
using Timer 1
Y Aruna Suhasini
69
Associate Professor
Timer:
:
Y Aruna Suhasini
70
Associate Professor