How to use timer Interrupt in 8051 Microcontroller, Timer interrupt in 8051 microcontroller, 8051microcontroller timer code,use timer with 8051,8051 timer code
Hello friends, hope you all are fine and having fun with your lives.In today's post, we are gonna see How to use timer interrupt in 8051 Microcontroller.8051 Microcontroller comes with timer as well. They normally have two timer in them named as Timer0 and Timer1. These timers are used for counting purposes like you want to start some countdown in your project then you can use these timers or you wanna create some clock then in that case as well you need timers. So, in short there are numerous uses of timers in a project. Timers are also used for delays like you wanna create some delay of 10 sec but you dont wanna use the delay function in your project so you can use timers. You start the timer and then when it comes to 10 seconds then you can do your work. So, these are different uses of a timer and clearly we can't neglect its importance, so today we are gonna see How to use these timer interrupt in 8051 Microcontroller.

Now coming towards interrupt, interrupt is interrupt :P Yeah really, we call it interrupt because its an interrupt. In programming codes there are many things which needs to run in background and appear when its time for them to appear. Here where interrupt comes handy. Interrupt is kind of a background code which keeps on running in the background while the main code keeps on running in front but when the interrupt condition is fullfilled then it interrupts the main program and executes the functions defined in it. For Timer interrupts, suppose I wanna blink my LED after every 2 seconds then what will I do is I will start a timer for 2 seconds and when this timer completes I will generate an interrupt. So, in this way after every two seconds the led will blink. So, let's start with timers interrupt in 8051 Microcontroller and see how we are gonna do this.

How to use Timer interrupt in 8051 Microcontroller ???

As I explained earlier, we are gonna use Timer interrupt in 8051 Microcontroller. so, now before gong into the details, let me first throw some light on how we are gonna implement this. Timers count from 0 to 255 in 8 bit mode as in 8 bit 255 is the maximum value and when timer hits the 255 number then we say that our timer is overflowed. Now when timer overflows, then it sends us a indication using which we generate our intterupt. In timers, there are few registers in which they store their value. If we are talking about Timer0 then timer0 stores its value in TL0 register. Now suppose I want my timer to start counting from 10 instead 0 then I will store 10 in my TL0 register and it will count from 10 instead 0 and when it reaches 255 it will overflow. Now when Timer0 will overflow then it will make TF0 bit HIGH. TF0 is another register value, if its 1 then it means that our timer is full and if its 0 then it means our timer is still counting. So, that's how we count from our timer and check the pin TF0. Now first of all, I am gonna use Timer0 and then we will have a quick look at Timer1.

Timer0 Interrupt
  • First of all, design a simple circuit as shown in below figure:
How to use timer Interrupt in 8051 Microcontroller, Timer interrupt in 8051 microcontroller, 8051microcontroller timer code,use timer with 8051,8051 timer code
  • Now upload the below code in your Keil software and get the hex file.
#include<reg51.h>

// Out Pin
sbit Out = P2^0;		   // Pin P2.0 is named as Out

//Function declarations
void cct_init(void);
void InitTimer0(void);


int main(void)
{
   cct_init();   	       // Make all ports zero
   InitTimer0();           // Start Timer0
 
   while(1)                // Rest is done in Timer0 interrupt
   {
   }
}

void cct_init(void)
{
	P0 = 0x00;   
	P1 = 0x00;   
	P2 = 0x00;   
	P3 = 0x00;  
}


void InitTimer0(void)
{
	TMOD &= 0xF0;    // Clear 4bit field for timer0
	TMOD |= 0x02;    // Set timer0 in mode 2
	
	TH0 = 0x05;      // 250 usec reloading time
	TL0 = 0x05;      // First time value
	
	ET0 = 1;         // Enable Timer0 interrupts
	EA  = 1;         // Global interrupt enable
	
	TR0 = 1;         // Start Timer 0
}


