SlideShare a Scribd company logo
16
Most read
17
Most read
18
Most read
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 single microcontroller can serve several devices by two ways
 Interrupts
 Whenever any device needs its service, the device notifies the
microcontroller by sending it an interrupt signal
 Upon receiving an interrupt signal, the microcontroller
interrupts whatever it is doing and serves the device
 The program which is associated with the interrupt is called
the interrupt service routine (ISR) or interrupt handler
 Polling
 The microcontroller continuously monitors the status of a given device
 When the conditions met, it performs the service
 After that, it moves on to monitor the next device until every one is
serviced
 Polling can monitor the status of several devices and serve each
of them as certain conditions are met
 The polling method is not efficient, since it wastes much of the
microcontroller’s time by polling devices that do not need service
 ex. JNB TF,target
 The advantage of interrupts is that the microcontroller
can serve many devices (not all at the same time)
 Each devices can get the attention of the
microcontroller based on the assigned priority
 For the polling method, it is not possible to assign priority
since it checks all devices in a round-robin fashion
 The microcontroller can also ignore (mask) a
device request for service
 This is not possible for the polling method
 For every interrupt, there must be an interrupt
service routine (ISR), or interrupt handler
 When an interrupt is invoked, the micro- controller
runs the interrupt service routine
 For every interrupt, there is a fixed location in
memory that holds the address of its ISR
 The group of memory locations set aside to hold the
addresses of ISRs is called interrupt vector table
 Upon activation of an interrupt, the microcontroller
goes through the following steps
1. It finishes the instruction it is executing and
saves the address of the next instruction (PC) on
the stack
2. It also saves the current status of all the interrupts
internally (i.e: not on the stack)
3. It jumps to a fixed location in memory, called the
interrupt vector table, that holds the address of
the ISR
4. The microcontroller gets the address of the ISR from
the interrupt vector table and jumps to it
 It starts to execute the interrupt service subroutine until it reaches
the last instruction of the subroutine which is RETI (return from
interrupt)
5. Upon executing the RETI instruction, the
microcontroller returns to the place where it was
interrupted
 First, it gets the program counter (PC) address from the stack
by popping the top two bytes of the stack into the PC
 Then it starts to execute from that address
 Six interrupts are allocated as follows
 Reset – power-up reset
 Two interrupts are set aside for the timers: one for timer
0 and one for timer 1
 Two interrupts are set aside for hardware external
interrupts
 P3.2 and P3.3 are for the external hardware interrupts INT0 (or
EX1), and INT1 (or EX2)
 Serial communication has a single interrupt that
belongs to both receive and transfer
Interrupt ROM
Location
(hex)
Name Pin Priority
Reset 0000 9 1
External HW (INT0) 0003 P3.2 (12) 2
Timer 0 (TF0) 000B 3
External HW (INT1) 0013 P3.3 (13) 4
Timer 1 (TF1) 001B 5
Serial COM (RI and TI) 0023 6
EA -- ET2 ES ET1 EX1 ET0 EX0
EA IE.7 Disables all interrupts
-- IE.6 Not implemented, reserved for future use
ET2 IE.5 Enables or disables timer 2 overflow or capture interrupt (8952)
ES IE.4 Enables or disables the serial port interrupt
ET1 IE.3 Enables or disables timer 1 overflow interrupt
EX1 IE.2 Enables or disables external interrupt 1
ET0 IE.1 Enables or disables timer 0 overflow interrupt
EX0 IE.0 Enables or disables external interrupt 0
IE (Interrupt Enable) Register
 The TCON register holds four of the interrupt
flags, in the 8051 the SCON register has the RI
and TI flags
Interrupt Flag SFR Register Bit
External 0 IE0 TCON.1
External 1 IE1 TCON.3
Timer 0 TF0 TCON.5
Timer 1 TF1 TCON.7
Serial Port TI SCON.1
Serial Port RI SCON.0
Interrupt flag bits
TCON SFR
IT1/IT0--- 0-> Level trigger (LOW LEVEL Trigger)
1-> Edge trigger (Falling edge Trigger)
 To enable an interrupt, we take the
following steps:
1. Bit D7 of the IE register (EA) must be set to
high to allow the rest of register to take
effect
2. The value of EA
 If EA = 1, interrupts are enabled and will be responded
