Interrupts in 8051 Microcontroller

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Interrupts in 8051 Microcontroller

It is a sub-routine calls that given by the microcontroller when some other program with high priority is request for acquiring the system buses than interrupt occur in current running program.

Interrupts provide a method to postpone or delay the current process, performs a sub-routine task and then restart the standard program again.


Types of interrupt in 8051 Microcontroller

Let's see the five sources of interrupts in 8051 Microcontroller:

  • Timer 0 overflow interrupt - TF0
  • External hardware interrupt - INT0
  • Timer 1 overflow interrupt - TF1
  • External hardware interrupt - INT1
  • Serial communication interrupt - RI/TI

The timer and serial interrupts are internally produced by the microcontroller, whereas the external interrupts are produced by additional interfacing devices or switches that are externally connected with the microcontroller. These external interrupts can be level triggered or edge triggered.

When interrupt occur then the microcontroller executes the interrupt service routine. Therefore the memory location corresponds to interrupt enables it. Consider the interrupt corresponding to the memory location is shown in the interrupt vector table below.

Interrupts in 8051 Microcontroller1


Interrupt Structure of 8051 Microcontroller

After 'RESET' all the interrupts get disabled, and therefore, all the interrupts is enabled by software. From all the five interrupts, if anyone or all interrupt are activated, this will sets the corresponding interrupt flags as represent in the figure which corresponds to Interrupt structure of 8051 microcontroller:-

Interrupts in 8051 Microcontroller2

All the interrupts can be set or cleared by some special function register that is also known as interrupt enabled (IE), and it is totally depends on the priority, which is executed by using interrupt priority register.

Interrupt Enable (IE) Register

IE register is used for enabling and disabling the interrupt. This is a bit addressable register in which EA value must be set to one for enabling interrupts. The individual bits in this register enables the particular interrupt like timer, serial and external inputs. Consider in the below IE register, bit corresponds to 1 activate the interrupt and 0 disable the interrupt.

Interrupts in 8051 Microcontroller3

Interrupt Priority Register (IP)

Using IP register it is possible to change the priority levels of an interrupts by clearing or setting the individual bit in the Interrupt priority (IP) register as shown in figure. It allows the low priority interrupt can interrupt the high-priority interrupt, but it prohibits the interruption by using another low-priority interrupt. If the priorities of interrupt are not programmed, then microcontroller executes the instruction in a predefined manner and its order are INT0, TF0, INT1, TF1, and SI.

Interrupts in 8051 Microcontroller4


Interrupt programming in 8051

  1. Timer Interrupt Programming: In microcontroller Timer 1 and Timer 0 interrupts are generated by time register bits TF0 AND TF1. This timer interrupts programming by C code involves:
  • Selecting the configuration of TMOD register and their mode of operation.
  • Enables the IE registers and corresponding timer bits in it.
  • Choose and load the initial values of TLx and THx by using appropriate mode of operation.
  • Set the timer run bit for starting the timer.
  • Write the subroutine for a timer and clears the value of TRx at the end of the subroutine.
  1. Let's see the timer interrupt programming using Timer0 model for blinking LED using interrupt method:
  1. #include< reg51 .h>   
  2. sbit Blink Led = P2^0;  // LED is connected to port 2 Zeroth pin  
  3. void timer0_ISR (void) interrupt 1  //interrupt no. 1 for Timer0  
  4.  {  
  5. Blink Led=~Blink Led;   // Blink LED on interrupt   
  6. TH0=0xFC;   // loading initial values to timer   
  7. TL0=0x66;  
  8. }   
  9. void main()   
  10. {  
  11. TMOD=0x0l;      // mode 1 of Timer0  
  12. TH0 = 0xFC:    // initial value is loaded to timer  
  13. TL0 = 0x66:           
  14. ET0 =1;         // enable timer 0 interrupt  
  15. TR0 = 1;        // start timer  
  16. while (1);      // do nothing  
  17.  }  
  1. External Hardware Interrupt Programming

Microcontroller 8051 is consisting of two external hardware interrupts: INT0 and INT1 as discussed above. These interrupts are enabled at pin 3.2 and pin 3.3. It can be level triggered or edge triggered. In level triggering, low signal at pin 3.2 enables the interrupt, while at pin 3.2 high to low transition enables the edge triggered interrupt.

Let us see the programmable feature of 8051 microcontroller are:

Consider the edge triggered external hardware interrupt programming is:-

  1. void main()  
  2. {  
  3. IT0 = 1;    // Configure interrupt 0 for falling edge on INT0  
  4. EXO = 1;    // Enabling the EX0 interrupt  
  5. EA =1;      // Enabling the global interrupt flag  
  6. }  
  7. void ISR_ex0(void) interrupt 0  
  8. {  
  9. <body of interrupt>  
  10. }   

 

  1. Serial Communication Interrupt Programming It is used when there is a need to send or receive data. Since one interrupt bit is used for both Transfer Interrupt (TI) and Receiver Interrupt (RI) flags, Interrupt Service Routine (ISR) must examine these flags for knowing the actual interrupt. By the logical OR operation of RI and TI flags causes the interrupt and it is clear by the software alone. Consider the steps involved in serial communication interrupt programming are:-

 

  • Configure the Interrupt Enable register for enabling serial interrupt.
  • Configure the SCON register for performing transferring and receiving operation.
  • Write a subroutine for given interrupt with appropriate function.

Let's see the program for sending 'E' through serial port with 9600 baud rate using Serial Interrupt:

  1. void main()   
  2. {  
  3. TMOD = 0x20:   
  4. TH10xFD;      // baud rate for 9600 bps   
  5. SCON = 0x50;   
  6. TR1=1;   
  7. EA=l;   
  8. whlle(l);   
  9. }  
  10. void ISR_Serial(void) interrupt 4   
  11. {  
  12. if(TI==l)   
  13. {   
  14. SBUF= ?E?;   
  15. TI=0;   
  16. }   
  17. else   
  18. RI =0;   
  19. }