Variable variable problem
Posted: Sun May 25, 2008 11:23 am
Can variables get muddled? (or just the author?)
18F2520 @ 20MHz
I've been wrestling with an intermittent problem for a while now and can't find a solution.
I'm trying to design a battery charger and have a number of parameters to read, including Load Current, i.e. the battery >> external load current.
To cut a long story short, I have a main routine which tries to maintain a constant voltage to the lead-acid battery with charge-current limit. It also keeps an eye on the battery output load current and does a 'peeep' if overloading.
But, now and then, it peeps (indicating overload) when there is no load.
I am using several subs and, for this, several global Word-sized variables.(it saves typing)
So, using the serial comm terminal screen, I get it to output values each time it thinks there is an overload.
On the whole everything is happy, but now and then, I get an overload warning when there is no load.
My Main routine calls sub 'GetValues()' in a While/Wend loop.
And 'GetValues()' calls several ADC routines including 'CurrentOut()'.
Only the "CurrentOut()" sub is giving the odd problem.
I have put in some Test Print lines to try and see what's going on:-
[NOTE: NONE of the individual ADC values ever go above 2000, normally in the hundreds]
I've got the array merely to try and pinpoint fault/problem.
So, on the odd occasion when it peeps it prints out the 'offending' value (IOutput) and the individual samples obtained in the 'CurrentOut()' sub.
As I say, it only happens now and then, but here is an example from the 'CurrentOut' routine Testprints in Serial Comm terminal:-
The individual sample values are fine
But how can 'CurrentOut = TempCo * 9 /10' get a value of 228 ?
(2 * 9 /10 = 228?)
And why only intermittent?
18F2520 @ 20MHz
I've been wrestling with an intermittent problem for a while now and can't find a solution.
I'm trying to design a battery charger and have a number of parameters to read, including Load Current, i.e. the battery >> external load current.
To cut a long story short, I have a main routine which tries to maintain a constant voltage to the lead-acid battery with charge-current limit. It also keeps an eye on the battery output load current and does a 'peeep' if overloading.
But, now and then, it peeps (indicating overload) when there is no load.
I am using several subs and, for this, several global Word-sized variables.(it saves typing)
Code: Select all
Dim ICharge , VBattery , VRegulator , IOutput , InputV , I , TempCO As Word
Dim CCS(10) as Word // array for testing
On the whole everything is happy, but now and then, I get an overload warning when there is no load.
My Main routine calls sub 'GetValues()' in a While/Wend loop.
Code: Select all
// Get all ADC parameter Values
Sub GetValues()
ICharge = ChargeCurrent()
VBattery = BatteryVoltage()
VRegulator = ReadRegulatorVoltage()
IOutput = CurrentOut()
InputV = InputVoltage()
End Sub
//
Only the "CurrentOut()" sub is giving the odd problem.
I have put in some Test Print lines to try and see what's going on:-
Code: Select all
// Read Output Current
Function CurrentOut() As Word
Dim CI As Word
CI = 0 // zero the values
CurrentOut=0
for I = 0 to 4 // Get samples to do an average.
CCS(I) = ADC.Read(outputi)
CI=CI+ CCS(I)
next
Tempco = CI/5 // Average of values
CurrentOut = Tempco * 9 /10 // Correction
if CurrentOut > 20 then // No load, so must be error
usart.write("CurrentOut: Error. CurrentOut= ",dectostr(currentout),13,10)
usart.write(" TempCo= ",dectostr(TempCo),13,10)
for i=0 to 4
usart.write(" ",dectostr(CCS(I))) // print individual samples
next
usart.write(13,10)
endif
End Function
I've got the array merely to try and pinpoint fault/problem.
So, on the odd occasion when it peeps it prints out the 'offending' value (IOutput) and the individual samples obtained in the 'CurrentOut()' sub.
As I say, it only happens now and then, but here is an example from the 'CurrentOut' routine Testprints in Serial Comm terminal:-
TempCo value is fine.CurrentOut: Error. CurrentOut= 228
TempCo= 2
3 3 2 1 1
The individual sample values are fine
But how can 'CurrentOut = TempCo * 9 /10' get a value of 228 ?
(2 * 9 /10 = 228?)
And why only intermittent?