Documente Academic
Documente Profesional
Documente Cultură
To serve several tasks or devices at a time by single microcontroller Conventional to polling: microcontroller continuously monitor the status of device/task, when the status condition is met, the microcontroller performs the service Interrupt: microcontroller interrupted by device/task causes execution of program associated with the interrupt called Interrupt Service Routine (ISR)
Advantages of interrupts:
Microcontroller can serve many devices/tasks Priority to the devices/tasks can assign Masking (ignoring) interrupt request of devices/tasks is possible Doesn t waste the time to check the status of devices/tasks
IP (Interrupt Priority) Register 8-bit, bit addressable SFR byte address: B8H,
Interrupt priority
Highest to lowest priority External interrupt 0 Timer 0 interrupt External interrupt 1 Timer 1 interrupt Serial port INT0 TF0 INT1 TF1 RI and TI
Program to generate square wave of 3kHz using timer 1 interrupt (12MHz Xtal frequency)
ORG 0 LJMP MAIN ORG 001BH ISR_T1:CPL P1.7 RETI ORG 0030H MAIN: MOV TMOD,#00100000B MOV TH1,#5AH MOV TL1,TH1 MOV IE,#10001000B SETB TR1 WAIT: SJMP WAIT END
; TIMER 1, MODE 2, AUTO RELOAD ; COUNT VALUE FOR 0.1666mS ; EA =1 and ET1 = 1
Program to generate a square pulse of 6kHz at pin P1.6 and a rectangular pulse of 5kHz, 25% duty cycle at pin P1.7
ORG 0 LJMP MAIN ORG 000BH CPL P1.6 RETI ORG 001BH ISR_T1:LJMP RECT_ISR ORG 0030H MAIN: MOV TMOD,#00100010B; TIMER 1 & , MODE 2- AUTO RELOAD MOV TH0,#0ADH ; COUNT VALUE FOR 83uS MOV TL0,TH0
WAIT:
MOV IE,#10001010B ; EI = 1, ET1 & ET0 = 1 SETB TR0 MOV TH1,#0D8H ; Count value for 50us MOV TL1,TH1 SETB TR1 SJMP WAIT
RECT_ISR: JNB P1.7,HIGH_PULSE LOW_PULSE: MOV TH1,#6AH ; Count value for 150us MOV TL1,TH1 CPL P1.7 RETI HIGH_PULSE: MOV TH1,# 0D8H ; Count value for 50us MOV TL1,TH1 CPL P1.7 RETI END
Lower nibble
IEx : set to 1 by microcontroller when external interrupt edge (H to L) is detected, cleared by microcontroller when the interrupt is processed ITx : Interrupt type control bit, set to 1 specify falling edge triggering external interrupt, cleared specify low level triggered external interrupt
Program for up/down counter and display the count. Switch SW1 on P3.2 for up count and SW2 on P3.3 for down count. Display is a seven segment display on port 1
ORG 0 LJMP MAIN ORG 0003H LJMP UP ORG 0013H LJMP DOWN
MAIN: MOV R1,#00 MOV DPTR,#LUT CONTINUE: CLR A ADD A,R1 MOVC A,@A+DPTR MOV P1,A MOV IE,#10000101B; EA = 1, EX1 = 1 & EX0 =1 SETB IT0 ; Edge triggering INT0 SETB IT1 ; Edge triggering INT1 SJMP CONTINUE UP: INCR: CJNE R1,#09,INCR RETI INC R1 RETI
DOWN: DECR:
Minimum duration for low level triggering signal is 4 Machine cycles Minimum pulse duration to detect edge triggered interrupt is 1 Machine cycle HIGH and 1 Machine cycle LOW Difference between RET and RETI instructions
Both return to main program RETI clears ISR flags, indicates the service is over If RET is used all other lower priority interrupts are blocked