to if their corresponding bits in IE are high
 If EA = 0, no interrupt will be responded to, even if the
associated bit in the IE register is high
Interrupt Priority Register (Bit-addressable)
D7 D0
Write a C program using interrupts to do the following:
(a) Generate a 10 KHz frequency on P2.1 using T0 8-bit auto-reload
(b) Use timer 1 as an event counter to count up a 1-Hz pulse and display it on
P0. The pulse is connected to EX1.
Assume that XTAL = 11.0592 MHz. Set the baud rate at 9600.
#include <reg51.h>
sbit WAVE = P2^1;
unsigned char cnt;
void timer0() interrupt 1
{
WAVE = ~WAVE;
}
void timer1() interrupt 3
{
cnt++;
P0 = cnt;
}
void main()
{
cnt = 0;
TMOD = 0x42;
TH0 = 0x46;
IE = 0x86;
TR0 = 1;
TR1 = 1;
while(1);
}
Write an 8051 C program to create a frequency of 2500 Hz
on pin P2.7. Use Timer 1, mode 2 to create delay.
1/2500 Hz = 400 μs
400 μs /2 = 200 μs
200 μs / 1.085 μs = 184
Solution
#include <reg51.h>
void T1M2Delay(void);
sbit mybit=P2^7;
void main(void)
{
unsigned char x;
while(1)
{
mybit=~mybit;
T1M2Delay();
}
}
void T1M2Delay(void)
{
TMOD=0x20;
TH1=-184;
TR1=1;
while(TF1==0);
TR1=0;
TF1=0;
}
Write a C program that continuously gets a single bit of data
from P1.7 and sends it to P1.0, while simultaneously
creating a square wave of 200 μs period on pin P2.5.
Use Timer 0 to create the square wave.
Assume that XTAL = 11.0592 MHz.
Solution:
We will use timer 0 mode 2 (auto-reload). One half of the
period is
100 μs. 100/1.085 μs = 92, and TH0 = 256 - 92 = 164 or A4H
#include <reg51.h>
sbit SW = P1^7;
sbit IND = P1^0;
sbit WAVE = P2^5;
void timer0(void) interrupt 1
{
WAVE = ~WAVE;
}
void main()
{
SW = 1;
TMOD = 0x02;
TH0 = 0xA4;
IE = 0x82;
TR0=1;
while(1)
{ IND = SW; }
}
Thank you

More Related Content

PDF
Traffic Lights Controller in VHDL
PPTX
LCD Interacing with 8051
PPTX
timer counter (1).pptx
DOCX
ARM7-ARCHITECTURE
PPT
Architecture of Smart Sensors.ppt
PDF
Unit II Arm7 Thumb Instruction
PPTX
8051 timer counter
PPT
8086-instruction-set-ppt
Traffic Lights Controller in VHDL
LCD Interacing with 8051
timer counter (1).pptx
ARM7-ARCHITECTURE
Architecture of Smart Sensors.ppt
Unit II Arm7 Thumb Instruction
8051 timer counter
8086-instruction-set-ppt

What's hot (20)

PPTX
Verilog presentation final
PPT
PIC 16F877A by PARTHIBAN. S.
PDF
8051 assembly programming
PPT
8085-microprocessor
PPT
Memory organization of 8051
PPSX
CPLD xc9500
PPTX
Sensor interfacing in 8051
PPT
VLSI subsystem design processes and illustration
PPTX
Distortion annalyser
PPTX
Bidirectional Visitor Counter using IR sensors and Arduino Uno R3
PPT
Interfacing 8255
PDF
Embedded Systems Training Report
PPTX
Transistor Transistor Logic
PPT
8086 micro processor
PPTX
8051 Assembly Language Programming
PPT
8051 MICROCONTROLLER
DOCX
A Report on Bidirectional Visitor Counter using IR sensors and Arduino Uno R3
PPTX
Interfacing Stepper motor with 8051
PPTX
Key board interfacing with 8051
Verilog presentation final
PIC 16F877A by PARTHIBAN. S.
8051 assembly programming
8085-microprocessor
Memory organization of 8051
CPLD xc9500
Sensor interfacing in 8051
VLSI subsystem design processes and illustration
Distortion annalyser
Bidirectional Visitor Counter using IR sensors and Arduino Uno R3
Interfacing 8255
Embedded Systems Training Report
Transistor Transistor Logic
8086 micro processor
8051 Assembly Language Programming
8051 MICROCONTROLLER
A Report on Bidirectional Visitor Counter using IR sensors and Arduino Uno R3
Interfacing Stepper motor with 8051
Key board interfacing with 8051
Ad

