Interrupts of microcontroller 8051

933 views 54 slides Jul 26, 2021
Slide 1
Slide 1 of 54
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54

About This Presentation

What is Interrupt
Introduction to 8051 Microcontroller Interrupts
Interrupts of 8051 Microcontroller
Interrupt Vs Polling
IE register
IP register
What happens when an interrupt occurs?
What happens when an interrupt Ends?
Programming Timer Interrupt
Serial Interrupt
External Hardware Interrupt
Examp...


Slide Content

Interrupts of 8051 Microcontroller
Dr. Nilesh Bhaskarrao Bahadure
https://www.sites.google.com/site/nileshbbahadure/home
July 25, 2021
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 1 / 54

Overview
1
What is Interrupt
Introduction to 8051 Microcontroller Interrupts
2
Interrupts of 8051 Microcontroller
3
Interrupt Vs Polling
4
IE register
5
IP register
6
What happens when an interrupt occurs?
7
What happens when an interrupt Ends?
8
Programming Timer Interrupt
9
Serial Interrupt
10
External Hardware Interrupt
Level Triggered Interrupt
Edge Triggered Interrupt
TCON Register
11
Examples
Example - 1: Example on IE
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 2 / 54

Overview
Example - 2: Example on IP
Example - 3: Example on IP
Example - 4: Generation of Square Wave Using Timer 0 Mode 2
Example - 5: Generation of Square Wave Using Timer 0 Mode 1
Example - 6: Generation of Square Wave of 50 Hz Using Timer 0
Example - 7: Transmission of bits serially using Interrupt
Example - 8: Reception of bits serially using Interrupt
Example - 9: LED ON using External Hardware Interrupt
Example - 10: LED ON using External Hardware Interrupt and Pulse
generator
Example - 11: Transfer - Receive & Square Wave using Interrupt
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 3 / 54

What is Interrupt
Interrupt is the routine program to break or stop the current execution and
branch to another program. Interrupt of microcontroller 8051 consists two
hardware interrupts as well as generated interrupts using timers and serial
communication.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 4 / 54

Introduction to 8051 Microcontroller Interrupts
Interrupts are signals that cause the Microcontroller to suspend what is doing
and transfer to a special program called an interrupt handler. The interrupt
handler is responsible for determining the cause of interrupt, servicing the
interrupt and returning the control to the point from where the interrupt
was caused.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 5 / 54

Introduction to 8051 Microcontroller Interrupts...
When Microcontroller executes any program then it is called Main pro-
gram. In between the main program, Microcontroller is interrupted then
Microcontroller will branch from main program to subprogram this subpro-
gram is called Interrupt service routine (ISR). After executing this ISR then
RETI/RET instruction comes then Microcontroller returns back from ISR
to the main program from where Microcontroller has left.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 6 / 54

Interrupts of 8051 Microcontroller
Question What events can trigger interrupts, and where do they go?
We can congure the 8051 so that any of the following events will cause
an interrupt:
1
Timer 0 Overow.
2
Timer 1 Overow.
3
Reception/Transmission of Serial Character.
4
External Event 0.
5
External Event 1.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 7 / 54

Interrupts of 8051 Microcontroller...
The six interrupts in the 8051 are allocated as follows:
1
RESET: When the reset pin is activated, the Microcontroller 8051
jumps to the address location0000h.This is also referred as power
on reset.
2
Timers: Two interrupts are set aside for the timers:Timer 0and
Timer 1. Memory location addresses000Bhand001Bhin the
interrupt vector table belongs to timer 0 and timer 1, respectively.
3
Hardware external interrupts: Two interrupts are set aside for
hardware external interrupts. Pin number12 (P3.2)and13 (P3.3)
are for the external hardware interrupts also referred asEX 1andEX
2. Memory location addresses0003hand0013hin the interrupt
vector table are assigned toINT0andINT1external hardware
interrupts respectively.
4
Serial communication: serial communication has a single interrupt
that belongs to both transmit and receive. The interrupt vector table
location0023hbelongs to the serial communication interrupt.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 8 / 54

Interrupts of 8051 Microcontroller...
Table :
Interrupt Flag Interrupt handler address/Vector location
External 0IE0 0003hTimer 0TF0 000BhExternal 1IE1 0013hTimer 1TF1 001BhSerialRI/TI 0023hMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 9 / 54

