After a lot more experimenting I think we have settled on the FVR2048 command along with this code
pause 1000
symbol in_charge_current = C.2 ' see "readings.xlsx" for value conversion with adc
symbol in_battery_voltage = C.4
symbol out_charge_enable = C.1 ' low = charging enabled
symbol analogue_charge_current = w1
symbol analogue_battery_voltage = w2
symbol currently_charging = b6
symbol CURRENT_THRESHOLD = 724 ' around 1A (5% of AH of battery = 5% of 22AH = approx 1A)
symbol VOLTAGE_THRESHOLD_START = 922 ' around 13.3V
symbol NUM_AVERAGE_LOOPS = 1
currently_charging = 1
low out_charge_enable
fvrsetup FVR2048
adcconfig %011 ' set adc reference voltage to FVR
main:
readadc10 in_charge_current, analogue_charge_current
readadc10 in_battery_voltage, analogue_battery_voltage
'sertxd("charge current = ", #analogue_charge_current, cr, lf)
'sertxd("battery voltage = ", #analogue_battery_voltage, cr, lf)
if currently_charging = 1 and analogue_charge_current < CURRENT_THRESHOLD then
currently_charging = 0
'sertxd("stopping charging because current is low: ", #analogue_charge_current, cr, lf)
endif
if currently_charging = 0 and analogue_battery_voltage < VOLTAGE_THRESHOLD_START then
currently_charging = 1
'sertxd("starting charging because voltage is low: ", #analogue_battery_voltage, cr, lf)
endif
if currently_charging = 1 then
low out_charge_enable
'sertxd("charging", cr, lf)
else
high out_charge_enable
'sertxd("discharging", cr, lf)
endif
pause 1000
goto main
As others have found, the FVR command does
NOT require re-insertion after each read ADC
we re-scaled the inputs to pins 3 & 5 to match the required ranges (<922 ADC voltage and <724 ADC current)
The only other issue we had was that when we read the ADC values there was still considerable jittering and fluctuation (maybe 5 or more counts which, with the miniscule values we are reading equates to quite a variation)
we tried averaging readings over different amounts and time periods but they were still present
The addition of C5 & C7 totally eliminated that even with no averaging which I dont understand as the inputs and 5V rail are varying by less than 0.1mV (yes, my meter has 4 decimal places and even the lowest digit wasnt changing) so it must be something in the ADC conversion but why the capacitors stop it is a mystery to me
I tried electrolytics which were fine but opted for tantalum for their stability and low leakage
so, to confirm operation.... charging at a 9A current limited 14.6V from the DC-DC converter commences when the battery voltage drops below 13.3V and remains so until the charge current dips below 1A then stops completely until the voltage drops again
Thanks to all that have contributed - it is appreciated !
