Joe and Bruce, thanks for the excellent inputs! I didn't totally grasp Joe's suggested use of the TMR0 as a counter, mainly because the wiring on microcontroller board doesn't enable me to use RA0 to generate a clock input to RA2 as Bruce's approach suggests.
However, I have gone back to the drawing board to try to count the input pulses from the external sensor by counting them as interrupts. I am enclosing my new code below for reference. I tried to set up the Int-Handler to test for either a RA2/INT or a RA1 on change interrupt by means of two IF_Then blocks. Unfortunately it doesn't appear to recognize any interrupts. I know this because I inserted a LED blink at the very start of the Int_handler to tell me when an interrupt has been recognized and it never blinks for either a RA2/INT or a RA1 on change interrupt. If the RA1 interrupt is received it would increment an index counter, i, for each pulse. I also put the write statement in the If_Then block for RA1 on change interrupt so I could use the PICkit2 to read the EEPROM memory address for the value and it never changes from the FF value. These two tests tell me that my interrupt handler isn't working. Can anyone tell me what I have done wrong from this code??
Code:
' -----[ Device Declaration ]--------------------------------------------------*
'
@device pic16F690, intrc_osc_noclkout, wdt_off, mclr_off, protect_off
' -----[ Variables Initialization ]--------------------------------------------*
'
k CON 15 ' Calibration factor for flow meter = # pulses per gal
i VAR Byte ' Index used in Gallon counter Repeat...Until loop
' -----[ Initialization of Registers ]-----------------------------------------*
'
'Register Settings
TRISA = %00001010 ' Set PORTA pins RA1 and RA3 to inputs, others as outputs
TRISB = %00000000 ' Set PORTB pins as outputs
TRISC = %01000000 ' Set PORTC pin RC6 to input, all others as outputs
PORTC = %0000000 ' Pre-set PORTC LED pins low, turning LEDs off
'A/D & Comparators disabled
ADCON1 = %01110000 ' Set PORTA to digital I/O & FRC (clock derived from a
' dedicated internal oscillator)
ANSEL=0 ' Set PortA to digital I/O
ANSELH=0 ' Analog module disabled
CM1CON0=0
CM2CON0=0
TRISA.1 = 1 ' Set RA1 as input port for simulated meter pulse inputs
TRISA.3 = 1 ' Set RA3 as Master Reset input port
PORTA.1 = 1 ' Pre-set RA1 at High value
' Setup Interrupts
INTCON = %10011000 ' Enable RA2/Int external interrupt and PORTA interrupts on
' change
IOCA = %00000010 ' Enable RA1 for interrupt on change for pulses received
' from momentary switch, activation which simulates external HS1 sensor pulses
on interrupt goto Int_Handler
i=0 ' Initialize counter index to zero
' -----[ Main Code ]-----------------------------------------------------------*
MAIN:
INTCON = %10011000 ' Clear interrupt flags
IF i < k Then 'k is 15 pulse threshold
' Keep the valve open...do nothing..let water flow
Else
'i >= 15....1.6 gallons reached...Close the valve
PULSOUT PORTC.3,1000 ' Generate required 10 msec pulse to RC3
ENDIF
GOTO Main 'Main program loops while waiting for RA1 on change interrupt to count pulses
Int_Handler:
Disable ' No interrupts past this point
'Blink LED on receipt of this interrupt
HIGH PORTC.0 ' Blink the LED once
PAUSE 100
LOW PORTC.0
If INTCON = %10011010 Then 'RA2/INT external interrupt received
PULSOUT PORTC.3,1000 ' Generate 10 msec pulse to RC3 to open valve
Endif
If INTCON = %10011001 Then ' Flow meter interrupt pulse received on PORTA
i = i + 1 'Increment pulse count value
WRITE 7,i 'Store updated count result
ENDIF
Resume ' Return to main program
Enable
END
Bookmarks