PDA

View Full Version : Trouble with RCTIME to monitor a 12V SLA Battery



DavidM
09-09-2007, 06:05 PM
HI,


I am having trouble with RCTIME to monitor a 12 VOLT SLA BATTERY ( the battery is used to power the propeller circuit and I want to display its "LEVEL" on 8 LED's. in this case I am just using the 8 LED's of the PROP DEMO Board.

Attached is my code and schematic..

I think I have the correct idea but i am not sure of some of the values/calculations for ..

1) PAUSE the start of the routine so as to CHARGE the CAPACITOR
2) ADJUST the time for the SPIN code that has executed so far.
3) Once I get some kind of value I need to "SCALE" / ADJUST" it to meet my desired output.

I am getting NO RESULTS so far and I also don't have any DEBUGGING setup, BUT I believe I need it for this kind of task.
My RCTIME routine is called "GetVoltage"
I don't even think my capacitor is charging!




'' RCTIME 12V BATTERY MONITOR TEST

CON
_CLKMODE =XTAL1 +PLL16X
_XINFREQ =5_000_000

VAR

LONG RcTimePinNo
LONG BatteryVoltage
LONG RcTemp

PUB Main

RcTimePinNo :=0

DIRA[16..23]~~ ''Make leds outputs
OUTA[16..23]:=%00000000 ''Turn them off

REPEAT
BatteryVoltage:=GetVoltage( RcTimePinNo) '' VALUE between 130 to 100 to be returned 13V to 10V ??

CASE BatteryVoltage
130..129: OUTA[16..23]:=%11111111 ''13.0 Volts - Fully Charged 13V??
128..127: OUTA[16..23]:=%11111110 ''12.8 Volts
126..125: OUTA[16..23]:=%11111100 ''12.6 Volts
124..123: OUTA[16..23]:=%11111000 ''12.4 Volts
122..121: OUTA[16..23]:=%11110000 ''12.2 Volts
120..116: OUTA[16..23]:=%11100000 ''12.0 Volts
115..101: OUTA[16..23]:=%11000000 ''11.5 Volts
100..0 : OUTA[16..23]:=%10000000 ''10.0 Volts - Battery Just about Dead, 10V??


PUB GetVoltage(vRcTimePinNo) : vBatteryVoltage

RcTemp := 0 ''Initialise RcTemp
OUTA[vRcTimePinNo] := 0 ''Set it Hi to CHARGE capacitor
DIRA[vRcTimePinNo] := 1 ''Make pin an OUTPUT
WAITCNT(CLKFREQ / 2000 + CNT) ''PAUSE for 0.5ms
DIRA[vRcTimePinNo] := 0 ''Make pin an INPUT
RcTemp := CNT ''TRAP the current Time in RcTemp
WAITPEQ( 1, vRcTimePinNo, 0 ) ''WAIT until a change occurs on pin
RcTemp := || ( CNT-RcTemp) ''Calculate the time LAPSED
RcTemp := RcTemp - 1600 ''ADJUST the time for SPIN code executed
vBatteryVoltage :=RcTemp ''Divide by 16 and store in the Battery Voltage Variable






Thanks

Dave M

Peter Jakacki
09-09-2007, 09:27 PM
Dave,
You need to discharge the capacitor first then let the +12V charge it which by the way is what you are actually doing. The RC time constant does not change but the of course the time it takes to get to the logic inputs threshold voltage will decrease with higher voltage due to higher charge rate.

Why isn't RcTimePinNo set to the pin mask because 0 will not achieve anything. If it is P8 for instance then the mask should be %1_0000_0000.

*Peter*

EDIT:
My mistake Dave, it was late and I sometimes forget this is Spin on the prop. OUTA and DIRA do take a pin number, not a mask. Is P0 the pin you are using?

*Peter*

Post Edited (Peter Jakacki) : 9/9/2007 10:36:05 PM GMT

DavidM
09-10-2007, 08:37 AM
HI pete,

Yes PIN 0 is the pin I am using that is connected to the capacitor, Did you look at my schematic?
It also declares is here in my code..
--------
PUB Main

RcTimePinNo :=0
--------

So I gather I have to discharge that cap first, Is that what the PAUSE is for?

So I have modified my code as below.




PUB GetVoltage(vRcTimePinNo) : vBatteryVoltage

RcTemp := 0 ''Initialise RcTemp
DIRA[vRcTimePinNo] := 1 ''Make pin an OUTPUT
OUTA[vRcTimePinNo] := 1 ''Set it HIGH to DISCHARGE capacitor
WAITCNT(CLKFREQ / 2000 + CNT) ''PAUSE for 0.5ms ( Is this enough time for discharge?)

OUTA[vRcTimePinNo] := 0 ''Set it LOW to CHARGE capacitor ( Is this correct ?)
WAITCNT(CLKFREQ / 2000 + CNT) ''PAUSE for 0.5ms ( Is this enough time for charging?)

DIRA[vRcTimePinNo] := 0 ''Make pin an INPUT
RcTemp := CNT ''TRAP the current Time in RcTemp
WAITPEQ( 1, vRcTimePinNo, 0 ) ''WAIT until a change occurs on pin ( is this syntax correct?)
RcTemp := || ( CNT-RcTemp) ''Calculate the time LAPSED
RcTemp := RcTemp - 1600 ''ADJUST the time for SPIN code executed
vBatteryVoltage :=RcTemp ''Divide by 16 and store in the Battery Voltage Variable





Thanks

Dave M

DavidM
09-10-2007, 08:44 AM
One change I need..

WAITPEQ( 1, vRcTimePinNo, 0 ) ''WAIT until a change occurs on pin

should be..

WAITPEQ( 1, |<vRcTimePinNo, 0 )
the |< operator is a bitwise decode which will take any pin NUMBER 0-32 and convert it to the correct bit pattern

ie. %00000000_00000000_00000000_00000001

Is this correct?

Thanks

Dave M

Peter Jakacki
09-10-2007, 09:50 AM
See, I am confused. I remember now why I said you needed a bit mask and that was the reason. So that is correct. I assume you have already tried it then?

*Peter*

Peter Jakacki
09-10-2007, 11:02 AM
PUB GetVoltage(vRcTimePinNo) : vBatteryVoltage

RcTemp := 0 ''Initialise RcTemp
DIRA[vRcTimePinNo] := 1 ''Make pin an OUTPUT
OUTA[vRcTimePinNo] := 1 ''Set it HIGH to DISCHARGE capacitor !!! WRONG, this needs to discharge first so OUTA[pin] := 0
WAITCNT(CLKFREQ / 2000 + CNT) ''PAUSE for 0.5ms ( Is this enough time for discharge?) !!! you could make this 10us (470x

OUTA[vRcTimePinNo] := 0 ''Set it LOW to CHARGE capacitor ( Is this correct ?) !!! WRONG - don't do this, you want the input to charge the cap.
WAITCNT(CLKFREQ / 2000 + CNT) ''PAUSE for 0.5ms ( Is this enough time for charging?) !!! SKIP

DIRA[vRcTimePinNo] := 0 ''Make pin an INPUT
RcTemp := CNT ''TRAP the current Time in RcTemp
WAITPEQ( 1, vRcTimePinNo, 0 ) ''WAIT until a change occurs on pin ( is this syntax correct?)
RcTemp := || ( CNT-RcTemp) ''Calculate the time LAPSED
RcTemp := RcTemp - 1600 ''ADJUST the time for SPIN code executed !!! this is simply to zero offset the count, might need trimming
vBatteryVoltage :=RcTemp ''Divide by 16 and store in the Battery Voltage Variable !!! the reason you want to divide is you have too much resolution and noise




Although the capacitor charging is non-linear you will find that the initial charge curve is fairly linear so you can read the value and perform a simply calculation to get the volts.

*Peter*