0% found this document useful (0 votes)
77 views148 pages

1 Course Material - All Chapter 04-01-2024

The document provides an overview of the microcontroller architecture Intel 8051. It discusses the organization and architecture of the 8051 microcontroller, including its RAM-ROM organization, machine cycle, instruction set and addressing modes. It also lists references and course outcomes related to comprehending the architecture and programming of the 8051 microcontroller.

Uploaded by

RU MATH Gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views148 pages

1 Course Material - All Chapter 04-01-2024

The document provides an overview of the microcontroller architecture Intel 8051. It discusses the organization and architecture of the 8051 microcontroller, including its RAM-ROM organization, machine cycle, instruction set and addressing modes. It also lists references and course outcomes related to comprehending the architecture and programming of the 8051 microcontroller.

Uploaded by

RU MATH Gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 148

MICROPROCESSORS AND

MICROCONTROLLERS
(BECE204L)
Dr. Satheesh Kumar S
Assistant Professor Sr. Gr2
School of Electronics Engineering

1
Module 3:
Microcontroller Architecture: Intel 8051
Microcontroller 8051 - Organization and Architecture, RAM-ROM Organization,
Machine Cycle, Instruction set: Addressing modes, Data Processing - Stack,
Arithmetic, Logical; Branching – Unconditional and Conditional, Assembly
programming.
References:
• Muhammed Ali Mazidi, Janice GillispieMazidi ,Rolin D Mc Kinlay ,The 8051 Microcontroller
and Embedded Systems Using Assembly and C , Second Edition ,2008 , Pearson Education
• The 8051 microcontroller : architecture, programming, and applications, Kenneth J. Ayala

Course Outcomes:
CO3: Comprehend the architectures and programming of 8051 microcontroller.

2
Microprocessor vs
Microprocessor
Microcontroller?
• A microprocessor is also known as a central processing unit in which numbers
of peripherals’ are fabricated on a single chip. It has ALU (arithmetic and logic
unit), a control unit, registers, bus systems and a clock to perform
computational tasks.
• Microprocessors are not made for a specific task as well as they are useful
where tasks are complex and tricky like the development of software, games,
and other applications that require high memory and where input and output
are not defined.
• Ex: A) Household devices: Complex home security, Home computers, Video
game systems and many more.
B) Transportation and Industrial Devices: Automobiles, trains, planes,
Computer servers, high tech medical devices, etc.
3
Microcontroller
• The microcontroller is designed for a specific task or to perform the assigned
task repeatedly. Once the program is embedded on a microcontroller chip, it
can’t be altered easily and you may need some special tools to reburn it.
• As per application, the process is fixed in microcontroller. Hence, the output
depends on the input given by the user or sensors or predefined inputs.
• Ex: Calculator, Washing Machine, ATM machine, Robotic Arm, Camera,
Printer, Microwave oven, Oscilloscope, Digital multimeter, ECG Machine, etc.

4
A microcontroller is a small and low-cost microcomputer, which is
designed to perform the specific tasks of embedded systems like
displaying microwave’s information, receiving remote signals, etc.

The general microcontroller consists of the processor, the


memory (RAM, ROM, EPROM), Serial ports, peripherals (timers,
counters).

29
Differences Between Microprocessor And
MICROPROCESSOR MICROCONTROLLER
Microcontroller
1. 1.

2.Microprocessor is the heart of 2.Microcontroller is the heart of embedded


computer system system
3.It is just a processor. Memory and I/O 3.Microcontroller has external processor
components has to be connected externally along with internal memory and i/o
components.
30
Differences Between Microprocessor And
Microcontroller
MICROPROCESSOR MICROCONTROLLER
4.Cannot be used in compact systems and 4.Can be used in compact systems
inefficient and
hence efficient
5.Cost of the entire system increases 5.Cost of the entire system is low

6.Due to external components the 6.Since external components are low


entire power consumption is high the entire power consumption is less.
7.Since memory and i/o components are all 7.Since components are internal most of
external each instruction will need external the operations are internal instruction,
operation. Hence it is relatively slower Hence speed is fast

8.Microprocessor has less number of 8.Microcontroller has more number of


registers hence most of the operations are registers hence the programs are easier to
memory based. write.
31
Differences Between Microprocessor And
Microcontroller
MICROPROCESSOR MICROCONTROLLER
9.Microprocessors are based on Von 9.Microcontrollers are based on Harvard
Neumann Architecture, where program and Architecture where program and data are
data are stored in same memory module. separate.
10. Microprocessors are used for 10. Microcontrollers are used to execute
big applications. a single task within an application.
11. It doesn’t consist of RAM, ROM, I/O 11.It consists of CPU, RAM, ROM, I/O ports.
ports. It uses its pins to interface to
peripheral devices.
12.Mainly used in personal computers 12.Used in Washing machines,MP3 players.

8
Advantages of Microcontroller based

System
As the peripherals are integrated into a single chip, the overall
system
• As the peripherals are integrated with a microprocessor the
system is more reliable.
• Though microcontroller may have on chip ROM,RAM and I/O
ports, addition ROM, RAM I/O ports may be interfaced externally
if required.
• On chip ROM provide a

9
Why/how we choose a
Microcontroller
Three criteria in Choosing a Microcontroller:
1. Meeting the computing needs of the task efficiently and cost effectively.
• speed, the amount of ROM and RAM, the number of I/O ports and timers,
size, packaging, power consumption.
•easy to upgrade.
•cost per unit.
•Noise of environment.
2. Availability of software development tools
• assemblers, debuggers, C compilers, emulator, technical
simulator, support
3. Wide availability and reliable sources of the microcontrollers
10
Types of
Microcontrollers areMicrocontrollers
divided into various categories based on
bits, memory, architecture and instruction sets.
• Based on bits: 8-bit, 16-bit, 32-bit
• Based on memory: External memory microcontroller, Embedded
memory microcontroller
• Based on instruction set: CISC, RISC

11
Types of
Based on bits:
Microcontrollers
8-bit microcontroller
• This type of microcontroller is used to execute arithmetic and
logical operations like addition, subtraction, multiplication
division, etc.
• For example, Intel 8031 and 8051 are 8 bits microcontroller.

12
16-bit microcontroller
• This type of microcontroller is used to perform arithmetic and
logical operations where higher accuracy and performance is
required.
• For example, Intel 8096 is a 16-bit microcontroller.

32-bit microcontroller
• This type of microcontroller is generally used in automatically
controlled appliances like automatic operational machines,
medical appliances, etc.

13
Based on memory:
External memory microcontroller
• This type of microcontroller is designed in such a way that they
do not have a program memory on the chip.
• Hence, it is named as external memory microcontroller.
• For example: Intel 8031 microcontroller.
Embedded memory microcontroller
• This type of microcontroller is designed in such a way that the
microcontroller has all programs and data memory, counters and
timers, interrupts, I/O ports are embedded on the chip.
• For example: Intel 8051 microcontroller.
14
Based on instruction set:
CISC
• CISC stands for Complex Instruction Set Computer.
• It allows the user to insert a single instruction as an alternative to
many simple instructions.
•Ex: JZ TEMP - if the number in the accumulator is not 0, jump to the address
marked as TEMP;
ADD A,R3 - add R3 and accumulator;
CJNE A,#20,LOOP - compare accumulator with 20. If they are not equal, jump to
the address marked as LOOP;
RISC
• RISC stands for Reduced Instruction Set Computers.
• It reduces the operational time by shortening the clock cycle per
instruction. 15
Basic technical jargons and concepts
in Microprocessor/controller
 Microprocessor (µp) - General purpose
 Microcontroller (µc) - Application specific
 Memory array and addressing: 2n=Memory size
Where n= address bus size
 Word – Equal to processor capacity
 Von Neumann Architecture – Same memory space for code
and data
 Harvard Architecture – Different spaces for code and data
Basic technical jargons and concepts
in Microprocessor/controller

 Instruction Level Parallelism (ILP) – different


operations
 Data Level Parallelism (ILP) – Different data
 Pipelining – Process through which parallelism
can be achieved
 Programming levels
High level; Medium level (Assembly level); low-level (machine level)
- Compiler - Translate High-level code to machine level code
- Assembler - Assembly language Programming
(ALP) code to machine language code
8051 basic
components
• Harvard Architecture
(separate code (ROM) and
data (RAM) memory) A Single chip Microcontroller
• 4 kilobytes of internal ROM
• 128 bytes of internal RAM
• Four 8-bit I/O ports
• Two 16-bit timers or counters
• one serial interface

18
Block Diagram Of 8051
Microcontroller

19
Features of 8051 microcontroller
• 8-bit ALU, Accumulator and 8-bit Registers; hence it is an 8-bit microcontroller
• 8-bit data bus - It can access 8 bits of data in one operation
• 16-bit address bus - It can access 216 memory locations - 64 KB (65536 locations) each
of RAM and ROM
• On-chip RAM - 128 bytes (data memory)
• On-chip ROM - 4 Kbyte (program memory)
• Four byte bi-directional input/output port
• UART (serial port)
• Two 16-bit Counter/timers: T0 and T1.
• Two-level interrupt priority: 2 external & 3 internal interrupt sources.
• Oscillator and clock circuits.
• Operating frequency: 11.04962 MHz (can vary up to 16MHz in some versions of
8051)
Address Bus (RAM)

128B = 27 7 bit address for RAM


8 bit: 00h – FFh
00h – 7Fh  internal RAM
80h – FFh SFR
Address Bus (ROM)
4KB = 4 x 1 KB = 4 x 1024 = 22 x 210
= 212
12 bit address for 4KB ROM
16 bit: 0000h – FFFFh
0000h – 0FFFh  internal ROM
1000h – FFFFh External Memory
Special Function Registers (SFR)of 8051
Internal RAM

25
Internal Data Memory and Special Function Register (SFR) Map

26
Pin diagram of 8051
Microcontroller

