You are on page 1of 31

Introduction to 8051

To make a complete microcomputer system, only microprocessor is not sufficient. It is


necessary to add other peripherals such as ROM, read/write memory (RAM), decoders, drivers,
number of input/output devices to make a complete microcomputer system.

In addition special purpose devices such as interrupt controller, programmable timers,


programmable I/O devices, DMA controllers may be added to improve the capability,
performance and flexibility of a microcomputer system. The key feature of microprocessor based
system is that is possible to design a system with great flexibility and possible to configure a
system as large or small system by adding suitable peripherals.

On the other hand the microcontroller incorporates all the features that are found in
microprocessor. However it has also added features to make a complete microcomputer system
on its own. The microcontroller has built-in ROM, RAM, parallel I/O, serial I/O, counters and a
clock circuit. The microcontroller has on-chip (built in) peripheral devices that make possible to
have single chip microcomputer system.

Comparisons of Microcontroller and Microprocessor:


Features:

● The 8051 is an 8-bit microcontroller designed by Intel.

● It was optimized for 8-bit math and single bit Boolean operations.

● Its family MCS-51 includes 8031, 8051 and 8751 microcontrollers.

● 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 64K ( 216) byte code memory space and a
separate 64K byte of data memory space.

● The 8051 has 4K on-chip read only code memory and 128 bytes of internal Random
Access Memory (RAM).

● Besides internal RAM, the 8051 has various Special Function Registers (SFR) such as the
Accumulator, the B register, and many other control registers.

● 34 8-bit general purpose registers in total.

● The ALU performs one 8-bit operation at a time.

● Two 16 bit /Counter timers

● 3 internal interrupts (one serial), 2 external interrupts. Four 8-bit I/O ports

● Some 8051 chips come with UART for serial communication and ADC for analog to
digital conversion.

Pin Diagram:

In 1981, Intel introduced an 8-bit microcontroller called the 8051 microcontroller. This is one of
the most popular and most commonly used microcontrollers in various fields like embedded
systems, consumer electronics, automobiles, etc. Microcontroller have all features that are found
in microprocessors with additional built-in ROM, RAM, I/O ports, Serial ports, Timers,
Interrupts, and Clock circuits.

8051 microcontroller is available in 40 pin Dual Inline package (DIP). All these 40 pins
perform different functions like read, write, interrupts, I/O operations, address etc. 8051
microcontroller have four I/O ports (Port-1, Port-2, Port-3, Port-4 ) where in each port has 8 pin.
Each pin configured as a input pin or output pin depends on logic state of the pin. Therefore, out
of 40 pins, 32 pins are allotted for I/O port. The remaining pins are assigned to VCC, GND,
XTAL1, XTAL2, RST, ALE, EA’ and PSEN’.
Pin Description:

Pin 1 to Pin 8 (Port – 1) – Pin 1 to Pin 8 is assigned to Port 1 for simple I/O operations. These
ports are work as a bidirectional port. It means all the pins of port 1 work as a input pin or output
pins. If Logic 1 (one) is applied to the I/O port it will act as a input pin and if logic 0 (zero) is
applied to the I/O port it will act as a output pin.

Pin 9 (RST) – It is a reset input Pin, which is used to reset the 8051 microcontrollers to its initial
values when logic 1 is applied to this pin. It is active high pin.

Pin 10 to Pin 17 (Port-3) – Pin 10 to Pin 17 are assigned to Port 3. This port is also a
bidirectional I/O port like port 1. This port performs some special functions like interrupts,
control signals, timer input, serial communication etc. The detail function of each pins are given
below:
 P 10 (RXD) – Pin 10 is used as a RXD (serial data receive pin) which is for serial
input pin. By using this input signal microcontroller receives data for serial
communication.
 P 11 (TXD) – Pin 11 is used as a TXD (serial data transmit pin) which is serial
output pin. By using this output signal microcontroller transmits data for serial
communication.
 P 12 and P 13 (INT0′, INT1′) – Pins12 and 13 are used for External Hardware
Interrupt 0 and Interrupt 1 respectively. When this interrupt is activated (i.e. when it
is low), 8051 gets interrupted means it stopped whatever it is doing and jumps to the
vector table where ISR’s (Interrupt Service Routine) are stored and starts performing
Interrupt Service Routine (ISR) from that vector location.
 P 14 and P 15 (T0 and T1) – Pin 14 and 15 are used for Timer 0 and Timer 1
external input. They can be connected with 16-bit timer/counter.
 P 16 (WR’) – Pin 16 is used for external memory write i.e. writing data to the
external memory.
 P 17 (RD’) – Pin 17 is used for external memory read i.e. reading data from external
