8051 Interrupt
8051 Interrupt
8051 Interrupt
• The state of continuous monitoring of any parameter is called polling. The microcontroller keeps
checking the status of other devices; and while doing so, it does no other operation and consumes
all its processing time for monitoring.
• While polling is a simple way to check for state changes, there's a cost. If the checking interval is too
long, there can be a long lag between occurrence and detection and you may miss the change
completely, if the state changes back before you check. A shorter interval will get faster and more
reliable detection, but also consumes much more processing time and power, since many more
checks will come back negative.
•
• Interrupts can be established for events such as a counter's value, a pin changing state, serial
communication receiving of information
Polling
Finish current instruction and saves the PC on stack.
0003H
External interrupt 0
After executing the ISR the microcontroller returns to 0000H
Reset
the place where it was interrupted. Get pop PC from
stack.
Polling and interrupts
• An alternative approach is to utilize interrupts. With this method, the state change generates an
interrupt signal that causes the CPU to suspend its current operation (and save its current state),
then execute the processing associated with the interrupt, and then restore its previous state and
resume where it left off.
• After the interrupt code is executed, the program continues exactly where it left off.
•
8051 Pin Diagram
P1.0 1 40 Vcc
P1.1 2 39 P0.0(AD0)
PORT1 P1.2
P1.3
3
4
38
37
P0.1(AD1)
P0.2(AD2)
PORT0
P1.4 5 36 P0.3(AD3)
Pin 1 to 8 P1.5
P1.6
6
7
35
34
P0.4(AD4)
P0.5(AD5) Pin 39 to 32
P1.7 8 33 P0.6(AD6)
RST 9 32 P0.7(AD7)
(RXD)P3.0
8051
10 31 EA/VPP
(TXD)P3.1 11 (8031) 30 ALE/PROG
PORT3 (INT0)P3.2
(INT1)P3.3
12
13
29
28
PSEN
P2.7(A15)
(T0)P3.4 14 27 P2.6(A14)
Pin 10 to 17 (T1)P3.5
(WR)P3.6
15
16
26
25
P2.5(A13)
P2.4(A12)
PORT2
(RD)P3.7 17 24 P2.3(A11)
XTAL2 18 23 P2.2(A10) Pin 21 to 28
XTAL1 19 22 P2.1(A9)
GND 20 21 P2.0(A8)
9
8051 Interrupts
• The 8051 controller has six hardware interrupts of which five are available to the programmer.
These are as follows:
8051 Interrupts
• 1. RESET interrupt – This is also known as Power on Reset (POR). When the RESET interrupt is
received, the controller restarts executing code from 0000H location. This is an interrupt which is not
available to or, better to say, need not be available to the programmer.
• void reset()
• {
• ((void (code *)(void))0x0000)();
• }
• 2. Timer interrupts – Each Timer is associated with a Timer interrupt. A timer interrupt notifies the
microcontroller that the corresponding Timer interrupt.
8051 Interrupts
• 1. RESET interrupt – This is also known as Power on Reset (POR). When the RESET interrupt is
received, the controller restarts executing code from 0000H location. This is an interrupt which is not
available to or, better to say, need not be available to the programmer.
• void reset()
• {
• ((void (code *)(void))0x0000)();
• }
• 2. Timer interrupts – Each Timer is associated with a Timer interrupt. A timer interrupt notifies the
microcontroller that the corresponding Timer interrupt.
8051 Interrupts
• 3. External interrupts – There are two external interrupts EX0 and EX1 to serve external devices.
Both these interrupts are active low. In AT89C51, P3.2 (INT0) and P3.3 (INT1) pins are available for
external interrupts 0 and 1 respectively. An external interrupt notifies the microcontroller that an
external device needs its service.
•
• 4. Serial interrupt – This interrupt is used for serial communication. When enabled, it notifies the
controller whether a byte has been received or transmitted.
Interrupt ROM Location(Hex) Pin Flag Clearing Interrupt no. in C
Reset 0000 9 Auto --
External HW 0003 P3.2(12) Auto 0
Interrupt 0 (INT0)
Timer 0 000B - Auto 1
Interrupt(TF0)
External HW 0013 P3.3(13) Auto 2
Interrupt 1 (INT1)
Timer 1 001B - Auto 3
Interrupt(TF1)
Serial Com 0023 - Programmer clears it 4
Interrupt(RI and TI)
Programming Interrupts
This is done by configuring the Interrupt Enable (IE) register which enables or disables the
various available interrupts.
The Interrupt Enable register has following bits to enable/disable the hardware interrupts of
the 8051 controller.
Programming Interrupts
Note that the IE register is bit addressable and individual interrupt bits can also be
accessed.
For example –
IE = 0x81; enables External Interrupt0 (EX0)
IE = 0x88; enables Serial Interrupt
Interrupt ROM Location(Hex) Pin Flag Clearing Interrupt no. in C
Reset 0000 9 Auto --
External HW 0003 P3.2(12) Auto 0
Interrupt 0 (INT0)
Timer 0 000B - Auto 1
Interrupt(TF0)
External HW 0013 P3.3(13) Auto 2
Interrupt 1 (INT1)
Timer 1 001B - Auto 3
Interrupt(TF1)
Serial Com 0023 - Program SW 4
Interrupt(RI and TI)
Programming Interrupts
{ <Body of ISR>
<Body of ISR>
}
EA
7 6 5 4 3 2 1 0
EA - ET2 ES ET1 EX1 ET0 EX0
TCON is an 8-bit control register and contains a timer and interrupt flags.
22
TCON Register (2/2)
TCON is an 8-bit control register and contains a timer and interrupt flags.
23
IP (Interrupt Priority)
The IP or Interrupt Priority Register is used to set the priority of the interrupt as High or Low. If a bit is CLEARED, the corresponding
interrupt is assigned low priority and if the bit is SET, the interrupt is assigned high priority.
Timer 0
Void timer0 (void) interrupt 1 { <Body of ISR> }
External Interrupt 1
Void ex0 (void) interrupt 2 { <Body of ISR> }
Timer 1
Void timer1_ISR (void) interrupt 3 { <Body of ISR> }
Serial Interrupt
Void serial (void) interrupt 4 { <Body of ISR> }