27
What is port
• “Port” refers to a group ?of pins on a microcontroller which can be
accessed simultaneously. (8051µC has 4-ports of 8-bits each
namely P0-P3)
• Physically, port is assigned with a register/memory location
to store the data temporarily while transferring through it.
• Ports can be programmed as output or input ports by sending
00H (all 0’s) or FFH (all 1’s) initially. By default all ports acts are
input ports.
• Due to functionality, some pins have two fold roles.
• All port pins can be designated as input or output, according to
the needs of a device that's being connected to it.
28
Pin Description of 8051
microcontroller
P0.0- P0.7(AD0-AD7) :
• No external memory
• The port 0 pins multiplexed with lower order address/data
pins if the microcontroller is accessing external memory
otherwise acts as general purpose i/o ports.
• Performs dual functions

29
Pin Description of 8051
microcontroller
P1.0- P1.7 :
• These 8-pins are dedicated for Port1 to perform
input or output port operations.
• Doesn’t perform dual functions.

30
Port-1 Internal structure
Pin Description of 8051
microcontroller
P2.0- P2.7(A8-A15) :
• The port2 pins are multiplexed with the higher order address
pins when the microcontroller is accessing external memory
otherwise they act as i/o pins.

32
Pin Description of 8051
P3.0- P3.7 microcontroller
:
• These 8-pins are meant for i/o operations and also for some
control operations like Read, Write, Timer0, Timer1, INT0,
INT1, RxD and TxD

33
Port programming
Data transfer through ports: Example 2 (Bit addressing)
Write an 8051 ALP to complement P1.2 continually with some delay.
BACK: CPL P1.2 ;complement P1.2
ACALL DELAY ;calling delay program (routine) to create time delay between each complement.
CPL P1.2
ACALL DELAY
SJMP BACK
---time delay program-------the following code is to create delay between every
complement (toggle) of P1.2 bit. A delay is required to notice the change (complement). A
random amount of delay will be created by decrementing the randomly loaded R3 value to
zero.
DELAY: MOV R3,#200 ; 200 is a decimal number. Its hexa decimal equivalent will be saved in R3.
HERE: DJNZ R3,HERE ; Decrement R3 (Any bank register R0-R7 can be used here) and remain here till it becomes
ZERO.
RET ; Return to main program where it left while calling (absolute
call i.e. ACALL) delay routine.
Pin Description of 8051
RXD: microcontroller
• 10th pin is RXD (serial data receive pin) which is for serial input.
• Through this input signal microcontroller receives data for serial
communication.
TXD:
• 11th pin is TXD (serial data transmit pin) which is serial
output pin.
• Through this output signal microcontroller transmits data
for serial communication.

36
Pin Description of 8051
microcontroller
INT0′ and INT1′ :
• 12th and 13th pins are for External Hardware Interrupt-0
(INT0) and Interrupt-1 (INT1) respectively.
• When this interrupt is activated, 8051 gets interrupted from
hardware connected to these pins.
• When interrupt comes, 8051 serves the interrupt based on
their priority. (INT0 is with highest priority)
• To serve the interrupt, the processor needs to run a program
called interrupt service routine (ISR) which is stored in fixed
memory locations (in memory) for particular interrupt.
• The interrupt vector address value of the INT0 and INT1 are
0003H and 0013H respectively. 37
Pin Description of 8051
T0 and T1: microcontroller
• 14th and 15th pin are for Timer 0 and Timer 1 external input.
• They can be connected with 16 bit timer/counter.
WR’:
• 16th pin is for external memory write i.e. it write data to
the external memory.
RD’:
• 17th pin is for external memory read i.e. it read data
from external memory.

38
Pin Description of 8051
microcontroller
XTAL2 and XTAL1:
• 18th and 19thpin these pins are connected to an
external oscillator which is generally a quartz crystal oscillator.
Frequency of operation is 11.0592 MHz. (it may vary up to
16MHz in some versions of 8051)

39
Pin Description of 8051
VCC: microcontroller
• This pin provides power supply voltage i.e. +5 Volts
to the circuit.
GND:
• This pin is connected to the ground.
• It has to be provided with 0V power supply.
• Hence it is connected to the negative terminal of the
power supply.

40
Pin Description of 8051
RST: microcontroller
• The RESET pin is an input pin and it is an active high pin.
• When a high pulse is applied to this pin the microcontroller will
reset and terminate all activities.
• Upon reset all the registers except stack pointer (SP)
register will reset to 0000H Value and SP register will reset
to
07H value.

41
Pin Description of 8051
EA'/ microcontroller
•VPPThis
: pin is connected to ground when microcontroller is accessing
the program code stored in the external memory and
connected to Vcc (+5V) when it is accessing the
program code in the on chip memory.

42
Pin Description of 8051
PSEN’ : microcontroller
• This is an output pin which is active low.
• It will be activated (zero) 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 external ROM.