void Timer0_ISR (void) interrupt 1   // It is called after every 250usec
{
	Out = ~Out;  // Toggle Out pin

	TF0 = 0;     // Clear the interrupt flag
}

  • In the above code, the main function is our InitTimer0 function.
  • In this function what I have done is I simply set the timer 0 to mode 2. In mode 2, it will auto reload means once the timer0 overflows then it will comes back to its original value and will start again.
  • TL0 has 0x05 in it which is the initial value of timer0 and it will count for 250 micro seconds.
  • TH0 also has the 0x05. On reload timer uploads the vlaue from TH0 into TL0 so thats why we have given the same value to TH0.
  • After that we make ET0 bit enabled which will enable the timer, if you dont set this pin HIGH then our timer will not work.
  • EA bit will enable the global interrupt. if we dont enable this pin then timer will work but it wont generate the interrupt.
  • Finally after setting all configurations, we started our timer.
  • Now when the Timer0 overflows after every 250 micro seconds, it will generate the interrupt and it will come to Timer0_ISR function.
  • In Timer0_ISR function, I simply toggled the OUt pin which is Pin2.0 and then I again set the interrupt bit to 0 which is TF0.
  • That's how our timer is working and if we check the P2.0 pin on oscilloscope then it will look something as shown in below figure:
How to use timer Interrupt in 8051 Microcontroller, Timer interrupt in 8051 microcontroller, 8051microcontroller timer code,use timer with 8051,8051 timer code
  • You can see in the above figure that our pin is toggling with an interval of 250 usec.
  • One important thing to note is there's no function written in while(1) loop and still its working because its running on background and performing the interrupt routine. You can add any function in your MAin code and it will keep on working and meanwhile at the background your interrupt will also keep on generating.
  • You can download this Simulation and programming code by clicking on below button.

Download Timer0 Code and Simulation

  • Now, lets have a quick look on Timer1 interrupt in 8051 Microcontroller.
Timer1 Interrupt
  • Now let's have a quick look on Timer1 interrupt in 8051 Microcontroller. For that, design the same simulation in Proteus as we did for Timer 0.
  • Now, upload the below code in your Keil software and get the hex file.
#include<reg51.h>

// Out Pin
sbit Out = P2^0;		   // Pin P2.0 is named as Out

//Function declarations
void cct_init(void);
void InitTimer1(void);


int main(void)
{
   cct_init();   	       // Make all ports zero
   InitTimer1();           // Start Timer1
 
   while(1)                // Rest is done in Timer1 interrupt
   {
   }
}

void cct_init(void)
{
	P0 = 0x00;   
	P1 = 0x00;   
	P2 = 0x00;   
	P3 = 0x00;  
}


void InitTimer1(void)
{
	TMOD &= 0x0F;    // Clear 4bit field for timer1
	TMOD |= 0x20;    // Set timer1 in mode 2
	
	TH1 = 0x05;      // 250 usec reloading time
	TL1 = 0x05;      // First time value
	
	ET1 = 1;         // Enable Timer1 interrupts
	EA  = 1;         // Global interrupt enable
	
	TR1 = 1;         // Start Timer 1
}


void Timer1_ISR (void) interrupt 3   // It is called after every 250usec
{
	Out = ~Out;  // Toggle Out pin

	TF1 = 0;     // Clear the interrupt flag
}

  • Now you can see in the above code that its exactly the same as we used for Timer0 with a slight difference that now we are using registers for Timer1.
  • Instead of TL0, now we are using TL1 and similarly TH1 instead of TH0 and TR1 instead of TR0.
  • Rest of the code is exactly the same and hence it will give the same result as for Timer0 and is shown in below figure:
How to use timer Interrupt in 8051 Microcontroller, Timer interrupt in 8051 microcontroller, 8051microcontroller timer code,use timer with 8051,8051 timer code
  • You can download the code for Timer1 along with simulation by clicking the below button.

Download Timer1 Code and Simulation

That's all for today, I hope you guys have got something out of today's post and gonna like this one. In the coming post, I am gonna design some simple project on 8051 Microcontroller in which I will use these Timers, then you will get know more about them. So, stay tuned and subscribe us by email. Take care !!! :)