Using the A/D for Monitoring a Solor Cell and Battery

Thread: Using the A/D for Monitoring a Solor Cell and Battery

1. Using the A/D for Monitoring a Solor Cell and Battery

I'm working with circuit that monitors both a Solor Cell Panel output and a lead acid battery. I know that the A/D can only upto 5 volts so I have used a voltage divider in both circuits. my question is How do i get the A/D to output to a LCD that shows a voyage reading from 0 to 25 volts for the Solor Cell and 13 volts for the battery.

2. Hi Chuck,

This should help out. I Hope.

In the sample voltage divider shown below. If the input is 25Vdc then the output voltage will be about 4.38 Vdc.
With 10-bit A/D you should get an ADCin of around 898.
If you first scale that A/D value up to match the voltage divider ratio, you can then calculate the voltage the same way you would if you were reading 0-5 V.

The ratio of the voltage divider is (R1+R2)/R2.
To scale up the A/D reading you just multiply the value times the total resistance of the divider (R1+R2) then divide that number by R2.

898 * 5700 / 1000 = 5118
This is what the A/D would be if it could actually read 0-25 volts directly.

Now then, for 1 decimal place, multiply that by 50 and divide by 1023.
For 2 decimal places, multiply by 500 instead.

5118 * 50 / 1023 = 250 or 25.0Vdc

The program below is one way to do it in PBP.

HTH,
Darrel

Code:
```Res1   Var Word
Res2   Var Word
Rt     Var Word
Volts  Var Word

Loop:
Res1 = 4700          ' Change these to match your Voltage divider
Res2 = 1000          ' resistor values for the Solar Cell
Gosub CalcVoltage
LCDout \$FE,2,"Solar= ",DEC Volts/10,".",DEC1 Volts Dig 0," Vdc"

Res1 = 2200          ' Change these to match your Voltage divider
Res2 = 1000          ' resistor values for the Battery
Gosub CalcVoltage
LCDout \$FE,\$C0,"Batt = ",DEC Volts/10,".",DEC1 Volts Dig 0," Vdc"

goto Loop

CalcVoltage:
Rt = Res1 + Res2        ' Total resistance of Voltage Divider
Volts = DIV32 Res2
Volts = Volts * 50      ' Convert AD to Voltage
Volts = DIV32 1023
Return```
Last edited by Darrel Taylor; - 30th January 2014 at 03:32.

3. Thanks Darrel.

I had almost the same idea in the voltage divide, but I was using a 100k resistor and a 50K variable resistor. then adjust for 5v output. What I was having problems with was the ratio between the input voltage, divider and the A/D. I've work with the PIC processor alot but this is the first time using the A/D part of it.

Also thank you for the code sample. I can modify it work with both the Solor Cell and Lead Acid Battery since the processor has to monitor both and give a read out on the LCD.

My next question is, I've read that using DIV32 really slow down the processor. Is this correct? If so is there another solution?

Again Thank you for your time.

4. Chuck,

You should use smaller resistors for the divider.
This is from the datasheet for a 16F877
The maximum recommended impedance for analog sources is 10 kohm
Your 100k and 50k are much higher and can cause error in the reading due to the sample and hold time requirements.

The example I gave already works with both the "Solar Cell and Lead Acid Battery" so, not much to modify.

And, as far as the DIV32 goes... NO it does not slow things down substantialy. In fact, when you divide 2 WORDs just like you always do in PBP, it actually uses the same DIV32 routine internally to accomplish it. So it doesn't take very much time at all. I've heard that statement before, to the extent of some people saying to "Avoid it at all costs". Rubbish!! Use it as much as possible, and forgive them, for they know not what they say.

Darrel

5. Hi Darrel

I've entered the code that you gave me and I getting a error stating "bad expression" on the lines that contain
Volts = DIV32 Res2 and Volts = DIV32 1023.
Is this due to my version of my complier being 2.31?

Chuck Sieveking

6. Yes it is,

DIV32 was added in version 2.40

The current release is 2.45

You're missing out on a lot of other additions too.

Darrel

7. Dang!

Looks like I'm going to have to invest \$25 to get the upgrade.
Oh well I'll have to do that next week.

Thanks