PDA

View Full Version : Unusual ADC use and math



Archiver
11-27-2003, 02:01 AM
Hi All,

I have been working on a two way wireless temp. and humidity sensor. The
basic premise is that the master sends a command to the sensor, the sensor
takes a sample, then transmits it back to the master. The master is a B2SX,
and the sensor uses a PIC12F675.

The sensor is run from 4 "AA" rechargeable batteries, which output 5.2V when
fully charged. I want to be able to monitor the battery voltage and send it
to the master. I can't use an external Vref due to the shortage of I/O pins.
So what I am doing is feeding a regulated 2.5V into the ADC input, and
running the 12F directly from the batteries. As the battery voltage drops
(i.e. the internal Vref drops), the ADC reading changes upward. The ADC is
10 bit, and I only need .1V resolution. It seems like the math to do this
should be easy, but for some reason I can't make it work.

Thanks for the help,

Jonathan

www.madlabs.info

Archiver
11-27-2003, 05:20 AM
Hi Jonathan:

You can just read ADSESH for the top 8 bits and be sure to set ADCON0
for Left Justified.

What is it about the math?

Note, on the PIC list a few weeks ago there was a neat way presented to
do this to save battery power.

The obvious way to measure battery voltage is as you described, connect
a voltage reference that's about 1/2 of the nominal supply voltage to
the battery positive through a resistor and measure it's voltage on an
A/D input. Many of these Vf chips are similar to a Zeener diode, except
they still draw maybe 10 uA all the time.

To get around the current drain you can connect one side of the Vf to
ground with a large plastic cap across it and a resistor (maybe 5 to 10
k Ohms) to the PIC pin (no connection to the battery supply). Now when
you want to know the battery voltage you set the pin as a digital output
and make it high for as long as it takes for the cap to charge up to the
Vf voltage. Now change the pin to an A/D input and read the voltage
quickly. The number you read this way will need some fudge factor
applied since the cap will be discharging, but it should be easy to
calibrate.

This method can also be used to measure the temperature. If you're
using a 32 kHz xtal the total power consumption may get way down.

I incorporated this idea into my 12 LED board, also driven by a 12F675,
but now see that the LED brightness will indicate the battery
condition. I was also going to have a temperature sensor on the same
pin. See:
http://www.pacificsites.com/~brooke/PRC68COM.shtml#12LED

I'm using a single 123 photo battery on this board, it's 3 Volts and
will work well below freezing, where normal Alkaline batteries will
quit. I'd like to use a 32 kHz watch crystal instead of the internal 4
MHz oscillator, but don't have enough pins on the 12F675 so am
considering using a 16F676, the 14 pin version of the same chip.

Have Fun,

Brooke Clarke, N6GCE

