PDA

View Full Version : ADC lcd display help



kevlar129bp
- 14th July 2010, 22:30
Hello all,

I am finally able to re-visit an old project of mine and am in need of a bit of assistance.
I've got a 16F886 up and going with one exception... the ADC.
I've got a DC source I'm monitoring with 10 bit res. on AN0.
The details:
DC can swing from 0-350 vdc.
This runs through a 70:1 scaler and is clamped to 5.1 vdc.
So at 350 vdc, max scaled voltage will be 5 vdc. (350/70)
...
This is where I get way confused :confused:
Can one of you fine folks explain how to determine the ADC "count" based on my 0-350 vdc?
and
How to convert the ADC counts back to the 0-350 vdc in order to display it on an lcd?
...
Thank you all,
Chris

Charles Linquis
- 15th July 2010, 00:19
10 bit A/D count = 1023 when Vin = Vref
12 bit A/D count = 4096 when Vin = Vref

Charles Linquis
- 15th July 2010, 00:20
Correction:

12 bit A/D count = 4095 when Vin = Vref

kevlar129bp
- 15th July 2010, 02:01
Thanks Charles,

I think I grasp that concept.
350/1024 = .3418 ... so for every .3418 volt change at the 350 side of things, there is a "count" change.
Therefore, if we start with, say:
165 vdc
...through the voltage scaler to the ADC in:
165/70 = 2.3571 vdc
VRef(5.1)/1024 = .004883
2.3571/.004883 = 482.7115 <<<<<<<<<<<<<<<<<<< The ADC "count" would be 482?
(ADC count * 4883 * Scaler ratio) / 10000
(482*4883*70) / 10000 = 16475.242<<<<<<<<<<<<<<<<< 16475 is the "high voltage"?
...for the lcd:
LCDOut 16475/100,".",16475//100," VDC"
"164.75" <<<<<<<<<<<<<<<<<<<<<<<<< Where did I go wrong in not getting "165"?

I am probably confusing myself more than I'm helping.
Would love a quick tutorial from one of you knowledgeable folks.

Thanks again,
Chris

mackrackit
- 15th July 2010, 03:43
At the risk of confusing you more...
http://www.picbasic.co.uk/forum/content.php?r=167-Sensor-Scaling

kevlar129bp
- 15th July 2010, 15:38
Thanks Mack,

Funny you point out that thread... That's the one I've been racking my brain over. :)
I'll take another look though.

Chris

kevlar129bp
- 15th July 2010, 15:53
Hey Mack,

Quick question. When we're talking about the ADC count based on the sampled voltage,
what happens to the count when it's fractional?
IE:
count = 557.26 ... does this become 557 or 558?
count = 952.76 ... does this become 952 or 953?

I guess I'm just wondering what the "rounding" routine is, if any?

Thanks again,
Chris

mackrackit
- 15th July 2010, 16:04
http://www.picbasic.co.uk/forum/showthread.php?t=12942&p=87177#post87177

kevlar129bp
- 15th July 2010, 17:14
Thanks for the heads up Mack,

I guess I was a bit confused by this in your ADC scaling tutorial:

Convert this to an 10 bit PICŪ resolution.

10 bits ADC = 0 to 1023, 1024 steps.
At 5.0 volts one volt of input will will read 205. (204.6 actually)

Why would that read 205, instead of 204?
Thanks Mack, for helping a newbie trying to understand. :)

Chris

mackrackit
- 15th July 2010, 18:10
Why would that read 205, instead of 204?


Thanks for catching that and I apologize for the confusion. :o

I changed the article to


10 bits ADC = 0 to 1023, 1024 steps.
At 5.0 volts one volt of input will will read 204. (204.8 actually)
But we will use 205 for accuracy.
204 * 5 = 1020
205 * 5 = 1025
Hope that clears things up a bit..

kevlar129bp
- 16th July 2010, 15:12
Hey Mack,

I sketched this code in notepad after reading some other posts...
Does it look like I'm on the right track? :confused:

ADVal VAR WORD
Quanta CON 489 'VREF/1024 = 5/1024 = .00488 = 489
VoltageDivider CON 70
ADCVolts VAR BYTE
LCDVolts VAR WORD

ADCIN 0,ADVal
ADCVolts=(ADVal/1000)*Quanta
LCDVolts=ADCVolts*VoltageDivider
LCDout $fe, 1
LCDout $fe, 2, "CURRENT READINGS ARE"
Lcdout $fe, $C0, "A/D VDC = ",DEC1 ADCVolts/100,".",DEC2 ADCVolts//100
Lcdout $fe, $94, "--------------------"
Lcdout $fe, $D4, "ACTUAL VDC = ",DEC3 LCDVolts/100,".",DEC2 LCDVolts//100

