Saturday, June 23, 2012

Interrupt Programming in 8051 MicroController with Keil


       Concept behind Interrupt
      Interrupt vs Polling
      What is the advantage of having interrupt based system over polling system
       Interrupt Process:
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.
    1. 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 and starts executing form that address.
Learn More about Interrupt, timer and Serial Programming in 8051 Micro Controller : Click here
Sample Programs for Interrup Programming in 8051 Micro Controller
/*
 Write a program to count from 0 to 256 with an interval of 100 ms and display the result on p2.
 */

#include <regx51.h>
void delay1ms(int n)
 {
  int i=0;
while(i<=n)
{
 TMOD=0x01;
 TL0=0x00;
 TH0=0x35;
 TR0=1;
 while(!TF0);
 TR0=0;
 TF0=0;
 i++;
 }
 }               
 void main()
 {
    int a=0;
  P2=0x00;
 while(a<=256)
  {
  delay1ms(20);
  P2=a;
 a++;
 }
  }

/*
Assume that there is a queue of people ready to enter inside a cinema hall whose capacity is 50 seats.
No morea than 50 people are allowed inside the hall and the gate is closed as soon as the hall is full.
There is a gate counter (sensor) to count the inflow of people and the number of people entered so far
is displayed in the display device at the gate.

                a. Write a program for the above scenario.
                b. Gate Sensor is connected to the external interupt 0 (EX0)
                c. Number can be connected using LED connected in port 1.

*/


#include<regx51.h>
int a = 0;
 
void main()
{
 P3_2=1; /* for external interrupt at port 3 bot 2*/
 P1=0x00;  //Port interrupt in input mode
 EA=1;     //For Enabling the interrupt register
 EX0=1;   //External interrupt set
 IT0=1;   //For Edge Triggered System
 P1=0x00;
 while (a<=50)
 {
 if (a<=50)
 {
 P1 = a;
 P3_2=1;               // Again the port is set for input mode
 }
 }

}
void intpt() interrupt 0  //Interrupt Service Routine
{
a++;
}

2 comments:

  1. Despite it’s relatively old age, the 8051 is
    one of the most popular microcontrollers in use
    today. Many derivative microcontrollers have since
    been developed that are based on--and
    compatible with--the 8051.8051 microcontroller pdf

    ReplyDelete