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.
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.
#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 }
Download Timer0 Code and Simulation
#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 }
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 !!! :)