Thanks again for your assistance,
Chris

mackrackit
- 16th July 2010, 17:12
Really close!!
But I think I would try it like this.


ADVal VAR WORD
Quanta CON 342 ' 350 / 1023 * 1000
LCDVolts VAR WORD
Z VAR WORD 'DUMMY VAR
Z1 VAR WORD 'DUMMY VAR

ADCIN 0,ADVal
Z = ADVal * 342
Z1 = DIV32 1000
LCDVolts = Z1

fratello
- 18th May 2015, 18:06
Need help ...with this math. Please !
I use voltage divider ; R1=1000 ohms; R2= 50 to 3500 ohms.
I read the voltage :

ADCON0 = %10000001
Pauseus 50 ' Wait for channel to setup
ADCON0.1 = 1 ' Start conversion
While ADCON0.1=1:Wend ' Wait for conversion
advalue.HighByte=ADRESH ' Read variable from ADC and save
advalue.LowByte=ADRESL
I try to calculate the resistor(s) R2, assuming that I do not know values.
Formula is (?) :
R2= 1000 * advalue/(1024-advalue)
I wrote this code :

dummy =1000 * advalue
resistor = div32 10
resistor = (resistor / (1024-advalue)) * 100
I want to display on Nokia 3310 :

'~~~~~~~~~~~~~~~~~ setting cursor for display the advalue
LcdReg = %10000000 + 5 ' cursor X
call PrintCtrlLcd
LcdReg = %01000011 ' cursor Y
call PrintCtrlLcd
'~~~~~~~~~~~~~~~~~ now display results

Char = (advalue dig 3)
call PrintChar

Char = (advalue dig 2)
call PrintChar

Char = (advalue dig 1)
call PrintChar

Char = (advalue dig 0)
call PrintChar
.............
Char = resistor dig 4
call PrintChar
Char = resistor dig 3
call PrintChar
Char = resistor dig 2
call PrintChar
Char = resistor dig 1
call PrintChar


But I can not to display the last digit (from right) of values of resistors ...I have "50" instead "54" ; "140" instead "147" ; "1030" instead "1036" ...

Please, point me to the right direction ! Thanks in advance !

HenrikOlsson
- 18th May 2015, 20:33
Hi,
The result from the ADC ranges from 0 to 1023, not 1024. Try using 1023 in the formula and see if that makes it better.

With that said the whole thing is a bit convoluted....Why do you multiply by 1000 only to divide the result by 10 directly after?

What's the voltage at the top of the resistor divider and what's VRef?
If both are 5V then the highest voltage on the ADC input would be 5/(1000+3500)*3500=3.888V giving an ADC reading of 3.888/(5/1024)=796.
If you multiply 796 by 100 (multiply by 1000, divide by 10) the result will still overflow a WORD.

/Henrik.

fratello
- 19th May 2015, 06:18
Thanks !
I tried with 1023, but no modification. I need to have the result of resistor(s) into a word variable like this "3499", so I can displaying on my N3310 display.

HenrikOlsson
- 19th May 2015, 07:13
Hi,
I'll try again.... If R1 is 1k and R2 is 3.5k and the voltage at the top of the divider as well as VRef is 5V then you'll get an ideal ADC reading of 796, which will be your highest reading. So far so good.

Now, doing the dummy=1000*advalue isn't a problem, the problem comes when you do Resistor = div32 10 becasue the result of that calculation WILL overflow the WORD sized variable resistor (79600) so the concept is flawed.

Apart from that you don't have enough resolution to "get to" the last digit. Your resistive range is 3450 ohms, the voltage at the ADC input will range from 0.238V to 3.889V giving you ADC readings ranging from 48 to 796.
So all you have is 796-48=748 "steps" for your total range of 3450 ohms, a resolution of roughly 3450/748 = 4 ohm which is why you get 1036 instead of 1030 and 54 instead of 50.

You can use oversampling to increase the resolution of the measurement.

/Henrik.

fratello
- 19th May 2015, 07:31
Now I understand ! Thank You so much !!!

Dave
- 19th May 2015, 12:29
Here is an OLD example from our not to be forgotten friend Daryl.

fratello
- 19th May 2015, 13:13
Thanks ! Verry good example ...
I miss him too :frown: ...