Hello jellis00,
Have you verified the presence and level of the pulses you are counting?
What happens if i = i + cin >15 then will it not overshoot the until and never make the statement true ? Maybe UNTIL i >= k would be better ?
Looked at the data sheet, no schmitt triggers to worry about, porta on change interrupts are available. I notice your code does not loop, one shot as is. I really did not write any code <font color=red>AND I DID NOT TEST THIS,</font color> but here is what I was thinking you might do differently. I would actually make the int handler a bit smaller and tidier but you get the jist.
Code:
' -----[ Title ]---------------------------------------------------------- -*
' *
' File...... Toilet_Meter.BAS *
' Compiler.. PICBASIC PRO Compiler from microEngineering Labs *
' Purpose... PIC16F690 microcontroller for Toilet Meter Project *
' Author.... John R. Ellis, *
' Started... 2/20/2009 *
' *
' Updated... Version 1.0.6 on 5/2/2009 *
' Program Size: 255 Words * *
' -----[ Program Description ]---------------------------------------------*
' This Program is for a PIC16F690 installed in a PICkit2 Starter Kit *
' Purpose:
' 1) Monitor an input for an external Interrupt from flush *
' 2) On Interrupt, command latching solenoid to open valve *
' 3) Measure flow meter output to determine when 1.6 *
' gallons have passed *
' 4) On 1.6 gallons, command latching solenoid to close *
' 5) Put Microcontroller back in Sleep State to await next *
' Flush Interrupt *
'
' This Program uses the PIC16F690 to control a solenoid operated water valve
' and to measure 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 toilet 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 are as follows:
'
' RA1...is connected as an input from the meter to count pulse inputs, which
' are simulated by multiple manual depressions of a switch on the PICKIT2
' demo board during program testing.
' RA3...is connected as an interrupt input from the FLUSH contact limit Switch.
' RC0...is connected as an output to an LED and is slowly flashed when the valve
' is open. It is also flashed even more slowly when the 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.
' -----[ Device Declaration ]----------------------------------------------
'
'@device pic16F690, intrc_osc_noclkout, BOD_OFF, PWRT_OFF, wdt_off,mclr_off,
' protect_off
' -----[ Revision History ]------------------------------------------------
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_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
Cin VAR Byte ' Storage variable for count from COUNTER
' -----[ Initialization ]--------------------------------------------------
'
'Registers Settings
Init:
ADCON1 = %01110000 ' Set PORTA to digital I/O & FRC (clock derived from a dedicated internal oscillator)
'
TRISA = %00000010 ' Set all PORTA pins except RA1 to outputs
TRISB = %00000000 ' Set all PORTB and PORTC pins to outputs
TRISC = %00000000
PORTC = %00000000 ' Pre-set PORTB pins low, turning LEDs off
ANSEL = 0 ' Set PortA to digital I/O; Unselect any of the A/D channels
ANSELH = 0
TRISA.1 = 1 ' Set RA1 as input port for meter pulse inputs
PortA.1 = 0 ' Pre-set RA1 at Low value
INTCON = %10011000 ' Set INTCON register to enable interrupts
' -----[ Main Code ]-------------------------------------------------------
on interrupt goto Int_Handler
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
Log:
WRITE 7,i '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
@ sleep
Disable ' Valve should be closed at this point and no water flowing
Int_Handler:
' Start measuring output of flow meter at RA1 input pin
Cin = 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,Cin
WRITE 5,Cin ' Write count into EEPROM location 5 during
' 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 + Cin ' Add count to total pulse accumulation indexer
'When i >= k, required gallons have flowed by the meter
UNTIL i >= k
goto Log
return
enable
END
Bookmarks