Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
8051 MICROCONTROLLER
A hand book of basic embedded system
Bivash mondal
2013
MICROCONTROLLER 8051 Page |2
A general purpose microprocessor contains no RAM, no ROM, and no I/O ports on the chip itself. A
system designed using a general purpose microprocessor must add RAM, ROM, I/O ports and timers externally
to make them functional, which makes these systems bulkier and much more expensive. But in microcontroller
there is a CPU in addition to a fixed amount of RAM, ROM, I/O ports, and timer on one chip. It makes
microcontroller ideal for many applications in which cost and space are important.
Microprocessors are used for versatile purpose, where as microcontrollers are used for dedicated job
only. So, microcontroller is an embedded system containing RAM, ROM, I/O ports, and timer.
Applications of microcontroller
Telephones, TVs, remote controls, video games, camera, calculators, microwaves, FAX machines etc.
Microcontroller ICs
There are different family of microcontroller. Here we read only 8051 family. Instructions are common
for all the family members and they are following the common architecture. They will differ in some interfacing
devices only. Various microcontrollers of 8051 family are 8051, 8052, 8031 etc.
EXTERNAL INTERRUPTS
8 BIT
PROCESSOR
CPU
P0 P1 P2 P3 TX RX
PORT 0 (PIN 39-32): 8 bit bi-directional and bit addressable I/O port. Lower order address and data bus (AD0
–AD7).
PORT 1 (PIN 1-8): 8 bit quasi-bi-directional and bit addressable I/O port. Quasi-bi-directional port due to the
fact that port 1 pins are internally pulled high with fixed pull up resister. On reset, port 1 is configured as an
input port.
PORT 2 (PIN 21-28): 8 bit quasi-bi-directional and bit addressable I/O port. Port 2 is multiplexed with higher
order address bus (A8-A15).
PORT 3 (PIN 10-17): 8 bit quasi-bi-directional and bit addressable I/O port. Pins are pulled high internally
using pull up resister. Port3 has the additional function of providing some extremely important signals such as
interrupts.
RST (PIN 9): For resetting the device the reset pin of 8051 is made high for at least two machine cycles (24
period of oscillation). Generally we run the microcontroller in power on reset (POR) mode.
VCC
40
10µF
8.2kΏ
MICROCONTROLLER 8051 Page |5
A 10µF capacitor and 8.2kΏ resistance is sufficient to provide high state for two machine cycles. We can also
manually reset the device by providing high state at the reset pin.
XTAL2 and XTAL1 (PIN 18, 19): In 8051 family we use an external crystal for oscillator circuit. The crystal
is connected between the pins XTAL1 and XTAL2. The frequency of oscillation of the crystal is 11.0592 MHz
two capacitors of 30 pF are connected to the crystal to trigger the crystal for starting oscillation.
30 pF 8051
18
19
20
ALE (PIN 30): Address latch enable is an active high pin use to access the external data memory and program
memory.
EA (PIN 31): External access pin is an active low pin. It also uses to access the external program memory.
When we access the internal program memory the pin is connected to the VCC. The pin must be connected to
ground when access the external program memory.
PSEN (PIN 29): The program strobe enable pin is active low. It is responsible for the physical separation of
program memory and data memory. When the pin is high then we can access the 64kB data memory and when
it is low then we can access the 64kB program memory.
MICROCONTROLLER 8051 Page |6
There are two separate program and data memories. The program codes are typically stored at
ROM/EPROM. Since the address bus is 16 bit the maximum ROM could be 64 KB (216). But there are only
4kB on chip ROM in 8051. In data memory we can store data. Internal data memory available in 8051 is 128B
only. Whereas the RAM could be of 64kB+128B.
IC ROM RAM
Internal memory
Internal ROM: The 8051 microcontroller has only 4 KB on chip ROM. The program is stored in ROM, that’s
why it is called program memory. But if the program size exceeds 4KB memory, then external memory is
needed.
Internal RAM: Internal RAM address is 8 bit wide. Hence we can have maximum 256 bytes memory. But out
of which 128 bytes internal RAM. For 8051 µc we have 128 internal RAM locations (00 to 7F). These internal
RAM addresses can be access by both direct and indirect addressing.
The upper portions 80 to FF are not provided for MCS 51 family. Some µc of MCS 52 family may have used
these upper portions by indirect addressing. In both cases of 8051 and 8052 family the address of SFR (special
function resisters) area is 80 to FF. But in case of 8052 the upper half of RAM also used by indirect addressing.
But SFR area always access by direct addressing mode. Hence by different addressing mode the upper portion
of memory and SFR area can be distinguished.
FF
80
7F
ACCESS BY BOTH
DIRECT AND INDIRECT
ADDRESSING MODE
00
MICROCONTROLLER 8051 Page |7
Details of internal RAM (00h to 7Fh): Internal RAM consists of three parts;
Resister bank: 00h to1Fh these 32 bytes memory is divided into four resister bank and each resister bank has
eight resisters (R0 to R7).
By default the resisters of bank 0 are selected. Other resister banks are can be selected by the help of program
status word (PSW) resister. The R0 and R1 resisters of bank 0 can be used as memory pointer.
Bit addressable area: the address location 20h to2Fh (16 byte) each of eight bit long and each bit location have
individual addresses, i.e., they are bit addressable.
20 00 01 02 03 04 05 06 07
21 08 09 0A 0B 0C 0D 0E 0F
22 10 11 12 13 14 15 16 17
2F 78 79 7A 7B 7C 7D 7E 7F
These bits are individually accessible as well as these 16 bytes memory of RAM (20h to 2Fh) also be address by
byte. This area generally use extensively for Boolean processing, e.g. These can be used as flags.
Instructions: SET B 09h ; 09h bit address is set to be 1
Scratch pad: 30h to 7Fh these 80 bytes data memory are byte addressable and used for storage data. It is also
known as user area. This user area is byte addressable.
7F
OR
USER AREA
30
20 27
18 19 1A 1B 1C 1D 1E 1F BANK 3
10 11 12 13 14 15 16 17 BANK 2
08 09 0A 0B 0C 0D 0E 0F BANK 1
00 01 02 03 04 05 06 07 BANK 0
MICROCONTROLLER 8051 Page |9
SFR (special function resister) area: 128 bytes memory reserved for SFR area. The address locations of SFR
area are 80h to FFh. These special function resisters are can be access by direct addressing mode only. These
special function resisters are used for control operations and show the status of various operations done by µc.
All special function resisters are directly addressable and can be read and written. Some special function
resisters (only resisters in 1st column) are bit addressable.
F8 FF
F0 B F7
E8 EF
E0 A E7
D8 DF
D0 PSW D7
C8 CF
C0 C7
B8 IP BF
B0 P3 B7
A8 IE AF
A0 P2 A7
98 SCON SBUF 9F
90 P1 97
SFR description
B (resister) F0h Bit addressable Use as a normal resister and special use for
multiplication and division operation.
A (accumulator) E0h Bit addressable Similar to µp Acc. is used for any arithmetic and
logic operations.
IP (interrupt priority ) B8h Bit addressable Using this resister we can change the priority
level of interrupt.
IE (interrupt enable) A8h Bit addressable Using this resister we can enable or disable any
interrupt.
SCON (serial control) 98h Bit addressable Used in asynchronous serial communication.
SBUF (serial buffer) 99h Byte addressable Used in asynchronous serial communication.
TMOD (timer mode) 89h Byte addressable Timer/ counter mode selection.
TL0/TH0 (T/C 0 lower 8Ah,8Ch Byte addressable Timer 0 lower and higher byte.
and higher byte)
TL1/TH1 (T/C 1 lower 8Bh,8Dh Byte addressable Timer 0 lower and higher byte.
and higher byte)
SP (stack pointer) 81h Byte addressable Stack pointer store the return address for call
instruction.
DPTR (data pointer) 82h,83h Byte addressable The only 16 bit resister in microcontroller. It can
be use d as a pointer of 16 bit memory address.
DPL (data pointer low)
PCON (power control) 87h Byte addressable Used for power control in µc.
MICROCONTROLLER 8051 P a g e | 11
D7 D6 D5 D4 D3 D2 D1 D0
C AC F0 RS1 RS0 OV - P
C: Carry flag
AC: Auxiliary carry
F0: General purpose flag. User can set or reset it. It is not affected by operation.
RS1, RS0: These two bits are used for resister bank selection.
OV: Over flow flag. This flag is used for signed operation. It is used to detect the error in sign operation.
P: Parity bit. P =1 for odd parity and P=0 for even parity.
Bank selection
RS1 RS0 Bank
0 0 Bank 0
0 1 Bank 1
1 0 Bank 2
1 1 Bank 3
On reset the content of PSW is 00h. So both RS0, RS1 are zero and by default bank 0 is selected on reset.
D7 D6 D5 D4 D3 D2 D1 D0
SMOD: BAUD rate double bit. Speed of data transfer in serial communication is defined by BAUD rate. The
unit of BAUD rate is bps (bits per second).
PD: Power down mode. If PD=1, then the µc will be in power down mode. In power down mode CPU and the
peripherals will be in inactive status. So in this mode least amount of power consumed.
IDL: Idle mode of operation. If IDL=1, then the µc will be in idle mode. In idle mode CPU will be in inactive
status but peripherals will be in active status. Since CPU is idle it will consume less power. From idle mode
CPU will be active by interrupt.
MICROCONTROLLER 8051 P a g e | 12
There are two timers/ counters in microcontroller 8051, timer0 and timer1. Each timer is of 16 bit. Both
timers and counters count the clock pulses in digital system. Now the question coming to everyone mind that,
what is the different between timer and counter? If the source of clock pulse is internal to the µc then the mode
of counting is called timer. If the source of clock pulse is external to the µc then the mode of counting is called
counter. There are six special function resisters (SFR’s) used in timer and counter mode of operation.
D7 D6 D5 D4 D3 D2 D1 D0
GATE: Gate control. If GATE =1, T/C x will run when INT x and TR x is high. If GATE =0, T/C x will run
when TR x bit of TCON resister is high.
C / T: Timer/ counter selector. If the bit is 1 then counter mode of operation is done and if the bit is 0 then timer
mode of operation is done.
M1 M0 Mode Operation
D7 D6 D5 D4 D3 D2 D1 D0
TF X: Timer X (=0/1) overflow flag bit. This bit is set by hardware when timer x overflows and clear by
hardware when processor vectors the interrupt service routine.
TR X: Timer X run control bit. This bit can be set by software to run the timer x and clear by software to stop
the timer x.
MICROCONTROLLER 8051 P a g e | 13
IE X: External interrupt edge detected flag. Set by CPU when the external interrupt edge (high to low
transition) is detected and cleared by CPU when the interrupt sub routine is processed.
IT X: External interrupt type control bit. This bit can be set or clear by software. When IT x =1, then IE x is set
at the falling edge of INT x and when IT x =0, then IE x is set at the low level of INT x.
Xtal
÷ 12 C/T= 0 (timer mode)
frequency
TL 0/1 TH 0/1
TF 0/1
TR 0/1
GATE
INT 0/1
When C/ T = 0 then it operates in timer mode and then it counts the internal clocks. Then the frequency of
internal clock (frequency of machine cycle) = (crystal frequency ÷12).
f = 12MHz
f = 1 MHz
M/C
When C/ T = 1 then it operates in counter mode and then it counts the external clocks in T0/1 pin. When GATE
= 0, then the output of OR gate is always 1 and then timer x run only when TR x bit is set. When gate =1, then
timer x will run when both TR x bit and INT x are set.
MICROCONTROLLER 8051 P a g e | 14
Timer modes
Mode 0: Mode 0 is also known as 13 bit timer mode. In this mode lower 5 bit of TLX (D 0 to D4) and THX is
used. THX will be increased after each and every 32 bit of count. This 32 is known as pre-scale value of 13 bit
timer mode.
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
× × ×
THX TLX
TRx
Mode 1: Mode 1 is also known as 16 bit timer mode. In this mode all 8 bits of TLX and THX are used. For
each and every 256 no of clock pulses THX will be increase by 1. The timer flag bit TFX is set with the next
clock after count FFFFh. So 256 is the pre-scale value of 16 bit timer mode.
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
THX TLX
By default on reset the content of TL and TH resister is 00h. Then the timer counts from 0000h to FFFFh and
over flow with the next clock and timer flag (TF) is set. Here the count value is maximum 216. We can count
any desire value by loading offset value in TL and TH resister. This is used to produce a fixed delay.
–
MICROCONTROLLER 8051 P a g e | 15
Problem: Find out the content of resisters TL0 and TH0 for producing 1ms delay using the timer0 in 16 bit
counter mode. Given that the frequency of the crystal is 12 MHz.
Mode 2: Mode 2 is also known as 8 bit auto reload mode. When a timer is in mode 2 TH x holds the offset
value and TL x is the timer itself. TL x starts counting from just after the offset value. When reaches to FFh and
with next clock timer flag (TF x) bit is set and TL x reload with the offset value from TH x, instead off resetting
to 00h.
Xtal
÷ 12
frequency TL x TF x
TR x Reload
TH x
–
Delay
Problem: We want a delay of 100 µs by using mode 2 and crystal of frequency 12 MHz, what will be the offset value?
–
Delay =
100×10 -6 =
Mode 3: This mode is also known as mix mode or 8 bit split timer mode. When timer0 is placed in mode 3, it
becomes two separate 8 bit timers. Then TL0 is timer 0and TH0 is timer 1. When TL0 is over flow, then TF0 is
set and when TH0 over flow then TF1 is set.
Interrupts
We already discuss that 8051 µc has 6 interrupt sources with two priority levels. These interrupt sources are;
For every interrupt, there must be an interrupt service routine (ISR). When a interrupt is invoked, the µc run the
interrupt service routine. There are fixed locations which hold the addresses of ISR. These locations are known
as vector location. There are 6 interrupts, but 5 interrupt service routines due to common ISR for serial
interrupts.
IE (interrupt enable)
D7 D6 D5 D4 D3 D2 D1 D0
EA: When EA= 0, disable all interrupts, no interrupt is acknowledged. When EA = 1, each interrupt is
individually enable/disable by setting or clearing the enable bit.
ES: Serial communication interrupt enable bit. When ES = 1, serial interrupt enable only when EA = 1.
MICROCONTROLLER 8051 P a g e | 17
IP (interrupt priority)
When the 8051 is power up, then the default priority is set.
We can change the priority level with the help of the resister IP.
D7 D6 D5 D4 D3 D2 D1 D0
Serial communication
Serial communication uses a single data line instead of the 8 bit data line of parallel combination. For
serial data communication to work the byte of data must be converted to serial bits using a parallel in serial out
shift resister; then it can be transmitted over a single data line. This also means that in receiving end there must
be a serial in parallel out shift resister to receive the serial data and peak them into a byte.
Serial data communication uses two methods, asynchronous and synchronous communication. The
8051 µc chip has a built in full duplex UART (universal asynchronous receiver transmitter). The 8051 has two
pins TxD and RxD for transferring and receiving data serially.
Simplex:
Transmitter Receiver
Half-duplex:
Transmitter Transmitter
Receiver Receiver
Full-duplex:
Transmitter Receiver
Receiver Transmitter
Data framing: In asynchronous serial communication transfer a single byte at a time, while a block of data
transfer in synchronous mode.
In the asynchronous method, each character is placed between start and stop bits. This is called framing. The
data frame may be 9/10/11 bits long. But here our point of interest on 10 bits frame. The data, such as ASCII
characters, are packed between a start bit (always 0) and a stop bit (always 1).For example, the frame of ASCII
code of ‘E’ is:
ASCII
stop start
0 1 0 0 0 1 0 1
bit bit
MICROCONTROLLER 8051 P a g e | 19
D7 D6 D5 D4 D3 D2 D1 D0
TI: transmit interrupt flag. Set by hardware at the beginning of the stop bit in mode 1.It must be cleared by
software. When TI is high it indicates that it is ready to transfer another byte.
RI: Receive interrupt flag. Set by hardware halfway through the stop bit time in mode 1. It must be cleared by
software. When RI bit is high it indicates that a byte has been received and picked up before it is lost.
0 1 Mode 1, 10 bits frame (8 bit data, 1start and 1stop), with variable BAUD rate using
timer 1 overflow rate in 8 bit auto reload mode.
1 1 Mode 3, 9bit UART with variable BAUD rate using timer 1 overflow rate in 8 bit
auto reload mode.
SBUF is an 8 bit resister used for serial communication in the 8051. For a byte of data to be transferred
via the TxD line, it must be placed in the SBUF resister and it also holds the data when it is received by RxD
line.
BAUD rate
MICROCONTROLLER 8051 P a g e | 20
Instruction set:
2) direct
3) immediate
4) indirect
MICROCONTROLLER 8051 P a g e | 21
EDITOR
PROGRAM
Myfile.asm
ASSEMBLER
PROGRAM
Myfile.1st
Myfile.obj
Myfile.obj Other obj files
LINKER
PROGRAM
Myfile.abs
OH
PROGRAM
Myfile.hex
This Hex file burn in ROM to load the program in microcontroller IC.