On this page
Timer Interrupt Handling
This program demonstrates interrupt handling using Timer 0 overflow interrupt on the 8051.
Description
The program uses Timer 0 in auto-reload mode. When the timer overflows, it generates an interrupt that toggles a pin (P2.1). Meanwhile, the main program continuously copies data from Port 0 to Port 1.
Code
| |
Explanation
Interrupt Vector Table
- ORG 0000H: Reset vector - jumps to main program
- ORG 00BH: Timer 0 overflow interrupt vector
Interrupt Service Routine (ISR)
- CPL P2.1: Toggle P2.1 pin
- RETI: Return from interrupt
Main Program
- MOV TMOD, #00000010B: Configure Timer 0 in mode 2 (8-bit auto-reload)
- MOV IE, #10000010B: Enable interrupts
- Bit 7 = 1: Global interrupt enable (EA)
- Bit 1 = 1: Timer 0 interrupt enable (ET0)
- SETB TR0: Start Timer 0
- BACK loop: Main task - copy Port 0 to Port 1
The timer generates periodic interrupts, and P2.1 toggles on each overflow while the main program continues running.