0% found this document useful (0 votes)
191 views28 pages

ExternalInterrupt PPT

This document discusses using interrupts with the 8051 microcontroller in C. It explains the difference between polling and interrupt methods. Interrupts allow devices to notify the microcontroller when they need service. The document provides details on interrupt service routines, the interrupt enable register, and interrupt vector tables. It then provides an example C program that uses external interrupt 0 to toggle an LED when a switch is pressed and displays a message on an LCD.

Uploaded by

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

ExternalInterrupt PPT

This document discusses using interrupts with the 8051 microcontroller in C. It explains the difference between polling and interrupt methods. Interrupts allow devices to notify the microcontroller when they need service. The document provides details on interrupt service routines, the interrupt enable register, and interrupt vector tables. It then provides an example C program that uses external interrupt 0 to toggle an LED when a switch is pressed and displays a message on an LCD.

Uploaded by

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

8051 Microcontroller Programming in C

Write 8051 ‘C’ program to illustrate interrupt


mechanism.
 To illustrate the concept of interrupt mechanism in 8051
Microcontroller.
To develop an 8051 ‘C’ program to test External Interrupt 0 of

8051 Microcontroller.
 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.
 There are two ways to do that

◦ Polling
◦ Interrupt
In Polling:
The microcontroller continuously monitors the status of

device.
When the status condition is met, it performs the service.
After that it moves to next device until each 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.
In Interrupt method:
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 associated with the interrupt is called Interrupt

Service Routine (ISR).


 The advantage of interrupt is that the microcontroller can
serve many devices(not all at the same time, of course).
 Each device can get the attention of microcontroller based on

the priority assigned to it.


 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:
 It finishes the instruction it is executing and saves the address of the next
instruction (PC) on the stack.
 It also saves the current status of all the interrupts internally (i.e.: not on the
stack).
 It jumps to a fixed location in memory, called the interrupt vector table, that
holds the address of the ISR.
 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)
 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
 Two interrupts are set aside for the timers:
 Timer 0
 Timer 1
 Two interrupts are set aside for hardware external interrupts
 ƒExternal hardware interrupt INT0 (or EX0)
 External hardware interrupt INT1 (or EX1)
 Serial communication has a single interrupt that belongs to
both receive and transfer.
 TI/RI
Interrupt ROM Location Pin
RESET 0000 9
EXTERNAL HARDWARE(INT0) 0003 P3.2(12)
TIMER 0(TF0) 000B
EXTERNAL HARDWARE(INT1) 0013 P3.3(13)
TIMER 1(TF1) 001B
SERIAL COM (RI and TI) 0023
 Upon reset, all interrupts are disabled (masked), meaning that
none will be responded to by the microcontroller if they are
activated.
 ‰The interrupts must be enabled by software in order for the

microcontroller to respond to them.


 There is a register called IE (interrupt enable) that is

responsible for enabling (unmasking) and disabling (masking)


the interrupts.
D7 D0
EA -- ET2 ES ET1 EX1 ET0 EX0

EA (enable all) must be set to 1 in order to enable interrupts that need to be enabled(Global Enable)

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
 To enable an interrupt, we take the following steps:
◦ Bit D7 of the IE register (EA) must be set to high to allow the rest of
register to take effect.
◦ 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
Pressed (EX0 occurs)

Switch
27

LED 25
8051
(P 1.5)
Microcontroller

2x16 LCD
LED TOGGLES UPON
EACH EXT INT0
1. INCLUDE HEADER FILE at89c51ed2.h
2. LCD FUNCTION PROTOTYPE DECLARATION (INITIALIZATION,
COMMAND, DATA, CLEAR).
3. CLEAR TEMP1 REGISTER, DECLARE TEMP2 REGISTER.

4. DECLARE ARRAY TO DISPLAY THE MESSAGE “LED TOGGLES UPON”

ON 1ST LINE OF LCD.


5. DECLARE ARRAY TO DISPLAY THE MESSAGE “EACH EXT INT0”
ON 2ND LINE OF LCD.
6. DECLARE THE VARIABLES i(unsigned int), L(sbit),

lcd_backlight(sbit).
7. BEGIN MAIN
8. AUXR=0X10 TO ACCESS FULL XRAM.
9. CLEAR INTERRUPT FLAG: IE=0.
10. IT0=1.
11. ENABLE EXTERNAL INTERRUPT 0: EX0=1.
12. ENABLE GLOBAL INTERRUPT: EA=1.
13. CLEAR L: LED L25=0.
14. CLEAR LCD.
15. LCD INTIALIZATION.
16. DISPLAY FIRST MESSAGE ON FIRST LINE OF LCD STARTING
FROM FIRST POSITION(LCD_COMMAND=0X80, LCD_DATA).
17. DISPLAY SECOND MESSAGE ON SECOND LINE OF LCD
STARTING FROM FIRST POSITION(LCD_COMMAND=0XC0,
LCD_DATA).
18. UPON INTERRUPT, INTERRUPT SERVICE ROUTINE FOR
EXTERNAL INTERRUPT 0 EXECUTES (i.e., WHEN SWITCH 27 IS
PRESSED, LED CONNECTED TO P1.5 SHOULD TOGGLE).
19. DISABLE INTERRUPT: IE0=0.
20.END
#include "at89c51ed2.h"

// LCD FUNCTION PROTOTYPE


void lcd_init(void);
void lcd_comm(void);
void lcd_data(void);
void clear_lcd(void);
void delay(int);

unsigned char temp1=0x00;


unsigned char temp2;

unsigned char arr1[17] = "LED TOGGLES UPON";


unsigned char arr2[17] = " EACH EXT INT0 ";
//unsigned char int0_flag = 0;
unsigned int i;
sbit L = P1^5; //LED L25 Operation

sbit lcd_backlight = P2^4;

void main(void)
{
AUXR = 0x10; //Accesiing Ful XRAM

IE0 = 0; //Clearing Interrupt Flag


IT0 = 1;
EX0 = 1; //Enabling External Interrupt INT0
EA = 1; //Enabling Global Interrupt
L = 0;

clear_lcd(); // Clear any previous message


lcd_init(); // LCD Initialization

temp1 = 0x80; // Display the data from first position of first line
lcd_comm(); // Command Writing
for(i=0;i<16;i++)
{
temp2 = arr1[i];
lcd_data(); // Data Writing
}
temp1 = 0xC0; // Display the data from first position of
//first line
lcd_comm(); // Command Writing
for(i=0;i<16;i++)
{
temp2 = arr2[i];
lcd_data(); // Data Writing
}

while(1);

}
void ie0_isr(void) interrupt 0
{
L=~L;
IE0 = 0;
}
 Pin-1&2 of the Jumper JP4 are shorted.
 Connect CN2 to CN11 and similarly CN3 to CN6 using 10 core

FRC, for LCD interface.


 When the switch SW27 is pressed the external interrupt will
occur and LED L25 will toggle.
 In this routine INT0 pin of the target device
AT89C51ED2 is used as external interrupt.
 The text message LED TOGGLES UPON EACH EXT INT0 will
be displayed on the LCD screen continuously.
• At the end of the session, students should be
able to
• Develop 8051 ‘C’ code to illustrate the Interrupt
Mechanism by testing External Interrupt 0 [L3]

You might also like