; sensor.asm ; Program to get interrupts from an external sensor connected to RB0. In particular, ; this program is designed to detect and count the number sensor events from a ; QRB1134 emitter/sensor pair. LIST P=16F877, R=DEC INCLUDE "P16F877.inc" ; __CONFIG _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC ; Define start of vars RAM EQU 0x20 ; Variables Temp EQU RAM+0 Count EQU RAM+1 ; Counter for events UpdateFlag EQU RAM+2 ; Temps for interrupt handling. Put in 0x70 to 0x7F as they are available across all ; RAM banks. W_Temp EQU 0x70 Stat_Temp EQU 0x71 ; Start of the Program ORG 3 goto Start ; ======================================================================= ISR Routine ORG 4 isr ; Save the current state movwf W_Temp swapf STATUS,W CLRF STATUS movwf Stat_Temp ; Handle the interrupt on RB0 btfss INTCON,INTF ; do we have one goto restore ; if not, we are done ; reneable interrupt bcf INTCON,INTF ; Do what we came to do, set a flag. bsf UpdateFlag,0 restore ; Restore the prior system state swapf Stat_Temp,W movwf STATUS swapf W_Temp,F swapf W_Temp,W retfie ; ========================================================================= Main program ORG 0x50 Start ; Set A4-A0 to be output and RD0-RD7 to be output for the LED bank and set ; RB0 for input bsf STATUS, RP0 clrf TRISA clrf TRISD clrf TRISB bsf TRISB,0 bcf STATUS, RP0 ; Initialize the Count and display clrf Count movf Count,W call DispLedBar ; Initialize the flag clrf UpdateFlag ; Initialize the interrupts call InitInts ; In the loop, watch for the flag indicating that an event has occurred ; and update the Counter and display. mainloop btfss UpdateFlag,0 goto mainloop incf Count,F movf Count,W call DispLedBar bcf UpdateFlag,0 goto mainloop ; Initialize interrupts InitInts ; Set up RB0 to interrupt on the falling edge, so that interrupts as the voltage ; drops. bsf STATUS, RP0 ; OPTION_REG is in bank 1 bsf OPTION_REG,0 ; pullups bcf OPTION_REG,0 ; interrupt on falling edge bcf STATUS, RP0 ; Enable the interrupts bsf INTCON,GIE bsf INTCON,INTE bcf INTCON,INTF return ; ----------------------------------------------- ; Output a value to the LED bar DispLedBar ; Enable the led bar and put the value in W there. movwf Temp ; save W movlw 0x12 movwf PORTA comf Temp,W movwf PORTD return END