memory.

Pin 18 and Pin 19 (XTAL2 And XTAL1) –Pins 18 and 19 i.e. XTAL 2 and XTAL 1 are the
pins for interfacing external oscillator. Mostly, a Quartz Crystal Oscillator is connected here to
get the system clock.

Pin 20 (GND) – Pin 20 is the Ground Pin. It is connected to the 0V (negative terminal) of the
Power Supply.

Pin 21 to Pin28 (Port 2) – Pin 21 to pin 28 are port 2 pins. Port 2 is also a bidirectional Input
/Output port i.e, all pins of port 2 work as a input pin or as a output pins. But, this is only
possible when we are not using any external memory. If we use external memory, then these pins
will work as high order address bus (A8 to A15).

Pin 29 (PSEN) – The Pin 29 is the Program Store Enable Pin (PSEN). It is used to enable
external program memory and read a signal from the external program memory.

Pin 30 (ALE) – Pin 30 is the Address Latch Enable Pin. This pin is used to enable or disable the
external memory interfacing.

Pin 31 (EA) – Pin 31 is the External Access Enable (EA) Pin. This pin allows external Program
Memory. It is an input pin and connected from VCC or GND. If we want to access the program
from external program memory, it must be connected with GND. If we want to use on-chip
memory, it must be high (connected with VCC).
Pin 32 to Pin 39 (Port 0) – Pin 32 to Pin 39 are Port 0 pins. when we don’t use any external
memory, these pins are used as a bidirectional pin like port 2 and port 3. But, when ALE or Pin
30 is at 1, then this port is used as data bus. And when the ALE pin is at 0, then this port is used
as a lower order address bus (A0 to A7).

Pin 40 (VCC) – This pin is used to provide (+5v ) power supply to the 8051 microcontroller
circuit.

8051 Microcontroller Basic Circuit:

Now that we have seen the 8051 Microcontroller Pin Diagram and corresponding Pin
Description, we will proceed to the basic circuit or schematic of the 8051 Microcontroller. The
following image shows the basic circuit of the 8051 Microcontroller.

This basic circuit of 8051 microcontroller is the minimal interface required for it to work. The
basic circuit includes a Reset Circuit, the oscillator circuit and power supply. Let us discuss a
little bit deeper about this basic circuit of 8051 Microcontroller.

First is the power supply. Pins 40 and 20 (VCC and GND) of the 8051 Microcontroller are
connected to +5V and GND respectively.
Next is the Reset Circuit. A logic HIGH (+5V) on Reset Pin for a minimum of two machine
cycles (24 clock cycles) will reset the 8051 Microcontroller. The reset circuit of the 8051
Microcontroller consists of a capacitor, a resistor and a push button and this type of reset circuit
provides a Manual Reset Option. If you remove the push button, then the reset circuit becomes a
Power-On Reset Circuit.

The next part of the basic circuit of the 8051 Microcontroller is the Oscillator Circuit or the
Clock Circuit. A Quartz Crystal Oscillator is connected across XTAL1 and XTAL2 pins i.e. Pins
19 and 18. The capacitors C1 and C2 can be selected in the range of 20pF to 40pF.

As mentioned in the 8051 Microcontroller Pin Description, PORTS 1, 2 and 3, all have internal
pull – ups and hence can be directly used as Bidirectional I/O Ports. But, we need to add external
Pull – ups for PORT 0 Pins in order to use it as an I/O Port. Generally, a 1KΩ Resistor Pack of 8
Resistors is used as a Pull – up for the PORT 0 of the 8051 Microcontroller.

Architecture of 8051

It is 8-bit microcontroller, means MC 8051 can Read, Write and Process 8 bit data. This is
mostly used microcontroller in the robotics, home appliances like mp3 player, washing
machines, electronic iron and industries. Mostly used blocks in the architecture of 8051 are as
follows:
1. Oscillator and clock generator:
All operations in a microcontroller are synchronized by the help of an oscillator clock.
The oscillator clock generates the clock pulses by which all internal operations are synchronized.
A resonant network connected through pins XTAL1 and XTAL2 forms up an oscillator. For this
purpose a quartz crystal and capacitors are employed. The crystal run at specified maximum and
minimum frequencies typically at 1 MHz to 16 MHz.

2. ALU:
It is 8 bit unit. It performs arithmetic operation as addition, subtraction, multiplication, division,
increment and decrement. It performs logical operations like AND, OR and EX-OR. It
manipulates 8 bit and 16 bit data. It calculates address of jump locations in relative branch
instruction. It performs compare, rotate and compliment operations. It consists of Boolean
processor which performs bit, set, test, clear and compliment. 8051 micro controller contains 34
general purpose registers or working registers.2 of them are called math registers A & B and 32
are bank of registers.

