Wednesday, March 23, 2016

8051 Programming Timer Interrupt

The timer flag (TF) is raised when the timer rolls over. It can be detected using interrupt. If the timer interrupt in the IE register is enabled, whenever the timer rolls over, TF is raised, and the microcontroller is interrupted in whatever it is doing, and jumps to the interrupt vector table to service the ISR.

; wake up and go to main, avoid using memory space allocated for ISR
ORG 0H
LJMP MAIN; bypass ISR

; ISR for timer 0
ORG 000BH
CPL P1.2; complement P1.2
MOV TL0, #00
MOV TH0, #0DCH; return from interrupt
RETI

; the main program for initialization
ORG 30H
MAIN :
MOV TMOD, #00000001B; timer 0, mode 1
MOV TL0, #00
MOV TH0, #0DCH
MOV IE, #82H; enable timer 0 interrupt
SETB TR0; start timer
HERE :SJMP HERE; stay here
END


Since the memory space allotted for ISR is limited, we can move ISR to any other location.

; wake up and go to main, avoid using memory space allocated for ISR
ORG 0H
LJMP MAIN; bypass ISR

; ISR for timer 0
ORG 000BH
LJMP ISR_T0

; the main program for initialization
ORG 30H
MAIN :
MOV TMOD, #00000001B; timer 0, mode 1
MOV TL0, #00
MOV TH0, #0DCH
MOV IE, #82H; enable timer 0 interrupt
SETB TR0; start timer
HERE :SJMP HERE; stay here

; Timer 0 ISR
ORG 100H
ISR_T0:CLR TR0
CPL P1.2; complement P1.2
MOV TL0, #00
MOV TH0, #0DCH; return from interrupt
SETB TR0; start timer
RETI
END



Related topics:
8051 Loop   |   8051 Delay   |   8051 IO Port Programming   |   8051 Timer Programming   |   8051 Counter   |   8051 Serial Port Programming   |   8051 Programming Serial Interrupt   |   8051 Programming External Interrupt

List of topics: 8051

No comments:

Post a Comment