PIC18 Interrupt
Transcription
PIC18 Interrupt
PIC18 Interrupt p Hsiao-Lung H i L Chan Ch Dept Electrical Engineering Chang Gung University, Taiwan chanhl@mail cgu edu tw [email protected] Interrupts p vs. polling p g Polling Continuously monitor the status of a given device Waste much of microcontroller’s time,, e.g. g AGAIN BTFSS BRA INTCON, TMR0IF AGAIN ; monitor Timer0 interrupt flag Interrupt Upon receiving an interrupt signal, microcontroller stops currentt iinstruction t ti stream t and d serves th the d device i 2 Interrupt p service routine (ISR) ( ) When an interrupt is invoked, the microcontroller runs the ISR Interrupt p vector table for PIC18 (ROM ( location)) Power-on reset High priority interrupt Low priority interrupt 0x0000 0x0008 0x0018 3 Steps p in executing g an interrupt p Finish the executing instruction, and save the address of the next instruction (program counter) on the stack Jump p to a fixed location byy interrupt p vector table which directs the address of ISR Execute the ISR until reach RETFIE (return from interrupt exit) Get PC from stack and return to the place where it was i interrupted d 4 Interrupt p programming p g g Step p 1. Write the service routine org … retfie org … retfie ox08 ; interrupt service for high priority interrupts 0x18 ; interrupt service for low priority interrupts Step p 2. Enable interrupts p to be serviced Note: retfie also perform clearing the GIE flag 5 Sources of interrupts p in the PIC18 Timers 0, 1, 2, 3 Pins RB0, RB1, RB2 for external hardware interrupts INT0, INT1,, INT2 PORTB-Change interrupt (any one of the upper four Port B pins) Serial communication’s USART interrupts: receive and transmit, respectively ADC (analog-to-digital converter) CCP (compare capture pulse-width-modulation) 6 Enable and disable an interrupt p 7 INTCON ((interrupt p control)) register g PEIE RBIE TMR0IF INT0IF RB0IF 8 Programming g g timer interrupts p Programming ISR Enable interrupt Recognize interrupt 9 Generate a square q wave on the PORTB.5 bit MAIN ORG GOTO ORG BTFSS RETFIE GOTO 0000H MAIN 0008H INTCON TMR0IF ; Timer0 interrupt ? INTCON, ; return from interrupt T0_ISR ORG BCF MOVLW MOVWF MOVLW MOVWF MOVLW MOVWF 00100H TRISB,, 5 0x08 T0CON 0xFF TMR0H 0xF2 TMR0L ; Timer0, 16-bit, internal clock, no prescale 10 Generate a square q wave on the PORTB.5 bit (cont.) ( ) OVER BCF BSF BSF BSF BRA INTCON, TMR0IF T0CON, TMR0ON INTCON, TMR0IE INTCON GIE INTCON, OVER ORG MOVLW MOVWF MOVLW MOVWF BTG BCF RETFIE 200H 0xFF TMR0H 0xF2 TMR0L PORTB, 5 INTCON, TMR0IF ; clear Timer0 interrupt flag ; start Timer0 ; enable Timer0 interrupt ; enable interrupt globally T0_ISR ; clear Timer0 interrupt flag ; return from interrupt 11 Example: p Programming g g two interrupts p PORTD counts up everytime Timer0 overflow 1-Hz pulse is fed into Timer1. Whenever the count reaches 200,, PORTB.b is toggled gg 12 PIR1 (interrupt control register 1) for peripheral interrupts 13 PIE1 (interrupt enable register 1) for peripheral interrupts 14 Programming g g two interrupts p (cont.) ( ) ORG GOTO ORG CHK INT CHK_INT BTFSC BRA BTFSC BRA BSF RETFIE MAIN ORG BSF CLEAR BCF 0000H MAIN 0008H INTCON, TMR0IF ; Timer0 interrupt ? T0_ISR PIR1, TMR1IF ; Timer1 interrupt ? T1_ISR INTCON,, PEIE ; enable peripheral p p interrupts p 00100H TRISC, T1CKI TRISD TRISB, 6 ; PORTC.0 as an input ; PORTD as output ; RB6 as an output 15 Programming g g two interrupts p (cont.) ( ) MOVLW MOVWF MOVLW MOVWF MOVLW MOVWF BCF 0x07 ; Timer0, 16-bit, internal clock, prescaler = 256 T0CON 0x00 TMR0H 0x00 TMR0L INTCON, TMR0IF ; clear Timer0 interrupt flag MOVLW MOVWF MOVLW MOVWF MOVLW MOVWF BCF 0x06 T1CON D’255’ TMR1H -D’200’ TMR1L PIR1, TMR1IF ; Timer1,, external clock,, no prescale p ; clear Timer1 interrupt flag 16 Programming g g two interrupts p (cont.) ( ) OVER T0_ISR BSF BSF BSF BSF BSF BSF BRA T0CON, TMR0ON T1CON, TMR1ON INTCON, TMR0IE PIE1, TMR1IE INTCON, PEIE INTCON, GIE OVER ORG INCF MOVLW MOVWF MOVLW MOVWF BCF GOTO 200H PORTD 0x00 TMR0H 0x00 TMR0L INTCON, TMR0IF CHK_INT CHK INT ; start Timer0 ; start Timer1 ; enable Timer0 interrupt ; enable Timer1 interrupt ; enable bl peripheral h l interrupts ; enable interrupt globally ; clear Timer0 interrupt flag 17 Programming g g two interrupts p (cont.) ( ) T1_ISR ORG BTG MOVLW MOVWF MOVLW MOVWF BCF GOTO 300H PORTB, 6 D’255’ D 255 TMR1H -D’200’ TMR1L PIR1, TMR1IF CHK_INT ; clear Timer1 interrupt flag 18 Programming g g external hardware interrupt p Upon power power-on on reset, positive edge-triggered edge triggered interrupts 19 Everyy time INT0 is activated, LED is toggled gg MAIN OVER ORG GOTO ORG BTFSS RETFIE GOTO 0000H MAIN 0008H INTCON INT0IF INTCON, ORG BCF BSF BCF BSF BSF BRA 00100H TRISB,, 7 ; RB7 as output p TRISB, INT0 ; INT0 as input pin INTCON, INT0IF ; clear INT0 interrupt flag INTCON INT0IE ; enable INT0 interrupt INTCON, INTCON, GIE ; enable interrupt globally OVER ; INT0 interrupt ? INT0_ISR 20 INT0 is activated, LED is toggled gg (cont.) ( ) INT0_ISR ORG BTG BCF RETFIE 200H PORTB, 7 INTCON INT0IF INTCON, ; clear INT0 interrupt flag 21 INTCON3 register g 22 Negative g edge-triggered g gg interrupts p Configuring INTCON2 register 23 LED is toggled gg on the falling g edge g of RB1 pin p MAIN OVER ORG GOTO ORG BTFSS RETFIE GOTO 0000H MAIN 0008H INTCON3 INT1IF INTCON3, ORG BCF BSF BCF BSF BCF BSF BRA 00100H TRISB,, 7 ; RB7 as output p TRISB, INT1 ; INT1 as input pin INTCON3, INT1IF ; clear INT1 interrupt flag INTCON3 INT1IE ; enable INT1 interrupt INTCON3, INTCON2, INTEDG1 ; negative edge-triggered INTCON, GIE ; enable interrupt globally OVER ; INT1 interrupt ? INT1_ISR 24 LED is toggled gg on the falling g edge g of RB1 pin p (cont.) ( ) INT1_ISR ORG BTG BCF RETFIE 200H PORTB, 7 INTCON3 INT1IF INTCON3, ; clear INT1 interrupt flag 25 PORTB-change g interrupt p Change status from HIGH to LOW, or LOW to HIGH RBIF (interrupt flag), RBIE (interrupt enable) in INTCON 26 Door sensor to RB4 and a buzzer to RC7 MYREG EQU DELRG EQU ORG GOTO ORG BTFSS RETFIE GOTO MAIN OVER ORG BCF BSF BCF BSF BSF BRA 0x20 0x80 0000H MAIN 0008H INTCON, RBIF ; PB-change interrupt ? PB_ISR 00100H TRISC, 7 TRISB 4 TRISB, INTCON, RBIF INTCON, RBIE INTCON, GIE OVER ; RB7 as output ; RB4 as input pin ; clear PB interrupt flag ; enable PB interrupt ; enable interrupt globally 27 Door sensor to RB4 and a buzzer to RC7 (cont.) ( ) PB_ISR BUZZ DELAY ORG MOVF MOVLW MOVWF BTG MOVLW MOVWF DECF BNZ DECF BNZ BCF RETFIE 200H POTB, W D’250’ D 250 MYREG PORTC, 7 D’255’ DELRG DELRG,, F DELAY MYREG, F BUZZ INTCON, RBIF ; must read PORTB for delay ; clear PB interrupt flag 28 Interrupt p priority p y Interrupt vector table for PIC18 Power-on-reset 0x0000 High-priority g p y interrupt p 0x0008 (Default ( on power-on p reset)) Low-priority interrupt 0x0018 (Selected with IP bit) 29 RCON register g 30 INTCON register g 31 Registers g for interrupt p flag, g enable and priority p y 32 INTCON2 33 IPR1 ((Interrupt p priority p y register) g ) 34 Use Timer0 and Timer1 interrupts to generate square waves in RB1 and RB7 respectively ORG GOTO 0000H MAIN ; --- high high-priority priority interrupts ORG 0008H BTFSC INTCON, TMR0IF BRA T0_ISR RETFIE 0x01 ; Timer0 interrupt ? ; --- low-priority interrupts ORG 0018H BTFSC PIR1 TMR1IF PIR1, BRA T1_ISR RETFIE ; Timer1 interrupt ? ; fast return to main 35 Use Timer0 and Timer1 interrupts to generate square waves (cont (cont.)) MAIN ORG BCF BCF MOVLW MOVWF MOVLW MOVWF MOVLW MOVWF BCF BSF 00100H TRISB, 1 ; RB1 as output TRISB, 7 ; RB7 as output pin 0x08 ; Timer0, Timer0 16 16-bit bit, internal clock, clock no prescale T0CON 0xFF TMR0H 0x00 TMR0L INTCON, TMR0IF ; clear Timer0 interrupt flag INTCON, TMR0IE ; enable Timer0 interrupt 36 Use Timer0 and Timer1 interrupts to generate square waves (cont (cont.)) OVER MOVLW MOVWF MOVLW MOVWF MOVLW MOVWF BCF BSF BCF BSF BSF BSF BSF BSF BRA 0x00 T1CON 0xFF TMR1H 0x00 TMR1L PIR1, TMR1IF PIE1, TMR1IE IPR1,, TMR1IP RCON, IPEN INTCON, GIEL INTCON GIEH INTCON, T1CON, TMR1ON T0CON, TMR0ON OVER ; Timer1, 16-bit, internal clock, no prescale ; clear Timer1 interrupt flag ; enable Timer1 interrupt ; make Timer1 low-priority p y interrupt p ; enable priority level ; enable low-priority interrupt ; enable interrupt globally ; start Timer1 ; start Timer0 37 Use Timer0 and Timer1 interrupts to generate square waves (cont (cont.)) T0_ISR ORG MOVLW MOVWF MOVLW MOVWF BTG BCF RETFIE 200H 0xFF TMR0H 0x00 TMR0L PORTB, 1 INTCON, TMR0IF 0x01 ; clear Timer0 interrupt flag 38 Use Timer0 and Timer1 interrupts to generate square waves (cont (cont.)) T1_ISR ORG MOVLW MOVWF MOVLW MOVWF BTG BCF RETFIE 300H 0xFF TMR1H 0x00 TMR1L PORTB, 7 PIR1, TMR1IF ; clear Timer1 interrupt flag 39 Fast context saving g in task switching g Context saving PUSHA (Push All) and POPA (Pop All): push or pop all the main registers onto stack High-priority interrupt PIC18 automatically save WREG, BSR, STATUS in shadow register when a high high-priority priority interrupt is activated. activated Restore the original content by RETFIE 0x01 (fast context switching) Low-priority interrupt No automatic save, so must save these 3 register at the beginning of ISR 40 Reference M.A. Mazidi, R.D. Mckinlay, D Causey, PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18, Pearson Education Inc., 2008. Han-Way Huang, PIC Microcontroller: An Introduction to Software and Hardware Interfacing, Thomson Delmar L Learning, i 2005. 2005 41