43
Pin Description of 8051
ALE /PROG:microcontroller
• This is an output pin, which is active high.
• When 8051 is connected to external memory, ALE becomes high
to de-multiplex the memory address which is available on port 0.
(Port 0 provides both address and data on time sharing basis i.e
address and data are multiplexed port 0 .

• This ALE pin will demultiplex the address and data bus .
• During flash programming i.e. Programming of EPROM, this
pin acts as program pulse input (PROG).
44
Architecture of 8051
microcontroller

45
• RAM address register and Program address register:
- RAM address register is an 8-bit register.
- Program address register is an 16-bit register.
 RAM address register is used to carry the address of the internal RAM (size of 128 Bytes-
address range from 00H – 7FH) location which is currently under access.
 Program address register is used to carry the address of the internal ROM (size of 4 kB Bytes-
address range from 000H – FFFH) location which is currently under access.

• Program counter (PC) and Data pointer (DPTR):


- Both are 16-bit registers.
Why 16 bit registers ? - The 8051 can be connected with a maximum of 64 kB of external
memory (including RAM and ROM). That is why 16-bit address bus also provided (A0 – A15).

• The DPTR can be used as a single 16-bit register or two 8-bit registers (as DPL and DPH).
• DPTR doesn’t have a physical Memory Address but the DPL (Lower Byte of DPTR) and DPH
(Higher Byte of DPTR) have separate addresses in the special function registers (SFR)
Memory Space. DPL = 82H and DPH = 83H.
46
Data pointer (DPTR)

47
Stack Pointer (SP)
• SP or Stack Pointer points out to the top of the Stack and it indicates the next data to be
accessed. Stack Pointer can be accessed using PUSH, POP, CALL and RET Instructions. The
Stack Pointer is an 8-bit register and upon reset, the Stack Pointer is initialized with 07H.

• When writing a new data byte into the stack, the SP (Stack Pointer) is automatically
incremented by 1 and the new data is written at an address SP+1. When reading data
from stack, the data is retrieved from the Address in SP and after that the SP is
decremented by 1 (SP-1).

48
Stack operation in 8051
STACK:
It is a part of RAM in which data will store temporarily during execution of program.
STACK work on last in first out principle.
To store and retrieve data during program execution in stack, push and pop instructions
are used.
PUSH:
its used to store data into stack.
POP:
to retrieve data from stack.
SP:
Stack pointer is 8 bit register which store the address of the stack location up to which it
is filled (top of the stack).
By default stack pointer contain 07h. This means Bank 1 of the internal RAM acts as
default stack area. The stack area can be relocated to general purpose RAM area by
changing the address present in the SP to appropriate value which comes under the
address range of general purpose area. 49
Stack operation in 8051
PUSH: --- SP value will be pre incremented automatically.
In PUSH operation, stack pointer (SP) will be incremented first and then content of register
or memory will store on that stack location which shown in SP.

The stack memory location which is available for access is called top of the stack, whose
address is shown by SP register.
Examples:
PUSH 0H; SP will be incremented by one and then the content of R0 (memory address 00H)
register store into 08H location.
As the default SP value is 07H, it will become 08H after incrementing first time.
PUSH 1H; SP will be incremented by one and then the content of R1 (memory address 01H)
register store into 09H location.
PUSH 31H; SP will be incremented again by one and the content of memory location with
address 31H stored into 0AH location.
50
Stack operation in 8051
POP: --- SP value will be post decremented automatically.
In POP operation, data will be retrieved first and then stack pointer decreased by one.

Examples:
POP 20H: The content of the top of the stack will be copied into memory location with
address 20h, then stack pointer (SP) will be decreased by 1.

Note: For the PUSH and POP instructions, we must specify the direct address
of the register/memory location being pushed or popped.

51
A or Accumulator (ACC)
• The Accumulator or Register A is the most important and most used 8051 Microcontroller
Special Function Register (SFR). The Register A is located at the address E0H in the SFR
memory space. The Accumulator is used to hold the data for almost all the ALU Operations.

Some of the operations where the Accumulator is used are:


• Arithmetic Operations like Addition, Subtraction, Multiplication etc.
• Logical Operations like AND, OR, NOT etc.
• Data Transfer Operations (between 8051 and External Memory)
• The name “Accumulator” came from the fact that, this register is used to accumulate (or
store) the result of all Arithmetic and most of the Logical Operations.

52
B (Register B)
• The B Register is used along with the ACC in Multiplication and Division operations.
These two operations are performed on data that are stored only in Registers A and B.
During Multiplication Operation, one of the operand (multiplier or multiplicand) is stores
in B Register and also the higher byte of the result.

• In case of Division Operation, the B Register holds the divisor and also the remainder of
the result. It can also be used as a General Purpose Register for normal operations and is
often used as an Auxiliary Register by Programmers to store temporary results.

• Register B is located at the address F0H of the SFR Address Space.

53
Program Status Word (PSW) : (Also called flag register)
• The PSW or Program Status Word Register is also called as Flag Register and is one of the
important SFRs. The PSW Register consists of Flag Bits, which help the programmer in
checking the condition of the result and also make decisions.

• Flags are 1-bit storage elements that store and indicate the nature of the result that is
generated by execution of certain instructions. The following image shows the contents
of the PSW Register.

54
The following table describes the function of each
flag.

55
Program Status Word (PSW): Address=D0H

56
CY:
• This is known as carry flag. This says whether there is a carry out of MSB or not.
E.g: 1000 1101 & 1011 1001, Let’s add both bytes
= 0011 0110 There is a carry out of MSB. Therefore in this case CY=1.
AC:
• This flag stands for an auxiliary carry. It is given as 1 whenever there is a carry from LN to HN.
LN and HN stands for lower nibble and higher nibble. As byte stands for 8 bits, similarly nibble
stands for 4 bits. Lower nibble is 0-3 bits and higher nibble is 4-7 bits.
• If seen in the above example there was a carry from 3rd bit to 4th bit. So in the above case
AC=1.
PARITY:
• If there are even number of 1s in the result then the parity will be zero, and if there are odd
number of 1s then , parity=1
Eg= 1010 1101 , here there are 5 ones, therefore parity =1
Reserved:
• This is a don’t care flag. It has nothing to do with.

57
OV: • The operations for signed and unsigned numbers are the same, but
signed numbers overflow between bit 6 and 7, while unsigned
This is an overflow flag.
numbers overflow between bit 7 and 8.
There are two types numbers • The OV flag represents the overflow into bit 7, while the CY flag
•Signed represents the overflow into bit 8. So, for a signed operation, you'd
•Unsigned check the OV flag, while for an unsigned operation you'd check the
CY flag after the operation. https://electronics.stackexchange.com/
Unsigned:
This is a 8 bit positive number which ranges from 0 to 255. In hexadecimal it’s range is from
00 to FF. In the OV flag, it has nothing to do with unsigned numbers. Only signed numbers are
considered in the OV flag.
Signed:
This is also a 8-bit number which is equally distributed among +ve and -ve numbers. By
considering MSB , it is decided whether it is a positive or negative number. If MSB=1 , it is a
negative number or else a positive number.
Eg: 1011 0011 is -ve
0111 1001 is +ve
So it’s positive range is from 0 to 127 [ 00 to 7F (in hexadecimal ) ], it consists of 128 +ve values.
It also has 128 negative numbers ranging from -1 to -128 [ -01 to -80 ( in hexadecimal ) ].
55
F0 :
• This is a user defined bit flag, where users can store any data.
RS1 and RS0 :
• Internal RAM of 8051 is 128 bytes out of which 32 bytes are stored in registers ( 8085, 8086
has 7,8 registers only ). These all 32 registers are divided into 4 banks, each bank has 8
registers starting from R0 to R7.
• Let’s assume there is an operation that should be performed on R1, then how it decides
from which bank the register is selected?
• This is performed based on banks
• By setting the values of RS1 and RS0 , we can select the register.
•This instruction stands for RS1 = 1 , and RS0 = 0, which means bank 2.

59
Internal memory
- Internal RAM
(Data memory)

Stack (default)

60
Internal Memory Here, * indicates the
- SFR (Special bit-addressable SFRs
function registers)
memory
Note: SFR memory is a separate memory
space available on-chip (internally).
It wont come under internal ROM
(code memory) or
Internal RAM (data memory).

61
Special Function Registers
(SFR)
Here, * indicates the

bit-addressable SFRs

62
Special Function Registers
(SFR)
Here, * indicates the bit-addressable SFRs

76
Widely used Register set of 8051
SP DPH DPL
PSW
DPTR
A
B
PC
R0
R1
R2
R3
R4
R5
R6
R7

8-bit Registers of 8051 16-bit Registers of 8051


Instruction set of 8051
DATA TRANSFER ARITHMETIC LOGICAL
MOV – Move with internal RAM ADD ANL – Logical AND
(data memory)
MOVC – Move with Code ADDC – Add with Carry ORL – Logical OR
memory (ROM)
MOVX – Move with external SUBB – Subtract XRL – Logical XOR
memory with Borrow
PUSH – push data in to stack INC - Increment CLR - Clear
POP– pop data from stack DEC - Decrement CPL - Complement
XCH – Exchange the contents of MUL – Multiplication RL – Rotate left the content of the operand.
two operands (Only A and B registers are allowed as operands with
MUL and DIV instructions)
Only direct addressing is allowed DIV – Division RLC – Rotate left the content of the operand
in PUSH and POP Examples: MUL AB, DIV AB. along with carry flag.
No comma required between A and B
DAA – Decimal Adjust Accumulator. (used in BCD RR– Rotate right the content of the operand.
arithmetic)
RRC– Rotate right the content of the operand
along with carry flag.
RLA– Rotate left the content of the Accumulator.
SWAP – Swap the lower and higher nibbles of
the operand.
Instruction set of 8051

BOOLEAN PROGRAM BRANCHING


SETB -Set Bit JMP – Jump (Unconditional)
JC -Jump if Carry AJMP – Absolute Jump (jump range 2kB.)
JNC – Jump if no carry SJMP - Short Jump (jump range 128 Bytes.)
JB – Jump if Bit set to 1 JZ – Jump if A=0
JNB- Jump if no Bit JNZ – Jump if A≠0
JBC – Jump if bit and clear the bit after CJNE – Compare and jump if not equal. (Compare the contents of the operands)
jump
DJNZ – Decrement the operand once and jump if it is not zero
NOP – No operation
RET – Return to main program from subprogram (Example: Delay routine.)
ACALL– Absolute Call (Range: 2kB)
LCALL - Long Call (range: 64kB)
8051 Addressing modes
An assembly language program (ALP) is a collection of instructions
which need to be executed in sequential order. An example ALP is given
as, MOV A,31H
Opcode Operand
Opcode defines the type of operation; Operand specifies the source and
destinations on which operand is going to work.
Addressing mode defines the style (method) of writing an
assembly language instruction while programming a particular
processor. Addressing modes are processor dependent.
8051 Addressing modes
1) Immediate addressing mode
MOV A, #6AH
Operand contains data on which operation is going to perform.
2) Direct addressing mode
MOV A, 04H
Operand contains address of the memory location where data is stored
(on which operation is going to perform).
8051 Addressing modes
3) Register direct addressing mode (Register addressing mode)
MOV A, R4
(Registers can be R0,R1…to R7 from register selected bank of internal
RAM. Direct data transfer between any two bank registers is not
allowed. Can be done through Accumulator (A)).
Example:
MOV R1,R3 (This is not allowed.)
This can be done through accumulator as follows.
MOV A,R3
MOV R1,A
8051 Addressing modes
4) Register indirect addressing mode
MOV A, @R0
Note: Only R0 and R1 are allowed to form a register indirect addressing
instruction.
5) Indexed addressing mode
MOVC A, @A+DPTR
6) Relative Addressing : SJMP LABEL_X (range of + 127 to –128 locations)
7) Absolute addressing : Absolute addressing within the 8051 is used
only by the AJMP (Absolute Jump) and ACALL (Absolute Call)
Instructions. Allows a jump with a 2KByte address boundary (a 2K page)
8051 Addressing modes
8) Implied/Implicit Addressing Mode
In the implied addressing mode, there will be a single operand. These
types of instruction can work on specific registers only.
These types of instructions are also known as register specific
instructions.
Examples:
RLA (Rotate left accumulator content by one time)
SWAPA (Swap the lower and higher nibbles (4-bits) of accumulator)
8051 ALP example 1 (addressing modes)
Write an ALP to copy the value 55H into RAM memory locations 40H to 41H using
(a) direct addressing mode, (b) register indirect addressing mode without a loop, and (c) with a loop

(a) ORG 0H ;start storing at 0H location onwards. (c) ORG 0H


MOV A,#55H ;load A with value 55H MOV A,#55H
MOV 40H,A ;copy A to RAM location 40H MOV R0,#40H
MOV 41H.A ;copy A to RAM location 41H MOV R2,#02
END ;end of asm source file AGAIN: MOV @R0,A
INC R0
(b) ORG 0H ; start storing at 0H location onwards. DJNZ R2,AGAIN
MOV A,#55H ;load A with value 55H END
MOV R0,#40H ;load the register R0=40H. (R0 is the pointer)
MOV @R0,A ;copy A in to RAM location whose address is pointed by R0.
INC R0 ;increment pointer. Now R0=41h
MOV @R0,A ;copy A to memory address where R0 points to
END ;end of asm source file. (end converting machine code)
Example 5-3

Write a program to copy the value 55H into RAM memory locations 40H to 45H using
(a) direct addressing mode, (b) register indirect addressing mode without a loop, and
(c) with a loop.

Solution:

(a)
MOV A,#55H ;load A with value 55H
MOV 40H,A ;copy A to RAM location 40H
MOV 41H,A ;copy A to RAM location 41H
MOV 42H,A ;copy A to RAM location 42H
MOV 43H,A ;copy A to RAM location 43H
MOV 44H,A ;copy A to RAM location 44H
Example 5-3

Write a program to copy the value 55H into RAM memory locations 40H to 45H using
(a) direct addressing mode, (b) register indirect addressing mode without a loop, and
(c) with a loop.
Solution:
(b) MOV A,#55H ;load A with value 55H
MOV R0,#40H ;load the pointer. R0=40H
MOV @R0,A ;copy A to RAM location R0 points to
INC R0 ;increment pointer. Now R0=41H
MOV @R0,A ;copy A to RAM location R0 points to
INC R0 ;increment pointer. Now R0=42H
MOV @R0,A ;copy A to RAM location R0 points to
INC R0 ;increment pointer. Now R0=43H
MOV @R0,A ;copy A to RAM location R0 points to
INC R0 ;increment pointer. Now R0=44H
MOV @R0,A
Example 5-3

Write a program to copy the value 55H into RAM memory locations 40H to 45H using
(a) direct addressing mode, (b) register indirect addressing mode without a loop, and
(c) with a loop.

Solution:

(c)
MOV A,#55 ;A=55H
MOV R0,#40H ;load pointer. R0=40H, RAM address
MOV R2,#05 ;load counter, R2=5
AGAIN: MOV @R0,A ;copy 55H to RAM location R0 points to
INC R0 ;increment R0 pointer
DJNZ R2,AGAIN ;loop until counter = zero
Example 5-4

Write a program to clear 16 RAM locations starting at RAM address 60H.

Solution:

CLR A ;A=0
MOV R1,#60H ;load pointer. R1=60H
MOV R7,#16 ;load counter, R7=16 (10 in hex)
AGAIN: MOV @R1,A ;clear RAM location R1
points to
INC R1 ;increment R1 pointer
DJNZ R7,AGAIN ;loop until counter = zero
Example 5-5

Write a program to copy a block of 10 bytes of data from RAM locations starting
at 35H to RAM locations starting at 60H.

Solution:

MOV R0,#35H ;source pointer


