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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
ORG 0000H
	LJMP MAIN
	ORG 00BH
		CPL P2.1
		RETI
	ORG 0030H
	MAIN: 
	MOV TMOD , #00000010B
	MOV IE , #10000010B;
	
	SETB TR0
	BACK:
		MOV A , P0;
		MOV P1,A;
		SJMP BACK
END

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

  1. MOV TMOD, #00000010B: Configure Timer 0 in mode 2 (8-bit auto-reload)
  2. MOV IE, #10000010B: Enable interrupts
    • Bit 7 = 1: Global interrupt enable (EA)
    • Bit 1 = 1: Timer 0 interrupt enable (ET0)
  3. SETB TR0: Start Timer 0
  4. 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.