Interrupt Vs Polling
A single Microcontroller can serve several devices. There are two ways to
do that: Interrupts and polling.
Interrupts:In the interrupt method, whenever any device needs its service, the device
noties the Microcontroller by sending an interrupt signal, upon receiving
an interrupt signal, the Microcontroller interrupts whatever it is doing and
serve the device. The program associated with the interrupt is called the
interrupt service routine (ISR) or interrupt handler.
Polling:In polling, the Microcontroller continuously monitors the status of a several
devices, when the status condition is met, it perform the service, after that,
it moves on to monitor the next device until each one is serviced. Although
polling can monitors the status of several devices and serve each of them as
certain conditions are met, it is not an ecient use of Microcontroller.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 10 / 54

Interrupt Vs Polling...
Figure :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 11 / 54

Interrupt Vs Polling...
The advantage of the interrupts is that the Microcontroller can serve many
devices, not all at the same time; 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 the devices in a round robin fashion
as shown in the gure 1. Also in the interrupt method, the Microcontroller
can also ignore or mask or reset a device request for service. This is not
possible in polling.
The most important reason that the interrupt method is preferable over
the polling is that the polling method waste the time of Microcontroller in
order to search the devices one by one, so in order to avoid tying down the
Microcontroller, interrupts are used.
For example, the instructionLABEL: JNB TFx, LABEL, waited until the
timer rollover and while we were waiting we could not do anything else
i.e. a waste of this Microcontroller's time that could have been used to
perform some other useful tasks. In this case, the Microcontroller can go
about doing other tasks, and when the timer ag bit is raised, the timer will
interrupt the Microcontroller in whatever it is doing.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 12 / 54