MOV R1,#60H ;destination pointer
MOV R3,#10 ;counter
BACK: MOV A,@R0 ;get a byte from source
MOV @R1,A ;copy it to destination
INC R0 ;increment source pointer
INC R1 ;increment destination pointer
DJNZ R3,BACK ;keep doing it for all ten bytes
8051 ALP example 2 (stack operation)
Move the data between registers and stack; Examine the stack, contents of the registers ,SP.
(All values are in hexa decimal.)

Note: For the PUSH and POP instructions, we must specify the direct address of the register/memory
location being pushed or popped.
(Note: if a hexa decimal number is starting with an alphabet, a
ZERO (0) should be appended in front of it)
ORG 0H In this question, it is not asked to relocate the stack and not
MOV R6,#25H asked to select the register bank. So, by default, BANK-1 will
MOV R1,#12H act as stack (range from 08H-0FH). BANK-0 acts as default
register bank. So, the registers (R0-R7) used in this program
MOV R4,#0F3H will be considered from BANK-0.
PUSH 6H
PUSH 1H In this program, the contents of R6 (06H), R1 (01H), R4 (04H)
are pushed in to stack locations 08H, 09H, and 0AH
PUSH 4H respectively. After this, top of the stack will be 0AH (can be
POP 33H seen as SP=09H). Then the contents of the top of the stack (i.e.
HERE: SJMP HERE contents of the 0AH location) will be poped in to memory
location whose address is 33H. After POP operation, the SP
END value will be decremented to 09H. (Which will now act as top
of the stack.)
8051 ALP example 3 (stack operation)
Example for data swapping (reversing)
Write an ALP to load given values into each of registers R0 - R4, then push each of these
register contents in to the stack and pop them back. Examine the stack and the SP register after
the execution of each instruction.
ORG 0000H PUSH 2 In this program, default stack location
i.e. BANK-1 will be available.
MOV R0, #25H PUSH 3
For default stack, SP=07H (initially).
MOV R1, #35H PUSH 4
All the registers such as R0-R7 used in
POP 0H this program are selected from default
MOV R2, #45H
POP 1H bank i.e. BANK-0.
MOV R3, #55H
POP 2H BANK-0 address range is from 00H to 07H named as R0 to
MOV R4, #65H R7 respectively, as given below
POP 3H 00H=R0; 01H=R1; 02H=R2; 03H=R3; 04H=R4; 05H=R5;
PUSH 0 POP 4H 06H=R6; 07H=R7;

PUSH 1 END
8051 ALP example 4 (stack operation)
Write an 8051 assemble language program (ALP) by considering following conditions while writing the
program.
(a) Relocate the stack to general purpose (scratch pad) RAM area; (b) select BANK-3 registers by writing
appropriate value in to PSW; (c) Load the R0 to R4 registers of the selected bank with data 0DH, F2H, 0BH,
05H, and 08H, respectively. (d) PUSH the contents of the registers R0-R4 in to stack. (e) POP the contents of
the stack in to R3 to R7 registers. (Observe the SP value after every PUSH and POP operation.)
ORG 0000H
MOV SP, #36H ; The SP is loaded with 36H to relocate the stack with in the range of scratch pad RAM area (30H to FFH).
MOV PSW, #18H ; RS1 and RS0 bits of PSW are set to ‘1’ to select BANK-3 (R0-R7 of BANK-3 will be selected).
MOV R0, #0DH See PSW format for more details. It will be 00011000=18H
MOV R1, #0F2H
MOV R2, #0BH
MOV R3, #05H
MOV R4 #08H
PUSH 18H
PUSH 19H POP 1CH BANK-3 address range is from 18H to 1FH named as R0 to R7 respectively,
PUSH 1AH POP 1DH as given below
PUSH 1BH POP 1EH 18H=R0; 19H=R1; 1AH=R2; 1BH=R3; 1CH=R4; 1DH=R5; 1EH=R6;
PUSH 1CH POP 1FH 1FH=R7;
POP 1BH END
ORG 0000H
8051 ALP example 5 (Data transfer between ROM and RAM)
MOV A, #00H
Write a program to transfer a string of data "VIT UNIVERSITY"
from code memory (ROM) space starting at address 200H to data MOV DPTR, #0200H ; starting ROM address
memory (RAM) locations starting at 40H. MOV R1, #0EH ; length of string
In this program, assembler directive ORG is used twice. Both MOV R0, #40H ; starting RAM address
ORG will be executed in parallel. LOOP: CLR A
One ORG is used to define the starting address location (here MOVC A,@A+DPTR
0000H) from which this program need to be stored in ROM. MOV @R0, A
Another ORG defines the starting address location (here 200H)
INC DPTR
where given string is stored in ROM.
INC R0
String length i.e number of characters in the given string is ; Decrement R1 once
DJNZ R1, LOOP
14=0EH and back to loop if R1
HERE: SJMP HERE is non zero.
String is a group of characters. ‘Space’ is also a character. All the
characters will be translated in to equivalent ASCII codes and ORG 0200H
stored in the memory. DB "VIT UNIVERSITY"

One character occupies one memory location. Hence, each END


character in the string is associated with one memory address.
Example program6 – Bit addressing

Find out to which by each of the following bits belongs. Give the address of the RAM byte
in hex
(a) SETB 42H, (b) CLR 67H, (c) CLR 0FH
(d) SETB 28H, (e) CLR 12, (f) SETB 05
Bit addressing in internal RAM (data memory)

Bit address
Example program6 – Bit addressing
Find out to which by each of the following bits belongs. Give the address of the RAM byte
in hex
(a) SETB 42H, (b) CLR 67H, (c) CLR 0FH
(d) SETB 28H, (e) CLR 12, (f) SETB 05
Example program7 – Bit addressing
Assume all RAM locations are cleared before running this program. Simulate the code and find
out the result.
ORG 0H
SETB 92H 2nd Bit address of port-1
SETB 93H 3rd Bit address of port-1
MOV C,P1.2
MOV 6,C 6H is the Address from Bit addressable area
MOV C,P1.3
MOV 7,C 7H is the Address from Bit addressable area
END
Bit addressing of ports
Machine cycle of 8051
• Machine cycle is a measure (unit) of calculating the time taken by the 8051 to complete
an instruction.
• The time taken to complete any instruction is measured in terms of machine cycle or
instruction cycle. In 8051 one machine cycle consists 12 clock cycles.
• A clock cycle is the time period of the square wave which is generated by the crystal
oscillator connected to XTAL pins of 8051.
• Which means, machine cycle frequency = 1/12 of the clock frequency

