40X2 and ReadADC

manie

Senior Member
Hi
I am running a 40x2 with the following program:

Code:
	tempw1=0
	for cntb2=1 to 15
		readadc10 b.1,tempw2
		tempw1=tempw1+tempw2
		pause 5
	next cntb2
	tempw2=tempw1/15
sertxd(#tempw2,cr,lf)
	tempw1=tempw2*493/100			'= mV
	
	cal3=tempw1/1000 min 0				'= whole Volts
	cal4=tempw1//1000 min 0				'= decimal Volts
sertxd(#cal3,"/",#cal4,cr,lf)
I am trying to read Volts from a battery on pin B.1.
I am getting nowhere fast. I do have an ADCSETUP in initialisation as follow:


Code:
	let adcsetup = %0000000001111111
	let adcsetup2 = %0000000000110000
What am I screwing up here ? Where do I make an error ?
Please help !
 

hippy

Technical Support
Staff member
What results do you get ? Is the value in 'tempw2' as would be expected ? What do you get if you undertake a single READADC10; is that as expected ?

B.1 is ADC10 on a 40X2 but your "adcsetup=" command does not set bit 10, but then it should not be necessary to manually set "adcsetup=" anyway.
 

manie

Senior Member
Hippy

I have tried both with and without the adcsetup variable. I get differing results, instead of +-12.0V I see 0.4 and 0.72 etc.
I am using a 400W switching PC power supply as "battery" here. Could that be the problem ?
I have tried with and without the averaging loop, ie. one single shot into tempw2 as well as accumulation and averaging.
Same result.

What you say about the adcsetup is the way I understood that. It sets the bit automatically with the READADC10 command.

I am at a loss here !
 

rossko57

Senior Member
Are you saying you are applying 12V , or possibly -12V, directly to pin B.1? This is not healthy for the 40X2
 

BESQUEUT

Senior Member
tempw1=tempw2*493/100 '= mV
I suppose that a voltage divisor is used so that battery voltage can be read as full scale when battery is fully charged.
So Tempw2 can be something like 500 to 1023...

So Tempw2*493 can be something like 246500 to 504339, that is far more than 65535...

Scaling is not so easy with Picaxe Math.
Depending on the significative part you need you may :
- approximate the calculations with numbers < 65535,
- use my Floating-Point-Library
- use an external FPU.

Also note that 4,93*1,024=5.05 V at full scale is not appropriate to read a 12 V battery actual voltage.
 
Last edited:

hippy

Technical Support
Staff member
I am at a loss here !
Without further specific information everyone else will be too.

As BESQUEUT notes, there may be overflow in your calculations; what you are reading may be correct but your calculations may be wrong or flawed.

Without knowing your full hardware configuration it is not possible to say what input values you should expect.

It is not even clear what input voltage you are applying; you say "instead of +-12.0V...", is that an AC signal being input or DC ? Are you trying to measure a voltage which varies between -12V and +12V ?
 

manie

Senior Member
Sorry Hippy and Besqueut is right too. I have changed from Readadc10 to Readadc because of overflow.
The results however, remain the same, now it reports 0.0 V each time.

The circuit is:
Code:
DC Volts (12V bat) in{-----10k--|--4k7--|to 0V
                                              |
                                             to Picaxe READADC input B.0 or B.1 (both used)
EDIT HERE:
The "to Picaxe..." part joins at the junction of the two resistors between 10K and 4k7.

I have changed the code to:

Code:
	let adcsetup = %0000011001111111

	'Feeding 12V(bat) DC via 10K/4k7 voltage divider

	tempw1=0
	readadc b.0,tempw2
sertxd(#tempw2,cr,lf)
	tempw1=tempw2*198/10		'= mV
	tempw2=tempw1*313/100/1000	'where 313=10k/4k7 = the integer number
	tempw3=tempw1*313 // 100	'= remainder or modulus
	tempw3=tempw3/ 1000		'final decimal volts 
sertxd(#tempw2," . ",#tempw3,cr,lf)
	cal3=tempw2 min 0			'= whole Volts
	cal4=tempw3 min 0			'= decimal Volts
sertxd(#cal3," . ",#cal4,cr,lf)
 

BESQUEUT

Senior Member
readadc b.0,tempw2
tempw1=tempw2*198/10 ' OK : tempw2*198 < 50688 ==> tempw1 < 5068

tempw2=tempw1*313/100/1000 ' KO : temw1*313=1586284 !

as intermediate calculation is < 65536
tempw2=65536/100 000=0
So you will always get zero.

I suppose that Picaxe Vref is 5V.
Example of correct code, but 100 mV increments...
Code:
'readadc10 b.1,w2

w2=1023 ' for simulation
w3=w2*64/419
sertxd (#w3,"00 mV",13,10)
Better one :
Code:
'readadc10 b.1,w2

w2=1023 ' for simulation
w3=w2*61/4
sertxd (#w3," mV",13,10)
 
Last edited:

hippy

Technical Support
Staff member
Code:
'Feeding 12V(bat) DC via 10K/4k7 voltage divider

readadc b.0,tempw2
sertxd(#tempw2,cr,lf)
That resistor divider will give a 0.32V input per 1V of battery. So what battery voltage do you have, what is the PICAXE power supply voltage, and what is the value placed in 'tempw2' ?

If you are connected to a 12V source that should be 3.837V presented to the ADC pin which, with a 5V PICAXE supply, should give a 'tempw2' value of around 197.

If you replace the READADC with a READADC10 that should give a 'tempw2' value of around 785.

What I guess is your conversion to voltage calculation also appears to be flawed.
 
Last edited:

marks

Senior Member
Hi manie,
and also try using a highword gives good accuracy

Vin x R1 / (R1+R2) / Vref x adc10 so
12 x 4700 / 14700 /5 x 1023 = 785

and too calculate your high word

Vreading / Vadc10
120 / 785 x 65536 + 1 = 10019

Code:
readadc10 b.1,w2

w2 = w2**10019
b6 = w2//10
b7 = w2/10
sertxd (#b7,".",#b6," V",13,10)
 

BESQUEUT

Senior Member
** calculation as suggested by marks is better than the 61/4 approximation.
As usual, I do not agree with the 1023 term, as there are 1024 steps in ADC measurements (see AN for the PIC...)
I have best results with :
w2=w2**10009
But this is not a big issue.

In order to achieve 15mV accuracy (not only resolution) you need a good 5V Vref...

By the way, if only measuring voltages from 11V to 16V
you can obtain 3 times better resolution (IE 5mV) with a voltage shifter in place of the resistor divider.
An op-amp with substractor configuration will do the job.
 
Last edited:

manie

Senior Member
Hi guys
Sorry for taking so long to get back to you, I have been away.
It seems the 40X2 is damaged as the circuit also has a 28x2 and using those ADC's it is fine.
Must have damaged the chip without knowing it somehow...........

I will get a new chip as I have done this a lot of times in the past. Only now I had problems.
But it seems to work out OK with the 28x2.

Thanks for your inputs, it was well worth the conversation.
 
Top