IE register
D7 D6 D5 D4 D3 D2 D1 D0
EA { { ES ET1 EX1 ET0 EX0
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 13 / 54

IE register...
Table :
Bit Name Bit address Function
7EA AFh Global Interrupt Enable / Disable6{ AEh Undened (don't care)5{ ADh Undened (don't care)4ES ACh Enable serial communication interrupt3ET1 ABh Enable timer 1 interrupt2EX1 AAh Enable external 1 hardware interrupt1ET0 A9h Enable timer 0 interrupt0EX0 A8h Enable external 0 hardware interruptMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 14 / 54

IE register...
The schematic representations of the interrupt are as follows:
Figure :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 15 / 54

IP register
D7 D6 D5 D4 D3 D2 D1 D0
{ { PT2 PS PT1 PX1 PT0 PX0
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 16 / 54

IP Register...
Table :
Bit Name Bit address Function
7{ BFh Undened (don't care)6{ BEh Undened (don't care)5PT2 BDh Timer 2 interrupt priority (8052 only)4PS BCh Serial communication interrupt priority3PT1 BBh External 1 hardware interrupt priority2PX1 BAh Timer 1 interrupt priority1PT0 B9h External 0 hardware interrupt priority0PX0 B8h Timer 0 interrupt priorityMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 17 / 54

What happens when an interrupt occurs?
When an interrupt is triggered, the following actions are taken
automatically by the Microcontroller:
1
The current Program Counter is saved on the stack, low-byte rst.
2
Interrupts of the same and lower priority are blocked.
3
In the case of Timer and External interrupts, the corresponding
interrupt ag is set.
4
Program execution transfers to the corresponding interrupt handler
vector address.
5
The Interrupt Handler Routine executes.
Take special note of the third step: If the interrupt being handled is a
Timer or External interrupt, the Microcontroller automatically clears the
interrupt ag before passing control to your interrupt handler routine.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 18 / 54

What happens when an interrupt Ends?
An interrupt ends when your program executes the RETI instruction.
When the RETI instruction is executed the following actions are taken by
the Microcontroller::
1
Two bytes are popped o the stack into the Program Counter to
restore normal program execution.
2
Interrupt status is restored to its pre-interrupt status.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 19 / 54

Programming Timer Interrupt
Figure :
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 20 / 54

Programming Timer Interrupt...
We know that timer ag is raised when the timer rolls over i.e. overows
and the overow condition are checked by monitoring TF bit using the in-
struction \JNB TFx, Label". In polling method, we have to wait until the
timer ag bitTFis raised. The problem with this method is that the Micro-
controller is tied down while waiting forTFag bit to be raised and cannot
do anything else. Using interrupts this problem getting solve and avoids
tied down the Microcontroller. If the timer interrupt inIE (Interrupt
enable)register is enabled, whenever the timer rolls over, timer agTF
bit is raised automatically and Microcontroller is interrupted in whatever
it is doing, and jump to the interrupt vector table to service the interrupt
service routine (ISR) of the timer. In this way the Microcontroller can do
other things until it is notied that the timer has rolled over. Whenever the
timer ag bit is raised and if the corresponding bit of the timer interrupt ag
bit is set in theIEregister, Microcontroller automatically switch or jump
to the vector location of timers.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 21 / 54

Serial Interrupt of Microcontroller 8051
The working of Serial Interrupts is slightly dierent than the rest of the
interrupts. This is due to the fact that there are two interrupt ags:RI
andTIand only one interrupt ag bit is associated for both. If either ag
is set, a serial interrupt is triggered. As you will recall from the previous
section on the serial port, the RI bit is set when a byte is received by the
serial port and the TI bit is set when a byte has been sent. This means that
when your serial interrupt is executed, it may have been triggered because
the RI ag was set or because the TI ag was set or because both ags were
set. Thus, your routine must check the status of these ags to determine
what action is appropriate. Also, since the 8051 does not automatically
clear the RI and TI ags you must clear these bits in your interrupt handler.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 22 / 54

Serial Interrupt of Microcontroller 8051...
INTSERIAL:JNB RI, CHECKTI ; If the RI ag is not set, we jump to check TIMOV A, SBUF; If we got to this line, it's because the RI bit was setCLR RI ; Clear the RI bit after we've processed itCHECKTI:JNB TI, EXITINT ; If the TI ag is not set, we jump to the exit pointCLR TI ; Clear the TI bit before we send another characterMOV SBUF, #'A'; Send another character to the serial portEXITINT:RETIMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 23 / 54

Programming External Hardware Interrupt
There are only two external hardware interrupts in the 8051 Microcontroller:
INT0andINT1. They are located on pinP3.2andP3.3of port 3
respectively. The interrupt vector table locations0003hand0013hare set
aside forINT0and
IE register. There are two types of activation for the external hardware
interrupts, they are
1. Level triggered2. Edge triggered
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 24 / 54

Programming External Hardware Interrupt...
Figure :
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 25 / 54

Level Triggered Interrupt
In the level triggered mode,INT0andINT1pins are normally high and if
a low level signal (as they are low level active pins) is applied to them, it
triggers the interrupt. Upon receiving the interrupt signal, Microcontroller
stops whatever it is doing and jumps to the interrupt vector table to service
the interrupt. This is called level triggered or level activated interrupt and is
the default mode of activation upon reset of the 8051. The low level signal
at the 8051 pin must be removed before the execution of the last instruction
of interrupt service routine i.e. RETI, otherwise, another interrupt will be
generated.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 26 / 54

Edge Triggered Interrupt
Upon reset the 8051 makesINT0andINT1low level triggered interrupts.
To make them edge triggered interrupts, we must program the bits of the
TCON register. The TCON register consists of IT0 and IT1 ag bits that
determine level or edge triggered mode of the hardware interrupts. IT0 and
IT1 are bits D0 and D2 of the TCON register respectively. Since TCON
is a bit addressable register, these bits are also referred as TCON.0 and
TCON.2. Upon reset TCON.0 and TCON.2 are both 0s i.e. external hard-
ware interrupts ofINT0andINT1pins are low level triggered. By making
TCON.0 and TCON.2 bits high with instruction such asSETB TCON.0and
SETB TCON.2the external hardware interruptsINT0andINT1becomes
edge triggered interrupts.
For example by applying High to Low pulse on
INT1pin, thenINT1hardware interrupt becomes edge triggered hardware
interrupts.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 27 / 54

TCON SFR
D7 D6 D5 D4 D3 D2 D1 D0
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 28 / 54

TCON SFR...
Bits Name Function
3IE1 External hardware interrupts 1 edge ag. Set by CPU when the ex-
ternal interrupt edge (High to Low transition) is detected. Cleared
by CPU when the interrupt is processed
2IT1 Interrupt 1 type control bit. Set/Cleared by software to specify
falling edge (Edge triggered)/Level triggered external interrupts.
1IE0 External hardware interrupts 0 edge ag. Set by CPU when the ex-
ternal interrupt edge (High to Low transition) is detected. Cleared
by CPU when the interrupt is processed
0IT0 Interrupt 0 type control bit. Set/Cleared by software to specify
falling edge (Edge triggered) / Level triggered external interrupts.
(IT0 = 0, then low level triggered mode is set
IT0 = 1, then edge triggered mode is set)
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 29 / 54

Example - 1 Show the instruction to
(a) Enable serial interrupt, timer 0 interrupt and hardware external
interrupt 0.
(b) Disable timer 0 interrupt(c) Show how to disable all the interrupts with a single instruction.Solution
EA { ET2 ES ET1 EX1 ET0 EX0
1 0 0 1 0 0 0 1 93h
IE = 93hMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 30 / 54

Assembly Language Program
(a)
MOV IE, #93H
Since IE register is a bit addressable register, we can use the following
instructions to access individual bits of register
SETB IE.7; EA = 1, global enableSETB IE.4; enable serial interruptSETB IE.1; enable timer 0 interruptSETB IE.0; enable external hardware interrupt 0
(b)
CLR IE.1 ; disable timer 0 interrupt only
(c)
CLR IE.7 ; disable all interrupts
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 31 / 54

Example - 2 Program the interrupt priority (IP) register to perform the following(a) Assign the highest priority to timer 0 and show the new priority order(b) Discuss what happen if external hardware interrupts 0 and timer 0
interrupt are set at the same time.
Solution
{ { PT2 PS PT1 PX1 PT0 PX0
0 0 0 0 0 0 1 0 02h
IP = 02h
(a) MOV IP, 02H
Or
SETB IP.2Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 32 / 54

Solution
(a)
Old priority order New priority order
External hardware interrupt 0 (INT0)Timer 0 interrupt (TF0)Timer 0 interrupt (TF0)External hardware interrupt 0 (INT0)External hardware interrupt 1 (INT1)External hardware interrupt 1 (INT1)Timer 1 interrupt (TF1)Timer 1 interrupt (TF1)Serial communication interrupt (RI/TI)Serial communication interrupt (RI/TI)
(b) Timer 0 is assigned the highest priority so the order of the interrupts
are
New priority order
Timer 0 interrupt (TF0)External hardware interrupt 0 (INT0)External hardware interrupt 1 (INT1)Timer 1 interrupt (TF1)Serial communication interrupt (RI/TI)Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 33 / 54

Example - 3 Assume that after reset, the IP register is program with the instruction
MOV IP, #0Bh. Discuss the sequence in which the interrupts are serviced.
Solution
{ { PT2 PS PT1 PX1 PT0 PX0
0 0 0 0 1 0 1 1 0Bh
IP = 0Bh
This instruction sets external hardware interrupt 0 (INT0), timer 0 (TF0)
and timer 1 (TF1) to the higher priority level compared with rest of the
interrupts. So according to program bits old and new priority level of the
interrupts are shown below
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 34 / 54

Solution
Old priority order New priority order
External hardware interrupt 0 (INT0)External hardware interrupt 0 (INT0)Timer 0 interrupt (TF0)Timer 0 interrupt (TF0)External hardware interrupt 1 (INT1)Timer 1 interrupt (TF1)Timer 1 interrupt (TF1)External hardware interrupt 1 (INT1)Serial communication interrupt (RI/TI)Serial communication interrupt (RI/TI)Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 35 / 54

Example - 4 Write an assembly language program that continuously gets 8 - bit data
from port P0 and send to port P1 while simultaneously creating a square
wave of 250Speriod on port pin P2.1. Use timer 0 in mode 2 to create
the square wave, assume that crystal frequency of 8051 is 11.0592 MHz.
Solution Step - I:Calculation of count value in TH0 Register
TON=TOFF= 125sTH0 = 8DhStep - II:Calculation of TMOD RegisterTMOD= 02hStep - III:Calculation of IE RegisterIE= 82h
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 36 / 54

Assembly Language Program
ORG 0000HSJMP MAIN; bypass vector locationORG 0030HMAIN:MOV P0, #0FFH ; make port P0 as an input portMOV TMOD, #02H; Timer 0 in mode 2MOV TH0, #8DH; load TH0 with auto - reload valueMOV IE, #82H; enable timer 0SETB TR0; start timer 0BACK:MOV A, P0 ; get data from port P0 until TF0 raisedMOV P1, A; send data from P0 to port P1SJMP BACK; repeat the processORG 000BHCPL P2.1; interrupt routine of timer 0CLR TF0RETI ; return from interruptMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 37 / 54

Example - 5 Repeat example 4 using timer 0 in mode 1.Solution Step - I:Calculation of count value in TH0 - TL0 Register
TON=TOFF= 125sTH0 = 8DhTL0 =FFhStep - II:Calculation of TMOD RegisterTMOD= 01hStep - III:Calculation of IE RegisterIE= 82h
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 38 / 54

Assembly Language Program
ORG 0000HSJMP MAIN; bypass vector locationORG 0030HMAIN:MOV P0, #0FFH ; make port P0 as an input portMOV TMOD, #01H; Timer 0 in mode 1MOV TH0, #0FFH; Load timer 0 higher byteMOV TL0, #8DH; load timer 0 lower byteMOV IE, #82H; enable timer 0SETB TR0; start timer 0BACK:MOV A, P0 ; get data from port P0 until TF0 raisedMOV P1, A; send data from P0 to port P1SJMP BACK; repeat the processORG 000BHCPL P2.1 ; interrupt routine of timer 0MOV TH0, #0FFHMOV TL0, #0A4HCLR TR0CLR TF0RETI ; return from interruptMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 39 / 54

Example - 6 Write an assembly language program to generate square wave of 50 Hz
frequency on port pin P1.2 using interrupt for timer 0. Assume crystal
frequency of 8051 is 11.0592 MHz.
Solution Step - I:Calculation of count value in TH0 - TL0 Register
TON=TOFF= 0:01sTH0 =DChTL0 = 00hStep - II:Calculation of TMOD RegisterTMOD= 01hStep - III:Calculation of IE RegisterIE= 82h
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 40 / 54

Assembly Language Program
ORG 0000HSJMP MAIN; bypass vector locationORG 0030HMAIN:MOV TMOD, #01H; Timer 0 in mode 1MOV TH0, #0DCH; Load timer 0 higher byteMOV TL0, #00H; load timer 0 lower byteMOV IE, #82H; enable timer 0SETB TR0; start timer 0BACK:SJMP BACK ; repeat the processORG 000BHCPL P2.1 ; interrupt routine of timer 0MOV TH0, #0DCHMOV TL0, #00HCLR TR0CLR TF0RETI ; return from interruptMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 41 / 54

Example - 7 Write an assembly language program to get data from port P1 and sends
to port P2 continuously, while giving a copy to the serial port to be
transferred serially. Assume that crystal frequency of 8051 is 11.0592 MHz
and baud rate for the serial communication is 9600.
Solution Step - I:Calculation of SCON Register
SCON= 50hStep - II:Calculation of TMOD Register (Timer 1 in Mode 2
for Baud Rate)
TMOD= 20hStep - III:Calculation of TH1 Register for Baud RateTH1 =FDhStep - IV:Calculation of IE RegisterIE= 90h
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 42 / 54

Assembly Language Program
ORG 0000HSJMP MAINORG 0030HMAIN:MOV P1, #0FFH ; make port P1 as an input portMOV TMOD, #20H; timer 1 in mode 2 for baud rateMOV TH1, #0FDH; to set baud rate 9600MOV SCON, #50H; serial mode 1MOV IE, #90H; enable serial communication interruptSETB TR1; start timer 1 for baud rateL1: MOV A, P1 ; get data from port P1MOV SBUF, A; send data seriallyMOV P2, A; send data to the port P2SJMP L1 ; repeat processORG 0023HJB TI, TRANS; check for TI ag bitCLR RIRETITRANS:CLR TIRETIENDMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 43 / 54

Example - 8 Write an assembly language program to get data from port P1 and send it
to the port P2 continuously, while sending; receive data from serial port
and transfer to port P0. Assume that crystal frequency of 8051 is 11.0592
MHz; baud rate for the serial communication is 9600.
Solution Step - I:Calculation of SCON Register
SCON= 50hStep - II:Calculation of TMOD Register (Timer 1 in Mode 2
for Baud Rate
TMOD= 20hStep - III:Calculation of TH1 Register for Baud RateTH1 =FDhStep - IV:Calculation of IE RegisterIE= 90h
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 44 / 54

Assembly Language Program
ORG 0000HSJMP MAINORG 0030HMAIN:MOV P1, #0FFH ; make port P2 as an input portMOV TMOD, #20H; timer 1 in mode 2 for baud rateMOV TH1, #0FDH; to set baud rate 9600MOV SCON, #50H; serial mode 1MOV IE, #90H; enable serial communication interruptSETB TR1; start timer 1 for baud rateL1: MOV A, P1 ; get data from port P1MOV P2, A; send data to the port P2SJMP L1 ; repeat processORG 0023HJB RI, RECEVCLR TIRETIRECEV:MOV A, SBUFMOV P0, ACLR RIRETIENDMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 45 / 54

Example - 9 Assume that INT0 pin is connected to a switch that is normally high. When-
ever the switch goes low, it should turn ON LED. The LED is connected to
port pin P1.4 and is normally OFF. When it is turned ON it should stay ON
for a fraction of seconds. As long as switch is pressed low, the LED should
stay ON.
Solution
Step - I:Calculation of IE RegisterIE= 81h
Main SlideDr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 46 / 54

Assembly Language Program
ORG 0000HSJMP MAINORG 0030HMAIN:MOV IE, #81HHERE:SJMP HEREENDORG 0003HSETB P1.4MOV R2, #0FFHREP:DJNZ R2, REPCLR P1.4RETIMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 47 / 54

Example - 10 Assume that INT1 pin is connected to a pulse generator; write a program in
which the falling edge of the pulse will send a high to port pin P1.4, which
is connected to an LED, i.e. LED is turned ON and OFF at the same rate
as the pulses are applied to the INT1 pin.
Solution
Step - I:Calculation of IE RegisterIE= 84h
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 48 / 54

Assembly Language Program
ORG 0000HSJMP MAINORG 0030HMAIN:SETB TCON.2 ; Initialize INT1 edge triggered interruptMOV IE, #84HHERE:SJMP HEREEND
Interrupt service routine (ISR) for hardware interrupt INT1 to turn ON LED
ORG 0013HSETB P1.4MOV R2, #0FFHREP:DJNZ R2, REPCLR P1.4RETIMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 49 / 54

Example - 11 Using interrupt, write an assembly language program to do the following
(a) Receive data serially and send it to port P0(b) Get data from port P1 and transferred serially and give a copy to port
P2.
(c) Make timer 0 in auto - reload mode to generate a square wave of 250s
frequency on the port pin P0.7
Assume crystal frequency 11.0592 MHz and set baud rate for the serial
communication is 4800.
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 50 / 54

Solution Step - I:Calculation of TH1 Register for Baud Rate
TH1 =FAhStep - II:Calculation of TH0 Register for 250 micro - sec
time
TON=TOFF= 125s
TH0 = 8Dh
Step - III:Calculation of TMOD RegisterTMOD= 22hStep - IV:Calculation of SCON RegisterSCON= 50hStep - V:Calculation of IE RegisterIE= 92h
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 51 / 54

Assembly Language Program
ORG 0000HSJMP MAIN; bypass vector locationORG 0030HMAIN:MOV P1, #0FFH ; make port P1 as an input portMOV TMOD, #22H; Timer 0 in mode 2 for square wave
;and timer 1 in mode 2 for baud rate generation
MOV TH0, #8DH; load TH0 with auto - reload valueMOV TH1, #0FAH; load TH1 value for 4800 baud rateMOV SCON, #50H; set serial mode 1MOV IE, #92H; enable serial communication and
; timer 0 interrupts
SETB TR1; start timer 1 for baud rateSETB TR0; start timer 0 for square waveBACK:MOV A, P1 ; get data from port P1 until TF0 raisedMOV P2, A; send data from P1 to port P2MOV SBUF, A; transfers data seriallySJMP BACK; repeat the process
Interrupt service routine (ISR) for timer 0 to generate square wave
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 52 / 54

Assembly Language Program
ORG 000BHCPL P0.7; interrupt routine of timer 0CLR TF0RETI ; return from interrupt
Interrupt service routine (ISR) for serial port to check whether data is transmitted or received completely or not
ORG 0023HJB TI, TRANSMOV A, SBUFMOV P0, ACLR RIRETITRANS:CLR TIRETIENDMain Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 53 / 54

Thank you
Please send your feedback at [email protected]
For more details and updates kindly visit
https://sites.google.com/site/nileshbbahadure/home
Main Slide
Dr. Nilesh Bhaskarrao Bahadure () Unit - II (Part II) July 25, 2021 54 / 54