a. Accumulator (A-register):
It is 8 bit register. Its address is E0H and it is bit and byte accessible. Result of arithmetic & logic
operations performed by ALU is accumulated by this register. Therefore it is called accumulator
register. It is used to store 8 bit data and to hold one of operand of ALU units during arithmetical
and logical operations. Most of the instructions are carried out on accumulator data. It is most
versatile of 2 CPU registers.

b. B-register:
It is special 8 bit math register. It is bit and byte accessible. It is used in conjunction with A
register as I/P operand for ALU. It is used as general purpose register to store 8 bit data.

c. PSW:
It is 8 bit register. Its address is D0H and It is bit and byte accessible. It has 4 conditional flags or
math flags which sets or resets according to condition of result. It has 3 control flags, by setting
or resetting bit required operation or function can be achieved. The format of flag register is as
shown below:
The bits B3 and B4 are denoted as RS0 and RS1. These bits are used to select the bank register
of the RAM location.

RS1 RS0 Register Bank

0 0 0
0 1 1
1 0 2
1 1 3

d. FLAG:

1. Carry Flag (CY): During addition and subtraction any carry or borrow is generated then carry
flag is set otherwise carry flag resets. It is used in arithmetic, logical, jump, rotate and Boolean
operations.

2. Auxiliary carry flag (AC): If during addition and subtraction any carry or borrow is
generated from lower 4 bit to higher 4 bit then AC sets else it resets. It is used in BCD arithmetic
operations.

3. Overflow flag (OV): If in signed arithmetic operations result exceeds more than 7 bit than
OV flag sets else resets. It is used in signed arithmetic operations only.

4. Parity flag (P): If in result, even no. of ones "1" are present than it is called even parity and
parity flag sets. In result odd no. of ones "1"are present than it is called odd parity and parity flag
resets.

CONTROL FLAGS:
1. FO: It is user defined flag. The user defines the function of this flag. The user can set , test n
clear this flag through software.

2. RS1 and RS0: These flags are used to select bank of register by resetting those flags which
are as shown in table.

3. Program counter (PC): The Program Counter (PC) is a 2-byte address which tells the 8051
where the next instruction to execute is found in memory. It is used to hold 16 bit address of
internal RAM, external RAM or external ROM locations. When the 8051 is initialized PC
always starts at 0000h and is incremented each time an instruction is executed. It is important to
note that PC isn’t always incremented by one and never decremented.

4. Data pointer register (DTPR): It is a 16 bit register used to hold address of external or
internal RAM where data is stored or result is to be stored. It is used to store 16 bit data. It is
divided into2- 8bit registers, DPH-data pointer higher order (83H) and DPL-data pointer lower
order (82H). Each register can be used as general purpose register to store 8 bit data and can also
be used as memory location. DPTR does not have single internal address. It functions as Base
register in base relative addressing mode and in-direct jump.

5. Stack pointer (SP): It is 8-bit register. It is byte addressable. Its address is 81H. It is used to
hold the internal RAM memory location addresses which are used as stack memory. When the
data is to be placed on stack by push instruction, the content of stack pointer is incremented by 1,
and when data is retrieved from stack, content of stack of stack pointer is decremented by 1.

iii. Special function Registers (SFR): The 8051 microcontroller has 11 SFR divided in 4
groups:

A. Timer/Counter register: 8051 microcontroller has 2-16 bit Timer/counter registers called
Timer-reg-T0 and Timer/counter Reg-T1.Each register is 16 bit register divide into lower and
higher byte register as shown below: These register are used to hold initial no. of count. All of
the 4 register are byte addressable.

1. Timer control register: 8051 microcontroller has two 8-bit timer control register i.e. TMOD
and TCON register. TMOD Register: it is 8-bit register. Its address is 89H. It is byte addressable.
It used to select mode and control operation of time by writing control word.

2. TCON register: It is 8-bit register. Its address is 88H. It is byte addressable. Its MSB 4-bit is
used to control operation of timer/ counter and LSB 4-bit are used for external interrupt control.

B. Serial data register: 8051 micro controller has 2 serial data register viz. SBUF and SCON.

1. Serial buffer register (SBUF): it is 8-bit register. It is byte addressable .Its address is
99H. It is used to hold data which is to be transferred serially.

2. Serial control register (SCON): it is 8-bit register. It is bit/byte addressable. Its address is
98H. The 8-bit loaded into this register controls the operation of serial communication.
C. Interrupt register: 8051 μC has 2 8-bit interrupt register.

