Thanks to the excellent advice from you guys, I modified the code and it now works in the form shown below. Notice I also changed the processing epoch of the COUNT loop and reduced the Pause, because of one of your comments. With this sampling rate of the sensor pulse input rate I shouldn't miss any events. Even if I miss a couple, this resolution will adequately support the application.
I am also going to continue to use WORD rather than BYTE for the variables since the production version may encounter more than 256 events during the combined epochs.
When I finally understood from your comments that WRITE was overwriting the EEPROM address that I was using to see if the indexer was indexing with a HighByte that was continuously zero, I was able to solve the problem. Actually, the indexer statement was working....I just couldn't see that it was by the way I was trying to monitor it.
Can't tell you all enough how much I appreciate your help in resolving this problem! As a long term Visual Basic programmer, my faith in PBP is restored!
Code:
'Registers Settings
TRISA = 0 ' Set PORTA pins to outputs
TRISB = 0 ' Set all PORTB and PORTC pins to outputs
TRISC = 0
ANSEL = 0 ' Set all pins to digital
ANSELH = 0
PORTC = 0 ' Preset LEDs off
TRISA.1 =1 ' Set RA1 as input port for meter pulse inputs
'Variables Initialization
switchstate var bit ' Stores value of last measured switchstate
k CON 60 ' Calibration factor for flow meter...# pulses per gal
i VAR WORD ' Index used in Gallon counter Repeat...Until loop
C1 VAR WORD ' Storage variable for count from COUNTER
'Start...normal code here
MAIN:
'Send PWM pulse to latching solenoid to open valve
'Generates 10 millisec pulse on RC3 output pin
LOW PORTC.3 ' Initialize output pulse polarity
PULSOUT PORTC.3,1000 ' Generate 10 msec pulse to RC3
' Valve should be open at this point and water flowing
' Start measuring output of flow meter at RA1 input pin
C1 = 0 ' Initialize pulse count variable to zero
i = 0 ' Initialize total pulse count index to zero
REPEAT ' Assume it takes k accumulated pulses for required gallons
' Count the number of pulses from Hall Effect Magnetic Sensor in 5 sec
' epochs and add count from each epoch to total accumulation and then
' shutoff the valve when count meets i>k criteria.
Count PORTA.1,5000,C1
'WRITE 5,C1.HighByte ' Write count into EEPROM location 5,6 during
'WRITE 6,C1.LowByte ' pre-production testing
'Slowly blink LED during water flow
HIGH PORTC.0 ' If we get here, Blink the LED once
PAUSE 100
LOW PORTC.0
i = i + C1 ' Add count to total pulse accumulation indexer
'When i >= k, required gallons have flowed by the meter
UNTIL i > k
'WRITE 7,i.HighByte 'Store result as test of code..not in production
'Write 8,i.LowByte
GoTo ShutValve
ShutValve: 'Send PWM pulse to latching solenoid to close valve
PULSOUT PORTC.3,1000 ' Generate required 10 msec pulse to RC3
' 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.
C1 = 0 ' Reset pulse counter to zero
Count PORTA.1,10000,C1 ' Collect meter pulse input, if any, for 10 secs
IF C1 > 0 Then
' Water is still flowing...close the valve!
PULSOUT PORTC.3,1000 ' Command valve solenoid to close
ENDIF
' Put code here to put Microcontroller back to Sleep State to wait for
' next Interrupt
SLEEP = 65,535
END
Bookmarks