Similar to Interrupts programming in embedded C using 8051 (20)

PPTX
Mc module5 ppt_msj
PPTX
Micro controller 8051 Interrupts
PPTX
Interrupt in 8051
PDF
8051-interrupts-temporary suspension of a program
PPTX
Unit 3 timer and counter and there application .pptx
PPTX
Interrupts in 8051
PDF
Microcontroller part 2
PPT
PDF
Interrupt
PDF
Interrupt
PPTX
Interrupts of 8051 microcontroller.newpp
PPTX
Interrupt 8085
PPTX
hardware interrupts in 8051 microcontroller
PPTX
Embedded systems, lesson 16
PPT
Interrupt
PPTX
interrupts programming in 8051 microcontroller
PPT
8051 interrupts
PDF
AVR_Course_Day7 timers counters and interrupt programming
PPTX
Chapter 4 - Interrupts of 8085
PPTX
Timing n interrupt.pptx
Mc module5 ppt_msj
Micro controller 8051 Interrupts
Interrupt in 8051
8051-interrupts-temporary suspension of a program
Unit 3 timer and counter and there application .pptx
Interrupts in 8051
Microcontroller part 2
Interrupt
Interrupt
Interrupts of 8051 microcontroller.newpp
Interrupt 8085
hardware interrupts in 8051 microcontroller
Embedded systems, lesson 16
Interrupt
interrupts programming in 8051 microcontroller
8051 interrupts
AVR_Course_Day7 timers counters and interrupt programming
Chapter 4 - Interrupts of 8085
Timing n interrupt.pptx
Ad

More from Vikas Dongre (20)

PPTX
Lcd interfaing using 8051 and assambly language programming
PPTX
Job opportunities for electronics engineering
PPTX
Educational video creation: Tools and tips
PPTX
Scope of job education and business after HSC
PPTX
Introduction to digital logic gates
PPTX
Introduction to binary number system
PPTX
Timer programming for 8051 using embedded c
PPTX
Arithmetic and Logic instructions in Embedded C
PPTX
Introduction to Embedded system programming using 8051
PPTX
Arithmetic and logic operations in c
PPTX
Arithmetic and logic operations in c
PPTX
Classification of embedded systems
PPTX
Characteristics of embedded systems
PPTX
Features of 89c51,pic,avr &amp; arm processors
PPTX
Microcontroller architecture
PPTX
2. block diagram and components of embedded system
PPTX
1. advantages and applications of embedded system
PPTX
Serial communication
PPTX
Innovative improvements in electronic engineering laboratory education using eml
PDF
Devnagari handwritten numeral recognition using geometric features and statis...
Lcd interfaing using 8051 and assambly language programming
Job opportunities for electronics engineering
Educational video creation: Tools and tips
Scope of job education and business after HSC
Introduction to digital logic gates
Introduction to binary number system
Timer programming for 8051 using embedded c
Arithmetic and Logic instructions in Embedded C
Introduction to Embedded system programming using 8051
Arithmetic and logic operations in c
Arithmetic and logic operations in c
Classification of embedded systems
Characteristics of embedded systems
Features of 89c51,pic,avr &amp; arm processors
Microcontroller architecture
2. block diagram and components of embedded system
1. advantages and applications of embedded system
Serial communication
Innovative improvements in electronic engineering laboratory education using eml
Devnagari handwritten numeral recognition using geometric features and statis...

Recently uploaded (20)

