8051 Microcontroller Overview and Features
8051 Microcontroller Overview and Features
8051MICROCONTROLLER
Overview of 8051 microcontroller, Architecture, I/O ports, Memory organization, addressing
modes and instruction set of 8051, Simple programs.
MICROCONTROLLERS VS MICROPROCESSORS
MICROPROCESSOR:
A CPU built into a single VLSI chip is called a microprocessor.
It is a general-purpose device and additional external circuitry is added to
make it a microcomputer.
The microprocessor contains arithmetic and logic unit (ALU), Instruction
decoder and control unit, Instruction register, Program counter (PC), clock
circuit (internal or external), reset circuit (internal or external) and registers.
But the microprocessor has no on chip I/O Ports, Timers, Memory etc.
For example, Intel 8085 is an 8-bit microprocessor and Intel 8086/8088 a 16-
bit microprocessor.
The block diagram of the Microprocessor is shown in Fig.1
MICROCONTROLLER:
A microcontroller is a highly integrated single chip, which consists of on chip
CPU (Central Processing Unit), RAM (Random Access Memory),
EPROM/PROM/ROM (Erasable Programmable Read Only Memory), I/O
(input/output) – serial and parallel, timers, interrupt controller.
For example, Intel 8051is 8-bit microcontroller and Intel 8096 is16-bit
microcontroller.
The block diagram of Microcontroller is shown in Fig. 2.
Page1
DISTINGUISH BETWEEN MICROPROCESSOR AND MICROCONTROLLER:
Page2
One16-bit program counter and One16-bit DPTR(data pointer)
One8-bit stack pointer
One Microsecond instruction cycle with12 MHz Crystal.
One full duplex serial communication port.
Page3
PIN DIAGRAM OF 8051:
The 8051 microcontroller is available as a 40 pin DIP chip and it works at+5
volts DC.
VCC→5Vsupply
VSS→GND
XTAL2/XTALI are for oscillator input
Port 0–32to39–AD0/AD7andP0.0toP0.7
Port1–1to 8– P1.0 to P1.7
Port2–21to28–P2.0 to P2.7and A 8to A15
Port3–10 to 17–P3.0 to P3.7
P3.0–RXD–Serial data input –SBUF
P3.1–TXD–Serial data output–SBUF
P3.2–INT0–External interrupt 0–TCON0.1
P3.3–INT1–External interrupt1 –TCON0.3
P3.4–T0–External timer 0 input–TMOD
Page4
Fig 4 Crystal Oscillator as Source Fig 5ExternalClockSource
P0.0-P0.7(AD0-AD7):
o The port 0 pins multiplexed with Address/data pins.
o If the microcontroller is accessing external memory these pins will act
as address/data pins otherwise they are used for Port 0 pins.
P2.0-P2.7(A8-A15):
o The port 2pins are multiplexed with the higher order address pins.
o When the microcontroller is accessing external memory these pins
provide the higher order address byte otherwise they act as Port 2
pins.
P1.0-P1.7:
o These 8-pins are dedicated for Port1 to perform input or output port
operations.
P3.0-P3.7:
o These8-pins are meant for Port3 operations and also for some control
operations like Read, Write, Timer0, Timer1, INT0, INT1, RxD and TxD
RST:
o The RESET pin is an input pin and it is an active high pin.
o When a high pulse is applied to this pin the microcontroller will reset
and terminate all activities.
o Upon reset all the registers except PC will reset to 0000 Value and PC
̅𝑬̅̅ 𝑨̅
register will reset to 0007 value.
(External Access): Active Low Pin
o This pin is connected to ground when microcontroller is accessing the
program code stored in the external memory and connected to Vcc
when it is accessing the program code in the on chip memory.
Page5
o When the pin is High, the AD bus will act as address bus otherwise the
AD bus will act as Data bus.
Page6
8051 ARCHITECTURE
Page7
Mostly used blocks in the architecture of 8051 are as follows:
ALU
o Arithmetic Logical Unit
o This unit is used for the arithmetic calculations.
A-Accumulator
o This register is used for arithmetic operations.
o This is also bit addressable and 8bit register.
B-Register
o This register is used in only two instructions MUL A Band DIV AB.
o This is also bit addressable and 8bit register
PC-Program Counter
o Points to the address of next instruction to be executed from ROM
o It is 16 bit register means the 8051 can access program address from
0000H to FFFFH. A total of 64KB of code.
o InitiallyPChas0000H
o ORG instruction is used to initialize the PC
ORG0000H means PC initialize by 0000H
o PC is incremented after each instruction. ROM
PSW Register (8051Flags)
o Used to indicate the Arithmetic condition of Accumulator.
o Flag register in 8051 is called as program status word (PSW).
o This special function register PSW is also bit addressable and 8 bit wide
means each bit can be set or reset independently.
Page8
o OV(over flow flag) →PSW0.2
This is used to detect error in signed arithmetic operation.
This is similar to carry flag but difference is only that carry flag is
used for unsigned operation.
o The selection of the register Banks and their addresses are given below.
Page9
instructions (SETB for high, CLR for low) independently.
o The data at any port which is transmitting or receiving is in these
registers.
o The port 0 can perform dual works. It is also used as Lower order
address bus (A0 to A7) multiplexed with 8 bit data bus P0.0 to P0.7 is
AD0 to AD7respectively the address bus and data bus is demultiplexed
by the ALE signal
o Port2 can be used as I/O port as well as higher order address bus A8 to
A15.
o Port 3 also have dual functions it can be worked as I/O as well as each
pin of P3 has specific function.
o P3.0–RXD–{Serial I/P for Asynchronous communication Serial O/P for
synchronous communication}.
o P3.1–TXD–Serial data transmit.
o P3.2–INT0–External Interrupt 0.
o P3.3–INT1–External Interrupt1.
o P3.4–T0–Clock input for counter 0.
o P3.5–T1–Clock input for counter 1.
o P3.6–WR–Signal for writing to external memory.
o P3.7–RD–Signal for reading from external memory.
o When external memory is interfaced with 8051 then P0 and P2 can’t be
worked as I/O port they works as address bus and data bus, otherwise
they can be accessed as I/O ports.
Oscillator
o It is used for providing the clock to MC8051 which decides the speed or
baud rate of MC.
o We use crystal which frequency varies from 4MHz to 30 MHz, normally
we use11.0592 MHz frequency.
Interrupts
o Interrupts are defined as requests because they can be refused
(masked) if they are not used, that is when an interrupt is
acknowledged.
o A few events or routines are followed to handle the interrupts.
o These special routines are known as interrupt handler or interrupt
service routines (ISR).
o These are located at a special allocation in memory.
o INT0andINT1arethepinsforexternalinterrupts.
Timers and Counters
o Timer means which can give the delay of particular time between
some events. For example on or off the lights after every 2 sec.
o This delay can be provided through some assembly program but
in microcontroller two hardware pins are available for delay
generation.
o These hardware pins can be also used for counting some external
events.
o How much time a number is repeated in the given table is calculated
Page10
by the counter?
In MC8051, two timer pins are available T0 and T1, by these
timers we can give the delay of particular time if we use these in
timer mode.
We can count external pulses at these pins if we use these pins
in counter mode.
16 bits timers are available. Means we can generate delay
between 0000H to FFFFH.
Two special function registers are available.
o If we want to load T0 with 16 bit data then we can load separate lower
8bitinTL0 and higher 8 bit in TH0.
o In the same way forT1.
o TMOD, TCON registers are used for controlling timer operation.
Serial Port
o There are two pins available for serial communication TXD and RXD
o Normally TXD is used for transmitting serial data which is in SBUF
register, RXD is used for receiving the serial data.
o SCON register is used for controlling the operation.
Memory organization:
o The 8051 microcontroller has128 bytes of Internal RAM and 4kB of on
chip ROM.
o The RAM is also known as Data memory and the ROM is known as
program memory.
o The program memory is also known as Code memory.
o This Code memory holds the actual 8051 program that is to be executed.
o In 8051 this memory is limited to 64K.
o Code memory may be found on-chip, as ROM or EPROM. It may also be
stored completely off-chip in an external ROM or, more commonly, an
external EPROM.
o The 8051 has only 128 bytes of Internal RAM but it supports 64kB of
external [Link] the name suggests; external RAM is any random
access memory which is off-chip.
o Since the memory is off-chip it is not as flexible in terms of accessing,
and is also slower.
o For example, to increment an Internal RAM location by 1, it requires
only 1 instruction and 1 instruction cycle but to increment a 1-byte
value stored in External RAM requires 4 instructions and 7 instruction
cycles.
o So, here the external memory is 7 times slower
Internal RAM OF 8051:
Page11
o This Internal RAM is found on-chip on the 8051.
o So it is the fastest RAM available, and it is also the most flexible in
terms of reading, writing, and modifying it’s contents.
o Internal RAM is volatile, so when the 8051is reset this memory is cleared.
o The128 bytes of internal
Four register banks (Bank0, Bank1, Bank2 and Bank3) each of 8-
bits (total 32 bytes). The default bank register is Bank 0. The
remaining Banks are selected with the help of RS0 and RS1 bits
of PSW Register.
16 bytes of bit addressable area and
80 bytes of general purpose area (Scratch pad memory) as
shown in the figure 8. This area is also utilized by the
microcontroller as a storage area for the operating stack.
o The 32 bytes of RAM from address 00 H to 1FH are used as working
registers organized as four banks of eight registers each.
o The registers are named as R0-R7 .Each register can be addressed by
its name or by its RAM address.
Page12
o In 8051,4KB read only memory(ROM) is available for program storage.
o This is used for permanent data storage. Or the data which is not
changed during the processing like the program or algorithm for
specific applications.
This is volatile memory; the data saved in this memory does not
disappear after power failure.
We can interface up to 64KB ROM memory externally if the
application is large.
o These sizes are specified different by their companies.
o Address Range of PC: Address range of PC means program counter
(which points the next instruction to be executing) can be moved
between these locations or we can save the program from this location
to this location. The address range can be calculated in the same way
just like the RAM which is discussed in previous section
Page13
Table 4: SFRs of 8051 Microcontroller
[Link] Symbol Name of SFR Address(Hex)
1 ACC* Accumulator E0
2 B* B-Register F0
3 PSW* Program Status word register DO
4 SP Stack Pointer Register 81
DPL Data pointer low byte 82
5 DPTR
DPH Data pointer high byte 83
6 P0* Port0 80
7 P1* Port1 90
8 P2* Port2 0A
9 P3* Port3 0B
10 IP* Interrupt Priority control B8
11 IE* Interrupt Enable control A8
12 TMOD Timer mode register 89
13 TCON* Timer control register 88
14 TH0 Timer0Higherbyte 8C
15 TL0 Timer0Lowerbyte 8A
16 TH1 Timer1Higher byte 8D
17 TL1 Timer1lowerbyte 8B
18 SCON* Serial control register 98
19 SBUF Serial buffer register 99
20 PCON Power control register 87
The*indicates the bit addressable SFRs
Page14
ADD R1, #45 H: Add the constant 45 to the contents of the
accumulator MOV DPTR, # 8245H: Move the data 8245 into the data
pointer register.
MOV P1, #21H: Move the data 21 to PORT1
Direct addressing mode:
o The addressing mode in which the data operand is in the RAM location
(00 -7FH) and the address of the data operand is given in the
instruction is known as Direct addressing mode. The direct addressing
mode uses the lower 128 bytes of Internal RAM and the SFRs
o Ex
MOV R1, 42H: Move the contents of RAM location 42 into R1register
MOV 49H, A: Move the contents of the accumulator into the RAM location
49.
ADD A, 56H: Add the contents of the RAM location 56 to the accumulator
Register addressing mode:
o The addressing mode in which the data operand to be manipulated in
one of the registers is known as register addressing mode.
o Ex
MOV A,R0: Move the contents of the register R0 to the
accumulator ADD A, R6: Add the contents of R6 register to
the accumulator MOV P1, R2: Move the contents of the R2
register into port 1
MOV R5, R2: This is invalid .The data transfer between the registers is not
allowed.
Register indirect addressing mode:
o The addressing mode in which a register is used as a pointer to the
data memory block is known as Register indirect addressing mode.
o Ex
MOV A,@R0: Move the contents of RAM location whose address is in R0
into A (accumulator)
MOV @ R1, B: Move the contents of B into RAM location whose address
is held by R1 When R0 and R1 are used as pointers, they must be
preceded by @ sign
o One of the advantages of register indirect addressing mode is that it makes
accessing the data more dynamic than static as in the case of direct
addressing mode.
Indexed addressing mode:
o This addressing mode is used in accessing the data elements of look
up table entries located in program ROM space of 8051.
o Ex: MOVC A,@ A+DPTR
The 16-bit register DPTR and register A are used to form the address of
the data element stored in on-chip ROM. Here C denotes code. In this
instruction the contents of A are added to the 16-bit DPTR register to
form the 16-bit address of the data operand
INSTRUCTION SETS:
The instructions of 8051 can be broadly classified under the following headings.
Page15
1. Data transfer Instructions
2. Arithmetic Instructions
3. Logical Instructions
4. Program Branching Instructions
5. Bit Manipulation Instructions/Boolean Variable Manipulation Instructions
Arithmetic Operations
o ADD A ,RnAdd register to Accumulator
o ADD A, directAdd direct byte to Accumulator
o ADD A, @RiAdd indirect RAM to Accumulator
o ADD A, #dataAdd immediate data to Accumulator
o ADDC A, RnAdd register to Accumulator with Carry
o ADDC A, directAdd direct byte to Accumulator with Carry
o ADDC A, @RiAdd indirect RAM to Accumulator with Carry
o ADDC A, #dataAdd immediate data to Acc with Carry
o SUBB A, RnSubtract Register from Acc with borrow
o SUBB A, directSubtract direct byte from Acc with borrow
o SUBB A, @RiSubtract indirect RAM from ACC with borrow
o SUBB A, #dataSubtract immediate data from Acc with borrow
o INC AIncrement Accumulator
o INC RnIncrement register
o INC directIncrement direct byte
o INC @RiIncrement direct RAM
o DEC ADecrement Accumulator
o DEC RnDecrement Register
o DEC directDecrement direct byte
o DEC @RiDecrement indirect RAM
o INC DPTRIncrement Data Pointer
o MUL A BMultiply A&B
o DIV A BDivide A by B
o DA ADecimal Adjust Accumulator
LOGICAL OPERATIONS
o ANL A, Rn AND Register to Accumulator
o ANL A, directAND direct byte to Accumulator
o ANL A, @RiAND indirect RAM to Accumulator
o ANL A, #dataAND immediate data to Accumulator
o ANL direct, AAND Accumulator to direct byte
o ANL direct, #dataAND immediate data to direct byte
o ORL A, RnOR register to Accumulator
o ORL A, directOR direct byte to Accumulator
o ORL A,@RiOR indirect RAM to Accumulator
o ORL A,#dataOR immediate data to Accumulator
o ORL direct ,AOR Accumulator to direct byte
o ORL direct, #dataOR immediate data to direct byte
o XRL A, RnExclusive - OR register to Accumulator
o XRL A, directExclusive-OR direct byte to Accumulator
Page16
o XRL A, @RiExclusive-OR indirect t RAM to Accumulator
o XRL A, #dataExclusive-OR immediate data to Accumulator
o XRL direct, AExclusive-OR Accumulator to direct byte
o XRL direct, #dataExclusive-OR immediate data to direct byte
o CLR AClear Accumulator
o CPL AComplement Accumulator
o RL A Rotate Accumulator Left
o RLC A Rotate Accumulator Left through the Carry
o RR A Rotate Accumulator Right
o RRC A Rotate Accumulator Right through the Carry
o SWAP ASwap nibbles within the Accumulator
DATATRANSFER
o MOV A, RnMove register to Accumulator
o MOV A, directMove direct byte to Accumulator
o MOV A, @RiMove indirect RAM to Accumulator
o MOV A, #dataMove immediate data to Accumulator
o MOV Rn, AMove Accumulator to register
o MOV Rn, directMove direct byte to register
o MOV Rn, #dataMove immediate data to register
o MOV direct, AMove Accumulator to direct byte
o MOV direct, RnMove register to direct byte
o MOV direct, directMove direct byte to direct
o MOV direct, @RiMove indirect RAM to direct byte
o MOV direct, #dataMove immediate data to direct byte
o MOV@ Ri, AMove Accumulator to indirect RAM
o MOV @Ri, directMove direct byte to indirect RAM
o MOV @ Ri ,#dataMove immediate data to indirect RAM
o MOV DPTR, #data16Load Data Pointer with a16-bit constant
o MOVC A, @A+DPTRMove Code byte relative to DPTR to Acc
o MOVC A,@A+PCMove Code byte relative to PC to Acc
o MOVX A, @RiMove External RAM(8-bitaddr) to Acc
o MOVX A,@DPTRMove Exernal RAM(16-bitaddr) to Acc
o MOVX @ Ri, AMove Acc to External RAM(8-bitaddr)
o MOVX @DPTR,AMove Acc to External RAM(16-bit addr)
o PUSH directPush direct byte on to stack
o POP direct Pop direct byte from stack
o XCH A, RnExchange register with Accumulator
o XCH A, directExchange direct byte with Accumulator
o XCH A, @RiExchange indirect RAM with Accumulator
o XCHD A, @RiExchange low-order Digit indirect RAM with Acc
Page17
o CPL CComplement Carry
o CPL bitComplement direct bit
o ANL C ,bit AND direct bit to CARRY
o ANL C,/bitAND complement of direct bit to Carry
o ORL C, bitOR direct bit to Carry
o ORL C,/bitOR complement of direct bit to Carry
o MOV C, bitMove direct bit to Carry
o MOV bit, CMove Carry to direct bit
o JC relJump if Carry is set
o JNC relJump if Carry not set
o JB bit, relJump if direct Bit is set
o JNB bit , relJump if direct Bit is Not set
o JBC bit, relJump if direct Bit is set& clear bit
PROGRAM BRANCHING
o ACALL addr11Absolute Subroutine Call
o LCALL addr16Long Subroutine Call
o RETReturn from Subroutine
o RETIReturn from interrupt
o AJMP addr11Absolute Jump
o LJMP addr16LongJump
o SJMP relShort Jump(relative addr)
o JMP @A+DPTRJump indirect relative to the DPTR
o JZ relJump if Accumulator is Zero
o JNZ relJump if Accumulator is Not Zero
o CJNE A, direct, relCompare direct byte to Acc and Jump if Not Equal
o CJNE A, #data, relCompare immediate to Acc and Jump if Not Equal
o CJNE Rn, #data, relCompare immediate to register and Jump if Not
Equal
o CJNE @Ri ,#data ,relCompare immediate to indirect and Jump if Not
Equal
o DJNZ Rn, relDecrement register and Jump if Not Zero
o DJNZ direct, relDecrement direct byte and Jump if Not Zero
o NOPNo Operation
PROGRAMS:
PROGRAM FOR 8-BIT ADDITION USING 8051 MICROCONTROLLER
Page18
ADDRESS LABEL OPCODE OPERAND COMMENTS
9000 MOV A, #80H MOVE THE VALUE TO A REGISTER
9001
9002 MOV R0, #90H MOVE THE VALUE TO R0 REGISTER
9003
9004 MOV R1, #00H CLEAR THE R1 REGISTER
9005
9006 ADD A,R0 ADD TWO 8 BIT VALUES
JUMP ON NO CARRY TO SPECIFIED
9007 JNC 900A
LOCATION
9008
9009 INC R1 INCREMENT R1 REGISTER
MOV MOVE DPTR TO SPECIFIED ADDRESS
900A #9500
DPTR, LOCATION
900B
900C
MOVE THE A REGISTER VALUE TO
900D MOVX @DPTR,A
DPTR
INC
900E DPTR INCREMENT DPTR
900F MOV A,R1 MOVE THE R1 REGISTER VALUE TO A
MOVE THE A REGISTER VALUE TO
9010 MOV X@DPTR,A
ADDRESS OF DPTR
SHORT JUMP TO SPECIFIED
9011 XXX SJMP 9011
ADDRESS
9012 XXX
9013
PROGRAM FOR 8-BIT SUBTRACTION USING 8051 MICROCONTROLLER
Page19
900E
900F MOVX @DPTR,A MOVE A REGISTER TO ADDRESS OF
DPTR
9010 INC DPTR INCREMENT DPTR VALUE
MOVE THE R1 REGISTER TO A
9011 MOV A,R1
REGISTER
MOVE A REGISTER TO ADDRESS OF
9012 MOVX @DPTR,A
DPTR
SHORT JUMP TO SPECIFIED
9013 XXX SJMP 9011
ADDRESS
9014 XXX
9015
PROGRAM FOR 8-BIT MULTIPLICATION USING 8051 MICROCONTROLLER
MOV
9000 #9500 MOVE DPTR TO SPECIFIED ADDRESS
DPTR,
9001
9002
MOVE THE ADDRESS OF DPTR TO A
9003 MOVX A,@DPTR
REGISTER
9004 MOV F0,A MOVE A REGISTER TO F0
9005 INC DPTR INCREMENT DPTR
MOVE THE ADDRESS OF DPTR TO A
9006 MOVX A,@DPTR
REGISTER
9007 MUL A B MULTIPLY 8 BIT VALUE
9008 INC DPTR INCREMENT DPTR
MOVE A REGISTER TO ADDRESS OF
9009 MOV X @DPTR,A
DPTR
900A INC DPTR INCREMENT DPTR
900B MOV A,F0 MOVE VALUE FROM F0TO A
MOVE A REGISTER TO ADDRESS OF
900C MOVX @DPTR,A
DPTR
SHORTJ UMP TO SPECIFIED
900D XXX SJMP 900D
LOCATION
900E XXX
900F
PROGRAM FOR 8-BITDIVISION USING 8051 MICROCONTROLLER
Page20
9004 MOV F0,A MOVE A REGISTER TO F0
9005 INC DPTR INCREMENT DPTR
MOVE THE ADDRESS OF DPTR TO A
9006 MOVX A,@DPTR
REGISTER
9007 DIV A B DIVIDE 8 BIT VALUES
9008 INC DPTR INCREMENT DPTR
MOVE A REGISTER TO ADDRESS OF
9009 MOVX @DPTR,A
DPTR
900A INC DPTR INCREMENT DPTR
900B MOV A,F0 MOVE VALUE FROM F0 TO A
MOVE A REGISTER TO ADDRESS OF
900C MOVX @DPTR,A
DPTR
SHORT JUMP TO SPECIFIED
900D XXX SJMP 900D
LOCATION
900E XXX
900F
Note1:In Multiplication,
A holds MSB of the result and B holds LSB of the result
This instruction always makes CY and OV flagas‘0’,if result is less than ‘FF’
If CY=0 and OV=1,indicates the result is above‘ FF’
Note2:Indivision,
A holds Quotient and B holds Remainder
This instruction always makes CY and OV flagas‘0’,if denominator is not‘0’
If CY=0andOV=1,indicates an error that denominator is zero
PORT 0:
Port0 is an 8-bit I/O port with dual purpose.
If external memory is used, these port pins are used for the lower address
byte address/data (AD0-AD7), otherwise all bits of the port are either input or
output.
Unlike other ports, Port 0 is not provided with pull-up resistors internally, so for
Page21
PORT0 pull-up resistors of nearly 10k are to be connected externally as
shown in the fig.4.10.
Port 1:
Port1 occupies a total of 8 pins (pins1through8).
It has no dual application and acts only as input or output port.
In contrast to port 0, this port does not need any pull-up resistors since pull-
up resistors connected internally.
Upon reset, Port1 is configured as an output port.
To configure it as an input port, port bits must be set i.e a high bit must be
sent to all the port pins.
This is normally done by the instruction “SETB”.
Ex:
MOV A,#0FFH;A=FFHEX
MOV P1,A; make P1 an input port by writing1’s to all of its pins
Port 2:
Port2 is also an eight bit parallel port.(pins21- 28).
It can be used as input or output port.
Page22
As this port is provided with internal pull-up resistors it does not need any
external pull- up resistors.
Upon reset, Port2 is configured as an output port.
If the port is to be used as input port, all the port bits must be made high by
sending FF to the port.
Ex:
MOV A, #0FFH; A=FF hex
MOV P2,A;makeP2aninputportbywriting all1’stoit
Dual role of port 2:
Port2 lines are also associated with the higher order address lines A8-A15.
In systems based on the 8751,8951,and DS5000,Port2 is used as simple I/O
port.
But, in 8051/31-based systems,port2 is used along with P0 to provide the16-
bit address for the external memory.
Sincean8051/31is capable of accessing 64Kbytes of external memory, it
needs a path for the 16 bits of the address.
While P0 provides the lower 8 bits via A0-A7, it is the job of P2 to provide bits
A8-A15 of the address.
In other words, when 8031 is connected to external memory, Port 2 is used
for the upper 8 bits of the 16 bit address, and it cannot be used for I/O
operations.
PORT 3:
Port3 is also an 8-bit parallel port with dual function.(pins10to17).
The port pins can be used for I/O operations as well as for control operations.
The details of these additional operations are given below in the table.
Port3 also do not need any external pull-up resistors as they are provided
internally similar to the case of Port2 & Port 1.
Upon reset port3 is configured as an output port.
If the port is to be used as input port, all the port bits must be made high by
sending FF to the port.
For ex,
MOV A, #0FFH; A=FF hex
MOV P3,A;makeP3 an input port by writing all1’s to it
Alternate Functions of Port3:
P3.0and P3.1are used for the RxD(Receive Data)andTxD(Transmit Data)serial
communications signals.
BitsP3.2andP3.3are meant for external interrupts.
Bits P3.4 and P3.5 are used for Timers 0 and 1 and P3.6 and P3.7 are used to
provide the write and read signals of external memories connected in 8031
based systems.
P3.0–RXD–Serial data receive
o P3.1–TXD–Serial data transmit.
o P3.2–INT0–External Interrupt0.
Page23
o P3.3–INT1–External Interrupt1.
o P3.4–T0–Clock input for counter0.
o P3.5–T1–Clock input for counter1.
o P3.6–WR–Signal for writing to external memory.
Page24
o P3.7–RD–Signal for reading from external memory.
Page26
Assume that bit P2.3 is an input and represents the condition o fan oven. If it goes
high, it means that the oven is hot. Monitor the bit continuously. Whenever it goes
high, send a high-to-low pulse to port P1.5 to turn on a buzzer.
OVEN_HOT BIT P2.3
BUZZER BIT P1.5
HERE:JNB OVEN_HOT,HERE
CPL BUZZER
ACALL
DELAY SJMP
HERE
A switch is connected to pin P1.7. Write a program to check the status of the
switch and make the following decision. (a) If SW = 0, send “0” to P2 (b) If SW = 1,
send “1“ to P2
SW EQU P1.7
MYDATA EQU P2
HERE:MOV C,SW
JC OVER
MOV
MYDATA,#’0’
SJMP HERE
OVER:MOV MYDATA,#’1’
SJMP HERE
Timer0
Timer1 registers is also a 16 bits register and is split into two bytes, referred
to asTL1and TH1.
Page27
Timer1
TMOD(timer mode)Register:
o This is an 8-bit register which is used by both timers 0 and 1 to set the
various timer modes.
o In this TMOD register, lower 4 bits are set aside for timer0 and the
upper 4 bits are set aside for timer1.
o In each case, the lower 2 bits are used to set the timer mode and
upper 2 bits to specify the operation.
TMOD Register
Page28
Mode1
o It is a16-bit timer; therefore it allows values from 0000 to FFFFH to be
loaded into the timer’s registers TL and TH.
o After TH and TL are loaded witha16-bit initial value, the timer must be
started.
o This is done by “SETB TR0”for timer 0 and“SETB TR1”for timer1.
o After the timer is started.
o It starts count up until it reaches its limit of FFFFH.
o When it rolls over from FFFF to 0000H, it sets high a flag bit called TF
(timer flag).
o 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.
o Again, it must be noted that each timer flagTF0 for timer0 andTF1for
timer1.
o 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.
Mode2
o It is an 8 bit timer that allows only values of 00 to FFH to be loaded into
the timer’s register TH.
o After TH is loaded with 8bit value, the8051 gives a copy of it to TL.
o Then the timer must be started.
o It is done by the instruction “SETB TR0”for timer0 and “SETB TR1”for
timer1.
o This is like mode1.
o After timer is started, it starts to count up by incrementing the TL
register.
o It counts up until it reaches its limit of FF H. When it rolls over from
FFHto [Link] sets high the TF (timer flag).
o If we are using timer0,TF0 goes high; if usingTF1thenTF1is raised.
o When Tl register rolls from FFHto00andTFis setto1,TL is reloaded
automatically with the original value kept by the TH register.
o To repeat the process, we must simply clear TF and let it go without
any need by the programmer to reload the original value.
o This makes mode 2 auto reload, in contrast in mode 1 in which
programmer has to reload TH and TL.
o Thefigure4.12explains the Mode2operation of timer.
o The figure 4.13 explains the Mode 1 and mode 2 operation of timer
with external input
Mode0
o Mode 0 is exactly same like mode 1 except that it is a 13-bit timer
instead of 16- bit.
o The13-bit counter can hold values between 0000to1FFFHinTH-TL.
o Therefore, when the timer reaches its maximum of 1FFH,it rolls over to
Page29
0000, and TF is raised.
Mode3
o Mode3 is also known as a split timer mode.
o Timer0and1 may be programmed to be in mode 0,1and 2
independently of similar mode for other timer.
o This is not true for mode3; timers do not operate independently if
mode3 is chosen for timer 0.
o 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.
TCON register
o Bits and symbol and functions of every bits of TCON are as follows:
TCON Register
SERIALCOMMUNICATION:
Baud Rate:
The 8051 transfers and receives data serially at many different baud rates.
The baud rate in the 8051 is programmable.
This is done with the help of Timer1.
The relationship between the crystal frequency and the baud rate is
o For XTAL=11.0592MHz,the machine cycle frequency is 921.6kHz
o The UART circuitry divides the machine cycle frequency of 921.6 kHz
by 32 once more before it is used by Timer 1 to set the baud rate.
Therefore it gives 28,800Hz.
o WhenTimer1 is used to set the baud rate it must be programmed in
Page30
mode2, i.e., 8-bit, auto reload.
o To get baud rates compatible with the PC,TH1 should be loaded with
the values shown in the following table by assuming
XTAL=11.0592MHz.
SBUF Register
SBUF is an 8-bit register used solely 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 register.
Similarly, SBUF holds the byte of data when it is received by the RxD line.
SBUF can be accessed like any other registers in
the 8051
MOV SBUF, #‘D’
MOV SBUF,A
MOV A, SBUF
The moment a byte is written into SBUF, it is framed with the start and stop
bits and transferred serially via the TxD pin
Similarly when the bits are received serially via RxD, the 8051deframes it by
eliminating the start and stop bits, making a byte out of the data received,
and then placing it in the SBUF
SCON Register:
The SCON register is an 8-bit register used to program the start bit, stop bit
and data bits of data framing, among other things.
SM0,SM1:
o These two bits determines the framing of data by the specifying the
number of bits per character, and the start and stop bits.
Page31
o They take the following combinations
Table 9:Serial Mode Operation
SM0 SM1
0 0 Serial Mode0
0 1 Serial Mode1,8-bitdata,1start bit,1stop bit
1 0 Serial Mode2
1 1 Serial Mode3
o Serial Mode 0,2 and 3 are not commonly used nowadays.
o Serial Mode1 is compatible with the COM port of IBM/PC’s
o Serial Mode1,allows the baud rate to be variable and is set by Timer1
of the 8051
o In Serial Mode1,for each character a total of 10 bits are transferred
SM2
o This bit enables the multiprocessing capability of the 8051by
assigning‘SM2=1’.
o If‘SM2=0’multiprocessing capability is disabled.
REN:(Receiver Enable)
o When the REN bit is high, it allows the 8051 to receive data on the RxD
pin
o If we want the 8051 to bit transfer and receive the data, REN must be set
to1
o By making REN=0,the receiver is disabled
o Instruction to set and Clear REN is
SETB SCON.4
CLR SCON.4
TB8:(Transfer Bit8)
o It is used for serial mode 2 and 3.
o If this is not used assign TB8 as 0
RB8(ReceiveBit8)
o In serial mode1,this bit gets a copy of the stop bit when 8-bit data is
received.
o This bit is rarely used any more, it is assigned as 0
TI(Transmit Interrupt)
o When 8051 finishes the transfer of the 8-bit character; it raises the TI
flag to indicate that it is ready to transfer another byte.
o The TI bit is raised at the beginning of the stop bit.
RI(Receive Interrupt)
o When the 8051receives data serially via RxD, it gets rid of the start
and stop bits and places the byte in the SBUF register.
o Then it raises the RI flag bit to indicate that a byte has been received
and should be picked up before it is lost.
o RI is raised half way through the stop bit.
INTERRUPT
Interrupts Vs Polling:
A single microcontroller can serve several devices. There are two ways to do
that:
o Interrupts
o Polling
In the interrupt method, whenever any device needs its service, the device
notifies the microcontroller by sending it can interrupt signal.
Page32
Upon receiving an interrupt signal, the microcontroller interrupts whatever it
is doing and serves the device.
The program associated with the interrupt is called the interrupt service
routine (ISR) or interrupt handler.
In polling, the microcontroller continuously monitors the status of a given
device; when the status condition is met, it performs the service.
After that it moves onto monitor the next device until each one is serviced.
Although polling can monitor the stats of several devices and serve each of
the mass certain conditions are met; it is not an efficient use of the
microcontroller.
The advantage of interrupts is that the microcontroller can serve many
devices; each device can get the attention of the microcontroller based on
the priority assigned to it.
The polling method cannot assign priority since it checks all devices in a
round-robin fashion.
In interrupt method, interrupts can be masked which is not possible in polling
method.
Polling method wastes microcontroller’s time by polling devices.
Interrupt Priorities
o 8051 has two levels of interrupt priorities:
High or Low.
o When 8051 is powered up, the priorities are assigned according to the
table4.13.
Page33