1. Interrupt enable register (IE): it is 8-bit register. It is bit/byte addressable. Its address is
A8H.it is used to enable and disable function of interrupt.

2. Interrupt priority register (IP): It is 8-bit register. It is bit/byte addressable. Its address is
B8H. It is used to select low or high level priority of each individual interrupts.

D. Power control register (PCON): it is 8-bit register. It is byte addressable .Its address is 87H.
Its bits are used to control mode of power saving circuit, either idle or power down mode and
also one bit is used to modify baud rate of serial communication.

Internal RAM
Internal RAM has memory 128-byte. See 8051 hardware for further internal RAM design.
Internal RAM is organized into three distinct areas: 32 bytes working registers from address 00h
to 1Fh 16 bytes bit addressable occupies RAM byte address 20h to 2Fh, altogether 128
addressable bits General purpose RAM from 30h to 7Fh.

Internal ROM
Data memory and program code memory both are in different physical memory but both have
the same addresses. An internal ROM occupied addresses from 0000h to 0FFFh. PC addresses
program codes from 0000h to 0FFFh. Program addresses higher than 0FFFh that exceed the
internal ROM capacity will cause 8051 architecture to fetch codes bytes from external program
memory.

8051 Memory Organization:

The 8051 Microcontroller Memory is separated in Program Memory (ROM) and Data Memory
(RAM). 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 used for storing temporary
variable data and intermediate results.

8051 Microcontroller has both Internal ROM and Internal RAM. If the internal memory is
inadequate, you can add external memory using suitable circuits.

1. Program memory: (4kB ROM)


Program memory accessed through EA pin. In program memory two categories takes
place:
a) If EA is high, internal program memory is accessed up to 0FFFH memory location and
external program memory accessed from 1000H to FFFFH memory locations.
b) If EA is low, only external program memory accessed from 0000H to FFFFH memory
locations.

2. Data memory:
Data memory is used to store the memory in the registers each of 64k bytes size, to
access the data memory instruction MOVX is used. Data memory is of two types Internal
and external.

i) Internal data memory:


The internal data memory consists of 256 bytes, these are divided into two parts:
a) 00H-7FH for internal data RAM (lower 128 bytes)
b) 80H-FFH for special function registers (upper 128 bytes)

ii) External data memory:


The 8051 gives the facility to interface external RAM and ROM. External RAM is
accessed by DPTR and up to 64KB of RAM can be interfaced. External data memory
interfacing is of two types i.e. RAM and ROM interfacing.

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 normal operation of the microcontroller. Original
Intel’s 8051 Microcontroller had 128B of internal RAM.

In the MCS-51 family, 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 upto 64K (external) + 128 bytes (internal). 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).

In the first 128B of RAM (from 00H to 7FH), the first 32B i.e., memory from addresses 00H to
1FH consists of 32 Working Registers that are organized as four banks with 8 Registers in each
Bank. So there are 3 separations/divisions of the data memory:- 1) Register banks 2)
Bit addressable area 3) Scratch pad area.
Register banks form the lowest 32 bytes on internal memory and there are 4 register banks
designated bank #0, #1, #2 and #3. Each bank has 8 registers which are designated as R0,
R1…R7. At a time only one register bank is selected for operations and the registers inside the
selected bank are accessed using mnemonics R0..R1.. etc. Other registers can be accessed
simultaneously only by direct addressing. Registers are used to store data or operands during
executions. By default register bank #0 is selected (after a system reset).
The bit addressable areas of 8051 are usually used to store bit variables. The bit addressable area
is formed by the 16 bytes next to register banks. They are designated from address 20H to 2FH
(total 128 bits). Each bit can be accessed from 00H to 7FH within these 128 bits from 20H to
2FH. Bit addressable area is mainly used to store bit variables from application program, like
status of an output device like LED or Motor (ON/OFF) etc.

Ex: MOV A, #03H; copy byte 03 to register A


MOV 26 H, A; copy data byte from register a to memory location 26 H

SETB 32H  set to 1


CLR 32H  set to 0

The scratch pad area is the upper 80 bytes which is used for general purpose storage. Scratch pad
area is from 30H to 7FH and this includes stack too. Used for read and write storage of data. If
we need more registers we simply use RAM locations 30H to 7FH.

Program memory organization (ROM)

8051 has an internal program of 4K size and if needed an external memory can be added (by
interfacing) of size 60K maximum. So in total 64K size memory is available for 8051 micro
controller. By default, the External Access (EA) pin should be connected Vcc so that
instructions are fetched from internal memory initially. When the limit of internal memory (4K)
is crossed, control will automatically move to external memory to fetch remaining instructions. If
the programmer wants to fetch instruction from external memory only (bypassing the internal
memory), then he must connect External Access (EA) pin to ground (GND).
Register Set of 8051

