X2 adc voltage reference

KTarke

Senior Member
According to manual2, adcsetup-page, there IS a way to use external Vref for ADC.
As a new user of Picaxe, I need some feedback from the more experienced users...

Have I understood correctly: If i put a resistor divider (10k-10k) to Vref-input, I get full adc-reading at 2,5V?

This would help expanding resolution ,and one could get rid of op-amp on input...

I have a sensor, which gives out 2,5V at reading 0 (it measure negative values too, but I don't need them). I only use the output range of 2,5V to 3V (2mV resolution). I have since used op-amp to "expand" the 2,5-3V area... (edit: sensor gives out max. Vcc ,and uses the same Vcc with Picaxe = (well)regulated 5V)

Has anyone ideas, if adc resolution can be changed, what kind of voltage dividers (for Vref AND adc input) should be used to measure 2,5V-3V with 2mV resolution AND get rid of the op-amp (which gives disturbance...)
 
Last edited:

hippy

Technical Support
Staff member
@ lanternfish : That's more comparators rather than ADC Vref, but well worth a link anyway. It did make me think that one could use the comparators and Vref to do auto-ranging to maximise low voltage resolution, but that's for another day !

@ KTarke : Yes the Vref capabilities can be used so what you say is correct; Vref+ to a 10K/10K gives a 2V5 reference for Vref+, ADC will read zero to full ($FF/$3FF) for an input between 0V and Vref.

I'm not sure what negative voltages into Vref- will do but I believe it is possible to set Vref- to 2V5, Vref+ to 3V0 and then ADC will read zero to full for between 2V5 and 3V0. Using READADC10 that's 1024 steps of resolution, about 500uV in theory. Reality is likely to be less accurate.

Which PICAXE are you using ? The 20X2 can only use Vref+ and not Vref- due to the silicon design.
 

Dippy

Moderator
:( Begin aside.

NEVER put a negative voltage on Vref- !!

Microchip are a bit silly calling it "VRef-".
It is very ambiguous and asking for trouble - unless the PIC Data Sheet is read.
You should consider "VREF-" as "VrefLow".

Microchip also refer to the selection of this as "VREFL" which is a far more sensible and less ambiguous description.
If you want the actual full spec (e.g Vss-0.3V min etc.) then read the Data Sheet.

End of aside :)

It's well worth looking at the limitations of V+ and V- (VREFH and VREFL).
There are some significant differences between non-K and K series for instance.
Well worth reading.
Example:- (18F2520 versus 18F25K20)

VREFH.
18F2520 Min = Vss
18F25K20 Min = Vdd/2
... certainly something to keep in mind if something goes funny.
 

MPep

Senior Member
Microchip are a bit silly calling it "VRef-".
It is very ambiguous and asking for trouble - unless the PIC Data Sheet is read.
You should consider "VREF-" as "VrefLow".
This reminds me of the many circuits I see with +12V and -12V. Does this mean 24V overall? Or is it in fact 12V, where the -12V is actually the -ve of the 12V supply, or as most others would prefer "0V"???:confused:

Too bad that we all try and follow standards. What makes it worse is considering the fact that there are sooo darn many standards.:eek:
 

KTarke

Senior Member
:( Begin aside.

NEVER put a negative voltage on Vref- !!

Microchip are a bit silly calling it "VRef-".
It is very ambiguous and asking for trouble - unless the PIC Data Sheet is read.
You should consider "VREF-" as "VrefLow".

Microchip also refer to the selection of this as "VREFL" which is a far more sensible and less ambiguous description.
If you want the actual full spec (e.g Vss-0.3V min etc.) then read the Data Sheet.

End of aside :)

It's well worth looking at the limitations of V+ and V- (VREFH and VREFL).
There are some significant differences between non-K and K series for instance.
Well worth reading.
Example:- (18F2520 versus 18F25K20)

VREFH.
18F2520 Min = Vss
18F25K20 Min = Vdd/2
... certainly something to keep in mind if something goes funny.
Thank You for warning!
But...
In my case,my sensor gives out only positive voltage, from 0 to 5V, and the "measuring 0" is at 2,5V. Meaning, that if the sensor reads "negative" values, output is between 0 and 2,5V...
EVEN IF I set the Vref- to 2,5V, and sensor gives to input less than that, it oly causes overflow... it is actually not negative (less than 0V) voltage anyway, and can't harm the chip. Am I right or wrong?

Picaxe 28X2 has label "PIC18F2520" on it.
 

KTarke

Senior Member
@ lanternfish : That's more comparators rather than ADC Vref, but well worth a link anyway. It did make me think that one could use the comparators and Vref to do auto-ranging to maximise low voltage resolution, but that's for another day !

@ KTarke : Yes the Vref capabilities can be used so what you say is correct; Vref+ to a 10K/10K gives a 2V5 reference for Vref+, ADC will read zero to full ($FF/$3FF) for an input between 0V and Vref.

I'm not sure what negative voltages into Vref- will do but I believe it is possible to set Vref- to 2V5, Vref+ to 3V0 and then ADC will read zero to full for between 2V5 and 3V0. Using READADC10 that's 1024 steps of resolution, about 500uV in theory. Reality is likely to be less accurate.

Which PICAXE are you using ? The 20X2 can only use Vref+ and not Vref- due to the silicon design.
I am using 28X2 = PIC18F2520.

Thank You Hippy! You verified just, what I was thinking (about setting references to 2,5 and 3V, and getting better resolution W/o opamp)

And yes, I was just reading Microchip datasheet for pic18F2520 and application note DS33023. (the latter was little hard to find, because it is "renamed" to DS33023A)
In those were explained the "onboard" resistor ladder for creating reference voltage, and it works through comparators...

For now I am still confused, didn't yet understand, HOW to use internal Vref, comparators and single adc input pin to do the job described earlier.
(or yet haven't found easy enough example code for me to understand...)
 

hippy

Technical Support
Staff member
The trick is all in ADCSETUP ( page 25 in PICAXE Manual 2 - Basic Commands ). Assuming you are operating your 28X2 from a 5V supply, so any voltages between 0V and 5V will be safe to use; untested, but this is what you are looking for ...

The lower 14-bits of ADCSETUP represent a bit mask of which ADC channels will be used as analogue. So if we are reading this 2V5-3V0 on leg 2, pin A.0, ADC channel 0 ...

Let adcSetup = %0000000000000001
ReadAdc 0, b0

That should read around 128 ($7F) when the signal is 2V5.

Now connect Vref+ ( leg 5, pin A.3, ADC 3 ) to 3V0

And connect Vref- ( leg 4, pin A.2, ADC 2 ) to 2V5

Because we wish to use Vref+ we have to set bit 14 of ADCSETUP, to use Vref- we set bit 15, and we enable ADC channels 2 and 3 ...

Let adcSetup = %1100000000001101
ReadAdc 0, b0

If it has all worked, as the input voltage goes from 2V5 to 3V0 the 'b0' reading should go from 0 to 255 ($FF).
 

KTarke

Senior Member
The trick is all in ADCSETUP ( page 25 in PICAXE Manual 2 - Basic Commands ). Assuming you are operating your 28X2 from a 5V supply, so any voltages between 0V and 5V will be safe to use; untested, but this is what you are looking for ...

The lower 14-bits of ADCSETUP represent a bit mask of which ADC channels will be used as analogue. So if we are reading this 2V5-3V0 on leg 2, pin A.0, ADC channel 0 ...

Let adcSetup = %0000000000000001
ReadAdc 0, b0

That should read around 128 ($7F) when the signal is 2V5.

Now connect Vref+ ( leg 5, pin A.3, ADC 3 ) to 3V0

And connect Vref- ( leg 4, pin A.2, ADC 2 ) to 2V5

Because we wish to use Vref+ we have to set bit 14 of ADCSETUP, to use Vref- we set bit 15, and we enable ADC channels 2 and 3 ...

Let adcSetup = %1100000000001101
ReadAdc 0, b0

If it has all worked, as the input voltage goes from 2V5 to 3V0 the 'b0' reading should go from 0 to 255 ($FF).
I have used nearly the whole day experimenting with this, and have already reached nearly same results, but not quite...

First, the manual (2) gave me headache, it is misleading... page 26 gives one expression, that when using 28X2 5V, pin masking is sequential ,and one can NOT use command "adcsetup = %1100000000000001" ....
After a (long) while I found out that it is not true, and managed to switch on external references on pins 2 and 3.

Then I first set up only negative Vref to Vcc/2. It did nicely move "0" to 2,5V, as wished, BUT it seemed NOT to have any affect on the resolution! That is odd, "halving" the voltage area SHOULD make resolution better, but it didn't.

Setting positive Vref to ~3V DID make resolution better... I don't understand, why adjusting negative Vref did not...

Reading the microchip datasheet I understood, that it is irrelevant, if analog ports 2 and 3 are set up as inputs or not... (in this case the connection is made BEFORE the input multiplexer) and test showed too, that doesn't matter if one uses %1100000000000001 or %1100000000001101

So, with external resistors this can be done! And one can leave away opamp from between sensor and input.

After succeeding in it, I have tried to use the internally generated reference voltage (to leave out the external resistors...)
THAT have I not been succesfull with, yet anyway...

I tried to use compsetup %01000011 , %10011100 to make internal reference voltage of 2,5 (negative ref), but it is not working
(I've tried some other combinations too, but yet no success)
When using compsetup, I also switched on ports 2 and 3 (adcsetup = % 1100000000001111) In sequential setup must all ports 0-3 switched on.

Interesting piece, this Picaxe, but takes a lot of time....:)
 

hippy

Technical Support
Staff member
First, the manual (2) gave me headache, it is misleading... page 26 gives one expression, that when using 28X2 5V, pin masking is sequential ,and one can NOT use command "adcsetup = %1100000000000001" ....
After a (long) while I found out that it is not true, and managed to switch on external references on pins 2 and 3.
The manual is correct - 28X2 (5V) has sequential masking ( all channels below the highest enabled will also be enabled ), the 28X2-3V has individual pin masking. I got that wrong in my description earlier, apologies for that.

Reading the microchip datasheet I understood, that it is irrelevant, if analog ports 2 and 3 are set up as inputs or not... (in this case the connection is made BEFORE the input multiplexer) and test showed too, that doesn't matter if one uses %1100000000000001 or %1100000000001101
It's not so much that it doesn't work but that it shouldn't be done. What's often not shown in the ADC diagrams is that the VREF and analogue channels also connect to the digital I/O internals and Microchip recommend that non-digital voltages are not applied to the digital I/O internals. Setting the bits disconnects the digital I/O parts from the analogue signal.

I suppose in some ways it should be thought of, not so much as, "analogue enabling" but "digital disabling".

Analogue ( and VREF ) will work with the digital parts enabled - Microchip say so - but it's the affect on the digital parts which is important, page 232, 19.5 note 2 in my datasheet, "Analog levels on any pin defined as a digital input may cause the digital input buffer to consume current out of the device’s specification limits".

Unfortunately that "may" is not quantified in any way. The choice is to follow the recommendation to disable I/O parts when used as ADC or hope that nothing bad will happen.

Interesting piece, this Picaxe, but takes a lot of time....:)
I do think Microchip made it particularly hard for everyone on this particular range of chips. Not so much in the use of analogue, but in the configuration of this disabling digital parts.

If only dealing with analogue it's not really a problem. If wanting digital inputs as well one has to take care how the analogue is allocated as using recommended settings means some digital inputs are lost ( always read low ). For example, use ADC 12 ( leg 22, pin B.0 ), set ADCSETUP as recommended and you lose digital input capability on a further 8 pins ( A.0-A.3 and B.0-B.3 ). They can still be used as digital outputs so not as bad as it could have been !
 

KTarke

Senior Member
The manual is correct - 28X2 (5V) has sequential masking ( all channels below the highest enabled will also be enabled ), the 28X2-3V has individual pin masking. I got that wrong in my description earlier, apologies for that.
I meant merely, that the examples in the manual gave me an expression, that one canNOT use a 16-bit MASK with X2-5V.(examples show only setting the low bits,cocerning ports) Therefore took me some time to understand, that bits 15 and 16 CAN be set also in sequential models.

It's not so much that it doesn't work but that it shouldn't be done. What's often not shown in the ADC diagrams is that the VREF and analogue channels also connect to the digital I/O internals and Microchip recommend that non-digital voltages are not applied to the digital I/O internals. Setting the bits disconnects the digital I/O parts from the analogue signal.

I suppose in some ways it should be thought of, not so much as, "analogue enabling" but "digital disabling".
Okay, understandable.
I was thinking, that when I set up a external voltage to pins 2 and 3, and the route to adc:s reference-part goes straight from the input (before multiplexer) , I shouldn't connect those inputs to adc at all... didn't consider the digital-input-part at all.

Thank You, Hippy, again for good advice.
Those things seem to be VERY hard to dig out even from Microchips document, not to mention Picaxe documents, which tell only the main parts...
 
Top