8051
8051
A smaller computer
On-chip RAM, ROM, I/O ports...
Example:Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and PIC
16X
• Dependent on other chips for many • Single chip computer which has
functions so hardware size increases. everything in-built.
• CPU is stand-alone, RAM, ROM, I/O, • CPU, RAM, ROM, I/O and timer are
timer are separate. all on a single chip.
• Designer can decide on the amount • fix amount of on-chip ROM, RAM,
of ROM, RAM and I/O ports. I/O ports.
• Program memory and data memory • Uses different program and data
are same. memory.
• Suited to processing information in • Suited to control of I/O devices
computer systems. requiring a minimum component count.
• Powerful addressing mode. • Extremely compact instructions.
• Data bus: 32,64,128 bits wide. • Data bus: 8,16 bits wide.
• Clock rate > 1GHz. • 10 to 20 MHz.
• general-purpose. • single-purpose.
• Expensive. • Cheap.
Features of 8051
• 8-bit CPU
• 4 KB of on chip program memory.
• 128 bytes of on chip data memory.
• 64 KB program external ROM and 64 KB external RAM addressability.
• 32 bidirectional and individually addressable I/O lines arranged as four 8
bit ports P0-P3.
• Two 16 bit timer/counters.
• Full duplex serial data transmitter/receiver.
• Four register banks.
• 8 bit program status word and stack pointer.
• Interrupt structure with two priority levels.
• On chip oscillator and clock circuits.
• Binary or decimal arithmetic.
• Power saving mode
8051 Architecture
Functional Blocks of 8051
• Eight bit register A (Accumulator) and register B.
• Arithmetic and Logic Unit (ALU).
• 16 bit Program counter (PC).
• 16 bit Data Pointer (DPTR).
• 8 bit program Status Word (PSW).
• 8 bit stack pointer (SP).
• 128 byte Internal RAM and 4 KB Internal ROM.
• Four 8 bit ports : Port 0, Port 1, Port 2, Port 3.
• Two 16 bit Timer/counters, serial port and interrupt
control.
• Control Registers.
• On chip oscillator.
Accumulator ( ACC )
8-bit Register.
It is a multipurpose register.
It holds operands & result of ALU operations.
Also used for data transfer between 8051 and
any external memory.
Rotate, swap etc. specifically apply on the
accumulator.
B Register
8-bit Register.
Used with the A register for multiplication and
division operations.
For other instructions it is treated as a scratch
pad register.
Arithmetic and Logic Unit ( ALU )
It can perform arithmetic and logic operations
on eight bit data.
It can perform arithmetic operations like
addition, subtraction, multiplication, division
and logical operations like AND, OR, EX-OR,
complement, rotate etc.
It also takes care of branching instructions.
CY AC F0 RS1 RS0 OV -- P
1 0 2 10H-17H
1 1 3 18H-1FH
Clock and Oscillator
Oscillator circuit generates the clock pulses to
synchronize all internal operations.
Quartz crystal is used.
8051 requires frequencies ranging from 1 MHz
to 16 MHz.
QUARTZ CRYSTAL OR 8051
CERAMIC RESONATOR
C1
XTAL2
C2
XTAL1
VSS
Program Counter ( PC )
It is a 16 bit register.
It is used to hold the address of a instruction in
the memory
When power supply is switched on, the PC
resets to 0000H.
Data Pointer ( DPTR )
It is a 16 bit register.
It is used to hold the address of data in the
memory
It can be accessed separately as lower eight bits
(DPL) and higher eight bits (DPH)
It serves as a base register in case of
instructions handling look up tables and
external data transfer.
Stack and Stack Pointer
Bit-Addressable
The stack pointer in 20H
RAM
30H
2FH
Bit-Addressable RAM
20H
1FH
Register Bank 3
18H
17H
Register Bank 2
10H
0FH
(Stack) Register Bank 1
08H
07H
Register Bank 0
00H
Four register banks ( 8 bytes each)
Each bank is made up of eight 1F
registers named R0 to R7.
Bank 3
For selecting a register bank
two bits RS0 and RS1 are 18
17
provided in the PSW.
Bank 2
Can be used as a simple
scratch pad RAM or general 100F
purpose RAM.
Bank 1
Upon power on or reset
BANK0 is selected by default. 0807 R7
06 R6
It reduces the latency period 0504 R5
R4
03 R3 Bank 0
for a subroutine call or an 02 R2
01 R1
interrupt. 00 R0
Bit addressable area (16 bytes)
2F 7F 78
8051 has reserved 16 bytes of 2E
internal RAM (128 bits 2D
TB1
Read pin Data P0.x
8051 IC
Writing “1” to Output Pin P1.X
TB1
Read pin
8051 IC
Writing “0” to Output Pin P1.X
TB1
Read pin
8051 IC
Reading “High” at Input Pin
1 1 P1.X pin
Internal CPU bus D Q
P1.X
0 M1
Write to latch Clk Q
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
8051 IC
Reading “Low” at Input Pin
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
8051 IC
Other Pins
• P1, P2, and P3 have internal pull-up resisters.
– P1, P2, and P3 are not open drain.
• P0 has no internal pull-up resistors and does not
connects to Vcc inside the 8051.
– P0 is open drain.
– Compare the figures of P1.X and P0.X.
• However, for a programmer, it is the same to
program P0, P1, P2 and P3.
• All the ports upon RESET are configured as output.
Port 0
Multifunctioned port of microcontroller used as
1) Simple Input Port.
2) Simple Output Port.
3) Multiplexed address / data bus (AD0 - AD7) for external memory.
Read latch
TB2
TB1
Read pin
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
Port 3
Also called as “quasi-bidirectional” port.
Multifunctioned port used as
1) Simple Input Port.
2) Simple Output Port.
3) One of the alternate functions.
R0
DPTR DPH DPL
R1
R2 PC PC
R3
R5
R6
R7
P1.0 1 40 Vcc
P1.1 2 39 P0.0(AD0)
P1.2 3 38 P0.1(AD1)
P1.3 4 8051 37 P0.2(AD2)
P1.4 5 36 P0.3(AD3)
P1.5 6 (8031) 35 P0.4(AD4)
P1.6 7 34 P0.5(AD5)
P1.7 8 33 P0.6(AD6)
RST 9 32 P0.7(AD7)
(RXD)P3.0 10 31 EA/VPP
(TXD)P3.1 11 30 ALE/PROG
(INT0)P3.2 12 29 PSEN
(INT1)P3.3 13 28 P2.7(A15)
(T0)P3.4 14 27 P2.6(A14)
(T1)P3.5 15 26 P2.5(A13)
(WR)P3.6 16 25 P2.4(A12)
(RD)P3.7 17 24 P2.3(A11)
XTAL2 18 23 P2.2(A10)
XTAL1 19 22 P2.1(A9)
GND 20 21 P2.0(A8)
Pins of 8051
• Vcc(pin 40):
– Vcc provides supply voltage to the chip.
– The voltage source is +5V.
• GND(pin 20):ground
• XTAL1 and XTAL2(pins 19,18)
• RST(pin 9):reset
– It is an input pin and is active high(normally low).
• The high pulse must be high at least 2 machine cycles.
– It is a power-on reset.
• Upon applying a high pulse to RST, the microcontroller will reset
and all values in registers will be lost.
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
Pins of 8051
• EA/VPP(pin 31):external access
– There is no on-chip ROM in 8031 and 8032 .
– The EA pin is connected to GND to indicate the code is stored
externally.
– For 8051, EA pin is connected to Vcc.
– This pin also receives 12V, programming enable voltage (VPP), during
flash programming.
Timers / Counters
Two 16 bit Timer / Counter register namely Timer 0
and Timer 1.
Timer: Register is programmed to count the internal
clock pulse.
− Internal clock pulses are generated from a constant clock generator, the
count loaded in the register gives constant time.
− Register is incremented every machine cycle
(1 machine cycle = 12 oscillator periods)
− Counting rate is 1/12 of the oscillator frequency.
Timers / Counters
Counter: microcontroller is programmed to count
external pulses.
− The register is incremented in response to high to low transition of the
corresponding external input pin, T0 and T1.
− The external input does not have a constant frequency and hence it is not
used for timing reference.
− T0 and T1 pins are sampled during S5P2 of every machine cycles.
− When the samples show high in one cycle and low in the next, the count
is incremented.
− The new count value is appears in S3P1 of the following detection cycle.
− Hence in order to recognize the high-low transition the microcontroller
requires two machine cycle i.e. 24 oscillator periods.
− Max count rate is 1/24 of oscillator frequency
Timers / Counters
The counter / timer registers are divided into 8 bit
registers.
− Timer low (TL0 and TL1)
− Timer high (TH0 and TH1)
− TL0 and TH0 together form the 16 bit Timer 0.
− TL1 and TH1 together forms the 16 bit Timer 1.
Counter action is controlled by the bits in the
− Timer mode control register (TMOD).
− Timer / Counter control register (TCON).
TMOD register
TL1 TH1
TF1 INTERRUPT
(5 bits) (8 bits)
T1 PIN
GATE
TR1
MODE 0
INT1 PIN
C/T=1
T1 PIN
TR1
GATE MODE 1
INT1 PIN
Same as Mode 0.
Timer / counter operates as a 16 bit register.
Mode 2
OSC 1/12
TL1
TF1 INTERRUPT
(8 bits)
T1 PIN
RELOAD
TR1
GATE TH1 MODE 2
(8 bits)
INT1 PIN
C/T=1
T0 PIN
TR0
GATE MODE 3
INT0 PIN
TH0
1/12 fosc TF1 INTERRUPT
(8 bits)
TR1
Timer 1 is not used, Timer 1 simply holds its count. The timer 0 register TL0
and TH0 are configured as two separate 8 bit counters.
TL0 register uses the Timer 0 control bits C / T, GATE, TR0, INT0 and TF0.
TH0 counts the machine cycles. It takes over the use of TR1 and TF1 from
Timer 1
When the Timer 0 is in mode 3, Timer 1 may be used in modes 0,1,2 and no
interrupts will be generated because Timer 0 is using the TF1 flag.
Serial Data Input and Output
• For communication between two computer system we can send and
receive the data bits serially.
• Supports full-duplex serial communication
• Serial port receive and transmit registers are both accessed through
Special Function Register SBUF
– Writing to SBUF loads the transmit register
– Reading from SBUF accesses a physically separated receive register
• SCON - Serial Port Control / Status Register controls the data
communication.
• PCON register along with timer 1 controls the data rates.
• SBUF register comprises of two registers physically via
− TXD Pin
− RXD Pin
• Double buffered receiver is used.
• Serial data communication is a relatively slow process, so in order not
to tie up with valuable processor time, serial data flags are included in
SCON to aid in efficient data transmission and reception.
SCON Register
SM2
• SM0 SM1:
– 00: Mode 0, Shift register, fosc//12
– 01: Mode 1, 8-bit UART, variable
– 10: Mode 2, 9-bit UART, fosc//32 or fosc//64
– 11: Mode 3, 9-bit UART, variable
• SM2: Enables multiprocessor features in Mode 2 and
Mode 3
– In Mode 2 or 3, if SM2 is set to 1 then RI will not be activated if the
received 9th data bit (RB8) is 0.
– In Mode 1, if SM2 is set to 1 then RI will not be activated if valid stop bit
was not received.
– In Mode 0, SM2 should be 0
• REN: Enables serial reception
– Set/Clear by software
SCON Register
synchronization event
Full duplex mode. It supports 8 bit asynchronous communication. Whether there is
a change in data 1 is transmitted, otherwise 0 is transmitted.
Serial data enters through RXD, exits through TXD
Although the data bits are 8 bit, the number of transmitted bits are 10 i.e. 1 start bit,
1 stop bit and 8 data bits.
Start bit is discarded, 8 bit data go to SBUF and stop bit goes into RB8 in SCON
register.
Baud rate is variable, determined by timer 1 overflow rate.
Timer is used in mode 2 as an auto reload 8 bit timer.
Baud rate = (2SMOD / 32) * (oscillator frequency / 12 + (256 – (TH1)))
SMOD is a control bit in the PCON register, it can be 0 or 1.
If timer 1 is not in mode 2 then the baud rate is
Baud rate = (2SMOD / 32) * (timer 1 overflow rate)
Mode 2
Serial data enters through RXD, exits through TXD
11 bits are transmitted/received:
start bit(0), 8 data bits (LSB first), a programmable 9th bit, stop bit(1)
On transmit, the 9th bit is TB8 in SCON register
On receive, the 9th bit goes into RB8 in SCON register, both start and stop
bits are discarded.
Baud rate is higher for many multiprocessor applications.
The baud rate is programmable to either
1/32 or 1/64 the oscillator frequency
Baud Rate =[(2SMOD)/64]*Oscillator frequency
Mode 2
Same as MODE 2 in all respects except baud rate
The baud rate is variable
Interrupts
External asynchronous input applied to the
microcontroller.
8051 supports five interrupts
• Three interrupts that are Automatically generated by internal
operations – TF0, TF1 and serial port interrupt (RI or TI).
• Two interrupts that are triggered by external signals are - INT0
and INT1
All interrupt functions are under program control.
• By setting or clearing the bits in IE, IP and TCON register the
programmer can block any or all of the interrupt.
Interrupts
IT0=0
• 5 interrupt sources INT0 IE0
• 2 external IT0=1
(INT0, INT1)
• 2 timers TF0
(TF0, TF1)
• Serial Port IT1=0
INTERRUPT
(RI or TI) INT1 IE1 SOURCE
IT1=1
TF1
RI
TI
Timer flag interrupt Part 4
– When the timer / counter overflows, the corresponding timer flag TF0 or TF1 is set to 1.
The flag is cleared to 0 when the interrupt generates program call to the timer subroutine
in the memory.
– Interrupts are generated by TF0 and TF1 in register TCON
– When a timer interrupt is generated, the flag that generated it is cleared by hardware
when the service routine is vectored to
TI
RI
59/175
External interrupts
IT0=0
INT0 IE0
IT0=1
− Two interrupts that are generated by external circuits are INT0 and INT1.
− These interrupts may be edge triggered or they may be level triggered.
– Level-activated or transition-activated depending on bits IT0, IT1 in register
TCON
– The inputs on the pins of these interrupts sets the interrupt IE0 and IE1 in the
TCON register.
– The flags that generate these interrupts are IE0, IE1 in TCON
• Cleared by hardware if the interrupt was transition-activated
• if the interrupt was level-activated, external source controls request bits
– If external interrupt is level-activated, the external source has to hold request
active, until the requested interrupt is actually generated.
– External source has to deactivate the request before interrupt service is
completed, or else another interrupt will be generated.
Interrupt Enable register (IE)
7 6 5 4 3 2 1 0
IE EA - - ES ET! EX1 ET0 EX0
Interrupt Priority
within Level Polling Sequence
1 (Highest) External Interrupt 0
2 Timer 0
3 External Interrupt 1
4 Timer 1
5 (Lowest) Serial Port