Re: 24 Volts Power Source - PIC based Voltmeter & ADC083
Hi
This circuit can be used in any digital cpu, no need for an A/D
Its quite simple, you need at least 2 pins.
PIN2 is used to turn on an NPN transistor with emmiter to gnd
PIN1 is used as voltage sense, i.e., zero or high
From your source voltage, you put a big resistor like 390K to a
100nF capacitor, This capacitor is discharged through gnd by the transistor.
You tie this capacitor to PIN1 with a 10K resistor.
Step 1: Discharge capacitor setting PIN2 to HIGH
Step 2: Turn PIN1 to INPUT
Step 3: ZERO or get a HIRES Counter
Step 4: Set PIN2 to LOW, stopp discharging
Step 5: Test PIN1 until HIGH or timeout
Step 6: Get COUNT
Step 7: Discharge until next loop or whatever, i.e., use PINS1&2 otherwise
So you will have a COUNT from start charging until the PIN1 fliped high.
Basically we can say that it will take twice the time to charge from 12V thant from 24v
Read these counts with 24V input, you will get a 24CAL count
so use this simple routine:
int getvalue(cal, count)
{
int result=0, shift=0x8000;
while(shift)
{
if( count >= cal) count -= cal;
else
{
result |= shift;
cal /= 2;
}
shift /= 2;
}
return (result);
}
Use this function passing 24CAL
so:
VBAT= (24 * getvalue(24CAL, COUNT)) /0x8000;
You may also try to make a mean voltage value doing
last_count = (last_count + count) /2 ;
But for indicating batery charge this very nice.
Best
PadsPCB