Accumulator
The accumulator which is also known as ACC or A is a bit as well as a byte-addressable register
by an address of the accumulator. If you want to use a bit-addressable register, you can use a
single bit (E0) of the register and you can use an 8-bit of the accumulator as a byte-addressable
register. The accumulator holds the results of most Arithmetic and logical operations.

B-Register
The B-register is a bit and byte-addressable general purpose register. You can access 1-bit or all
8-bits by a physical address F0h. Suppose to access a bit 1, we have to use f1. The B register is
only used for multiplication and division operations.

PSW (Program Status Word) Register

The PSW register is a bit and byte-addressable register. This register reflects the status of the
operation that is carried out in the controller. The PSW register determines bank selection by an
RS1 and RS0, as shown below. The physical address of the PSW starts from D0h and the
individual bits are accessed with D0h to D7h.
DPTR (DATA POINTER REGISTER)

This is a 16 bit register. It is used to access external code or data memory


The Data Pointer (DPTR) is the 8051’s only user-accessible 16-bit (2-byte) register. It is used as
a base register in JUMP/Lookup table instructions and external data transmission.

Program Counter (PC):

It is a 16 bit register used to hold the address of memory location from which the next instruction
is to be fetched.

Stack pointer register:

The stack refers to an area of internal RAM that is used in conjunction with certain opcode data
to store and retrieve data quickly. The stack pointer register is used by the 8051 to hold the
internal RAM address that is called the top of the stack. It is 8-bit wide. The stack array can
reside anywhere in on-chip RAM

Special Function registers:

8051 uses memory mapped I/O through a set of SFRs that are implemented in the address space
immediately above the 128bytes of RAM. All access to four I/O ports, CPU registers, interrupt-
control registers, timer/counter, UART and power control are performed through registers
between 80H and FFH.
Timers and Counters:

The 8051 has two counters/timers which can be used either as timer to generate a time delay or
as counter to count events happening outside the microcontroller.

The 8051 has two timers: timer0 and timer1. They can be used either as timers or as counters.
Both timers are 16 bits wide. Since the 8051 has an 8-bit architecture, each 16-bit is accessed as
two separate registers of low byte and high byte.

8051 timers always count up. Each counter has a 16 bit count register in the SFR area. The low
and high bytes can be accessed as separate bytes. When their count rolls over from the maximum
count to 0000, they set the corresponding timer flag (TF1 or TF0) in TCON. The 8051 can be set
up so that an interrupt occurs whenever TF1 or TF0 is set. When 8051 branches to the interrupt
vector, it automatically clears the TF flag.

When used as timers, the 8051 timers count up every 12th clock cycle. This is selected by
clearing the corresponding C/T flags in the TMOD special function register, placed at the
address 89H.
Timer0 registers is a 16 bits register and accessed as low byte and high byte. The low byte is
referred as a TL0 and the high byte is referred as TH0. These registers can be accessed like any
other registers.

Timer1 registers is also a 16 bits register and is split into two bytes, referred to as TL1 and
TH1.

Registers used in Timers/Counters:

TMOD (timer mode) Register: This is an 8-bit register which is used by both timers 0 and 1 to
set the various timer modes. In this TMOD register, lower 4 bits are set aside for timer0 and the
upper 4 bits are set aside for timer1. In each case, the lower 2 bits are used to set the timer mode
and upper 2 bits to specify the operation.
In upper or lower 4 bits, first bit is a GATE bit. Every timer has a means of starting and stopping.
Some timers do this by software, some by hardware, and some have both software and hardware
controls. The hardware way of starting and stopping the timer by an external source is achieved
by making GATE=1 in the TMOD register. And if we change to GATE=0 then we do no need
external hardware to start and stop the timers.

The second bit is C/T bit and is used to decide whether a timer is used as a time delay generator
or an event counter. If this bit is 0 then it is used as a timer and if it is 1 then it is used as a
counter.

In upper or lower 4 bits, the last bits third and fourth are known as M1 and M0 respectively.
These are used to select the timer mode.

M1 M0 Operating Mode

0 0 Mode 0: 13 –bit timer mode, 8-bit timer /counter with 5-bit prescalar

Mode1: 16–bit timer /counter


0 1
THx & TLx are cascaded

1 0 Mode 2: 8-bit auto reload timer or counter

1 1 Split timer mode: The function depends on Timer 0 and Timer 1

