MPMC-UNIT 2 R20 Microcontrollers
MPMC-UNIT 2 R20 Microcontrollers
Architecture - Addressing modes and Instruction Sets – Interrupt structure – Timer –I/O
ports – Serial communication.
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.4.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 8051 is 8-bit microcontroller and Intel 8096 is 16-bit microcontroller.
The block diagram of Microcontroller is shown in Fig.4.2.
Page 1
DISTINGUISH BETWEEN MICROPROCESSOR AND MICROCONTROLLER:
Microprocessor Vs Microcontroller
S. No Microprocessor Microcontroller
A microprocessor is a general
A microcontroller is a dedicated chip which
1 purpose device which is called a
is also called single chip computer.
CPU
A microcontroller includes RAM, ROM,
A microprocessor do not contain on
serial and parallel interface, timers,
2 chip I/O Ports, Timers, Memories
interrupt circuitry (in addition to CPU) in a
etc..
single chip.
Microprocessors are most Microcontrollers are used in small,
3 commonly used as the CPU in minimum component designs performing
microcomputer systems control-oriented applications.
Microprocessor instructions Are Microcontroller instructions are both bit
4
mainly nibble or byte addressable addressable as well as byte addressable.
Microprocessor instruction sets are Microcontrollers have instruction sets
5 mainly intended for catering to catering to the control of inputs and
large volumes of data. outputs.
Microprocessor based system Microcontroller based system design is
6
design is complex and expensive rather simple and cost effective
The instruction set of a Microcontroller is
The Instruction set of
very simple with less number of
7 microprocessor is complex with
instructions. For, ex: PIC microcontrollers
large number of instructions.
have only 35 instructions.
A microprocessor has zero status
8 A microcontroller has no zero flag.
flag
Page 2
PIN DIAGRAM OF 8051:
The 8051 microcontroller is available as a 40 pin DIP chip and it works at +5 volts DC.
VCC → 5V supply
VSS → GND
XTAL2/XTALI are for oscillator input
Port 0 – 32 to 39 – AD0/AD7 and P0.0 to P0.7
Port 1 – 1 to 8 – P1.0 to P1.7
Port 2 – 21 to 28 – P2.0 to P2.7 and A 8 to A15
Port 3 – 10 to 17 – P3.0 to P3.7
P 3.0 – RXD – Serial data input – SBUF
P 3.1 – TXD – Serial data output – SBUF
P 3.2 – INT0 – External interrupt 0 – TCON 0.1
P 3.3 – INT1 – External interrupt 1 – TCON 0.3
P 3.4 – T0 – External timer 0 input – TMOD
Page 3
Fig 4.4 Crystal Oscillator as Source Fig 4.5 External Clock Source
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 port2 pins 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 These 8-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.
o This pin should not be left unconnected.
𝑷̅̅𝑺̅̅𝑬̅̅𝑵̅̅(Program Store Enable): Active Low Pin
o When the microcontroller is accessing the program code stored in the external
ROM, this pin is connected to the OE (Output Enable) pin of the ROM.
ALE (Address latch enable): Active High Pin
o When connected to external memory, port 0 provides both address and data i.e
address and data are multiplexed through port 0.
o This ALE pin will demultiplex the address and data bus.
o When the pin is High, the AD bus will act as address bus otherwise the AD bus
will act as Data bus.
Page 4
8051 ARCHITECTURE
Page 6
o OV (overflow flag) → PSW 0.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.
Page 7
o Each port is 8 bit wide and has special function register P0, P1, P2, P3 which are
bit addressable means each bit can be set or reset by the Bit 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 AD7
respectively the address bus and data bus is demultiplexed by the ALE signal
and latch which is further discussed in details.
o Port 2 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 Interrupt 1.
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 use
11.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 special set of 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 location in memory.
o INT0 and INT1 are the pins for external interrupts.
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 by the
counter?
Page 8
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 8 bitin TL0
and higher 8 bit in TH0.
o In the same way for T1.
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 has 128 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
RAM. As 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 interms 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:
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.
Page 9
o Internal RAM is volatile, so when the 8051 is reset this memory is cleared.
o The 128 bytes of internal
Four register banks (Bank0, Bank1, Bank2 and Bank3) each of 8-bits
(total 32 bytes). The default bank register is Bank0. 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 4.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.
Structure of RAM
On chip ROM
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
Page 10
o Address range of PC is 0000H to 0FFFH means total 4KB locations are available
from 0000H to 0FFFH.
Page 11
12 TMOD Timer mode register 89
13 TCON* Timer control register 88
14 TH0 Timer 0 Higher byte 8C
15 TL0 Timer 0 Lower byte 8A
16 TH1 Timer 1Higher byte 8D
17 TL1 Timer 1 lower byte 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
Page 12
Register addressing mode:
oThe addressing mode in which the data operand to be manipulated lies 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 lookup 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 An are added to the 16-bit DPTR register to form the 16-bit address
of the data operand
Page 13
INSTRUCTION SETS:
The instructions of 8051 can be broadly classified under the following headings.
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,Rn Add register to Accumulator
o ADD A,direct Add direct byte to Accumulator
o ADD A,@Ri Add indirect RAM to Accumulator
o ADD A,#data Add immediate data to Accumulator
o ADDC A,Rn Add register to Accumulator with Carry
o ADDC A,direct Add direct byte to Accumulator with Carry
o ADDC A,@Ri Add indirect RAM to Accumulator with Carry
o ADDC A,#data Add immediate data to Acc with Carry
o SUBB A,Rn Subtract Register from Acc with borrow
o SUBB A,direct Subtract direct byte from Acc with borrow
o SUBB A,@Ri Subtract indirect RAM from ACC with borrow
o SUBB A,#data Subtract immediate data from Acc with borrow
o INC A Increment Accumulator
o INC Rn Increment register
o INC direct Increment direct byte
o INC @Ri Increment direct RAM
o DEC A Decrement Accumulator
o DEC Rn Decrement Register
o DEC direct Decrement direct byte
o DEC @Ri Decrement indirect RAM
o INC DPTR Increment Data Pointer
o MUL AB Multiply A & B
o DIV AB Divide A by B
o DA A Decimal Adjust Accumulator
LOGICAL OPERATIONS
o ANL A,Rn AND Register to Accumulator
o ANL A,direct AND direct byte to Accumulator
o ANL A,@Ri AND indirect RAM to Accumulator
o ANL A,#data AND immediate data to Accumulator
o ANL direct,A AND Accumulator to direct byte
o ANL direct,#data AND immediate data to direct byte
o ORL A,Rn OR register to Accumulator
o ORL A,direct OR direct byte to Accumulator
o ORL A,@Ri OR indirect RAM to Accumulator
o ORL A,#data OR immediate data to Accumulator
o ORL direct,A OR Accumulator to direct byte
Page 14
o ORL direct,#data OR immediate data to direct byte
o XRL A,Rn Exclusive-OR register to Accumulator
o XRL A,direct Exclusive-OR direct byte to Accumulator
o XRL A,@Ri Exclusive-OR indirect RAM to Accumulator
o XRL A,#data Exclusive-OR immediate data to Accumulator
o XRL direct,A Exclusive-OR Accumulator to direct byte
o XRL direct,#data Exclusive-OR immediate data to direct byte
o CLR A Clear Accumulator
o CPL A Complement 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 A Swap nibbles within the Accumulator
DATA TRANSFER
o MOV A,Rn Move register to Accumulator
o MOV A,direct Move direct byte to Accumulator
o MOV A,@Ri Move indirect RAM to Accumulator
o MOV A,#data Move immediate data to Accumulator
o MOV Rn,A Move Accumulator to register
o MOV Rn,direct Move direct byte to register
o MOV Rn,#data Move immediate data to register
o MOV direct,A Move Accumulator to direct byte
o MOV direct,Rn Move register to direct byte
o MOV direct,direct Move direct byte to direct
o MOV direct,@Ri Move indirect RAM to direct byte
o MOV direct,#dataMove immediate data to direct byte
o MOV @Ri,A Move Accumulator to indirect RAM
o MOV @Ri,direct Move direct byte to indirect RAM
o MOV @Ri,#data Move immediate data to indirect RAM
o MOV DPTR,#data16 Load Data Pointer with a 16-bit constant
o MOVC A,@A+DPTR Move Code byte relative to DPTR to Acc
o MOVC A,@A+PC Move Code byte relative to PC to Acc
o MOVX A,@Ri Move External RAM (8- bit addr) to Acc
o MOVX A,@DPTR Move Exernal RAM (16-bit addr) to Acc
o MOVX @Ri,A Move Acc to External RAM (8-bit addr)
o MOVX @DPTR,A Move Acc to External RAM (16-bit addr)
o PUSH direct Push direct byte onto stack
o POP direct Pop direct byte from stack
o XCH A,Rn Exchange register with Accumulator
o XCH A,direct Exchange direct byte with Accumulator
o XCH A,@Ri Exchange indirect RAM with Accumulator
o XCHD A,@Ri Exchange low-order Digit indirect RAM with Acc
Page 15
o CLR bit Clear direct bit
o SETB C Set Carry
o SETB bit Set direct bit
o CPL C Complement Carry
o CPL bit Complement direct bit
o ANL C,bit AND direct bit to CARRY
o ANL C,/bit AND complement of direct bit to Carry
o ORL C,bit OR direct bit to Carry
o ORL C,/bit OR complement of direct bit to Carry
o MOV C,bit Move direct bit to Carry
o MOV bit,C Move Carry to direct bit
o JC rel Jump if Carry is set
o JNC rel Jump if Carry not set
o JB bit,rel Jump if direct Bit is set
o JNB bit,rel Jump if direct Bit is Not set
o JBC bit,rel Jump if direct Bit is set & clear bit
PROGRAM BRANCHING
o ACALL addr11 Absolute Subroutine Call
o LCALL addr16Long Subroutine Call
o RET Return from Subroutine
o RETI Return from interrupt
o AJMP addr11 Absolute Jump
o LJMP addr16 Long Jump
o SJMP rel Short Jump (relative addr)
o JMP @A+DPTR Jump indirect relative to the DPTR
o JZ rel Jump if Accumulator is Zero
o JNZ rel Jump if Accumulator is Not Zero
o CJNE A,direct,rel Compare direct byte to Acc and Jump if Not Equal
o CJNE A,#data,rel Compare immediate to Acc and Jump if Not Equal
o CJNE Rn,#data,rel Compare immediate to register and Jump if Not Equal
o CJNE @Ri,#data,rel Compare immediate to indirect and Jump if Not Equal
o DJNZ Rn,rel Decrement register and Jump if Not Zero
o DJNZ direct,rel Decrement direct byte and Jump if Not Zero
o NOP No Operation
Page 16
PROGRAMS:
PROGRAM FOR 8-BIT ADDITION USING 8051 MICROCONTROLLER
COMMENTS
ADDRESS LABEL OPCODE OPERAND
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 MOVX @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
COMMENTS
ADDRESS LABEL OPCODE OPERAND
Page 17
9009 CPL A COMPLEMENT THE ACCUMULATOR
900A INC A INCREMENT A REGISTER
900B INC R1 INCREMENT R1 REGISTER
MOV MOVE THE DPTR TO SPECIFIED
900C #9500
DPTR, ADDRESS
900D
900E
900F MOVX @DPTR,A MOVE A REGISTER TO ADDRESS OF
Page 18
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
COMMENTS
ADDRESS LABEL OPCODE OPERAND
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 AB MULTIPLY 8 BIT VALUE
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
PROGRAM FOR 8-BIT DIVISION USING 8051 MICROCONTROLLER
COMMENTS
ADDRESS LABEL OPCODE OPERAND
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
Page 19
9005 INC DPTR INCREMENT DPTR
MOVE THE ADDRESS OF DPTR TO A
9006 MOVX A,@DPTR
REGISTER
9007 DIV AB 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
Note 1: In Multiplication,
A holds MSB of the result and B holds LSB of the result
This instruction always makes CY and OV flag as ‘0’, if result is less than ‘FF’
If CY=0 and OV=1, indicates the result is above ‘FF’
Note 2: In division,
A holds Quotient and B holds Remainder
This instruction always makes CY and OV flag as ‘0’, if denominator is not ‘0’
If CY=0 and OV=1, indicates an error that denominator is zero
PORT 0:
Port 0 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
PORT0 pull-up resistors of nearly 10k are to be connected externally as shown in the
fig.4.10.
Page 20
PORT 0 With Pull-Up Resistor Connection
Dual role of port 0:
Port 0 can also be used as address/data bus (AD0-AD7), allowing it to be used for both
address and data.
When connecting the 8051 to an external memory, port 0 provides both address and
data.
The 8051 multiplexes address and data through port 0 to save the pins.
ALE indicates whether P0 has address or data.
When ALE = 0, it provides data D0-D7, and when ALE =1 it provides address and data
with the help of a 74LS373 latch
Port 1:
Port 1 occupies a total of 8 pins (pins 1 through 8).
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, Port 1 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=FF HEX
MOV P1, A; make P1 an input port by writing 1’s to all of its pins
Port 2:
Port 2 is also an eight bit parallel port. (pins 21- 28).
It can be used as input or output port.
As this port is provided with internal pull-up resistors it does not need any external pull-
up resistors.
Upon reset, Port 2 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.
Page 21
Ex:
MOV A, #0FFH; A=FF hex
MOV P2, A; make P2 an input port by writing all 1’s to it
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, port 2 is used along with P0 to provide the 16-bit
address for the external memory.
Since an 8051/31 is capable of accessing 64K bytes 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.( pins 10 to 17).
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.
Port 3 also do not need any external pull-up resistors as they are provided internally
similar to the case of Port2 & Port 1.
Upon reset port 3 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; make P3 an input port by writing all 1’s to it
Alternate Functions of Port 3:
P3.0 and P3.1 are used for the RxD (Receive Data) and TxD (Transmit Data) serial
communications signals.
Bits P3.2 and P3.3 are 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.
o P3.1 – TXD – Serial data transmit.
o P3.2 – INT0 – External Interrupt 0.
o P3.3 – INT1 – External Interrupt 1.
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.
Page 22
Programming I/O Ports:
Write a test program for the 8051 chip to toggle all the bits of P0,P1 and P2 after a
delay.
MOV P0,#00
MOV P1,#00
MOV P2,#00
BACK: MOV A,#55
MOV P0,A
MOV P1,A
MOV P2,A
ACALL DELAY
MOV A,#AA
MOV P0,A
MOV P1,A
MOV P2,A
ACALL DELAY
SJMP BACK
DELAY:MOV R1,#FF
J1: DJNZ R1,J1
RET
Write a program to generate a square waveform from bit0 of port1.
BACK: SETB P1.0
LCALL DELAY
CLR P1.0
LCALL DELAY
SJMP BACK
DELAY:MOV R1,#FF
J1: DJNZ R1,J1
RET
OR
BACK: CPL P1.0
LCALL DELAY
SJMP BACK
A switch is connected to pin P1.7 and an LED to pin P2.0. Write a program to get
the status of the switch and send it to the LED.
LED BIT P1.7
SW BIT P2.0
HERE: MOV C, SW
MOV LED, C
SJMP HERE
Page 23
Assume that bit P2.3 is an input and represents the condition of an 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
Page 24
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.
First we shall discuss about Timer0 registers.
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.
Timer 0
Timer1 registers is also a 16 bits register and is split into two bytes, referred to as TL1
and TH1.
Timer 1
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
Page 25
o 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.
o If this bit is 0 then it is used as a timer and if it is 1 then it is used as a
counter.
o In upper or lower 4 bits, the last bits third and fourth are known as M1 and M0
respectively.
o These are used to select the timer mode.
Mode 1
o 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.
o After TH and TL are loaded with a 16-bit initial value, the timer must be started.
o This is done by “SETB TR0” for timer 0 and “SETB TR1” for timer 1.
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 flag TF0 for timer 0 and TF1 for 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.
Page 26
o The figure explains the Mode 1 operation of timer
Mode 2
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 8 bit value, the 8051 gives a copy of it to TL.
o Then the timer must be started.
o It is done by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer1.
o This is like mode 1.
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 FFH. When it rolls over from FFH to 00. It
sets high the TF (timer flag).
o If we are using timer 0, TF0 goes high; if using TF1 then TF1 is raised.
o When Tl register rolls from FFH to 00 and TF is set to 1, 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 The figure 4.12 explains the Mode 2 operation of timer.
o The figure 4.13 explains the Mode 1 and mode 2 operation of timer with external
input
Page 27
Figure : 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 The 13- bit counter can hold values between 0000 to 1FFFH in TH-TL.
o Therefore, when the timer reaches its maximum of 1FFH, it rolls over to 0000,
and TF is raised.
Mode3
o Mode 3 is also known as a split timer mode.
o Timer 0 and 1 may be programmed to be in mode 0, 1 and 2 independently of
similar mode for other timer.
o This is not true for mode 3; timers do not operate independently if mode 3 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
Page 28
TCON Register Description
Page 29
Example
Indicate which mode and which timer are selected for each of the following.
(a) MOV TMOD, #01H (b) MOV TMOD, #20H (c) MOV TMOD, #12H
Solution:
We convert the value from hex to binary. From Figure 9-3 we have:
(a) TMOD = 00000001, mode 1 of timer 0 is selected.
(b) TMOD = 00100000, mode 2 of timer 1 is selected.
(c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are
Find the timer’s clock frequency and its period for various 8051-based system,
with the crystal frequency 11.0592 MHz when C/T bit of TMOD is 0.
Solution:
(1/12 )× 11.0529 MHz = 921.6 MHz;
T = 1/921.6 kHz = 1.085 us selected.
Write a program to create square wave of 50% duty cycle (with equal portions high
and low) on the P1.5 bit use Timer 0 to generate the time delay.
Solution:
MOV TMOD,#01 ;Timer 0, mode 1(16-bit mode)
HERE: MOV TL0,#0F2H ;TL0=F2H, the low byte
MOV TH0,#0FFH ;TH0=FFH, the high byte
CPL P1.5 ;toggle P1.5
ACALL DELAY
SJMP HERE
DELAY:
SETB TR0 ;start the timer 0
AGAIN: JNB TF0,AGAIN ;monitor timer flag 0
;until it rolls over
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
Page 30
Write a program segment that uses timer 1 in mode 2 to toggle P1.0 once
whenever the counter reaches a count of 100. Assume the timer clock is taken
from external source P3.5 (T1).
The TMOD value is 60H
The initialization value to be loaded into TH1 is
256 - 100 = 156 = 9CH
SERIAL COMMUNICATION:
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 Timer 1.
The relationship between the crystal frequency and the baud rate is
o For XTAL = 11.0592 MHz, the machine cycle frequency is 921.6kHz
o The UART circuitry divides the machine cycle frequency of 921.6kHz by 32 once
more before it is used by Timer 1 to set the baud rate. Therefore it gives
28,800Hz.
o When Timer 1 is used to set the baud rate it must be programmed in mode 2,
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 accesed 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
Page 31
transferred serially via the TxD pin
Similarly when the bits are received serially via RxD, the 8051 deframes 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.
o They take the following combinations
Serial Mode Operation
SM0 SM1
0 0 Serial Mode 0
0 1 Serial Mode 1, 8-bit data,1 start bit, 1 stop bit
1 0 Serial Mode 2
1 1 Serial Mode 3
o Serial Mode 0, 2 and 3 are not commonly used nowadays.
o Serial Mode 1 is compatible with the COM port of IBM/PC’s
o Serial Mode 1, allows the baud rate to be variable and is set by Timer 1 of the
8051
o In Serial Mode 1, for each character a total of 10 bits are transferred
SM2
o This bit enables the multiprocessing capability of the 8051 by 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 to 1
o By making REN=0, the receiver is disabled
o Instruction to set and Clear REN is
SETB SCON.4
CLR SCON.4
Page 32
TB8: (Transfer Bit 8)
o It is used for serial mode 2 and 3.
o If this is not used assign TB8 as 0
RB8 (Receive Bit 8)
o In serial mode 1, this bit gets a copy of the stop bit when 8-bit data is received.
o This bit is rarely used anymore, 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 8051 receives 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 halfway through the stop bit.
Page 33
Doubling the Baud Rate:
Use a higher frequency crystal
Change bit in the PCON register, shown below
PCON Register
7 6 5 4 3 2 1 0
SMOD - - - GF1 GF0 PD IDL
Example
Write a program to transfer a letter ‘Y’ serially at 9600 baud continuously, and also
to send a letter ‘N’ through Port 0, which is connected to a display device.
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON, #50H
SETB TR1
YY: MOV SBUF,#’Y’
XX: JNB TI, XX
CLR TI
MOV P0, #’N’
SJMP YY
Write a program to receive the data which has been sent in serial form and send it
out to port 0 in parallel form. Also save the data at RAM location 60H.
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON, #50H
SETB TR1
CLR RI
XX: JNB RI, XX
MOV A, SBUF
MOV P0, A
MOV 60H, A
Page 34
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 an interrupt signal.
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 on to monitor the next device until each one is serviced.
Although polling can monitor the stats of several devices and serve each of them as
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 Sources
Page 35
The 8051 architecture can handle interrupts from 5 sources.
These are:
o Two external interrupt lines,
o Two timers
o Serial interface
Each one of these is assigned an interrupt vector address. This is quite similar to the
RST interrupt vectors in the case of 8085.
External Interrupts
o Port P3 of 8051 is a multi-function port.
o Different lines of this port carry out functions which are additional to data input-
output on the port.
o Lines P3.2 and P3.3 can be used as interrupt inputs.
o Interrupts will be caused by a ‘LOW’ level, or a negative edge on these lines.
o Half of the special function register TCON is used for setting the conditions for
causing interrupts from external sources. This register is bit addressable.
SFR TCON at byte address 88H
o If the flag is 1, the selected type of event (edge or level) has occurred on the
corresponding interrupt line.
Page 36
Internal Interrupts
o Internally generated interrupts can be from either timer, or from the serial
interface.
o The serial interface causes interrupts due to a receive event (RI) or due to a
transmit event (TI).
o The receive event occurs when the input buffer of the serial line (SBUF in) is full
and a byte needs to be read from it.
o The transmit event indicates that a byte has been sent a new byte can be written
to output buffer of the serial line (SBUF out).
o 8051 timers always count up. When their count rolls over from the maximum
count to 0000, they set the corresponding timer flag TF1 or TF0 in TCON.
o Counters run only while their run flag (TR1 or TR0) is set by the user program.
o When the run flag is cleared, the count stops incrementing.
o The 8051 can be setup so that an interrupt occurs whenever TF1 or TF0 is set.
Enabling Interrupts
o At power-up, all interrupts are disabled.
o Suppose Timer 0 is started. When it times out, TF0 in the special function
register TCON will be set.
o However, this will not cause an interrupt.
o To enable interrupts, a number of steps need to be taken.
o Interrupts are enabled in a manner which is quite similar to the 8085.
o There is an interrupt enable special function register IE at byte address A8H.
o This register is bit addressable. (The assembler gives special mnemonics to
each bit address.)
SFR Interrupt Enable (IE) Register
Page 37
Interrupt Vectors
o When an interrupt occurs, the updated PC is pushed on the stack and is loaded
with the vector address corresponding to the interrupt.
o The following table 4.12 gives the vector addresses.
o The order of entries in the table is also the order in which the 8051 will poll these
in case of multiple interrupts
Page 38
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 table 4.13.
Serial Interrupts
Serial interrupts are handled somewhat differently from the timers.
There are independent interrupt flags for reception and transmission of serial data,
called RI and TI.
RI indicates that a byte has been received and is available for reading in the input buffer.
TI indicates that the previous byte has been sent serially and a new byte can be written
to the serial port.
A serial interrupt occurs if either of these flags is set. (Of course the serial interrupt must
be enabled for this to occur).
The interrupt service routine should check which of these events caused the interrupt.
This can be done by examining the flags.
Either or both of the flag might be set, requiring a read from or write to the serial buffer
SBUF (or both).
Recall that the input and output buffers are distinct but are located at the same address.
A read from this address reads the input buffer while a write to the same address writes
to the output buffer.
The RI and TI flags are not automatically cleared when an interrupt is serviced.
Therefore, the interrupt service routine must clear them before returning.
Here is an example handler for serial interrupts:
Page 40
Write a program in which the 8051 reads data from P1 and writes it to P2
continuously while giving a copy of it to the serial COM port to be transferred
serially. Assume that XTAL=11.0592MHz. Set the baud rate at 9600.
ORG 0
LJMP MAIN
ORG 23H
LJMP SERIAL
ORG 30H
MAIN: MOV P1,#0FFH
MOV TMOD,#20H
MOV TH1, #0FDH
MOV SCON,#50H
MOV IE,#10010000B
SETB TR1
BACK: MOV A,P1
MOV SBUF, A
MOV P2,A
SJMP BACK
ORG 100
SERIAL: JB TI, TRANS
MOV A, SBUF
CLR RI
RETI
TRANS: CLR TI
RETI
Write a program in which 10 bytes of data stored in RAM locations starting from
45H are transferred serially. At the end of data transfer, the value of R0 is
displayed on P1. Assume that XTAL=11.0592MHz. Set the baud rate at 9600.
ORG 0
LJMP MAIN
ORG 23H
LJMP SERIAL
ORG 30H
MAIN: MOV TMOD,#20H
MOV TH1, #0FDH
MOV SCON,#50H
MOV IE,#10010000B
SETB TR1
MOV R0,#10
MOV R1,#45H
BACK: MOV A,@R1
MOV SBUF, A
DJNZ R0, BACK
HERE: SJMP HERE
ORG 100
Page 41
SERIAL: JNB TI, RECE
MOV A,R0
MOV P1,A
CLR TI
RETI
RECE: MOV A, SBUF
CLR RI
RETI
Page 42
Example:
Write a program that displays a value of ‘Y’ at port 0 and ‘N’ at port 2 and also
generates a square wave at 10kHz, with Timer 0 in mode 2 at port pin P1.2.
XTAL=22MHz.
ORG 0000H
LJMP MAIN
ORG 000B
CPL 1.2
RETI
ORG 0030H
MAIN: MOV TMOD,#02H
MOV TH0,#0B6H
MOV IE,#82H
SETB TR0
BACK: MOV P0,#’Y’
MOV P2,#’N’
SJMP BACK
END
Page 43
Write a ALP to generates a wave form with high portion of 1085uS and a low
portion of 15us. Also acquire the value from Port 0 continuously and move it to
Port 1. Assume XTAL=11.059MHz. Use Timer 1.
ORG 0000H
LJMP MAIN
ORG 000B
LMP ISR_T1
ORG 0030H
MAIN: MOV TMOD,#10H
MOV TH1,#0FCH
MOV TL1,#018H
MOV P0,#0FFH
MOV IE,#88H
SETB TR1
BACK: MOV A, P0
MOV P1,A
SJMP BACK
ORG 0000
LJMP MAIN
ORG 0003
LED1: MOV P0,#0FF
MOV R0,#255
DJNZ R0,LED1
RETI
ORG 0013
LED2: MOV P2,#0FF
MOV R2,#255
DJNZ R0,LED2
RETI
ORG 0030
MAIN: MOV IE,#85H
HERE: SJMP HERE
END
Generate from all pins of Port 0, a Square wave which is half the frequency of the
signal applied at INT0 pin.
ORG 0000
LJMP MAIN
ORG 0003
CPL P0
RETI
ORG 0030
MAIN: SETB TCON.0 ; EDGE TRIGGERED
MOV IE,#81
HERE: SJMP HERE
Page 45