Timer 0 interrupt in 8051/89c51
This program explained you how to handle the interrupts in 8051. In this program I have configure the Timer0 interrupt of 50 msec. The interrupt number in 8051 is given below:-
0. External interrupt 0 (INT0)
1. Timer0 interrupt
2. External interrupt 1 (INT1)
3. Timer1 interrupt
4. Serial interrupt
These are the interrupt numbers in 8051 through which we can access any interrupt by enabling it. In this program I have configure the Timer0 interrupt of 50 msec and also toggle a LED to check the interrupt time. The main program is shown in image below:-
In main function firstly I pull all pin of Port 2 low from internal pull up. Then after I called a function to initialize Timer0 interrupt of 50 msec. This function “ini_Timer0_int()” is explain in image below:-
In this function I have configure the Timer0 in TMOD register. In this register I have selected timer with 16-bit mode for Timer0. Then after I have loaded the timer value in TL0 and TH0 register for generating 50 msec interrupt. Calculation of 50 msec time is as follows:-
Timer Frequency = crystal Freq /12 = 11.0592 Mhz/12 = 921.6 kHz
Time = 1/f = 1/921.6 khz = 1.085 usec
Timer is of 16 bit so max count is = 2^16 =65536
Values loaded to timer0/1 = TH0*256+TL0 =75*256+253 =19453
Delay time = 65536-19453 = 46083 *1.085 us = 50 msec.
So you can generate different time delay by loading different values to TH0 and TL0 registers.
After that I have set the TR0 bit to start Timer0.Main thing is here with IE Special function register in which I have loaded 0x82 value to enableTimer0 interrupt. The IE Register explanation is given in below table:-
IE Register
Bit 7
|
Bit 6
|
Bit 5
|
Bit 4
|
Bit 3
|
Bit 2
|
Bit 1
|
Bit 0
|
EA
|
Reserved
|
Reserved
|
ES
|
ET1
|
EX1
|
ET0
|
EX0
|
To enable any interrupt in 8051 7th bit in IE register must be set. Then we have to set the corresponding interrupt bit.
After these initialization of timer interrupt we will enter in the infinite while loop. Here while Loop is empty so only the Timer0 interrupt will occur after every 50 msec. now to handle this interrupt I have called an interrupt function i.e “void timer 0 (void) interrupt 1” in which I have also toggle a LED to check the interrupt time at oscilloscope (CRO).
Also Consider the “timer.h” file for your reference which we have include in main program is explain in image below:-
To Download Complete code you can click here.
Reference
For more explanation of about 8051 you can contact me or you can read “8051 by mazidi”.
No comments:
Post a Comment