Mode 1- It is a 16-bit timer; therefore it allows values from 0000 to FFFFH to be loaded into the
timer’s registers TL and TH. After TH and TL are loaded with a 16-bit initial value, the timer
must be started. We can do it by “SETB TR0” for timer 0 and “SETB TR1” for timer 1.

After the timer is started, it starts count up until it reaches its limit of FFFFH. When it rolls over
from FFFF to 0000H, it sets high a flag bit called TF (timer flag). This timer flag can be
monitored. When this timer flag is raised, one option would be stop the timer with the
instructions “CLR TR0“ or CLR TR1 for timer 0 and timer 1 respectively. Again, it must be
noted that each timer flag TF0 for timer 0 and TF1 for timer1.
After the timer reaches its limit and rolls over, in order to repeat the process the registers TH and
TL must be reloaded with the original value and TF must be reset to 0.

Mode0- Mode 0 is exactly same like mode 1 except that it is a 13-bit timer instead of 16-bit. The
13-bit counter can hold values between 0000 to 1FFFH in TH-TL. Therefore, when the timer
reaches its maximum of 1FFH, it rolls over to 0000, and TF is raised.

A prescalar is more like a multiplier or a step for incrementing the timer. Normally the timer
will increment for every clock cycle. Instead we can use prescalar to increment it every 2 clock
cycle once or 4 clock cycle once etc. The purpose of the prescalar is to allow the timer to be
clocked at the rate a user desires. For shorter (8 and 16-bit) timers, there will often be a tradeoff
between resolution (high resolution requires a high clock rate) and range (high clock rates cause
the timer to overflow more quickly).

Mode 2- It is an 8 bit timer that allows only values of 00 to FFH to be loaded into the timer’s
register TH. After TH is loaded with 8 bit value, the 8051 gives a copy of it to TL. Then the
timer must be started. It is done by the instruction “SETB TR0” for timer 0 and “SETB TR1” for
timer1. This is like mode 1.

After timer is started, it starts to count up by incrementing the TL registers. It counts up until it
reaches its limit of FFH. When it rolls over from FFH to 00, it sets high the TF (timer flag). If we
are using timer 0, TF0 goes high; if using TF1 then TF1 is raised. When TL register rolls from
FFH to 00H and TF is set to 1, TL is reloaded automatically with the original value kept by the
TH register.
To repeat the process, we must simply clear TF and let it go without any need by the
programmer to reload the original value. This makes mode 2 auto reload, in contrast in mode 1 in
which programmer has to reload TH and TL.

Mode3- Mode 3 is also known as a split timer mode. Timer 0 and 1 may be programmed to be in
mode 0, 1 and 2 independently of similar mode for other timer. This is not true for mode 3;
timers do not operate independently if mode 3 is chosen for timer 0. Placing timer 1 in mode 3
causes it to stop counting; the control bit TR1 and the timer 1 flag TF1 are then used by timer0.

1) When the Timer0 is working in mode 3, the TL0 will be used as an 8-bit timer/counter. It will
be controlled by the standard Timer0 control bits, T0 and INT0 inputs.

The TH0 is used as an 8-bit timer but not the counter. This is controlled by Timer1 Control bit
TR1. When the TH0 overflows from FFH to 00H, then TF1 is set to 1. In the following diagram,
we can Timer0 in Mode 3.

2) When the Timer1 is working in Mode 3, it simply holds the count but does not run.
TCON (timer control) Register: Timer Control or TCON Register is 8-bit register used to start
or stop the Timers of 8051 Microcontroller. It also contains bits to indicate if the Timers has
overflowed. The TCON SFR also consists of Interrupt related bits.

 TF1, TF0 (Timer Overflow Flag): Both the bits work independently. If these bits are equal to
one, it indicates that Timer interrupt has occurred. These bits are auto cleared.
 TR1, TR0 (Timer Run Control bit): If these bits are equal to one it indicates to start the timer
and if they are equal to zero then it is to stop the timer.
 IE1, IE0 (External interrupt bit): If external interrupt has occurred then these bits are equal to
one else they remain zero. Technically these bits are similar to timer overflow bits. These bits are
also auto cleared.
 IT1, IT0 (Interrupt trigger bit): Considering active high condition, if the bits are equal to one
then it indicates edge triggered, if the bits are equal to zero then it indicates level triggered.

Timer/Counter Control Logic:


● Crystal oscillator is divided by 12 to get the operating frequency for the timer.

● If C/T = 0, it act as timer and C/T = 1 act as counter.

● We can control the timer operations by using the signals from AND gate, where one input
bit is from TR bit of TCON.

