Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8552
Скачиваний: 0
Interrupts |
227 |
RB4-7 Change Interrupt Service Routine
The Service Routine for the RB4-7 change interrupt depends on the specific application. Nevertheless, the following processing steps should be considered:
1.Determine if the source is an RB4-7 change interrupt.
2.Clear the RBIF interrupt flag in the INTCON Register.
3.Save the context. Which registers and variables need to be saved depends on the specific application.
4.Perform the interrupt action.
5.Restore the context.
6.Return from the interrupt with the retfie instruction.
In addition, the interrupt handler may have to perform operations that are specific to the application; for example, debounce a switch or initialize local variables. The following Interrupt Service routine is from the program RB4to7Int in the book’s online software:
;=======================================================
; |
Interrupt Service Routine |
;=======================================================
;Service routine receives control whenever any of
;port-B lines 4 to 7 change state
IntServ:
; First test: make sure source is an RB4-7 interrupt
btfss |
INTCON,rbif |
; RBIF flag is interrupt |
goto |
notRBIF |
; Go if not RBIF origin |
; Save context |
||
movwf |
old_w |
; Save w register |
swapf |
STATUS,w |
; STATUS to w |
movwf |
old_status |
; Save STATUS |
;=========================
;interrupt action ;=========================
;The interrupt occurs when any of port-B bits 4 to 7
;have changed status.
movf |
portb,w |
; Read port-B bits |
|
movwf |
temp |
; Save reading |
|
xorwf |
bitsB47,f |
; |
Xor with old bits, |
; |
result in f |
||
; Test each meaningful bit (4 and 7 in this example)
btfsc |
bitsB47,4 |
; Test bit 4 |
goto |
bit4Chng ; Routine for changed bit 4 |
|
; At this point bit 4 did not change |
||
btfsc |
bitsB47,7 |
; Test bit 7 |
goto |
bit7Chng ; Routine for changed bit 7 |
|
; Invalid port line change. Exit |
||
goto |
pbRelease |
|
228 |
Chapter 11 |
;========================
;bit 4 change routine ;========================
;Check for signal falling edge, ignore if not bit4Chng:
btfsc |
portb,4 |
; Is bit |
4 high |
goto |
pbRelease |
; Bit is |
high. Ignore |
; Toggling bit 1 of Port-A turns LED on and off |
|||
movlw |
b’00000010’ |
; Xoring |
with a 1-bit produces |
; the complement |
|||
xorwf |
porta,f |
; Complement bit 1, Port-A |
|
goto |
pbRelease |
||
;======================== |
|||
;bit 7 change routine ;========================
;Check for signal falling edge, ignore if not bit7Chng:
btfsc |
portb,7 |
; Is bit 7 high |
goto |
exitISR |
; Bit is high. Ignore |
; Toggling bit 0 of Port-A turns LED on and off |
||
movlw |
b’00000001’ |
; Xoring with a 1-bit produces |
; the complement |
||
xorwf |
porta,f |
; Complement bit 1, Port-A |
; |
||
pbRelease: |
||
call |
delay |
; Debounce switch |
movf |
portb,w ; Read port-B into w |
|
andlw |
b’10010000’ ; Eliminate unused bits |
|
btfsc |
STATUS,z ; Check for zero |
|
goto |
pbRelease |
; Wait |
;At this point all port-B pushbuttons are released ;=========================
;exit ISR
;========================= |
|||
exitISR: |
|||
; Store new value of port-B |
|||
movf |
temp,w |
; This port-B value to w |
|
movwf |
bitsB47 |
; Store |
|
; Restore context |
|||
swapf |
old_status,w |
; Saved status to w |
|
movfw |
STATUS |
; To STATUS register |
|
swapf |
old_w,f |
; Swap file register in itself |
|
swapf |
old_w,w |
; re-swap back to w |
|
; Reset,interrupt |
|||
notRBIF: |
|||
bcf |
INTCON,rbif |
; Clear INTCON bit 0 |
|
retfie |
|||
Interrupts |
229 |
Processing by the interrupt service routine is straightforward. The code first determines which line caused the interrupt and takes the corresponding action in each case. In either case, the handler waits until all pushbuttons have been released before returning from the interrupt. This serves to debounce the switches.
11.4 Sample Programs
The following programs demonstrate the programming discussed in this chapter.
11.4.1 The RB0Int Program
;File: RB0Int.ASM
;Date: April 22, 2006
;Author: Julio Sanchez
;Processor: 16F84A
;Description:
;Program to test interrupt on port RB0
;A pushbutton switch is connected to port RB0.
;The pushbutton toggles a LED on port-B, line 2
;Another LED on port-B, line 1, flashes on and off
;at 1/2 second intervals ;===========================
;switches
;===========================
; Switches used in __config directive:
; |
_CP_ON |
Code protection ON/OFF |
|
; * |
_CP_OFF |
||
; |
* |
_PWRTE_ON |
Power-up timer ON/OFF |
;_PWRTE_OFF
; |
_WDT_ON |
Watchdog timer ON/OFF |
||
; * _WDT_OFF |
||||
; |
_LP_OSC |
Low power crystal |
occilator |
|
; * _XT_OSC |
External parallel |
resonator/crystal oscillator |
||
; |
_HS_OSC |
High speed crystal resonator (8 to |
10 MHz) |
|
; |
Resonator: Murate |
Erie CSA8.00MG = |
8 MHz |
|
; |
_RC_OSC |
Resistor/capacitor oscillator (simplest, 20% |
||
error)
;|
;|_____ * indicates setup values
;========================= ; setup and configuration ;=========================
processor 16f84A include <p16f84A.inc>
__config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF ;=====================================================
230 |
Chapter 11 |
|
; |
variables in PIC RAM |
|
;===================================================== |
||
; Local variables |
||
cblock |
0x0d |
; Start of block |
J |
; counter J |
|
K |
; counter K |
|
count1 |
; Auxiliary counter |
|
count2 |
; ISR counter |
|
old_w |
; Context saving |
|
old_STATUS ; Idem |
||
endc |
||
;======================================================== |
||
; |
m a i n |
p r o g r a m |
;======================================================== |
||
org |
0 |
; start at address 0 |
goto |
main |
|
;
;============================= ; interrupt handler ;=============================
org 0x04 goto IntServ
;=============================
;main program ;============================= main:
;Set up interrupt on falling edge
;by clearing OPTION register bit 6
movlw |
b’10111111’ |
||
option |
|||
movlw |
b’11111111’ |
; Set port a |
for input |
tris |
PORTA |
||
movlw |
b’00000001’ |
; Port-B bit |
0 is input |
tris |
PORTB |
; all others |
are output |
clrf |
PORTB |
; All port-B |
to 0 |
; Initially turn on LED |
|||
bsf |
PORTB,0 |
; Set line 0 |
bit |
;============================ |
|||
;setup interrupts ;============================
;Clear external interrupt flag (INTF = bit 1)
bcf |
INTCON,INTF |
; Clear flag |
;Enable global interrupts (GIE = bit 7)
;Enable RB0 interrupt (INTE = bit 4)
bsf |
INTCON,GIE |
; |
Enable |
global int (bit |
7) |
bsf |
INTCON,INTE |
; |
Enable |
RB0 int (bit 4) |
;============================