PPTX
Concept of Safe and Wholesome Water.pptx
PPTX
"One Earth Celebrating World Environment Day"
PDF
Tree Biomechanics, a concise presentation
PDF
Effect of anthropisation and revegetation efforts on soil bacterial community...
PDF
Session 1 Introduction to the IPCC - Programme Officer M Shongwe
DOCX
Epoxy Coated Steel Bolted Tanks for Crude Oil Large-Scale Raw Oil Containment...
PPTX
Plant_Cell_Presentation.pptx.com learning purpose
PPTX
Delivery census may 2025.pptxMNNN HJTDV U
PDF
Blue Economy Development Framework for Indonesias Economic Transformation.pdf
PPTX
carbon footprint, emissioncontrol and carbon tax
PPTX
Envrironmental Ethics: issues and possible solution
PDF
Bai bao Minh chứng sk2-DBTrong-003757.pdf
PPTX
Making GREEN and Sustainable Urban Spaces
PPTX
Session8a AR6 Findings Working Group I Vice-Chair Nana Ama Browne Klutse
PPTX
Environmental Ethics: issues and possible solutions
PPTX
Topic Globalisation and Lifelines of National Economy (1).pptx
PDF
Lecture 2 investigation of renal diseses.pdf
PPTX
NOISE-MITIGATION.-pptxnaksnsbaksjvdksbsksk
PPTX
9.-Sedatives-and-Hypnotics.pptxcccccccccccccccccccppt
PPTX
FIRE SAFETY SEMINAR SAMPLE FOR EVERYONE.pptx
Concept of Safe and Wholesome Water.pptx
"One Earth Celebrating World Environment Day"
Tree Biomechanics, a concise presentation
Effect of anthropisation and revegetation efforts on soil bacterial community...
Session 1 Introduction to the IPCC - Programme Officer M Shongwe
Epoxy Coated Steel Bolted Tanks for Crude Oil Large-Scale Raw Oil Containment...
Plant_Cell_Presentation.pptx.com learning purpose
Delivery census may 2025.pptxMNNN HJTDV U
Blue Economy Development Framework for Indonesias Economic Transformation.pdf
carbon footprint, emissioncontrol and carbon tax
Envrironmental Ethics: issues and possible solution
Bai bao Minh chứng sk2-DBTrong-003757.pdf
Making GREEN and Sustainable Urban Spaces
Session8a AR6 Findings Working Group I Vice-Chair Nana Ama Browne Klutse
Environmental Ethics: issues and possible solutions
Topic Globalisation and Lifelines of National Economy (1).pptx
Lecture 2 investigation of renal diseses.pdf
NOISE-MITIGATION.-pptxnaksnsbaksjvdksbsksk
9.-Sedatives-and-Hypnotics.pptxcccccccccccccccccccppt
FIRE SAFETY SEMINAR SAMPLE FOR EVERYONE.pptx