● The other input is given by the OR gate, for this OR gate there are again two inputs given
: One is from GATE bit of TMOD and the other is external hardware interrupt INT x
from TCON register.

Serial Communication:

DATA COMMUNICATION

The 8051 microcontroller is parallel device that transfers eight bits of data simultaneously over
eight data lines to parallel I/O devices. Parallel data transfer over a long is very expensive.
Hence, a serial communication is widely used in long distance communication. In serial data
communication, 8-bit data is converted to serial bits using a parallel in serial out shift register
and then it is transmitted over a single data line. The data byte is always transmitted with least
significant bit first.

BASICS OF SERIAL DATA COMMUNICATION

Communication Links

1. Simplex communication link: In simplex transmission, the line is dedicated for


transmission. The transmitter sends and the receiver receives the data.

2. Half duplex communication link: In half duplex, the communication link can be used
for either transmission or reception. Data is transmitted in only one direction at a time.
3. Full duplex communication link: If the data is transmitted in both ways at the same
time, it is a full duplex i.e. transmission and reception can proceed simultaneously. This
communication link requires two wires for data, one for transmission and one for
reception.

Types of Serial communication:

Serial data communication uses two types of communication.

1. Synchronous serial data communication:


In this transmitter and receiver are synchronized. It uses a common clock to synchronize
the receiver and the transmitter. First the synch character is sent and then the data is
transmitted. This format is generally used for high speed transmission. In Synchronous
serial data communication a block of data is transmitted at a time.

2. Asynchronous Serial data transmission:


In this, different clock sources are used for transmitter and receiver. In this mode, data is
transmitted with start and stop bits. A transmission begins with start bit, followed by data
and then stop bit. For error checking purpose parity bit is included just prior to stop bit. In
Asynchronous serial data communication a single byte is transmitted at a time.

Baud rate:
The rate at which the data is transmitted is called baud or transfer rate. The baud rate is the
reciprocal of the time to send one bit. In asynchronous transmission, baud rate is not equal to
number of bits per second. This is because; each byte is preceded by a start bit and followed by
parity and stop bit. For example, in synchronous transmission, if data is transmitted with 9600
baud, it means that 9600 bits are transmitted in one second. For bit transmission time = 1 second/
9600 = 0.104 ms.
Framing the data
 Each block (usually a byte) of data transmitted is actually sent in a packet or frame of bits.
 Frames are created by appending synchronization and parity bits to our data.
 Some symbols in the frame have configurable bit sizes.

8051 SERIAL COMMUNICATION


The 8051 supports a full duplex serial port.
Three special function registers support serial communication.

1. SBUF Register:
Serial Buffer (SBUF) register is an 8-bit register. It has separate SBUF registers for data
transmission and for data reception. For a byte of data to be transferred via the TXD line, it
must be placed in SBUF register. Similarly, SBUF holds the 8-bit data received by the RXD
pin and read to accept the received data.

2. SCON Register:

The Serial Control or SCON SFR is used to control the 8051 Microcontroller’s Serial Port. It
is located as an address of 98H. Using SCON, you can control the Operation Modes of the
Serial Port, Baud Rate of the Serial Port and Send or Receive Data using Serial Port.
SCON Register also consists of bits that are automatically SET when a byte of data is
transmitted or received.

The Serial Port Mode Selection Bits (SM0 and SM1) determine the mode of UART and also the
baud rate. The following table gives an overview of how the Serial Port Mode Selection Bits can
be used to configure Serial Port (UART) of 8051.

SM0 SM1 Programmable Mode

0 0 Mode 0: Shift register – Baude rate f/12

0 1 Mode 1: 8-bit UART – variable data rate

1 0 Mode 2: 9-bit UART – fixed data rate

1 1 Mode 3: 9-bit UART – variable data rate

SM2: Enables multiprocessor communication in modes 2 and 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=1 then
RI will not be activated if a valid stop bit was not received. In mode 0, SM2 should be 0.

REN: Receiver Enable control bit enables/disables data reception. Set HIGH to receive data on
the RxD pin.

TB8: Transmit bit 8

The TB8 bit is used in modes 2 and 3. In modes 2 and 3, a total of 9 bits are transmitted. The first
8 bits are the 8 bits of the main value, and the 9th bit is taken from TB8. If TB8 is set and a value
is written to the serial port the data’s bits will be written to the serial line followed by a “set” 9th
bit. If TB8 is clear the 9th bit will be clear.

RB8: Receive Bit 8

The RB8 also operates in mode 2 and 3 and functions essentially the same way as TB8, but on
the reception side. When a byte is received in modes 2 or 3, a total of nine bits are received. In
this case, the first 8 bits received are the data of the serial byte received and the value of ninth bit
will be placed in RB8.

