ARCHITECTURE OF 8051 MICROCONTROLLER
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.
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.
Oscillator connected between terminals XTAL1 and XTAL2.A quartz crystal used as oscillator.
Generally the crystal oscillator frequency is 11.0952 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 performs compare, rotate and compliment operations.
3.Accumulator
4.B Registor
5.PSW
6.Program counter
7.DPTR
8.Stack Pointer
9.RAM
10.ROM
11.PORTS
12.Temp1 and Temp2
PIN DIAGRAM
Pinout Description Pins 1-8 PORT 1. Each of these pins can be configured
as an input or an output.
Pin 9 RESET. A logic one on this pin disables the
microcontroller and clears the contents of most
registers. In other words, the positive voltage
on this pin resets the microcontroller. By
applying logic zero to this pin, the program
starts execution from the beginning.
Pins10-17 PORT 3. Similar to port 1, each of these pins
can serve as general input or output. Besides,
all of them have alternative functions
Pin 10 RXD. Serial asynchronous communication
input or Serial synchronous communication
output.
Pin 11 TXD. Serial asynchronous communication
output or Serial synchronous communication
clock output.
Pin 12 INT0.External Interrupt 0 input
Pin 13 INT1. External Interrupt 1 input
Pin 14 T0. Counter 0 clock input
Pin 15 T1. Counter 1 clock input
Pin 16 WR. Write to external (additional) RAM
Pin 17 RD. Read from external RAM
Pin 18, 19 XTAL2, XTAL1. Internal oscillator input and
output. A quartz crystal which specifies
operating frequency is usually connected to
these pins.
Pin 20 GND. Ground.
Pin 21-28 Port 2. These port pins are configured as
general inputs/outputs. In case external
memory is used, the higher address byte, i.e.
addresses A8-A15 will appear on this port..
Pin 29 PSEN. If external ROM is used for storing
program then a logic zero (0) appears on it
every time the microcontroller reads a byte
from memory.
Pin 30 ALE. Prior to reading from external memory,
the microcontroller puts the lower address
byte (A0-A7) on P0 and activates the ALE
output. After receiving signal from the ALE pin,
the external latch latches the state of P0 and
uses it as a memory chip address. Immediately
after that, the ALE pin is returned its previous
logic state and P0 is now used as a Data Bus.
Pin 31 EA. By applying logic zero to this pin, P2 and P3
are used for data and address transmission
with no regard to whether there is internal
memory or not. It means that even there is a
program written to the microcontroller, it will
not be executed. Instead, the program written
to external ROM will be executed. By applying
logic one to the EA pin, the microcontroller will
use both memories, first internal then external
(if exists).
Pin 32-39 PORT 0. Similar to P2, if external memory is
not used, these pins can be used as general
inputs/outputs. Otherwise, P0 is configured as
address output (A0-A7) when the ALE pin is
driven high (1) or as data output (Data Bus)
when the ALE pin is driven low (0).
Pin 40 VCC. +5V power supply.
REGISTER STRUCTURE OF 8051
1) Accumulator(A-reg):
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 stored 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.
2)B-register
It is special 8 bit register. It is bit and byte accessible. It is used in conjunction with
A for multiplication and division operations. It is used as general purpose register to store 8 bit
data.It is having an address location FOH
3)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 result of math operation. It has 3 control
flags, by setting or resetting bit ,required operation or function can be achieved.
3.1) Carry Flag(CY): During addition and subtraction any carry or borrow is generated then
carry flag is set otherwise carry flag resets.
3.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.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.
3.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.
3.5) 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
3.6) RS1 and RS0: These flags are used to select bank of register by resetting those flags which
are as shown in table
4)STACK AND STACK POINTER
The stack is a section of RAM used by the CPU to store information temporarily.
This information could be data or an address. The register used to access the stack is called the SP
(stack pointer) register. Stack pointer is a 8 bit register and having an address 81H.
The stack pointer in the 8051 is only 8 bit wide, which means that it can take value of 00 to FFH
When the 8051 is powered up, the SP register contains value 07
RAM location 08 is the first location begin used for the stack by the 8051
The storing of a CPU register in the stack is called a PUSH
– SP is pointing to the last used location of the stack
– As we push data onto the stack, the SP is incremented by one
– This is different from many microprocessors
• Loading the contents of the stack back into a CPU register is called a POP
– With every pop, the top byte of the stack is copied to the register specified by
the instruction and the stack pointer is decremented once
5) DATA POINTER REGISTER(DPTR)
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)
6)RAM
Internal RAM has memory 128-byte. 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
7)ROM
Internal ROM has a memory 4KB. An internal ROM occupied addresses from 0000h to 0FFFh. PC
addresses program codes from 0000H to 0FFFH.ROM used for storing programs. Program
addresses higher than 0FFFh that exceed the internal ROM capacity will cause 8051
architecture to fetch codes bytes from external program memory.
8)TEMPORARY REGISTERS-TEMP1,TEMP2
Used to store data temporarily. They are not user accessible.
9)PROGRAM COUNTER
It is 16 bit wide. Program counter holds the address of next instruction to be executed. When
the 8051 is initialized PC always starts at 0000h and is incremented each time an instruction is
executed.
10)CONTROL REGISTERS
TMOD,TCON-Timer mode control register, Timer control register
SCON-Serial port control register
PCON-Power control register
IP-Interrupt priority register
IE-Interrupt Enable register
11)TIMER/COUNTER REGISTER
8051 microcontroller has 2-16 bit Timer/counter registers called Timer-reg-T0 And
Timer/counter Reg-T1.These registers can be accessed as their lower and upper bytes(TH0,TL0)
and (TH1,TL1).
Address range
TL0-8AH
TL1-8BH
TH0-8CH
TH1-8DH
SPECIAL FUNCTION REGISTERS
The 8051 operations that does not use internal 128 byte RAM address from 00H to 7FH done by
a specific group of special function registers called as SFR. It can be addressed like RAM using
address from 80H to FFH. Not all address from 80H to FFH are used for SFR and attempting to
use address that is not defined or empty may results in unpredictable outputs. There are 21
special function registers.
1) Timer/Counter register
8051 microcontroller has two 8-bit timer control register i.e.
TMOD and TCON register.
1.1)TMOD Register: it is 8-bit register. Its address is 89H. It is byte addressable. It used to select
mode and control operation of timer
1.2) TCON register: It is 8-bit register. Its address is 88H. It is byte addressable. Its MSB 4-
bit are used to control operation of timer/ counter and LSB 4-bit are used for external
interrupt control
2)Serial data register: 8051 micro controller has 2 serial data register viz. SBUF and SCON.
2.1Serial 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.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
3) Interrupt register: 8051 μC has 2, 8-bit interrupt register.
3.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.
3.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
4)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
5)A Register and B Register
6)DATAPOINTER
7)STACK POINTER
8)PSW
9)PORTS P0,P1,P2 AND P3: There are 4 8-bit ports: P0, P1, P2 and P3. All of them are dual
purpose ports except P1 which is only used for I/O.
PO-80H(Address in SFR bank)
P1-90H(Address in SFR bank)
10)TH1,TL1,TH0,TL0
INTERNAL AND EXTERNAL MEMORY
The 8051 has a separate memory space for code (programs) and data.
8051 Memory representation
1)EXTERNAL ROM(64 KB)
External Code Memory The executable program code is stored in this code memory. The code
memory size is limited to 64KBytes (in a standard 8051). The code memory is read-only in
normal operation and is programmed under special conditions e.g. it is a PROM or a Flash RAM
type of memory. External ROM is accessed when EA(External access ) pin is connected to
ground or when PC(pgm counter) having an address higher than the last address of internal
ROM(0FFFH).
EA=0 In this case, the microcontroller completely ignores internal program memory and
executes only the program stored in external memory.
EA=1 In this case, the microcontroller executes first the program from built-in ROM, then the
program stored in external memory
In both cases, Ports P0 and P2 are not available for use since being used for data and address
transmission
2)EXTERNAL RAM
This is read-write memory and is available for storage of data. Up to 64KBytes of
external RAM data memory is supported (in a standard 8051).The 8051 access external RAM
whenever certain program instruction are executed
3)INTERNAL ROM
Internal ROM having a capacity of 4KB. An internal ROM occupied addresses from 0000H to
0FFFH.
4)INTERNAL RAM
1) 00h to 1Fh- Register Banks
2) 20h to 2Fh -Bit Addressable RAM
3) 30 to 7Fh -General Purpose RAM
4.1) Register Banks: 00h to 1Fh
The 8051 having four register banks. These four register banks having 8 general-purpose
registers R0 to R7 (R0, R1, R2, R3, R4, R5, R6, and R7). ADD A, R2 ; adds the value contained
in R2 to the accumulator
Note since R2 happens to be memory location 02h in the Internal RAM the following
instruction has the same effect as the above instruction.
ADD A, 02h
Now, things get more complicated when we see that there are four banks of these
general-purpose registers defined within the Internal RAM. For the moment we will
consider register bank 0 only. The register banks are selected by RS0, and RS1 flag bits in PSW
register.
4.2) Bit Addressable RAM: 20h to 2Fh
The 8051 supports a special feature which allows access to bit variables. This is
where individual memory bits in Internal RAM can be set or cleared. In all there are
128 bits numbered 00h to 7Fh. Being bit variables any one variable can have a value 0
or 1. A bit variable can be set with a command such as SETB and cleared with a
command such as CLR.
Example instructions are:
SETB 25h ; sets the bit 25h (becomes 1)
CLR 25h ; clears bit 25h (becomes 0)
Note, bit 25h is actually bit b5 of Internal RAM location 24h.
The Bit Addressable area of the RAM is just 16 bytes of Internal RAM located
between 20h and 2Fh.
4.3) General Purpose RAM: 30h to 7Fh
These 80 bytes of Internal RAM memory are available for general-purpose data
storage. Access to this area of memory is fast compared to access to the main memory
However, these 80 bytes are used by the system stack and in practice little space is left for
general storage. The general purpose RAM can be accessed using direct or indirect addressing
modes.
Examples of direct addressing
MOV A, 6Ah ; reads contents of address 6Ah to accumulator
Examples for indirect addressing (use registers R0 or R1):
MOV R1, #6Ah ; move immediate 6Ah to R1
MOV A, @R1 ; move indirect: R1 contains address of Internal RAM which
contains data that is moved to A.
These two instructions have the same effect as the direct instruction above.
PORTS OF 8051
All 8051 microcontrollers have 4 I/O ports each comprising 8 bits which can be configured as
inputs or outputs. Accordingly, in total of 32 input/output pins enabling the microcontroller to be
connected to peripheral devices are available for use.
1)PORT 0:Port 0 can be used as I/O ports. Its having an address range in SFR from 80H to
87H.Port 0 can be used as address/data lines during external memory access. they occupied the
pins 32 to 39 of the processor of the chip. And they are also written as P0.0 to P0.7; the way it
we are writing it as P0.0 to P0.7 because of the fact that this ports are bit accessible; so, you can
control individual bits separately .Port 0 having an external pull up resistor.
2)PORT 1:It can be used as 8 bit bidirectional I/O port. It is given an address range in SFR
register(90H-97H).It is having an internal pull up resistor.
3)PORT 2:May be used as I/O ports similar to that of PORT 1.The other use of PORT 2 is to
supply high order byte in conjunction with PORT 0 low order byte address for external memory
access. This will occupy pins 21 to 28 and written as P2.0 to P2.7.
4)PORT 3:It can be used as I/O ports similar to PORT 1.The other functions of pins of PORT 3
are given below.
TIMERS AND COUNTERS
Timers/Counters are used generally for
1. Creating delay
2. Wave form properties measurement
3. Periodic interrupt generation
4. Waveform generation
The 8051 microcontroller has 2 timers/counters called T0 and T1. As their names suggest, their main
purpose is to measure time and count external events. Besides, they can be used for generating clock
pulses to be used in serial communication, so called Baud Rate
1) Timer T0
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
.
2) Timer 1 registers, T1
Timer1 registers is also a 16 bits register and is split into two bytes, referred to as TL1 and TH1.
Formula used to calculate values in these two registers is very simple:
TH0 × 256 + TL0 = T
Matching the previous example it would be as follows:
3 × 256 + 232 = 1000
Since the timer T0 is virtually 16-bit register, the largest value it can store is 65535. In case of
exceeding this value, the timer will be automatically cleared and counting starts from 0. This
condition is called an overflow. Two registers TMOD and TCON are closely connected to this
timer and control its operation.
3)TMOD (timer mode) Register
The TMOD register selects the operational mode of the timers T0 and T1. As seen in figure
below, the low 4 bits (bit0 – bit3) refer to the timer 0, while the high 4 bits (bit4 – bit7) refer to
the timer 1.
Bits of this register have the following function:
1. GATE1 enables and disables Timer 1 by means of a signal brought to the INT1 pin (P3.3):
1. 1 – Timer 1 operates only if the INT1 bit is set.
2. 0 – Timer 1 operates regardless of the logic state of the INT1 bit.
2. C/T1 selects pulses to be counted up by the timer/counter 1:
1. 1 – Timer counts pulses brought to the T1 pin (P3.5).
2. 0 – Timer counts pulses from internal oscillator.
3. T1M1,T1M0 These two bits select the operational mode of the Timer 1.
1.GATE0 enables and disables Timer 1 using a signal brought to the INT0 pin (P3.2):
1. 1 – Timer 0 operates only if the INT0 bit is set.
2. 0 – Timer 0 operates regardless of the logic state of the INT0 bit.
2. C/T0 selects pulses to be counted up by the timer/counter 0:
1. 1 – Timer counts pulses brought to the T0 pin (P3.4).
2. 0 – Timer counts pulses from internal oscillator.
3. T0M1, T0M0 These two bits select the oprtaional mode of the Timer 0.
4) Timer Control (TCON) Register
TCON register is also one of the registers whose bits are directly in control of timer operation.
Only 4 bits of this register are used for this purpose, while rest of them is used for interrupt
control to be discussed later.
1. TF1 bit is automatically set on the Timer 1 overflow.
2. TR1 bit enables the Timer 1.
1. 1 – Timer 1 is enabled.
2. 0 – Timer 1 is disabled.
3. TF0 bit is automatically set on the Timer 0 overflow.
4. TR0 bit enables the timer 0.
1. 1 – Timer 0 is enabled.
2. 0 – Timer 0 is disabled.
5. IE1 External interrupt 1 Edge flag. This is set by hardware when external interrupt edge is
detected.And cleared by hardware when the interrupt is processed.
6. IT1 External interrupt1 signal type control bit. Set to 1 by program to Enable external interrupt 1
to be triggered by a falling edge signal. Set To 0 by program to enable a low level signal on external
interrupt1 to generate an interrupt.
7. IE0 External interrupt 0 Edge flag. Not related to timer operations.
8. IT0 External interrupt 0 signal type control bit. Same as IT0.
TIMER MODES
1)TIMER 0 MODE 0
In this mode, the timer is used as a 13-bit UP counter as follows. In this figure Timer0 is
considered(Timer 1 is also same). In this mode Timer0 act as a 13-bit timer which consists of all 8
bits of TH0 and the lower 5 bits of TL0. As a result, the Timer 0 uses only 13 of 16 bits. Each
incoming pulses to(TH0,TL0) register causes the lower register to change their state. After
receiving 32 pulses, this register is loaded and automatically cleared, while the higher byte
(TH0) is incremented by 1.
2)TIMER 0 MODE 2
Mode 1 configures timer 0 as a 16-bit timer comprising all the bits of both registers TH0 and
TL0. That’s why this is one of the most commonly used modes. Timer operates in the same way
as in mode 0, with difference that the registers count up to 65 536 as allowable by the 16 bits
3)TIMER 0 MODE2
Mode 2 configures timer 0 as an 8-bit timer. Actually, timer 0 uses only one 8-bit register for
counting and never counts from 0, but from an arbitrary value (0-255) stored in another (TH0)
register. In this mode when the timer overflows i.e. TL0 becomes FFH, it is fed with the value stored in
TH0. For example if we load TH0 with 50H then the timer in mode 2 will count from 50H to FFH. After
that 50H is again reloaded. This mode is useful in applications like fixed time sampling.
4)Timer 0 in Mode 3 (Split Timer)
In Mode 3 registers TL0 and TH0 operate as separate 8-bit timers. In other words, the 16-bit
timer consisting of two registers TH0 and TL0 is split into two independent 8-bit timers. This
mode is provided for applications requiring an additional 8-bit timer or counter. The TL0 timer
turns into timer 0, while the TH0 timer turns into timer 1
SERIAL 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
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
• Synchronous serial data communication: In this transmitter and receiver are synchronized.
It uses a common clock to synchronize the receiver and the transmitter. This format is
generally used for high speed transmission. In Synchronous serial data communication a
block of data is transmitted at a time.
• 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
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 contents of the Serial Control (SCON) register are shown below. This
register contains mode selection bits, serial port interrupt bit (TI and RI) and also the ninth data bit
for transmission and reception (TB8 and RB8).
Address: 098H (Bit addressable)
3. PCON register: The SMOD bit (bit 7) of PCON register controls the baud rate in asynchronous
mode transmission. The seventh bit (b7)(SMOD) is used to generate the baud rate of serial
communication.
Address: 87H
BASICS OF INTERRUPTS
During program execution if peripheral devices needs service from microcontroller, device will
generate interrupt and gets the service from microcontroller. When peripheral device activate
the interrupt signal, the processor branches to a program called interrupt service routine. After
executing the interrupt service routine the processor returns to the main program .
Steps taken by processor while processing an interrupt:
1. It completes the execution of the current instruction.
2. PSW is pushed to stack.
3. PC content is pushed to stack.
4. Interrupt flag is reset.
5. PC is loaded with ISR address.
ISR(Interrupt service routine) will always ends with RETI instruction. The execution of
RETI instruction results in the following.
1. POP the current stack top to the PC.
2. POP the current stack top to PSW.
Classification of interrupts.
1. External and internal interrupts.
External interrupts are those initiated by peripheral devices through the external pins of the
microcontroller.
Internal interrupts are those activated by the internal peripherals of the microcontroller like
timers, serial controller etc.)
2. Maskable and non-maskable interrupts.
The category of interrupts which can be disabled by the processor using program is called
maskable interrupts.
Non-maskable interrupts are those category by which the programmer cannot disable it using
program.
3. Vectored and non-vectored interrupt.
Starting address of the ISR is called interrupt vector. In vectored interrupts the starting address is
predefined. In non-vectored interrupts, the starting address is provided by the peripheral as
follows.
• Microcontroller receives an interrupt request from external device.
• Controller sends an acknowledgement (INTA) after completing the execution of
current instruction.
• The peripheral device sends the interrupt vector to the microcontroller.
•
8051 INTERRUPT STRUCTURE.
8051 has five interrupts. They are maskable and vectored interrupts. Out of these five, two are
external interrupt and three are internal interrupts
8051 makes use of two registers to deal with interrupts
1. IE Register
This is an 8 bit register used for enabling or disabling the interrupts. The structure of IE register is
shown below. It is bit addressable
2. IP Register.
This is an 8 bit register used for setting the priority of the interrupts