My excerpted code as listed below all works until it gets to the point where it is suppose to count the input pulses to pin RA1 during each loop of a REPEAT...UNTIL epoch and write the count valuable to a variable Cin. Then the accumulated count for all epochs is incremented for each epoch by adding Cin to i. The accumulated count is then suppose to be written to EEPROM after the accumulated count reaches its limit and exits the loop.
I know the loop is working because I see the LED blinking on each loop and because the WRITE statement is actually writing over the FF value in EEPROM. However, the Cin value that is written is always zero (0), indicating that COUNT is actually not counting. Therefore, the loop is never exiting, because i=i+Cin is always zero and the WRITE 7,i statement is therefore never executed. For the life of me I can't figure out why this is happening.
I had a similar problem when I first wrote this code and solved the problem by adding the missing ANSEL =0 and ANSELH = 0 statements, which are in this code now, so that can't be the problem. I checked my register settings and I can see no reason why COUNT is not counting. Can anyone see what is wrong?
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 ]------------------------------------------------
'
' -----[ 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 = %10010000 ' Set INTCON register to enable interrupts
' -----[ Main Code ]-------------------------------------------------------
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
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
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
' Valve should be closed at this point and no water flowing
END
Bookmarks