TI: Transmit Interrupt Flag is set, when 8051 finishes transfer of 8-bit character.

RI: Receive Interrupt Flag is set to indicate a byte has been received by 8051 and placed in SBUF.

3. PCON Register:
PCON (Power Control) register is used to force the 8051 microcontrollers into power-saving
mode. The power control register of 8051 contains two power-saving mode bits and one serial
baud rate control bit. It is located at 87H of the SFR Memory Space. Using two bits in the
PCON Register, the microcontroller can be set to Idle Mode and Power Down Mode.

During Idle Mode, the Microcontroller will stop the Clock Signal to the ALU (CPU) but it is
given to other peripherals like Timer, Serial, Interrupts, etc. In order to terminate the Idle Mode,
you have to use an Interrupt or Hardware Reset.

In the Power Down Mode, the oscillator will be stopped and the power will be reduced to 2V.
To terminate the Power Down Mode, you have to use the Hardware Reset.

Apart from these two, the PCON Register can also be used for few additional purposes. The
SMOD Bit in the PCON Register is used to control the Baud Rate of the Serial Port.

There are two General purpose Flag Bits (GF1 & GF0) in the PCON Register, which can be
used by the programmer during execution.
Interrupts in 8051

The most powerful and important features are interrupts in 8051 microcontroller. In most of the
real-time processes, to handle certain conditions properly, the actual task must be halt for some
time – it takes required action – and then must return to the main task. For executing such type of
programs, interrupts are necessary. It entirely differs from the polling method wherein the
processor must check sequentially each device and ask whether the service is required or not
while consuming more processor time.

Interrupts in 8051 microcontroller are more desirable to reduce the regular status checking of the
interfaced devices or inbuilt devices. Interrupt is an event that temporarily suspends the main
program, passes the control to a special code section, executes the event-related function and
resumes the main program flow where it had left off.

Interrupts are of different types like software and hardware, maskable and non-maskable, fixed
and vector interrupts, and so on. Interrupt Service Routine (ISR) comes into the picture when
interrupt occurs, and then tells the processor to take appropriate action for the interrupt, and after
ISR execution, the controller jumps into the main program.

Types of Interrupts in 8051 Microcontroller

The 8051 microcontroller can recognize five different events that cause the main program to
interrupt from the normal execution. These five sources of interrupts in 8051are:
1. Timer 0 overflow interrupt- TF0
2. Timer 1 overflow interrupt- TF1
3. External hardware interrupt- INT0
4. External hardware interrupt- INT1
5. Serial communication interrupt- RI/TI

The Timer and Serial interrupts are internally generated by the microcontroller, whereas the
external interrupts are generated by additional interfacing devices or switches that are externally
connected to the microcontroller. These external interrupts can be edge triggered or level
triggered. When an interrupt occurs, the microcontroller executes the interrupt service routine so
that memory location corresponds to the interrupt that enables it. The Interrupt corresponding to
the memory location is given in the interrupt vector table below.
Interrupt Structure of 8051 Micro controller

Upon ‘RESET’ all the interrupts get disabled, and therefore, all these interrupts must be enabled
by software. In all these five interrupts, if anyone or all are activated, this sets the corresponding
interrupt flags as shown in the figure. All these interrupts can be set or cleared by bit in some
special function register that is Interrupt Enabled (IE), and this in turn depends on the priority,
which is executed by IP interrupt priority register.

Interrupt Enable (IE) Register: This register is responsible for enabling and disabling the
interrupt. It is a bit addressable register in which EA must be set to one for enabling interrupts.
The corresponding bit in this register enables particular interrupt like timer, external and serial
inputs. In the below IE register, bit corresponding to 1 activates the interrupt and 0 disables the
interrupt.
Interrupt Priority Register (IP): It is also possible to change the priority levels of the interrupts
by setting or clearing the corresponding bit in the Interrupt priority (IP) register as shown in the
figure. This allows the low priority interrupt to interrupt the high-priority interrupt, but prohibits
the interruption by another low-priority interrupt. Similarly, the high-priority interrupt cannot be
interrupted. If these interrupt priorities are not programmed, the microcontroller executes in
predefined manner and its order is INT0, TF0, INT1, TF1, and SI.
TCON Register: In addition to the above two registers, the TCON register specifies the type of
external interrupt to the 8051 microcontroller, as shown in the figure. The two external
interrupts, whether edge or level triggered, specify by this register by a set, or cleared by
appropriate bits in it. And, it is also a bit addressable register.

You might also like