Sunday, 15 July 2018

External Interrupt Using 8051 Microcontroller

Aim:

In this tutorial, we will look at 8051 Interrupts. Interrupts are useful in many cases wherein the process simply wants to continue doing its main job and other units(timers or external events) seek its attention when required. In other words, the microcontroller, need not monitor the timers, the serial communication or the external pins P3.2 and P3.3. Whenever an event related to these units occur, it is informed to the microcontroller with the help of interrupts.
A single microcontroller can serve several devices by two ways:
  • Polling: The microcontroller continuously monitors the status of all the devices. Whenever any device needs the service,it provides the service and moves on to the next device until everyone is serviced. This will be done in an infinite loop.
  • 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

Description:

An interrupt is an external or internal event to get the CPU’s attention. Once the controller detects the interrupt, it suspends the current job and executes a special service routine know as Interrupt Service Routine(ISR).
Upon activation of an interrupt, the microcontroller goes through the following steps
  1. First 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.
  3. 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)
  4. Upon executing the RETI instruction, the microcontroller returns to the place where it was interrupted.
  5. First, it gets the program counter (PC) address from the stack by popping the top two bytes of the stack into the PC.
  6. Then it starts to execute from that address.

8051 Interrupt Structure

8051 Microcontroller has six interrupt sources as shown in the table below:
InterruptROM Location(Hex)PinFlag ClearingInterrupt no. in C
Reset00009Auto
External HW Interrupt 0 (INT0)0003P3.2(12)Auto0
Timer 0 Interrupt(TF0)000BAuto1
External HW Interrupt 1 (INT1)0013P3.3(13)Auto2
Timer 1 Interrupt(TF1)001BAuto3
Serial Com Interrupt(RI and TI)0023Program SW4
  • the reset vector has just 3 bytes allocated to it, meaning it can hold a jump instruction to the location where the main program is stored.
  • The other interrupts have 8 bytes allocated to each of them, hence a small Interrupt service routine(ISR) can be placed here. However, if the ISR needs to larger in length, it has to placed else where and the allocated 8 bytes need to have the code that simple redirects the control to the ISR.
  • INT0 and INT1 are external interrupts on P3.2 and P3.3 respectively. These can be configured to be low level triggered or edge triggered interrupt sources.
  • TF0 and TF1 are timer overflow interrupts for timer 0 and 1 respectively
  • The Serial COM Interrupt can be configured to trigger upon transmit or receipt of a byte during serial communication.

Enabling and Disabling the Interrupts

It should be noted that when the MCU is reset, all the interrupts are disabled. Hence in order to use them, we should enable them. In 8051 Interrupt Enable(EA) Register is used to enable or disable the interrupt. The register is shown below:
EA
76543210
EAET2ESET1EX1ET0EX0
  • EA: Global interrut controlle 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

Block Diagram

3 switch

Schematic

interrupt

Code

// ******************************************************
// Project: Interfacing external interrupt to 8051
// Author: Code Bloges
// Module description: Operate single LED
// ******************************************************
#include<reg51.h>
// LED Pin
sbit LED = P1^0;                                   // Pin P1.0 is named as LED
//Function declarations
void cct_init(void);
void InitINT0(void);
// Main function
void main(void)
{
   cct_init();          // Make all ports zero
   InitINT0();      // Intialize INT0 interrupts
   while(1)
   {}
}
// Init CCT function
void cct_init(void)
{
                P0 = 0x00;    // Make all pins zero
                P1 = 0x00;    // Make all pins zero
                P2 = 0x00;    // Make all pins zero
                P3 = 0x00;    // Make P3.2 (INT0) pin high only
}
// External INT0 pin interrupt init function
void InitINT0(void)
{
                IT0 = 1;      //Edge triggered interrupt mode (Neg Edge)
                EX0 = 1;      //Enable external interrupt INT0
                EA  = 1;      //Enable global interrupts
}
// INT0 ISR
void external0_isr(void) interrupt 0    
{
                LED = ~LED;   // Toggle LED pin
}

Downloads:

The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.

Further Reading suggestions:

No comments:

Post a Comment