Escolar Documentos
Profissional Documentos
Cultura Documentos
Abdel-Rahman M. Jaradat
April 24, 2012
Abstract
PIC 16F877 Interrupt System
The RB interrupt will happen when there is any change (input or output) on pins B4-B7. There is only one interrupt
and the PIC does not tell you which pin changed. The programmer must determine the change based on the previously
known value of the port. Furthermore, a single button press may cause several interrupts due to bounce in the switch.
A debounce algorithm (or hardware) will need to be used. The following is a simple example:
byte previous_b; // global variable (byte=unsigned int8)
#int_rb
rb_isr() {
byte changes, port_b; // local variables
port_b=input_b(); // read portB (input or output)
changes = previous_b ^ port_b; // locate pins changed level (old xor new)
previous_b = port_b;
// take a current reading and save it for next RB int
if
if
if
if
(bit_test(changes,4)&&
(bit_test(changes,5)&&
(bit_test(changes,6)&&
(bit_test(changes,7)&&
#include "16f877a.h"
#use delay (clock=20000000) // 4MHz crystal
#fuses HS, NOWDT, NOPUT, NOPROTECT
;=============================
; interrupt handler
;=============================
org 0x04
goto IntServ
;=============================
; main program
;=============================
main:
; Disable port-B internal pullups
; Interrupts on falling edge of pushbutton action
movlw B10111111
option
; Wiring:
; 7 6 5 4 3 2 1 0 <= port-B
; |
|_______________
red pushbutton
; |________________________ black pushbutton
;
; 7 6 5 4 3 2 1 0 <= Port-A
;
| |_____
red LED
;
|________
green LED
;
movlw b00000000 ; Set Port-A for ouput
tris PORTA
movlw b11110000 ; Port-B bit 0-3 are output
; bits 4-7 are input
tris PORTB ; all others are output
clrf PORTB ; All Port-B to 0
movlw b00000000 ; Zero to w
movwf bitsB47 ; Store in local variable
; Initially turn on LEDs
bsf PORTA,0 ; Set LEDs on line 0
bsf PORTA,1 ; and on line 1
;============================
; set up interrupts
;============================
; Clear external interrupt flag (intf = bit 1)
bcf INTCON,RBIF ; Clear flag
; Enable global interrupts (GIE = bit 7)
; Enable RB0 interrupt (inte = bit 4)
bsf INTCON,GIE ; Enable global int (bit 7)
bsf INTCON,RBIE ; Enable RB0 int (bit 3)
;============================
; flash LED
;============================
; Main program does nothing. All action takes place in
; Interrupt Service Routine
lights:
nop
goto lights
;=======================================================
; 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