>Date: Wed, 26 Nov 2003 10:01:11 -0800
> From: "Jonathan Peakall" <jpeakall@p... (http://forums.parallaxinc.com/group/basicstamps/post?postID=DQIug3Yfs76L2uQcqQ2ZsYNrAboEgYtyuB30I-dgis4bL-u9L4hVNn7cnGYCWr6IFn0YOk_E6yOPfOC6vw)>
>Subject: Unusual ADC use and math
>
>Hi All,
>
>I have been working on a two way wireless temp. and humidity sensor. The
>basic premise is that the master sends a command to the sensor, the sensor
>takes a sample, then transmits it back to the master. The master is a B2SX,
>and the sensor uses a PIC12F675.
>
>The sensor is run from 4 "AA" rechargeable batteries, which output 5.2V when
>fully charged. I want to be able to monitor the battery voltage and send it
>to the master. I can't use an external Vref due to the shortage of I/O pins.
>So what I am doing is feeding a regulated 2.5V into the ADC input, and
>running the 12F directly from the batteries. As the battery voltage drops
>(i.e. the internal Vref drops), the ADC reading changes upward. The ADC is
>10 bit, and I only need .1V resolution. It seems like the math to do this
>should be easy, but for some reason I can't make it work.
>
>Thanks for the help,
>
>Jonathan
>
>www.madlabs.info
>
>
>
>
>
>
>
>

Archiver
11-27-2003, 05:43 AM
>Hi All,
>I have been working on a two way wireless temp. and humidity sensor. The
>basic premise is that the master sends a command to the sensor, the sensor
>takes a sample, then transmits it back to the master. The master is a B2SX,
>and the sensor uses a PIC12F675.
>The sensor is run from 4 "AA" rechargeable batteries, which output 5.2V when
>fully charged. I want to be able to monitor the battery voltage and send it
>to the master. I can't use an external Vref due to the shortage of I/O pins.
>So what I am doing is feeding a regulated 2.5V into the ADC input, and
>running the 12F directly from the batteries. As the battery voltage drops
>(i.e. the internal Vref drops), the ADC reading changes upward. The ADC is
>10 bit, and I only need .1V resolution. It seems like the math to do this
>should be easy, but for some reason I can't make it work.
>Thanks for the help,
>Jonathan
>www.madlabs.info

Hi Jonathan,

More math!

Bvolts = 51200/ADCresult*5 ' adc result is 10 bits
DEBUG Dec Bvolts/100,".",Dec2 Bvolts,CR ' resolution 0.05 volt

example ADCresult=512 1/2 scale exactly
Bvolts=5.00
example ADCresult=506
Bvolts=5.05

Reasoning: volts_per_bit=Bvolts/1024=2.5/ADCresult
==> Bvolts = 2.5 * 1024 / ADCresult
= 2560 / ADCresult
= 51200 / ADCresult * 5 /100
The division by 100 is taken care of by the placement of the decimal
point in the DEBUG. Best precision in division with large numerator.

To round off up and down you might use this formula instead:

Bvolts = (51200+(ADCresult/2))/ADCresult*5 ' adc result is 10
bits, rounded


-- regards,
Tracy
http://www.emesystems.com

Archiver
11-27-2003, 06:10 AM
In a message dated 11/26/2003 4:46:25 PM Eastern Standard Time,
jpeakall@p... (http://forums.parallaxinc.com/group/basicstamps/post?postID=20WwG0GvPE6Szk2Hzj_3M1K83JyJiqcP51jcl0 LeJdpqtdcDJJ5NdKU-C_JnbDhfoVVPsXdt7jMDLFSRxjM) writes:


> I have been working on a two way wireless temp. and humidity sensor. The
> basic premise is that the master sends a command to the sensor, the sensor
> takes a sample, then transmits it back to the master. The master is a B2SX,
> and the sensor uses a PIC12F675.
>

Jonathan, without doing a lot of math, I think your best bet would be to hook
a pot across the 5.2V - at least 10K - and feed the wiper to the ADC.
Adjust the pot so that the ADC sees 2.5 volts - full count - when the supply
voltage is
5.2V. Then multiply the ADC output by 2.08.

A DECIMAL?? Easy - write:

ADC = ADC*/$0214

That should get you pretty close.

Sid






















[Non-text portions of this message have been removed]

Archiver
11-27-2003, 11:47 AM
Tracy,

>
> More math!

I know, it's always math. As I was just complaining to Brooke, I didn't get
the math gene OR the art gene. Jeez.
>
> Bvolts = 51200/ADCresult*5 ' adc result is 10 bits
> DEBUG Dec Bvolts/100,".",Dec2 Bvolts,CR ' resolution 0.05 volt
>
> example ADCresult=512 1/2 scale exactly
> Bvolts=5.00
> example ADCresult=506
> Bvolts=5.05

Works perfectly, of course.


>
> Reasoning: volts_per_bit=Bvolts/1024=2.5/ADCresult
> ==> Bvolts = 2.5 * 1024 / ADCresult
> = 2560 / ADCresult
> = 51200 / ADCresult * 5 /100
> The division by 100 is taken care of by the placement of the decimal
> point in the DEBUG. Best precision in division with large numerator.
>
> To round off up and down you might use this formula instead:
>
> Bvolts = (51200+(ADCresult/2))/ADCresult*5 ' adc result is 10
> bits, rounded

And that makes it clear too. All hail Tracy, the math god!

Jonathan

www.madlabs.info

Archiver
11-27-2003, 11:51 AM
Sid,

I'll play with that when I get back home from Thanksgiving.

Thanks!

Jonathan

----- Original Message -----
From: <Newzed@a... (http://forums.parallaxinc.com/group/basicstamps/post?postID=BhENPdoKQNPk2AWiWJnZCHRsLNeVXl9hTcJZ_K UKXshqQBb5clRbyNwYdD1jaA2DwjFz1g)>
To: <basicstamps@yahoogroups.com (http://forums.parallaxinc.com/group/basicstamps/post?postID=9vNynavQyoI_6reaevfXJ7KUnAsYr13eUJYKrR EEgDuoicd1XqidhAOjrUh5dnUxqPuuMgNnjYqmzeAIGd2O2Qo)>
Sent: Wednesday, November 26, 2003 2:10 PM
Subject: Re: [basicstamps] Unusual ADC use and math


> In a message dated 11/26/2003 4:46:25 PM Eastern Standard Time,
> jpeakall@p... (http://forums.parallaxinc.com/group/basicstamps/post?postID=6N41bKReN_Fo6wdCc7Ov5y5EvqLtL1McMS5wdn xhKbRjnORVDk7Q0lPW3xs_NwIlJtTSPlHs7gFxIIJHDA) writes:
>
>
> > I have been working on a two way wireless temp. and humidity sensor. The
> > basic premise is that the master sends a command to the sensor, the
sensor
> > takes a sample, then transmits it back to the master. The master is a
B2SX,
> > and the sensor uses a PIC12F675.
> >
>
> Jonathan, without doing a lot of math, I think your best bet would be to
hook
> a pot across the 5.2V - at least 10K - and feed the wiper to the ADC.
> Adjust the pot so that the ADC sees 2.5 volts - full count - when the
supply
> voltage is
> 5.2V. Then multiply the ADC output by 2.08.
>
> A DECIMAL?? Easy - write:
>
> ADC = ADC*/$0214
>
> That should get you pretty close.
>
> Sid
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> [Non-text portions of this message have been removed]
>
>
> To UNSUBSCRIBE, just send mail to:
> basicstamps-unsubscribe@yahoogroups.com (http://forums.parallaxinc.com/group/basicstamps/post?postID=J5qYvHdJY_bqPjHLCq1g4AFSZD1JLlwqOtY4pz LMJecp4gU_L5Bm9CfwtUy_81YuokvswnrOhbWiqTWgn-haFHF-EdNhIKqQZ7WgaFbi)
> from the same email address that you subscribed. Text in the Subject and
Body of the message will be ignored.
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>
>