I have attached my code below. My application requires sensing an external interrupt from a contact limit switch closure which I am sensing as an On-change interrupt at the RA3 pin of my 16F690. I am also counting pulses from an external interface to a Hall-effect sensor using a TMR0 interrupt when it overflows. I think I have all the register setting correct, but in my attempts during testing to isolate which interrupt occured, I have to If-Then blocks in the code to test the interrupt flags to determine which one occured. I know that neither of these IF-THEN blocks are being triggered in this current code, because of the specific placement of different write statements in the code. The only Write statement that is actually being triggered is the Write 7, low_bat statement right before the first IF-THEN block...none of the others are writing anything which confirms the IF-THEN blocks are not being triggered. Yet when I activate the RA3 switch closure the code is actually triggering the Interrupt Handler.
Can anyone tell me why my code is not triggering the first IF-THEN block to detect the RA3 On-change interrupt?
Code:' This Program uses the PIC16F690 and simulates control of a solenoid operated ' water valve and measures the volume of water flowing through a flow meter. ' This program places the MCU in sleep mode most of the time until it receives ' an interrupt from the activation of the flush handle that closes a ' contact limit switch that grounds an input pin of the microcontroller as an ' interrupt. I/O pin connections to the PIC16F690 MCU are as follows: ' ' RA0...is connected as ICSPDAT when used for ICSP and also to a low battery ' monitor R/C circuit. ' RA1...is connected as ICSPCLK when used for ICSP. ' RA2...is connected to the external Flush contact limit switch as TMR0 input. ' During testing it is connected to a momentary switch on the PICkit2 ' board as a clock input for TMR0 counting of pulses as interrupt. ' RA3...During testing it is connected to a momentary switch on the PICkit2 ' board as a manually simulated On_interrupt from the FLUSH switch. ' RA4...is connected to pin 6 of the ICSP header for use during programming. ' RC0...is connected as an output to an LED and is on when the valve ' is open. ' RC1...is connected as an output to an LED and is flashed when battery is low ' RC3...is connected as an output to the latching solenoid that opens or ' closes the valve. During program testing with the PICkit2 demo ' board it is connected to an LED to show when the pulse is sent. ' RC4...is connected to pin PDN on the TRM-XXX-LT transceiver. ' RC5...is connected to pin DATA on the TRM-XXX-LT transceiver. ' RC6...is connected to pin RSSI on the TRM-XXX-LT transceiver. ' RB4...is connected via a 10 ohm resistor to provide power to Wireless option. ' RB5...is connected to Hall Effect sensor circuit #2. ' RB6...is connected to Hall Effect sensor circuit #1. ' RB7...is connected to pin R/T SEL on the TRM-XXX-LT transceiver. ' the PICkit2 board momentary switch to simulate Meter pulse inputs. ' -----[ Device Declaration ]---------------------------------------------- ' @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF '@device pic16F690, intrc_osc_noclkout, BOD_OFF, PWRT_OFF, wdt_off,mclr_off, ' protect_off ' For PM assembler only ' -----[ Revision History ]------------------------------------------------ ' ' ' -----[ Variables & Aliases Intitialization ]----------------------------- ' led1 VAR PORTC.0 ' Set RC0 as LED indicator of valve open..water flowing led2 VAR PORTC.1 ' Set RC1 as LED indicator of low battery valve VAR PORTC.3 ' Set RC3 as valve solenoid open/close command low_bat VAR PortA.0 ' Set RA0 as battery low power monitor meter VAR PORTA.2 ' Set RA2 as input for Hall Sensor meter pulse Interrupt flush VAR PORTA.3 ' Set RA3 as input for sensing flush switch closure Int bat_mon VAR Byte ' bat_mon value when Vtrh multiplied by low_bat dummy VAR Byte ' For read of on-interrupt port & clear mismatch condition ' -----[ Constants Initialization ]---------------------------------------- ' k CON 10 ' Calibration factor for flow meter...# pulses per gal i VAR Byte ' Index used in Gallon counter loop Vthr CON 4 ' Low Battery Monitor threshold ' -----[ Initialization ]-------------------------------------------------- ' 'EEPROM PRESETS 'DATA @0,0 'OSC DEFINE 'DEFINE OSC 4 ' Setup Timer0 as an 8-bit counter with the clock input on RA2. ' 1:1 TMR0 prescaler ' TMR0 counts on high-to-low transitions OPTION_REG = %0111000 ' PORTA pullups disabled, TMR0 clock source is RA2, ' high-to-low transitions, prescaler to TMR0 'Register Settings Init: PORTC = 0 ' LEDs off, PULSOUT RC3 provides a high-going pulse PORTA = 0 TRISA = %11111111 ' Set all PORTA pins to inputs...RA0, RA2 & RA3 are used TRISB = %00000000 ' Set all PORTB and PORTC pins to outputs TRISC = %00000000 PORTC = %00000000 ' Pre-set PORTC pins low, turning LEDs off TMR0 = 256-K ' Preload TMR0 to overflow after k counts 'A/D & Comparators disabled ADCON0 = %01110000 ' Set PORTA to digital I/O & FRC (clock derived from a ADCON1 = %00000000 ' dedicated internal oscillator) ANSEL=0 ' Set PortA to digital I/O ANSELH=0 ' Analog module disabled CM1CON0=0 CM2CON0=0 ' Initialization of inputs/outputs TRISC.3 = 0 ' Set RC3 as output port for valve latching solenoid control Low Valve ' Initialize RC3 (valve) at Low value TRISA.2 = 1 ' Set RA2 as input port for clock to TMR0 TRISA.3 = 1 ' Set RA3 as input port for sensing METER switch closure High meter ' Initialize RA2 (meter) at High value for METER pulse inputs ' RA2 = TMR0 clock input for simulated meter pulse inputs High flush ' Initialize RA3 (flush) at High value for flush interrupt 'Interrupts Settings FLUSH_INT_FLAG VAR INTCON.0 ' RA3 (FLUSH) On-change-interrupt flag bit TMR0_INT_FLAG VAR INTCON.2 ' Timer0 overflow flag bit INTCON = %10101000 ' Enable global, TMR0 & RABIE (RA3 on-change-INT) IOCA = %00001000 ' Enable RA3 as on-change-INT ' -----[ Main Code ]------------------------------------------------------- 'Set INT Handler ON INTERRUPT GOTO Int_handler 'Start...normal code here MAIN: PORTC = %00000000 ' Turn all LEDs off SLEEP 30 ' Put MC in Sleep state at power up for 30 secs ' Microcontroller is in Sleep State waiting for external FLUSH Interrupt ' Valve should be closed at this point and no water flowing ' Put code here to test if flow meter has stopped as an indication that ' no water is flowing...is a double check on valve closure. GOTO Main ' Loop to Main to wait for next Flush interrupt on RA3 change DISABLE Int_handler: PULSOUT valve,1000 ' Generate required 10 msec pulse to RC3 to open valve High led1 ' Light indicator that valve is open & water flowing Low low_bat ' Simulate battery monitor input is low DEFINE WRITE_INT 1 Write 7, low_bat ' Remove comment for test only IF FLUSH_INT_FLAG = 1 Then ' Interrupt was from RA3 on change write 9, flush ' Write RA3 (FLUSH) value during testing ' Put code here to start a timer to run for 50 secs as a fail safe ' to prevent overflow of toilet tank in case of sensor failure. 'Goto Shutvalve dummy = flush ' Clear mismatch condition FLUSH_INT_FLAG = 0 ' Clear interrupt flag & enable 'INTCON = %10100000 ' global & TMR0 interrupts..preclude RA3 interrupt Endif IF TMR0_INT_FLAG = 1 Then ' Interrupt was a TMR0 overflow..volume reached PULSOUT valve,2000 ' Generate required 20 msec pulse to RC3..close valve WRITE 5, TMR0 ' Write TMR0 value..remove comment for test only bat_mon = low_bat * Vthr Write 13, bat_mon ' Remove comment for test only If bat_mon < Vthr Then 'Light the low battery monitor light HIGH led2 ENDIF dummy = TMR0 ' Clear mismatch condition TMR0 = 256-k ' Reload TMR0 to overflow after k counts TMR0_INT_FLAG = 0 ' Clear overflow flag and enable 'INTCON = %10001000 ' global & RABIE (RA3 on-change-INT) ' ..preclude TMRO overflow interrupt ENDIF LOW led1 Low led2 RESUME ENABLE ' If the user program ends by getting to the last statement of the program ' at an END instruction, the MCU will SLEEP and await a wakeup. END




Bookmarks