Explain the interrupts, timer interrupts, external hardware interrupts, serial communication interrupts and the interrupts priority
Objectives
Contra and compare interrupts versus pooling
Explain the purpose of the ISR List all the major interrupts of the PIC18 Explain the purpose of the IVT Enable and disable PIC18 interrupts Program the PIC18 interrupt using assembly language
Interrupt
Whenever any device needs the microcontrollers
service the device notifies it by sending an interrupt signal. Upon receiving an interrupt signal, the microcontroller stops whatever it is doing and serve the device. The program associated with the interrupt is called ISR (interrupt service routine) or interrupt handler. Each device can get the attention of the microcontroller based on the priority assign to it. Can ignore a device request for service
Polling
The microcontroller continuously monitors the status
of a given device; when the status condition met, it performs the service. After that, it moves on to monitor the next device until each one is service. Cannot assign priority because it checks all devices in a round-robin fashion. Cannot ignore a devices for service
Interrupt Service Routine (ISR)
Interrupt Power-on-Reset High Priority Interrupt Low Priority Interrupt ROM Location (hex) 0000 0008 (Default upon power-on reset) 0018
Interrupt Vector Table for the PIC18 Fixed Location in Memory
Step in Executing an Interrupt
It finishes the instruction it is executing and saves the address of the next instruction (program counter) on the stack 2. It jumps to a fixed location in memory (interrupt vector table (IVT)). The IVT directs the microcontroller to the address of the ISR 3. The microcontroller gets the address of the ISR from the IVT and jumps to it. It start to execute the interrupt service subroutine until it reaches the last instruction of the subroutine - RETFIE (Return from Interrupt Exit) 4. Upon executing the RETFIE instruction, the microcontroller returns to the place where it was interrupted
1.
Sources of Interrupt
Each Timers
3 interrupts for external hardware. Pin RB0 (INT0),
RB1 (INT1) and RB2 (INT2) 2 interrupts for serial communication (Receive and Transmit) The PORTB-Change interrupt (RB4-RB7) The ADC The CCP
Enable and Disable an Interrupt
BSF INTCON,GIE
BCF INTCON,GIE
Timer Interrupts
Interrupt
Timer0 Timer1
Flag Bit
TMR0IF TMR1IF
Register
INTCON PIR1
Enable Bit Register
TMR0IE TMR1IE INTCON PIE1
Timer2
Timer3
TMR2IF
TMR3IF
PIR1
PIR3
TMR3IE
TMR3IE
PIE1
PIE2
Timer Interrupt Flag Bits and Associated Registers
INTCON Register with Timer0 Interrupt Enable and Interrupt Flag
Program 11-1 (pg 430)
ORG GOTO 0000H MAIN ORG MAIN ORG 0008H BTFSS INTCON,TMR0IF RETFIE GOTO T0_ISR 00100H T0_ISR BCF TRISB,5 CLRF TRISD SETF TRISC MOVLW 0x08 MOVWF T0CON MOVLW 0xFF MOVWF TMR0H MOVLW 0xF2 MOVWF TMR0L BCF INTCON,TMR0IF BSF T0CON,TMR0ON BSF INTCON,TMR0IE BSF INTCON,GIE MOVFF PORTC,PORTD BRA OVER ORG 200H MOVLW 0xFF MOVWF TMR0H MOVLW 0xF2 MOVWF TMR0L BTG PORTB,5 BCF INTCON,TMR0IF RETFIE END
Timer0 Interrupt
OVER
Revisit
Please see Program 11-2 (pg 432) and Program 11-3 (pg 433)
External Hardware Interrupts
Interrupt
INT1 (RB1)
Flag Bit
INT1IF
Register
INTCON INTCON3 INTCON3
Enable Bit Register
INT0IE INT1IE INT2IE INTCON INTCON3 INTCON3
INT0(RB0) INT0IF INT2 (RB2) INT2IF
Hardware Interrupt Flag Bits and Associated Registers
Positive-edge-triggered interrupt
PIC18 External Hardware Interrupt Pins
Program 11-4 (pg 440)
ORG GOTO 0000H MAIN ORG MAIN ORG 0008H BTFSS INTCON,INT0IF RETFIE GOTO T0_ISR OVER 00100H T0_ISR BCF TRISB,7 BSF TRISB, INT0 CLRF TRISD SETF TRISC BSF INTCON,INT0IE BCF INTCON2,INTEDG1 BSF INTCON,GIE MOVFF PORTC,PORTD BRA OVER ORG 200H BTG PORTB,7 BCF INTCON,INT0IF RETFIE END
Hardware Interrupt
Negative-edge-trigerred
Please see Program 11-5 (pg 442)
Serial Communication Interrupts
Interrupt TXIF (Trasmit) RCIF (Receive) Flag Bit TXIF RCIF Register PIR1 PIR1 Enable Bit Register TXIE RCIE PIE1 PIE1
Serial Port Interrupt Flag Bits and Associated Registers
PIE1 Register Bits Holding TXIE and RCIE
Program 11-6 (pg 446)
ORG GOTO 0000H MAIN ORG MAIN ORG 0008H BTFSC PIR1,TXIF BRA TX_ISR RETFIE 00100H SETF TRISD MOVLW 0x20 MOVWF TXSTA MOVLW D'15' MOVWF SPBRG BCF TRISC, TX BSF RCSTA, SPEN BSF PIE1,TXIE BSF INTCON,PEIE BSF INTCON,GIE BRA OVER END
Serial Port Interrupt
ORG 0040H TX_ISR
MOVWFF PORTD,TXREG RETFIE
OVER
Enable peripheral Interrupt
Please see Program 11-7 (pg 446)
PORTB-Change Interrupt
INTCON Register
* Use in Keypad Interfacing
* Status: H-L or L-H
Differences Between External Hardware and PORTB-Change Interrupt
External Hardware
This (RB0-RB2) interrupts has its own pin and is independent each other Has it own flag (INTxIF) and independent each other Can be programmed to trigger on the negative or positive edge
PORTB-Change
Use s all four (RB4-RB7) and considered to be a single interrupt even though it can use up to four pins Single flag only (RBIF)
Cause an interrupt either pin changes status from HIGH-LOW or LOW-HIGH
Please see Program 11-8 (pg 451) and Program 11-9 (pg 452)
Interrupt Priority
Interrupt Power-on-Reset High Priority Interrupt Low Priority Interrupt ROM Location (hex) 0000 0008 (Default upon power-on reset) 0018
Interrupt Priority (Contd)
Upon Power-on Reset
IPR1 Peripheral Interrupt Priority Register
Please see Program 11-10 (pg 459) and Program 11-11 (pg 460)
Never interrupt your enemy when he/she is making a mistake
*****Assignment
1) Compare and differentiate the features of the Timer 0, Timer1, and Timer2. 2) Write a program using Timer0 to generate a 1 kHz square wave frequency on PORTB RA5. Assuming XTAL=10kHz 3) Write a program using Timer0 to generate a 1 kHz and a 2 kHz square wave frequency on PORTB RB0 and PORTB RB4 respectively. PORTB RB7 will be used as a switch to select the frequencies. If RB7 = 0, a 1 kHz square wave will be generated and if RB7 = 1, a 2 kHz square wave will be generated.