Time period of a 8051 machine cycle for 11.0592 MHz crystal (clock) frequency:
Machine cycle frequency = 11.0592M/12 = 921.6 kHz;
Time period of machine cycle is 1/921.6 kHz = 1.085μs
Time delay calculation with Machine cycle
• For 8051 system of 11.0592 MHz, see the below list to find how
long 8051 takes to execute each of the following instruction.
(a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2 target (d) LJMP (e) SJMP
(f) NOP (g) MUL AB
Machine cycles Time to execute
(a) 1 1x1.085μs = 1.085μs
(b) 1 1x1.085μs = 1.085μs
(c) 2 2x1.085μs = 2.17μs
(d) 2 2x1.085μs = 2.17μs
(e) 2 2x1.085μs = 2.17μs
(f) 1 1x1.085μs = 1.085μs
(g) 4 4x1.085μs = 4.34μs
Calculation of execution time with Machine cycle
• Find the time (delay) taken by 8051 for the execution of following
program, if the crystal (clock) frequency is 11.0592MHz.
MOV R3,#250 1
HERE: NOP 1 In this program, number of machine
cycles consumed by each instruction is
NOP 1 listed.
NOP 1 A value of 250 (which is in decimal form)
NOP 1 is loaded in to R3.
The hexa decimal equivalent of decimal
DJNZ R3,HERE 2 number 250 will be loaded in to R3.
Solution: 250 (Decimal) = FA (Hexa)
The time delay inside HERE loop is
[250(1+1+1+1+2)]x1.085μs = 1627.5μs.
Considering the instruction outside the loop we have
1627.5μs + (1 x 1.085μs) = 1630.755μs
Time delay calculation with Machine cycle
• Find the size of the delay created by delay routine (sub program) in following program, if
the crystal frequency is 11.0592MHz.
MOV A,#55H
AGAIN: MOV P1,A
ACALL DELAY ; calling delay program (routine)
CPL A
SJMP AGAIN
---delay program (routine) to generate delay-------An amount of delay will be created by
decrementing the loaded R3 value to zero.
DELAY: MOV R3,#200 ; 200 is a decimal number. Its hexa decimal equivalent will be saved in R3.
HERE: DJNZ R3,HERE ; Decrement R3 (Any bank register R0-R7 can be used here) and remain here till it becomes
ZERO.
RET
; Return to main program where it left while calling (absolute
call i.e. ACALL) delay routine.

• Solution: Delay produced by delay routine: [1+(200x2)+2]x1.085μs = 436.255μs.


Module 4:
Microcontroller 8051 Peripherals
I/O Ports, Timers-Counters, Serial Communication and Interrupts.

References:
• Muhammed Ali Mazidi, Janice GillispieMazidi ,Rolin D Mc Kinlay ,The 8051 Microcontroller
and Embedded Systems Using Assembly and C , Second Edition ,2008 , Pearson Education
• The 8051 microcontroller : architecture, programming, and applications, Kenneth J. Ayala

Course Outcomes:
CO4: Deploy the implementation of various peripherals such as general purpose input/
output, timers, serial communication, LCD, keypad and ADC with 8051
microcontroller 2
Instruction set of 8051
DATA TRANSFER ARITHMETIC LOGICAL
MOV – Move with internal RAM ADD ANL – Logical AND
(data memory)
MOVC – Move with Code ADDC – Add with Carry ORL – Logical OR
memory (ROM)
MOVX – Move with external SUBB – Subtract XRL – Logical XOR
memory with Borrow
PUSH – push data in to stack INC - Increment CLR - Clear
POP– pop data from stack DEC - Decrement CPL - Complement
XCH – Exchange the contents of MUL – Multiplication RL – Rotate left the content of the operand.
two operands (Only A and B registers are allowed as operands with
MUL and DIV instructions)
Only direct addressing is allowed DIV – Division RLC – Rotate left the content of the operand
in PUSH and POP Examples: MUL AB, DIV AB. along with carry flag.
No comma required between A and B
DAA – Decimal Adjust Accumulator. (used in BCD RR– Rotate right the content of the operand.
arithmetic)
RRC– Rotate right the content of the operand
along with carry flag.
RLA– Rotate left the content of the Accumulator.
SWAP – Swap the lower and higher nibbles of
the operand.
Instruction set of 8051

BOOLEAN PROGRAM BRANCHING


SETB -Set Bit JMP – Jump (Unconditional)
JC -Jump if Carry AJMP – Absolute Jump (jump range 2kB.)
JNC – Jump if no carry SJMP - Short Jump (jump range 128 Bytes.)
JB – Jump if Bit set to 1 JZ – Jump if A=0
JNB- Jump if no Bit JNZ – Jump if A≠0
JBC – Jump if bit and clear the bit after CJNE – Compare and jump if not equal. (Compare the contents of the operands)
jump
DJNZ – Decrement the operand once and jump if it is not zero
NOP – No operation
RET – Return to main program from subprogram (Example: Delay routine.)
ACALL– Absolute Call (Range: 2kB)
LCALL - Long Call (range: 64kB)
I/O ports of 8051
• 4 I/O ports namely PORT-0, PORT-1, PORT-2, PORT-3 are available.
• All ports are 8-bit ports. (P0.0-P0.7; P1.0-P1.7; P2.0-P2.7; P3.0-P3.7;)
• All ports are bit as well as byte addressable.
• For example P1 refers to all 8-bits of PORT-0 i.e P0.0-P0.7 (byte addressing).
On the other hand, a single bit let us say P1.2 can also be accessed (bit
addressing).
• Physically, port is assigned with a register/memory location to store the data
temporarily while transferring through it.
• Ports can be programmed as output or input ports by sending 00H (all 0’s) or
FFH (all 1’s) initially.
• By default all ports acts are input ports.
• All port pins can be designated as input or output, according to the needs of
the device (peripheral) that's being connected to it.
Port programming: Data transfer through ports: Example 1
• By default all ports acts are input ports.
• Since any port is an input port by default, it is not necessary to write all 1s in
to it (before receiving data). But if we want to send data, then we have to
make the port as output port by sending all 0’s initially. However, if we want
to reprogram that port (which is already an output port) as input port, then
we have to send all 1’s before receiving data.
Note: Accumulater (A) is the only register allowed for the data transfer through ports.
MOV A,#00H ;A=00H; hexa decimal (all 0’s)
MOV P1,A ;make P1 an o/p port by writing it all 0s
BACK: MOV A,P0 ;get data from P0. (Assume an input peripheral (hardware)
is connected to port-0)
MOV P1,A ;send it to port 1
SJMP BACK ;keep doing it
Data transfer through ports: Example 2 (Bit addressing)
Write an 8051 ALP to complement P1.2 continually with some delay.
BACK: CPL P1.2 ;complement P1.2
ACALL DELAY ;calling delay program (routine) to create time delay between each complement.
CPL P1.2
ACALL DELAY
SJMP BACK
---time delay program-------the following code is to create delay between every
complement (toggle) of P1.2 bit. A delay is required to notice the change (complement). A
random amount of delay will be created by decrementing the randomly loaded R3 value to
zero.
DELAY: MOV R3,#200 ; 200 is a decimal number. Its hexa decimal equivalent will be saved in R3.
HERE: DJNZ R3,HERE ; Decrement R3 (Any bank register R0-R7 can be used here) and remain here till it becomes
ZERO.
RET ; Return to main program where it left while calling (absolute
call i.e. ACALL) delay routine.
Duty cycle of a waveform
Defines the % of time the waveform is in ON state in one cycle time period (100%).

One cycle time period


Data transfer through ports: Example 3
Write an 8051 ALP to generate a square wave of 30% duty cycle on bit 0 of port 2.
Assume that the time period of 1-cycle = 436μs
Solution:
Therefore, we toggle (complement) P2.0 with a time delay in between each
state such that the ON time is 30% of each cycle.
BACK: SETB P2.0 ;complement P2.0
ACALL DELAY_ON ;calling delay program to create ON-time delay (30% of the total cycle time).
CPL P2.0
ACALL DELAY_OFF ;calling delay program to create OFF-time delay.
SJMP BACK
DELAY_ON: MOV R4,#----
HERE: DJNZ R4,HERE
RET
DELAY_OFF: MOV R5,#----
HERE2: DJNZ R5,HERE2
RET
Data transfer through ports: Example 4 (Byte addressing)
• A program is written below to send complemented data AAH and 55H continually on
PORT-3 (all 8-bits P3.0-P3.7). A finite delay is assumed between each complemented data.
Byte addressing means, all the 8-bits of port-3 will be accessed by referring P3.
We have to send all 0’s to P3 initially since all ports are input ports by default.
MOV A,#00H ;A=00H; hexa decimal (all 0’s)
MOV P3,A ;make P3 an o/p port by writing it all 0s
BACK: MOV A,#0AAH ;A=AAH; hexa decimal
MOV P3,A ;send the contents of A-register in to Port-3
ACALL DELAY ; Calling delay program (routine)
MOV A,#55H ;A=55H; hexa decimal
MOV P3,A ;send the contents of A-register in to Port-3
ACALL DELAY ; Calling delay program (routine)
SJMP BACK
---time delay program-------
DELAY: MOV R4,#200 ; 200 is a decimal number. Its hexa decimal equivalent will be saved in R4.
HERE: DJNZ R4,HERE ; Decrement R4 and remain here till it becomes ZERO.
RET
Data transfer through ports: Example 5
• 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.
• Solution:
HERE: JNB P2.3,HERE ;keep monitoring for high
SETB P1.5 set bit P1.5=1
CLR P1.5 ;make high-to-low
SJMP HERE ;keep repeating
Data transfer through ports: Example 6
• A switch is connected to pin P1.7. Write a program to check the status of SW and perform
the following:
(a) If SW=0, send letter ‘N’ to P2
(b) If SW=1, send letter ‘Y’ to P2
Use the carry flag to check the switch status.
• Solution:
SETB P1.7 ;make P1.7 an input
AGAIN: MOV C,P1.7 ;read SW status into carry flag (C)
JC OVER ;jump if SW=1
MOV P2,#’N’ ;SW=0, issue ‘N’ to P2
SJMP AGAIN ;keep monitoring
OVER: MOV P2,#’Y’ ;SW=1, issue ‘Y’ to P2
SJMP AGAIN ;keep monitoring
Timers-Counters in 8051 µC
• In Intel 8051, there are two 16-bit timer registers. These registers are known as Timer-0
and Timer-1.
• The same timer registers can be used as Timer as well as Counter.
• The only difference between timer and counter operation is the source for incrementing
the timer registers.
• Both Timer 0 and Timer 1 are 16 bits wide. Since 8051 has an 8-bit architecture, each
16-bits timer is accessed as two separate registers of low byte (TLx) and high byte
(THx). These are accommodated in SFR memory area as shown here.
• Timer/counter registers can be Accessed as follows,
MOV TL0,#4FH
MOV R5,TH0

• TCON and TMOD registers are used for timer/counter control and mode selection.

Timer/Counter-0 looks like this


as a 16-bit register.
Timers-Counters in 8051 µC
• The only difference between timer and counter operation is the source for incrementing the timer registers.
Timer operation
• In the timer mode, the internal machine cycles are counted. So the timer is increments in
each machine cycle.
• Which means, the timer operating frequency is equal to machine cycle frequency i.e.
(1/12) of XTAL (clock) frequency.
Counter operation
• In the counter mode, the external events are counted. Here, the timer/counter register
will be incremented for each 1 to 0 transition of the external input pin T0 (for counter-0)
and T1 (for counter-1). These type of transitions are treated as events.
• The external input pins are sampled once in each machine cycle, and to determine the
1or 0 transitions, another machine cycle will be needed.
• So in counter mode, at least two machine cycles are needed to increment the counter.
Hence the maximum operating frequency of counter is equal to (1/24) of XTAL (clock)
frequency.
• The maximum frequency of counter can be varied with XTAL (Crystal oscillator)
frequency.
Timers-Counters in 8051 µC – Format of TCON register
• TCON (Timer Control) is an SFR. The address of this register is 88H. This is bit-addressable.
Timers-Counters in 8051 µC – Format of TMOD register
• TMOD (Timer Mode) is an SFR. The address of this register is 89H. This is not bit-addressable.

Bit Details High Value (1) Low Value (0)

C/T’ Configure for the Counter operations Configure for the Timer operations

Gate (G) Timer0 or Timer1 will be in RunMode when Timer0 or Timer1 will be in RunMode when
TRX bit of TCON register is high. TRX bit of TCON register is high
and INT0 or INT1 is high.

M1 M0 00 (Mode-0) 01 (Mode-1) 10 (Mode-2) 11 (Mode-3)

8-bit timer/counter, 16-bit 8-bit auto reload timer/counter; Split mode, Timer split
with 5-bit pre-scaler. timer/counter THx holds a value which is to be in to two separate 8-bit
(Total 13-bits in use) reloaded into TLx each time it timers.
overflows.
Timers-Counters in 8051 µC – Generating delay using Timers
DELAY generated by timer=(Max.value –Initial value+1)*(Time period of one machine cycle)
• max. value = maximum count value a timer/counter can reach (based on selected
mode in TMOD register)
• Initial value= the value we need to load into timer to the get the required delay.
• +1 is required in the above equation as timer increments one more time (to roll over to
Zero) after reaching its maximum value. After rollover, the timer will stop and TF
(timer overflow) flog in TCON (Timer control) register is Set. For example, see below.

NOTE: 8051 timers works at a rate of 1/12 of the CLK (XTAL) frequency (equal
to machine cycle frequency).
• If XTAL frequency is 11.0592MHz, then the delay required/generated for each
count (pulse) is as follows,
(11.0592MHz/12)=926kHz=1.085µsec
Generating delay using Timers- Example program-1
Find the delay generated by timer 0 in the following code. Do not include the overhead due to
instructions.
ORG 0H
Loading TMOS
for mode CLR P2.3 ;Clear P2.3
selection MOV TMOD,#01 ;Timer 0, 16-bit mode (mode-1) is selected
Loading initial BACK: MOV TL0,#3EH ;TL0=0, the low byte By default, operating
value. MOV TH0,#0B8H ;TH0=0, the high byte frequency of the timer is
CPL P2.3 ;Complement P2.3 same as machine cycle
Start timer-0
SETB TR0 ;Start timer 0 frequency as it is
Wait till timer-0 AGAIN: JNB TF0,AGAIN ;Monitor TF0 counting the machine
stops (overflow) cycles to generate delay.
CLR TR0 ;Stop the timer 0 So, the delay generated
Clear TR and TF CLR TF0 ;Clear TF0 flag by the timer for each tick
as the timer
created required
SJMP BACK is equal to the time period
delay. END of the machine cycle.
Solution: Assume, XTAL frequency is 11.0592MHz. Then, time period of one machine cycle is 1.085µs.
(FFFFH – B83E + 1) = 47C2H (18370 in decimal)
18370 × 1.085 µs = 19.93145 ms
Generating delay using Timers- Example program-2
In the following program, Calculate the frequency of the square wave generated on pin P1.5. Assume XTAL
frequency is 22MHz. Also consider the delay caused by instruction overhead.
Machine Cycles Since it is asked to consider the instruction
MOV TMOD,#01H overhead (time delay caused by the execution
HERE: MOV TL0,#0F2H 2 of the instructions), we need to add this delay
MOV TH0,#0FFH 2 with timer generated delay for frequency
CPL P1.5 1 calculation.
ACALL DELAY 2 By default, operating
SJMP HERE 2 frequency of the timer is same
DELAY: Timer runs for 14 steps as machine cycle frequency as
SETB TR0 1 to reach overflow/roll it is counting the machine
AGAIN: JNB TF0,AGAIN 14 over, as it’s initial value cycles to generate delay. So,
is FFF2H.
CLR TR0 1 the delay generated by the
Max. FFFFH + 1 for roll
CLR TF0 1 over
timer for each tick is equal to
RET 2 the time period of the machine
Total= 28 machine cycles. cycle.
Solution
To get a more accurate timing, we need to add the clock cycles due to the other instructions in the
loop (along with the clock cycles taken by timer).
Note: XTAL frequency is given as 22MHz to calculate the time period of one machine cycle.
Generating delay using Timers- Example program-2
Solution:
To get a more accurate timing, we need to add the clock cycles due to the other instructions in
the loop (along with the clock cycles taken by timer).
Note: XTAL frequency is given as 22MHz to calculate the time period of one machine cycle.
T = 2 × 28 × 0.545 µs = 30.52 µs and f = 32.765 kHz

DELAY generated by timer=(Max.value –Initial value+1)*(Time period of one machine cycle)


Generating delay using Timers- Example program-3
Assume that XTAL = 12 MHz, write a program to generate a square wave of 2 kHz frequency
on pin P1.5. Use timer-1 in appropriate mode.

DELAY generated by timer=(Max.value –Initial value+1)*(Time period of one machine cycle)


Generating delay using Timers- Example program-3
Solution:
Except that we must toggle the bit to generate the square wave. Look at the following steps.
(a) T = 1 / f = 1 / 2 kHz = 500 us ;the period of square wave.
(b) 1 / 2 of it is the high and low portion of the square wave (one cycle) which is 250 us.
(c) Select mode-1 for getting maximum possible delay. FFFFH = 65535 in decimal.
(c) 250 us / 1 us = 250 and 65535 – 250 +1= 65286 which is equal to FF06H in hexa.
(d) TL1 = 06H and TH1 = FFH, The program is as follow.

MOV TMOD,#10H ;Timer 1, 16-bit mode


AGAIN: MOV TL1,#06H ;TL1=06, load lower byte of timer
MOV TH1,#0FFH ;TH1=FF, load the higher byte of timer
SETB TR1 ;Start timer-1
BACK: JNB TF1,BACK ;wait until timer rolls over
CLR TR1 ;Stop the timer 1
CPL P1.5 ;COMPLEMENT P1.5
CLR TF1 ;Clear timer 1 flag
SJMP AGAIN ;Reload timer
Generating delay using Timers- Example program-4 (mode-2 auto reload)
Assume XTAL = 11.0592 MHz, find the frequency of the square wave generated on pin P1.0 in
the following program.
MOV TMOD,#20H ;T1/8-bit/auto reload
MOV TH1,#05H ;TH1= 5 (will be auto reloaded in to TL1 and TL1 works as timer)
SETB TR1 ;start the timer 1
BACK: JNB TF1,BACK ;till timer rolls over
CPL P1.0 ; complement P1.0
CLR TF1 ;clear TF1 flag
SJMP BACK ;Jump back and auto-reload initial value in to TL1
Solution:
First, notice the point to which we are going back from SJMP. We are not loading initial
value again. In mode 2 we do not need to reload TH with initial value always since it is auto-
reload. In auto reload mode, timer works with 8-bit capacity. So, maximum value is FFH (255 in decimal).
Now, (255-5+1) × 1.085 µs =251 × 1.085 µs = 272.33 µs. This is the delay for one iteration
which means for ON or OFF of square wave. Since The time period (T) of square wave is twice
of that; as a result T = 2 × 272.33 us = 544.67 µs and the frequency =1.83597 kHz
Generating delay using Timers- Example program-5 (mode-2 auto reload)
Find (a) the frequency of the square wave generated in the following code, and (b) the duty
cycle of this wave.
MOV TMOD,#2H ;Timer 0, mode 2 (8-bit, auto-reload)
MOV TH0,#-150 ;TH0 = 6AH = 2's Complement of -150
AGAIN: SETB P1.3 ; Pl.3 = 1
ACALL DELAY ; Delay called twice
In auto reload mode, timer works
ACALL DELAY with 8-bit capacity. So, maximum
CLR P1.3 ; Pl.3=0 value is FFH (255 in decimal).
ACALL DELAY ; Delay called once
SJMP AGAIN Since it is not asked to consider
DELAY: the instruction overhead (time
SETB TR0 ;start Timer 0 delay caused by the execution of
the instructions), we can ignore it
BACK : JNB TF0,BACK ;stay until timer rolls over for frequency calculation.
CLR TF0 ;clear TF for next round Consider only timer generated
RET delay.
Generating delay using Timers- Example program-5 (mode-2 auto reload)
Solution:
Here, a negative value is loaded in to TH as an initial value. This negative number will be
converted in to its 2’s complement form by the assembler. So, the 2,s complement of the -150
i.e. 6A will be loaded in to TH which will be auto loaded in to TL (mode-2).
150 (decimal)= 96H. Since -150 is given, find the 2,s complement for 96H as shown below.
96H= 1001 0110
0110 1001 1’s complement
+1
6AH=0110 1010 2’s complement representation of -150. So, 6AH will be loaded in to TH.
Duty cycle calculation:
Here, the delay is called twice for ON period (SETB P1.3). Hence the logic high level of the
square wave at P1.3 is twice that of the low portion. Which means ~66% duty cycle.
Frequency calculation:
Delay= (FF-6A+1)x1.085µs=162.75µs. Since, delay is called twice during ON period,
TON=325.5µs. Delay is called once for OFF period, TOFF=162.75µs.
Ttotal=325.5µs+162.75µs=488.25µs.
Frequency = (1/T )=1/488.25µs = 2.048 kHz.
Programming with Counters- Example program
Assuming that external clock pulses are fed into pin T1, write a program for counter 1 in mode
2 to count the pulses and display the TL1 count on P2, which connects to 8 LEDs.
Solution:
MOV TMOD,#01100000B ;counter 1, mode 2 (8-bit auto reload.); C/T’=1
MOV TH1,#0 ;clear TH1 (will be auto reloaded in to TL1 and TL1 starts counting)
SETB P3.5 ;make T1 pin as input
AGAIN: SETB TR1 ;start the counter
BACK: MOV A,TL1 ;get copy of TL
MOV P2,A ;display it on port 2
JNB TF1, BACK ;keep doing, if TF ≠ 1 (no over flow/roll over)
CLR TR1 ;stop the counter 1
CLR TF1 ;make TF=0
SJMP AGAIN ;keep doing it
Notice in the above program, the role of the instruction SETB P3.5. We make T1 pin (pin P3.5)
an input port by making it high.
In auto reload mode, counter works with 8-bit capacity. So, maximum value is FFH (255 in decimal).
SERIAL COMMUNICATION in 8051

Types of serial communication based on direction of data transfer.


SERIAL COMMUNICATION in 8051
Serial data communication uses two methods
• Synchronous method transfers a block of data at a time
• Asynchronous method transfers a single byte at a time
There are special IC chips made by many manufacturers for serial communications.
ƒ UART (universal asynchronous Receiver-transmitter)
ƒ USRT (universal synchronous Receiver-transmitter)
• 8051 supports asynchronous serial communication since it is provided with on-chip UART.
• There are two 8051 pins namely TXD (P3.1) and RXD (P3.0), used for transmitting and
receiving serial data bits.
• There are two dedicated registers available in 8051 for serial communication. They are
Serial Buffer register (SBUF) and Serial Control register (SCON). Both are accommodated
in SFR memory area.
• SBUF is a shift register which is used to store the received data bits in serial
communication. Same register can be used to hold the data bets to transmit them serially.
• SCON register is used to control the serial communication. E will see the format of SCON
register in coming slides.
SERIAL COMMUNICATION in 8051-Asynchronous-
data frame
SERIAL COMMUNICATION in 8051-Asynchronous-
Baud rate

• Baud rate defines the rate at which serial communication is happening.


• Baud rate means number of characters received/transmitted per second.
SERIAL COMMUNICATION in 8051-Asynchronous-
How to select required Baud rate
The list of supported baud rates by 8051 microcontroller is given bellow. Default frequency of
serial communication.
Baud rate (upon RESET)
9600
4800
2400 Timer-1 Mode-2
to set the Baud
1200 rate
• These baud rates can be derived from the default serial communication frequency of 8051.
• The default serial communication frequency is the (1/32) of machine cycle frequency (which
is 1/12 of XTAL frequency).
• The default serial communication frequency (28800 Hz here) will be divided with a factor to
get the required baud rate. The 2’s complement of division factor will be loaded in to Timer-1
in Mode-2. For the considered XTAL frequency of 11.0592MHz, default frequency is 28800 Hz.
Baud rate 9600 4800 2400 1200
Division factor 3 (28800/3=9600) 6 (28800/6=4800) 12 (28800/12=2400) 24 (28800/24=1200)
SERIAL COMMUNICATION in 8051-Asynchronous –
Baud rate selection with Timer-1 Mode-2
• The bellow table shows the 2’s complement representations of division factors for
respective baud rates.
• The 2’s complement values will be loaded in to TH1 of Timer-1 since it is in Mode-2 (8-
bit auto reload mode.)
• Then the timer divides the default serial communication frequency with loaded factor and
generate the required baud rate.
For the considered XTAL frequency of 11.0592MHz, default serial communication frequency is 28800 Hz.
• In Mode-2. higher byte of timer will be loaded with initial value, which will be then auto
loaded in to TL1 (lower byte). Now, TL1 start acting as the timer.
SERIAL COMMUNICATION in 8051-Asynchronous-
Baud rate selection with Timer-1 Mode-2
Why loading 2’s
complement: For
example, when
timer is loaded with
2’s complement of
the factor 3 i.e. FD
H, it will take
exactly 3 steps to
reach overflow (roll
over). After
overflow, timer will
divide the default
frequency i.e. 28800
with 3 to get 9600
baud.
SERIAL COMMUNICATION in 8051- SCON Register format
Serial Control (SCON) Register
SM0 SM1
1 0 Mode-2
This mode send/receive data between
multiprocessors. (environment where
communication between multiple
8051 processors is required.)
Mode-0 and 3 are used to choose
frame length with 9-data bits (old
method).
SERIAL COMMUNICATION in 8051-Asynchronous –
Serial Transmission -example program 1
Write a program for the 8051 to transfer letter ‘A’ serially at 4800 baud, continuously.
{2’s complement of -6 that is FA H will be loaded in to TH1 as a
Solution: division factor to get 4800 baud rate (28800/6=4800).}
MOV TMOD,#20H ;timer 1,mode 2(auto reload)
MOV TH1,#-6 ;4800 baud rate
CLR P3.1 ; making TXD pin as output port
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
From FA H, timer will
AGAIN: MOV SBUF,#'A' ;letter ‘A’ to transfer
take 6 steps to reach
HERE: JNB TI,HERE ;wait for the last bit
overflow (roll over).
CLR TI ;clear TI for sending next byte
Then it will divide the
SJMP AGAIN ;keep sending A
default value i.e. 28800
END
with 6 to get 4800 baud.
SERIAL COMMUNICATION in 8051-Asynchronous –
Serial Transmission -example program 2
Write a program for the 8051 to transfer “YES” serially at 9600 baud, 8-bit data, 1 stop bit, do this continuously
{2’s complement of -3 that is FD H will be loaded in to TH1 as a division factor to get
Solution: 9600 baud rate (28800/3=9600).}
MOV TMOD,#20H ;timer 1,mode 2(auto reload) • Since SBUF is an 8-bit register, it
MOV TH1,#-3 ;9600 baud rate can hold one character (8-bit
MOV SCON,#50H ;Mode-1 (8-bit, 1 stop), REN enabled ASCII) at a time. So, SBUF need
SETB TR1 ;start timer 1 to be loaded with first character
AGAIN: MOV A,#'Y' ;transfer 'Y' and reload with another character
ACALL TRANS after sending previous character.
MOV A,#'E' ;transfer 'E' • From FD H, timer will take 3
ACALL TRANS steps to reach overflow (roll
MOV A,#'S' ;transfer 'S' over). Then it will divide the
ACALL TRANS default value i.e. 28800 with 3 to
SJMP AGAIN ;keep doing it get 9600 baud.
;serial data transfer subroutine (sub program, will be called from the main program when ever required)
TRANS: MOV SBUF,A ;load SBUF
HERE: JNB TI,HERE ;wait for the last bit transmission (transmit interrupt TI=1)
CLR TI ;get ready for sending next byte after clearing TI
RET
SERIAL COMMUNICATION in 8051-Asynchronous – Serial Transmission -example program 3
Write an 8051 assembly program to transfer data serially at baud rate 9600 with 8 bit data, one stop bit and observe
the transmitted data in the serial window of the simulator.
Solution: 2’s complement of -3 that is FD H will be loaded in to TH1 as a division factor to get 9600
ORG 0000H baud rate (28800/3=9600). From FD H, timer will take 3 steps to reach overflow (roll over).
Then it will divide the default value i.e. 28800 with 3 to get 9600 baud.
XX: MOV DPTR, #MYDATA
MOV TMOD, #20H • In this program, a string “VIT UNIVERSITY” is saved in the ROM (code memory).
MOV TH1, #-3 • MYDATA is the label which represents the starting address of the string where it is saved in the
memory.
MOV SCON, #50H • Since each character in the string will be saved as its equivalent ASCII code (8-bits), each character
SETB TR1 occupies one memory location after saving it in the memory. So, each memory location and hence
MOV R1, #14 each character have an unique memory address.
AGAIN: CLR A • This address need to be copied in to DPTR register first. Then the content (character) in the memory
location whose address is pointed by DPTR will be passed in to Accumulator (A) register. Then
MOVC A,@A+DPTR from A-register, it will be sent to SBUF for serial transmission.
MOV SBUF, A • We have to follow the same process for all characters to sent. After sending each character, Transmit
HERE: JNB TI, HERE interrupt (TI) bit of SCON register will be set which indicates the successful transmetion of one
CLR TI character.
• A counter (R1-register) which represents (loaded with) string length will be decremented after
INC DPTR sending each character of the string..
DJNZ R1, AGAIN • SBUF need to be loaded with first character and reload with another character after sending
SJMP XX previous character. (SBUF is an 8-bit register, it can hold one character (8-bit ASCII) at a time.)
;-----string saved in the memory-----
MYDATA: DB 'VIT UNIVERSITY'
END
SERIAL COMMUNICATION in 8051-Asynchronous –
Serial Reception -example program 4
Write a program for the 8051 to receive bytes of data serially, and put them in P1, set the baud
rate at 4800, 8-bit data, and 1 stop bit
Solution: {2’s complement of -6 that is FA H will be loaded in to TH1 as a division
factor to get 4800 baud rate (28800/6=4800).}
MOV TMOD,#20H ;timer 1,mode 2(auto reload) From FA H, timer will take 6 steps to
MOV TH1,#-6 ;4800 baud rate reach overflow (roll over). Then it will
MOV SCON,#50H ;8-bit, 1 stop, REN enabled divide the default value i.e. 28800 with
SETB TR1 ;start timer 1 6 to get 4800 baud.
HERE: JNB RI,HERE ;wait for the last bit to receive (receive interrupt RI=1)
MOV A,SBUF ;saving incoming byte in A
MOV P1,A ;send to port 1
CLR RI ;get ready to receive next byte after clearing RI
SJMP HERE ;go back and receive the next byte.
Power control register (PCON) of 8051
SMOD X X X GF1 GF0 PD IDL

Unused bits
PCON is an 8-bit special function register (SFR) accommodated in SFR memory area with an
address 87H. It is not bit addressable.
SMOD
This bit if set (1), it will double the baud rate of serial communication.
GF0 and GF1: These are general purpose flag bits.
Idle mode
• When IDL bit of the PCON register is set, the microcontroller turns off the CPU unit while
peripheral units such as serial port, timers and interrupt system continue operating
normally.
• In Idle mode, the state of all registers and I/O ports remains unchanged.
• To exit the Idle mode, it is necessary to enable and execute any interrupt or reset.
• It is recommended that, first three instructions to be executed after coming out of Idle mode
are NOP instructions to stabilize the microcontroller and prevents undesired changes on the
Power control register (PCON) of 8051

Power down mode


• By setting the PD bit of the PCON register within the program, the microcontroller enter in
to Power down mode.
• Internal oscillator will be turned off and reduces power consumption.
• The microcontroller can operate using only 2V power supply in power- down mode.
• The only way to get the micro controller back to normal mode is by reset.
• While the microcontroller is in Power Down mode, the state of all SFR registers and I/O
ports remains unchanged.
• By setting it back into the normal mode, the contents of the SFR register is lost, but the
content of internal RAM is saved.
• Reset signal must be long enough, approximately 10ms, to enable stable operation of the
crystal oscillator.
Interrupts in 8051
An interrupt is an external or internal event that interrupts the microcontroller to inform it that a
device needs its service

A microcontroller can serve several devices by two ways


• Interrupts
 Whenever any device (peripheral) 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.
• Polling (old approach/outdated)
 The microcontroller continuously monitors the status of a connected device (peripheral).
 When the conditions met, it performs the service.
 After that, it moves on to monitor the next device until every one is serviced.
 Due to this, significant amount of microprocessor’ time will be wasted since the
microprocessor is visiting all the peripherals with out knowing the requirement of peripheral.
Types of Interrupts in 8051

Six interrupts are allocated in 8051, as follows;


• Reset : power-up reset
• Two interrupts are set aside for the timers: one for timer-0 overflow (TF0) and one more for
timer-1 overflow (TF1).
• Two interrupts are set aside for hardware external interrupts
Pins P3.2 and P3.3 have dual functionality for the external hardware interrupts INT0 (EX0)
and INT1 (EX1).
• Serial communication has a single interrupt that belongs to both receive (receive
interrupt (RI)) and transfer (transmit interrupt (TI)).
Enable/disable Interrupts in 8051
• Upon reset, all interrupts are disabled (masked), meaning that none will be responded to
by the microcontroller if they are invoked.
• There is a register called IE (interrupt enable) that is responsible for enabling (unmasking)
and disabling (masking) the interrupts.
• IE register is accommodated in SFR memory area.
D7 D0
EA -- ET2 ES ET1 EX1 ET0 EX0

• If EA = 1, interrupts are enabled and will be responded to, if their corresponding bits in IE
register are high;
• If EA = 0, no interrupt will be responded to, even if the associated bit in the IE register is
high.
Enable/disable Interrupts in 8051
• Pin 12 (P3.2) and pin 13 (P3.3) of the 8051, designated as INT0 and INT1, are used as
external hardware interrupts
• There are two activation levels for the external hardware interrupts (INT0 and INT1).
Level trigged - In the level-triggered mode, INT0 and INT1 pins are normally high. So,
If a low-level signal is applied to them, it triggers the interrupt.
Edge trigged – Set IE0/IE1 bit (TCON.1/TCON.3) to enable edge triggering for
INT0/INT1
8051-Interrupt Priority Upon Reset
8051- Modifying the default Interrupt Priority
Interrupt Priority (IP) Register (Bit-addressable)
D7 D0
-- -- PT2 PS PT1 PX1 PT0 PX0

• Priority bit=1 assigns high priority


• Priority bit=0 assigns low priority

• In the 8051, a low-priority interrupt can be interrupted by a higher-priority Interrupt.


• But it prohibits the interruption of higher-priority Interrupt by using another low-priority
interrupt.
Interrupt service routine - Interrupt Vector Table
• interrupt service routines (ISR) or interrupt handler is a pre-written program which defines
the service for an interrupt request.
• Each interrupt request have its own ISR. All these ISRs will be stored in the memory and
the addresses of the starting memory locations of these ISRs will be updated in to interrupt
vector table.

• Interrupt vector table is an index of memory addresses where, the starting address of the
interrupt service routines (ISRs) are saved.
• When an interrupt request comes, microcontroller search the interrupt vector table to find
the address of the starting memory location of the required ISR.
• So that, corresponding ISR will be executed to serve the interrupt request.
Interrupt Vector Table of 8051 µC
Programming using Interrupts-Example1

Discuss what happens if interrupts INT0, TF0, and INT1 are activated at the same time.
Assume priority levels were set by the power-up reset and the external hardware interrupts
are edge triggered.
Solution:
priority levels were set by the power-up reset means default priority levels.
If these three interrupts are activated at the same time, their statuses are saved internally.
Then the 8051 checks all five interrupts according to the sequence listed in the default
priority order. If any is activated, it services it in sequence.
Therefore, when the above three interrupts are activated, INT0 (external interrupt- IE0) is
serviced first, then timer 0 (TF0), and finally INT1 (external interrupt- E1).
Programming using Interrupts-Example2
(a) Program the IP register to assign the highest priority to INT1(external interrupt 1), then
(b) discuss what happens if INT0, INT1, and TF0 are activated at the same time. Assume the
interrupts are edge-triggered.
Solution:
(a) MOV IP,#00000100B ;IP.2=1 assign INT1 higher priority. The instruction SETB IP.2 also
will do the same thing as the above line since IP is bit-addressable.
(b) The instruction in Step (a) assigned a higher priority to INT1 than the others; therefore,
when INT0, INT1, and TF0 interrupts are activated at the same time, the 8051 services INT1
first, then it services INT0, then TF0.
The instruction in Step (a) makes both the INT0 and TF0 bits in the IP register 0. As a result,
the sequence in default priority order is followed which gives a higher priority to INT0 over
TF0.
Programming using Interrupts-Example3
Assume that after reset, the interrupt priority is set by the instruction MOV IP,#00001100B.
Discuss the sequence in which the interrupts are serviced.
Solution:
The instruction “MOV IP #00001100B” (B is for binary) assigned INT1 and timer 1 (TF1) to a
higher priority level compared with the rest of the interrupts. However, INT1 and timer 1 (TF1)
both are set, they are prioritized as per the default priority order (INT1 gets highest priority and
TF1 gets second priority). The priorities of the remaining interrupts are rearranged by
following the default priority order.
Finally, the updated priorities are as follows.
External Interrupt 1 (INT1) : Highest Priority
Timer Interrupt 1 (TF1) : Second Priority
External Interrupt 0 (INT0) : rearranged as per default.
Timer Interrupt 0 (TF0) : rearranged as per default.
Lowest Priority Serial Communication (RI+TI) : rearranged as per default.
D7 D0
Interrupt priority (IP) register: -- -- PT2 PS PT1 PX1 PT0 PX0
Level triggering/Edge triggering of External Interrupts-
INT0 and INT1
Low-level triggering:
• On RESET, IT0 (TCON.0) and IT1 (TCON.2) are both low, making external
interrupts (INT0 and INT1) low-level triggered by default.
Minimum Duration of the low level required to consider the Low Level-Triggered
Interrupt is 4-machine cycles. (1 machine cycle=1.085µs if XTAL = 11.0592 MHz)

(if XTAL = 11.0592 MHz)


Level triggering/Edge triggering of External Interrupts-
INT0 and INT1
Falling-edge triggering:
• Can be enabled by setting IE0/IE1 (TCON.1/TCON.3); making external
interrupts (INT0/INT1) falling-edge triggered.

Falling edge (negative edge)


Programming using Interrupts-Example4
Assuming that pin 3.3 (INT1) is connected to a pulse generator, write a program in which the
falling edge of the pulse will send a high to P1.3, which is connected to an LED (or buzzer). In
other words, the LED is turned on and off at the same rate as the pulses are applied to the INT1 pin.
Solution: IE register format
ORG 0000H EA -- ET2 ES ET1 EX1 ET0 EX0 Interrupt vector table
LJMP MAIN
;--ISR for hardware interrupt INT1 to turn on the LED
ORG 0013H ;INT1 ISR vector address
SETB P1.3 ;turn on the LED
MOV R3,#55
BACK:DJNZ R3,BACK ;keep the LED on for a while
CLR P1.3 ;turn off the LED
RETI ;return from ISR to main program
;--MAIN program for initialization
ORG 30H
MAIN: SETB TCON.3 ;make INT1 edge-trigger interrupt using TCON register
MOV IE,#10000100B ;enable External INT1 using IE register
HERE: SJMP HERE ;stay here until interrupted
Programming using Interrupts-Example5 page1
Write a program that continuously gets 8-bit data from P0 and sends it to P1 while simultaneously
creating a square wave of 200 µs period on pin P2.1. Use Timer 0 to create the square wave.
Assume that XTAL = 11.0592 MHz.
Solution:
;—-Upon wake-up go to main program.
ORG 0000H
LJMP MAIN ;bypass interrupt vector table
;—-ISR for Timer 0 to generate square wave
ORG 000BH ;Timer 0 overflow interrupt vector address Interrupt vector table
CPL P2.1 ;toggle P2.1 pin
RETI ;return from ISR to main program
;—-The main program for initialization IE register format
ORG 0030H EA -- ET2 ES ET1 EX1 ET0 EX0
MAIN: MOV TMOD,#02H ;Timer 0, mode 2(auto-reload)
MOV P0,#0FFH ;make P0 an input port
MOV TH0,#-92 ;TH0=6EH for -92. (it will generate 100µs delay)
MOV IE,#82H ;IE=10000010 (binary) enable Timer 0 interrupt
SETB TR0 ;Start Timer 0
Programming using Interrupts-Example5 page2
Write a program that continuously gets 8-bit data from P0 and sends it to P1 while simultaneously
creating a square wave of 200 µs period on pin P2.1. Use Timer 0 to create the square wave.
Assume that XTAL = 11.0592 MHz.
Solution:
BACK: MOV A,P0 ;get data from P0
MOV P1,#00H ; make P1 as output port
MOV P1,A ;issue it to P1
SJMP BACK ;keep doing it; loop unless interrupted by TF0
END
Programming using Interrupts-Example6 Page1
Write a program using interrupts to do the following:
(a) Receive data serially and sent it to P0,
(b) Have P1 port read and transmitted serially, and a copy given to P2, Interrupt vector table
(c) Make timer 0 generate a square wave of 5kHz frequency on P0.1.
Assume that XTAL 11.0592MHz. Set the baud rate at 4800.
Solution: IE register format
ORG 0 EA -- ET2 ES ET1 EX1 ET0 EX0
LJMP MAIN
ORG 000BH ;ISR vector address for Timer-0 overflow Interrupt.
CPL P0.1 ;toggle P0.1; ISR for Timer-0 overflow Interrupt, runs when TF0=1.
RETI ;return from ISR to main program
ORG 0023H ;ISR vector address for serial com. Interrupt as show in above table.
LJMP SERIAL ;jump to serial interrupt ISR
ORG 30H
MAIN: MOV IE,10010010B ;enable timer overflow for timer-0, serial (RI/TI) interrupts.
;-------solving part-b of the question--------continuation to above code----------
MOV P1,#0FFH ;make P1 an input port
Programming using Interrupts-Example6 Page2
Write a program using interrupts to do the following:
(a) Receive data serially and sent it to P0,
(b) Have P1 port read and transmitted serially, and a copy given to P2,
(c) Make timer 0 generate a square wave of 5kHz frequency on P0.1.
Assume that XTAL 11.0592MHz. Set the baud rate at 4800.
Solution:
MOV SCON,#50H ;8-bit data frame, 1 stop, REN enabled
MOV TMOD,#22H ;timer 0 and timer 1 in mode 2(auto reload)
MOV TH1,#-6H ;for 4800 baud rate, initial value for timer-1 mode-0
SETB TR1 ;start timer 1
BACK: MOV A,P1 ;read data from port 1
MOV SBUF,A ;give a copy to SBUF
MOV P2,A ;send it to P2
SJMP BACK ;stay in loop indefinitely
;-------solving part-c of the question--------continuation to above code----------
MOV TH0,#6EH ;initial value for delay of 100µs (1/200µs=5kHZ), already timer-0 in mode-2.
SETB TR0 ;start timer 0
Programming using Interrupts-Example6 page3
Write a program using interrupts to do the following:
(a) Receive data serially and sent it to P0,
(b) Have P1 port read and transmitted serially, and a copy given to P2,
(c) Make timer 0 generate a square wave of 5kHz frequency on P0.1.
Assume that XTAL 11.0592MHz. Set the baud rate at 4800.
Solution:
;-----SERIAL PORT ISR for solving part-a of the question------continuation to previous
code---;----this part of the code will execute when ever TI/RI interrupts come.
ORG 100H
SERIAL:JB TI,TRANS ;jump if TI is high
MOV A,SBUF ;otherwise due to receive
MOV P0,A ;send serial data to P0
CLR RI ;clear RI since CPU doesn’t
RETI ;return from ISR
TRANS: CLR TI ;clear TI since CPU doesn’t
RETI ;return from ISR
END

You might also like