Interrupts programming in embedded C using 8051

  • 2.  An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service  A single microcontroller can serve several devices by two ways  Interrupts  Whenever any device needs its service, the device notifies the microcontroller by sending it an interrupt signal  Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves the device  The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler
  • 3.  Polling  The microcontroller continuously monitors the status of a given device  When the conditions met, it performs the service  After that, it moves on to monitor the next device until every one is serviced  Polling can monitor the status of several devices and serve each of them as certain conditions are met  The polling method is not efficient, since it wastes much of the microcontroller’s time by polling devices that do not need service  ex. JNB TF,target
  • 4.  The advantage of interrupts is that the microcontroller can serve many devices (not all at the same time)  Each devices can get the attention of the microcontroller based on the assigned priority  For the polling method, it is not possible to assign priority since it checks all devices in a round-robin fashion  The microcontroller can also ignore (mask) a device request for service  This is not possible for the polling method
  • 5.  For every interrupt, there must be an interrupt service routine (ISR), or interrupt handler  When an interrupt is invoked, the micro- controller runs the interrupt service routine  For every interrupt, there is a fixed location in memory that holds the address of its ISR  The group of memory locations set aside to hold the addresses of ISRs is called interrupt vector table
  • 6.  Upon activation of an interrupt, the microcontroller goes through the following steps 1. It finishes the instruction it is executing and saves the address of the next instruction (PC) on the stack 2. It also saves the current status of all the interrupts internally (i.e: not on the stack) 3. It jumps to a fixed location in memory, called the interrupt vector table, that holds the address of the ISR
  • 7. 4. The microcontroller gets the address of the ISR from the interrupt vector table and jumps to it  It starts to execute the interrupt service subroutine until it reaches the last instruction of the subroutine which is RETI (return from interrupt) 5. Upon executing the RETI instruction, the microcontroller returns to the place where it was interrupted  First, it gets the program counter (PC) address from the stack by popping the top two bytes of the stack into the PC  Then it starts to execute from that address
  • 8.  Six interrupts are allocated as follows  Reset – power-up reset  Two interrupts are set aside for the timers: one for timer 0 and one for timer 1  Two interrupts are set aside for hardware external interrupts  P3.2 and P3.3 are for the external hardware interrupts INT0 (or EX1), and INT1 (or EX2)  Serial communication has a single interrupt that belongs to both receive and transfer
  • 9. Interrupt ROM Location (hex) Name Pin Priority Reset 0000 9 1 External HW (INT0) 0003 P3.2 (12) 2 Timer 0 (TF0) 000B 3 External HW (INT1) 0013 P3.3 (13) 4 Timer 1 (TF1) 001B 5 Serial COM (RI and TI) 0023 6
  • 10. EA -- ET2 ES ET1 EX1 ET0 EX0 EA IE.7 Disables all interrupts -- IE.6 Not implemented, reserved for future use ET2 IE.5 Enables or disables timer 2 overflow or capture interrupt (8952) ES IE.4 Enables or disables the serial port interrupt ET1 IE.3 Enables or disables timer 1 overflow interrupt EX1 IE.2 Enables or disables external interrupt 1 ET0 IE.1 Enables or disables timer 0 overflow interrupt EX0 IE.0 Enables or disables external interrupt 0 IE (Interrupt Enable) Register
  • 11.  The TCON register holds four of the interrupt flags, in the 8051 the SCON register has the RI and TI flags Interrupt Flag SFR Register Bit External 0 IE0 TCON.1 External 1 IE1 TCON.3 Timer 0 TF0 TCON.5 Timer 1 TF1 TCON.7 Serial Port TI SCON.1 Serial Port RI SCON.0 Interrupt flag bits
  • 13. IT1/IT0--- 0-> Level trigger (LOW LEVEL Trigger) 1-> Edge trigger (Falling edge Trigger)
  • 14.  To enable an interrupt, we take the following steps: 1. Bit D7 of the IE register (EA) must be set to high to allow the rest of register to take effect 2. The value of EA  If EA = 1, interrupts are enabled and will be responded to if their corresponding bits in IE are high  If EA = 0, no interrupt will be responded to, even if the associated bit in the IE register is high
  • 15. Interrupt Priority Register (Bit-addressable) D7 D0
  • 16. Write a C program using interrupts to do the following: (a) Generate a 10 KHz frequency on P2.1 using T0 8-bit auto-reload (b) Use timer 1 as an event counter to count up a 1-Hz pulse and display it on P0. The pulse is connected to EX1. Assume that XTAL = 11.0592 MHz. Set the baud rate at 9600.
  • 17. #include <reg51.h> sbit WAVE = P2^1; unsigned char cnt; void timer0() interrupt 1 { WAVE = ~WAVE; } void timer1() interrupt 3 { cnt++; P0 = cnt; } void main() { cnt = 0; TMOD = 0x42; TH0 = 0x46; IE = 0x86; TR0 = 1; TR1 = 1; while(1); }
  • 18. Write an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1, mode 2 to create delay. 1/2500 Hz = 400 μs 400 μs /2 = 200 μs 200 μs / 1.085 μs = 184 Solution
  • 19. #include <reg51.h> void T1M2Delay(void); sbit mybit=P2^7; void main(void) { unsigned char x; while(1) { mybit=~mybit; T1M2Delay(); } } void T1M2Delay(void) { TMOD=0x20; TH1=-184; TR1=1; while(TF1==0); TR1=0; TF1=0; }
  • 20. Write a C program that continuously gets a single bit of data from P1.7 and sends it to P1.0, while simultaneously creating a square wave of 200 μs period on pin P2.5. Use Timer 0 to create the square wave. Assume that XTAL = 11.0592 MHz. Solution: We will use timer 0 mode 2 (auto-reload). One half of the period is 100 μs. 100/1.085 μs = 92, and TH0 = 256 - 92 = 164 or A4H
  • 21. #include <reg51.h> sbit SW = P1^7; sbit IND = P1^0; sbit WAVE = P2^5; void timer0(void) interrupt 1 { WAVE = ~WAVE; } void main() { SW = 1; TMOD = 0x02; TH0 = 0xA4; IE = 0x82; TR0=1; while(1) { IND = SW; } }