8051 Interrupt

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Why need interrupts

Polling and interrupts

• 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.

 Jumps to a fixed location in memory depend on type


of 0023H
interrupt. Serial interrupt Memory 001BH
Timer interrupt 1
 Starts to execute the interrupt service routine(ISR) External interrupt 1
0013H
and return from interrupt. Timer interrupt 0
000BH

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.

• An interrupt is one of the fundamental features in a microcontroller. It is a signal to the processor


emitted by hardware or software indicating an event that needs immediate attention. Whenever an
interrupt occurs, the controller completes the execution of the current instruction and starts the
execution of an Interrupt Service Routine (ISR) or Interrupt Handler. ISR tells the processor or
controller what to do when the interrupt occurs .

• 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

While programming interrupts, first thing to do is to specify the microcontroller which


interrupts must be served.

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

For example : Interrupt routine for Timer1

void ISR_timer1(void) interrupt 3

{ <Body of ISR>

For example : Interrupt routine for External Interrupt0 (EX0)

void ISR_ex0(void) interrupt 0

<Body of ISR>

}
EA

7 6 5 4 3 2 1 0
EA - ET2 ES ET1 EX1 ET0 EX0

 EA: Global interrupt controller bit.


0-Disables all interrupts.
1-Enables all interrupts.
For all the below interrupts, setting(1) the bit enables the interrupt, 0
disables it.
 ET2: Timer 2 Overflow interrupt(8052)
 ES:Serial Port Interrupt
 ET1:Timer 1 overflow interrupt
 EX1:External Interrupt 1 on P3.3
 ET0:Timer 0 overflow interrupt
 EX0:External Interrupt 0 on P3.2
#include <reg51.h> /* Include x51 header file */
sbit LED = P1^0; /* set LED on port1 */
void Ext_int_Init()
{
EA = 1; /* Enable global interrupt */
EX0 = 1; /* Enable Ext. interrupt0 */
IT0 = 1; /* Select Ext. interrupt0 on falling edge */
}
void External0_ISR() interrupt 0
{
LED = ~LED; /* Toggle pin on falling edge on INT0 pin */
}
void main()
{
Ext_int_Init(); /* Call Ext. interrupt initialize */
while(1);
}
TCON Register (1/2)

TCON is an 8-bit control register and contains a timer and interrupt flags.

Bit 1 – IE0: External Interrupt0 Edge Flag

1 = External interrupt0 occurred.

0 = External interrupt0 Processed.

It is set and cleared by hardware.

Bit 0 – IT0: External Interrupt0 Trigger Type Select Bit

1 = Interrupt occurs on falling edge at INT0 pin.

0 = Interrupt occur on a low level at INT0 pin.

22
TCON Register (2/2)

TCON is an 8-bit control register and contains a timer and interrupt flags.

Bit 3 - IE1: External Interrupt1 Edge Flag

1 = External interrupt1 occurred.

0 = External interrupt1 Processed.

It is set and cleared by hardware.

Bit 2 - IT1: External Interrupt1 Trigger Type Select Bit

1 = Interrupt occurs on falling edge at INT1 pin.

0 = Interrupt occur on a low level at the INT1 pin.

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.

Bit Symbol Description


7 — —
6 — —
5 PT2 Reserved
4 PS Priority of Serial Port Interrupt
3 PT1 Priority of Timer 1 Overflow Interrupt
2 PX1 Priority of Ext. Interrupt 1
1 PT0 Priority of Timer 0 Overflow Interrupt
0 PX0 Priority of Ext. Interrupt 0
IP Register (Interrupt Priority
Register)
External Interrupt 0
Void ISR(void) interrupt 0 { <Body of ISR> }

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> }

You might also like