Image may be NSFW. Clik here to view. ![]() |
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:
- INT0 - Interrupt from external request to P3.2 of 8051
- Timer 0 - This interrupt gets activated whenever Timer 0 activates the Flag TF0.
- INT1 - Interrupt made from external request to P3.3
- Timer 1 - This interrupt gets activated whenever Timer 1 activates the flag TF1.
- 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.
Image may be NSFW. Clik here to view. ![]() |
IE Register |
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= 0x81 ; enables External Interrupt0 (EX0)
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.
Image may be NSFW. Clik here to view. ![]() |
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:
- Configure TMOD register to specify the mode of operation of timers in your system.
- Load the hex values in THx and TLx register.
- Start the Timer using TRx or TCON.
- Monitoring of TFx flag depends on the time period you are about to generate for the system.
- Enable the Timer interrupt using the IE register.
- Write the subroutine for Timer Interrupt with correct keyword listed in the above table.
- 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:
|
Like this article? Share this with others through social sites and dont forget to follow us there. Post your comments and queries regarding this article below. Thank you for visiting us and Enjoy Electronics.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.![]()
Image may be NSFW.Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.