Page 1 of 1

Variable variable problem

Posted: Sun May 25, 2008 11:23 am
by Francis
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)

Code: Select all

Dim  ICharge , VBattery , VRegulator , IOutput , InputV  , I , TempCO As Word
Dim CCS(10) as Word  // array for testing
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.

Code: Select all

// Get all ADC parameter Values
Sub GetValues()
  ICharge       = ChargeCurrent()
  VBattery      = BatteryVoltage()
  VRegulator    = ReadRegulatorVoltage()
  IOutput       = CurrentOut()
  InputV        = InputVoltage()
End Sub
//
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:-

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
[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:-
CurrentOut: Error. CurrentOut= 228
TempCo= 2
3 3 2 1 1
TempCo value is fine.
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?

Re: Variable variable problem

Posted: Sun May 25, 2008 8:48 pm
by xor
Francis wrote:[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.
10-Bit ADC's max at 1023.

Posted: Mon May 26, 2008 7:32 am
by Francis
Yes, sorry. Brain not working. I meant my 'running total' CI (which I used in all my ADCing subroutines).

Anyway, back to the plot.

Found the problem ... possibley ... well I think so anyway.. but I'm surprised. Even though I'd decoupled/bypassed PIC with tant and ceramic capacitors, there was a noise problem.

I'd attached a mini-sounder and written a sound routine for various warning peeps. I'd also got it to do a 'heartbeat' click on my timer interrupt once per second.
Now whilst I'd C/R/C'd the sounder something must have been getting back and upsetting things. I removed the 'heartbeat' from the timer interrupt and the problem went.

Note: it didn't affect actual ADC readings which was why I didn't suspect noise. Weird. Next time I do a peeper/transducer it'll def be buffered. A lesson learned over 2 days.

However, if the problem comes back then so will I haha :lol: