What are the timer interrupts of PIC microcontroller?
What are the timer interrupts of PIC microcontroller? Timer interrupts enable you to do a task at precise intervals, regardless of what else is going on in your code.
Timer Interrupts can be used for a variety of things, including:
- At regular intervals, measuring an incoming signal (constant sampling frequency).
- Calculating the amount of time that has passed after two events have occurred.
- Using a specific frequency to send out a signal.
- Checking for new serial data on a regular basis.
The timer flag is raised when the timer rolls over. We also know the process to monitor the timer flag with the instructions “BTFSS TMROIF”. In pulling TMR0IF, we have to wait until TMR0IF is raised.
The problem with this method is that the microcontroller is tied down waiting for TMR0IF to be raised, and cannot do anything else. Using interrupts avoid tying down to the controller. If the timer interrupt in the interrupt register is enabled, TMR0IF is raised whenever the timer rolls over and micro controller jumps to the interrupt vector table to service the ISR.
In this way, the microcontroller can do other things until it is notified that the timer has rolled over period to use an interrupt in place of polling, first we must enable the interrupt because all the interrupts are masked upon power on reset. The TMR XIE bit enables the interrupt for a given timer. TMRxIE bits are held by various registers. In other words, The 8051 has two internal interrupts, timers 0 and 1.
Timer overflow flags (TF0/TF1) are set whenever a timer overflows. The microcontroller then jumps to their vector address in order to serve the interrupt. Global and timer interrupts should be enabled for this.
The very first step in programming interrupts is to tell the microcontroller which interrupts must be served. This is accomplished by modifying the Interrupt Enable (IE) register, which allows or prevents the numerous interrupts available. The Interrupt Enable register contains the following bits that enable/disable the 8051 controller’s hardware interrupts.
To permit any one of the interrupts, initially set the EA bit to 1. The bits relating to the desired interrupts are then enabled. ET0, ET1, and ET2 bits enable Timer Interrupts 0, 1, and 2, respectively. Because there are only two timers in AT89C51, ET2 is not used. External interrupts 0 and 1 are enabled by using EX0 and EX1. Serial interrupts are handled by ES. Establishing the bits of the IE register is both required and adequate to enable interrupts.
The following step is to tell the control system what to do when an interrupt occurs. This is accomplished by creating an interrupt subroutine or function. This is the interrupt handler, and it is called whenever an interrupt takes place. It is not necessary to clearly call the Interrupt Subroutine in the code.
It is critical that the topic interrupt be guided by the interrupt quantity in the description of a subroutine. This quantity identifies a subroutine for a specific interrupt. The timer interrupts IT0 and IT1 are referred to Timers 0 and 1, respectively.
Steps for programming Timer Interrupts:
The following steps are involved in interrupt programming for timers:
- Set up the TMOD register to choose the the timer(s) and their mode.
- Load initial values in THx and TLx for modes 0 and 1, or THx only for mode 2.
- Enable Timer Interrupt by configuring the bits of the IE register.
- Start the timer by setting the timer run bit TRx.
- Create a subroutine for the Timer Interrupt. Timer0 has an interrupt number of 1 and Timer1 has an interrupt number of 3. It should be noted that clearing the timer flag TFx is not needed.
- Clear TRx at the end of the subroutine to cease the timer. Or else, it will reactivate from 0000H in modes 0 and 1, and from original values in mode 2.
- If the Timer must be run repeatedly, the initial values must be reloaded within the routine itself (in case of mode 0 and 1). Alternatively, the timer will begin counting from 0000H after one cycle.
Timer interrupt to blink an LED; Time delay in mode1 using interrupt method
// Use of Timer mode0 for blinking LED using interrupt method
// XTAL frequency 11.0592MHz
sbit LED = P1^0; //LED connected to D0 of port 1
void timer(void) interrupt 1 //interrupt no. 1 for Timer 0
led=~led; //toggle LED on interrupt
TH0=0xFC; // initial values loaded to timer
TMOD = 0x01; // mode1 of Timer0
TH0 = 0xFC; // initial values loaded to timer
TL0 = 0x66;
IE = 0x82; // enable interrupt
TR0 = 1; //start timer
while(1); // do nothing
Also read here