Professional Documents
Culture Documents
TAB4333 ES
Interrupt Mechanism
Interrupts are mechanisms which enable instant response to events such as counter overflow, pin change, ADC data conversion completed, data received, etc, i.e. in general interrupts in embedded systems are meant to handle those asynchronous events. In normal mode, PIC executes the main program as long as there are no event causes an interrupt. Upon interrupt, microcontroller stops the execution of main program and commences the special part of the program which will analyze and handle the interrupt. This part of program is known as the interrupt service routine (ISR). Whatever code stored in ISR will be executed upon interrupt. First, we need to determine which event caused the interrupt, after that comes the interrupt handling, which is executing the appropriate code for the triggered event.
TAB4333 ES 2
18F452 Interrupts
It has 10 regs to control interrupts: RCON, INTCON, INTCON2, INTCON3, PIR1, PIR2, PIE1, PIE2, IPR1, IPR2 (ref: datasheet) The PIC18F452 has two priority levels (high and low). High priority interrupts cause a jump to program address 0x0008 (the high-priority interrupt vector). Low priority interrupts cause a jump to program address 0x0018 (the low-priority interrupt vector). The entry point for ISR is therefore either at 0x0008 or 0x0018 i.e. goto xxxisr Both the low-priority and high-priority interrupts can potentially have multiple sources. If either does, the ISR must poll the interrupt sources to determine which need service.
TAB4333 ES
The polling routine jumps back to the loop after running each interrupt service routine in case other interrupts become active while running the ISR. Note: Allowing the ISR to exit and immediately re-interrupt in this case may wastes machine cycles.
TAB4333 ES 4
Interrupt Latency
The worst-case time between the occurrence of an interrupt source and the start of its handler is called interrupt latency. Interrupt latency actually includes hardware overhead for interrupt processing and instructions in the polling routine.
Tp Mainline program
ISR I/latency
Ti
Tp time interval between interupts Ti time to execute ISR
Note: Ti < Tp
TAB4333 ES
If any registers are altered by any of the handler routines, they should be saved to temporary locations before ISR is executed and later restored when the ISR returns context saving and restoring. Register saving/restoring is much easier if: 1) BSR (Bank Select Register) always contains 0x00 in program. 2) FSR0 and FSR1 used only by main program and FSR2 used only by interrupts. (FSR File Select Register) 3) PCL never used as an operand in interrupts.
The low-priority ISR needs to at least save/restore W and STATUS. STATUS should always be the first to be saved and the last register to be restored in the ISR since mov instructions can alter STATUS.
movff movwf
movf movff
TAB4333 ES
Low-Priority ISR
;starting addrs of isr ;save the status ;save wreg contents ;perform polling ;restore wreg contents ;restore status ;return from interrupt enabled
TAB4333 ES
TAB4333 ES
On power-up all PIC18F452 interrupt sources generate high-priority interrupts by default (1 level only). To change a source to low priority reset the priority bit for the source. For example, to make Timer1 a low-priority source reset the TMR1IP bit of the IPR1 register: bcf IPR1, TMR1IP
TAB4333 ES
TAB4333 ES
10
GIEH = 1 Sources priority bit = 1 Sources enable bit = 1 Sources event flag = 1 No high priority interrupts are in progress.
11
17 Interrupt Sources
External interrupts: INT0 (RB0 pin), INT1 (RB1 pin), INT2 (RB2 pin). PORTB change: Any change on RB4, RB5, RB6, or RB7 (this is a single source). Timer overflow (increment from 0xFFFF to 0x0000): Timer0, Timer1, or Timer2. Timer value match: Timer2 value matches contents of PR2 register. Capture, compare, pulse-width-modulation unit (CCP) events: CCP1 and CCP2. A/D converter value ready USART has received a new value (RC). USART is ready to send a new value (TX). Synchronous serial port (SSP) is ready. Parallel slave port (PSP) is ready. Low voltage condition detected. A bus collision has been detected.
TAB4333 ES 12
Other Notes: 1. High-Priority Interrupts High-priority interrupts automatically save three registers to shadow (fast saving) copies of the registers: STATUS, W, and BSR. To restore the shadow registers value into the actual registers use: retfie FAST
2. External Interrupts PORTB pins 0, 1, and 2 can be used (independently) as interrupts sources. The interrupts can be either positive-edge-triggered or negative-edge triggered (positive on power up by default) The INTEDG0 bit of the INTCON2 register is set to make INT0 (RB0 pin) positive-edge triggered and reset to make it negative-edge triggered. The INTEDG1 and INTEDG2 bits of INTCON2 control INT1 (RB1) and INT2 (RB2) respectively.
TAB4333 ES 13
TAB4333 ES
14
Example: Make RB7-RB4 inputs which read the rows of a 4X4 keypad matrix. If all of the columns of the keypad are enabled (using 4 other port bits set up as outputs), then pressing any key will cause a port B change event what is the effect and application?
Lab Exercise:
Vdd S1
INT0
RB7 RB3
RC0
Gnd Gnd
LEDs at RB3 to RB7 is always on until S1 is pressed then LED at RC0 is toggled while LEDs at RB3 to RB7 are off for approx 2 sec.
TAB4333 ES
16