Interrupt Service Routine using Timers in 8051 Microcontroller

Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest
Timer-interrupt-service-routine-8051-microcontroller
Interrupt service routine using Timers in 8051

Interrupts are one of the most important concepts in a 8051 Microcontroller and we can say that no real world Embedded System exists without using the concept of Interrupts. Interrupt is nothing but a notification or request signal that was received by the controller from device hardware, software or from other external sources. On receiving the Interrupt the Microcontroller completes the execution of current instruction and starts executing a ISR ( Interrupt service Routine ).

ISR is nothing but a piece of code which will be executed in case the Microcontroller encounters any interrupt in during its operation. After the execution of ISR the controller returns back to the instruction from where it performed the jump previously to perform its normal operation.

INTERRUPTS IN 8051:

The 8051 has five interrupt resources. Each of them can be programmed to two priority levels. The interrupt sources are:

  1. INT0 – Interrupt from external request to P3.2 of 8051
  2. Timer 0 – This interrupt gets activated whenever Timer 0 activates the Flag TF0.
  3. INT1 – Interrupt made from external request to P3.3
  4. Timer 1 – This interrupt gets activated whenever Timer 1 activates the flag TF1.
  5. Serial Interrupt – Gets activated whenever controller receives or transmits data.

PROGRAMMING INTERRUPTS:

The first step of programming interrupts starts with configuring the Interrupt Enable register (IE) which which enables or disable the available interrupts in the Microcontroller.

IE register

IE= 0x81 ; enables External Interrupt0 (EX0)Using this IE register we can enable the interrupts we are about to use in our design. This IE register is also bit addressable. For example:

IE= 0x82; enables Timer Interrupt

Next step is to write a sub routine program for microcontroller to execute when an interrupt occurs , this is commonly known as Interrupt service routine(ISR). For writing a ISR we must include a keyword followed by the interrupt number and using this subroutine for particular interrupt is identified by this number.

Interrupt number table
Interrupt Number and Keyword

The above table illustrates the keyword usage for respective interrupts, we should opt for the correct keyword for writing our ISR. For example if we need to write a sub routine for Timer0 interrupt then your code will look like

Void Timer0(void) Interrupt 1
             {
                 Your subroutine Program Goes here….
              }

PROGRAMMING TIMER INTERRUPTS:

There are two timer Interrupts IT0 and IT1 in 8051 microcontroller. Each of these interrupts are triggered by Timer0 and Timer1 respectively. Before moving into the Interrupt using Timers you must be familiar with Programming Timers of 8051 controller. To program timer interrupts you must follow these steps:

  1. Configure TMOD register to specify the mode of operation of timers in your system.
  2. Load the hex values in THx and TLx register.
  3. Start the Timer using TRx or TCON.
  4. Monitoring of TFx flag depends on the time period you are about to generate for the system.
  5. Enable the Timer interrupt using the IE register.
  6. Write the subroutine for Timer Interrupt with correct keyword listed in the above table.
  7. To stop the timer you have to clear the TRx in the end of the sub routine program or it will restart form 0000H in case of Timer modes 0 or 1 and from loaded values in mode 2.

We are about to see a sample program in which Timer was programmed to give five seconds delay and Timer interrupt was used to blink LED’s in port 0. For each five seconds the interrupt will toggle the LED’s infinite times. In the above design 6MHz crystal was used so to give five seconds delay

Timer count at Fosc / 12 = 6.0 MHz/12 = 0.5MHz

Each count Takes 1/0.5 =2us

Total counts by a 16 bit timer 65535 * 2us =0.131 sec

To give 5 seconds delay 0.131 sec * 38 = 5 Sec delay

CODE:

#include<stdio.h>  
#include<reg51.h>
unsigned char count;
unsigned int value;
void main()
{
P0=0x00;                                            
TMOD=0x01;                                 //16 bit mode for timer 0
TH0=0x00;                                   //Upper Timer Register
TL0=0x00;                                   //Lower Timer Register
TCON=0x10;                                //Start Timer 0
IE=0x82;                                     //Enable Timer 0 overflow interrupt
while(1);
}
void Timer0(void)interrupt 1
{
value++;
if(value==38)                             //To activate LED's after 5 seconds
{
P0=~P0;
value=0;
}
}
Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest

Related Designs and Schematics

The following two tabs change content below.

Frank Donald

Junior Network Engineer at Tata Communications
Frank Donald is an Electronics and Communication Engineer who is very passionate about his subject. Reading, Surfing and Blogging are the things he loves.

Ask your query

Be the First to Comment!

Notify of
avatar
wpDiscuz