Entire Forum This board This topic Members Entire Site
 Pages: [1] 2  All
 Author Topic: A question about adc converting  (Read 8531 times) 0 Members and 1 Guest are viewing this topic.
sphinx
Hero Member

Offline

Posts: 917

Thank You
-Given: 609

 « on: August 11, 2019, 04:53:34 16:53 »

I am fiddling with a software pic project with a pic6f677 and use 10bit adc for measuring analog voltages.
I was curious about how to use the adc conversion the full 10bits 0-1023 to measure or do i use only 0-1000,
or do i make the 1 bit value for calculation a simple value like 0.1 and calculate from there.
i am curious about how you pro guys do it.
 Logged

laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
h0nk
Senior Member

Offline

Posts: 257

Thank You
-Given: 216

 « Reply #1 on: August 11, 2019, 06:08:13 18:08 »

Hello sphinx,

to correct the range from 0 to 1023 to the range 0 to 999 You can
do the following easy math:

display = x - (3 * x / 128)

This should even for a midrange PIC easy enough
(four 16 bit additions/subractions and one 16 bit shift).

E.g. if i want to measure the range from 0 to 5 V, i would do 5 conversions
This gives an integer from 0 to 5115. If needed this can be corrected
by the above formula. I think you get the idea...

If Your measurements require more complicated ratios, You should look for
factors which can be scaled by shifts afterwards.

Best Regards
 Logged
PICker
Active Member

Offline

Posts: 162

Thank You
-Given: 207

 « Reply #2 on: August 11, 2019, 07:09:40 19:09 »

Hi,
if the data acquisition is ratiometric (from GND to VCC), with no external VREf, you can simply do this simple calculation:

VCC mV / ADC nbits = value in mV of a single LSB

i.e. if you have 3.3V VCC you will have:

3300 / 1024 = ~3.22 mV (rounded) for every ADC step (LSB)

If you multiply your digital data (i.e. 511 from the ADC) for 3.22 mV you obtain the real voltage (511 x 3.22 = ~1650 mV, close to 1/2 VCC) .
 Logged
Magnox
Senior Member

Offline

Posts: 250

Thank You
-Given: 983

Oink!

 « Reply #3 on: August 11, 2019, 08:28:40 20:28 »

If you run the PIC at 5.12V Vdd (and are using Vdd as the ADC's reference), then one ADC count will be 5mV.
 Logged
FTL
Junior Member

Offline

Posts: 83

Thank You
-Given: 170

 « Reply #4 on: August 11, 2019, 08:37:38 20:37 »

An important consideration is how much speed you need and how often you do the calculations on your AD results. If you are doing A->D conversions every millisecond or more, speed counts. Scale the result into 16 bit integers and try to multiply and divide by 2 wherever possible. They will be compiled as shifts (single instructions) not actual multiplies (several instructions) or divides (even more instructions). Even scale down to 8-bit integers if you can since that will more than double the calculation speed (probably more like 4-8 times faster).

Try to use unsigned integers so you don't suddenly get a negative value in the middle of a calculation because a value exceeding the maximum positive value for a signed integer. Also sometimes you need to use lots of brackets to force the calculation to be done in an order that will make sure that an intermediate result does not get too big or too small. the compiler may still try to optimise the order in which multiplies and divides are done, since in pure math it does not matter, but in the real world where numbers have a maximum value and you are doing integer arithmetic it can matter. I have sometimes broken up a complex calculation into several lines using intermediate variables to try to stop the compiler from re-ordering my calculations.

If you are only doing a conversion once a second and nothing is time critical, just go ahead and use floats. Convert the 0-1023 integer to a float right after the AD conversion and use the float from there on. They are slow (many cycles (100+) used for just about any arithmetic operation), but it doesn't matter as the processor will just be in a wait loop most of the time anyway.

I was building something that was doing calculations for an LCD display. After a bunch of head scratching trying to optimise the arithmetic, I realised that I was only updating the LCD twice a second, so doing a floating point divide (which can use in the range of hundreds of instructions) twice a second, which is normally a very high overhead operation used less than 0.1% of the CPU cycles, so it just did not matter and was way easier to code and design.
 Logged
sphinx
Hero Member

Offline

Posts: 917

Thank You
-Given: 609

 « Reply #5 on: August 12, 2019, 06:05:28 06:05 »

I am measuring values on 4 inputs converted into 4 values 2 will display 0-2.56 and the other 2 will display 0-25.6 done with pic16f677 and dispplaying thosee on a 2 row lcd display. I have the
oscillator inputs free so i can use external clock if i want. i dont think there is anything time critical in doing this i would like 2-4 updates per second on the LCD. This the only thing the pic will
do.

Operation of the circuit:
inputs 1 and 2 are used with 0-5V one for set voltage and one for set maximum amperage both will use 10turm potentiometers
input 3 will read the actual voltage 0-25.6V
input 4 will read the actual drawn ampere 0-2.56A or 0-5.12A
those values are then converted/calculated to display on 2x16 LCD
i would like the updates to be between 2-5 times per second.

this will be done with CCS PIC-C Compiler 5.xxx

/sphinx

p.s. i thats all i can think of.
 Logged

laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
Magnox
Senior Member

Offline

Posts: 250

Thank You
-Given: 983

Oink!

 « Reply #6 on: August 12, 2019, 05:54:44 17:54 »

This is what I would do for input 3, the 25.6V full scale, assuming Vdd=Vref=5.0V.

Divide the source voltage by 5.12 to give a 5.0V full scale into the ADC. A potential divider with 7.5kΩ and 30.9kΩ does that perfectly. That will give a 25mV per ADC count resolution.

Since you are displaying three digits with 100mV resolution, you are not actually using bits 0 and 1 of the ADC reading. That's OK as it will give a tiny bit of noise 'absorption' in those low order bits.

So, ADC=4 is 0.1V, ADC=8 is 0.2V etc. You could just divide the ADC by 4, insert a decimal point before the last digit, and display that. However, the highest reading* will actually be 25.6*1023/1024 = 25.575 = 25.5 if you use integers... not 25.6. To make the reading more sensible, you can add 2 to the ADC value (before dividing by 4) which will give a rounding up effect on each 50mV step.

Now, after all of the above, 0V to <50mV source voltage will give zero value. 50mV to <150mV will give 001 value. 150mV to <250mV will give 002, and so on. Right up to 25.55V to <25.65 will give a value of 256.

Code example for the above

Code:
// Divide source voltage by 5.12 using a potential divider with 7.5kΩ and 30.9kΩ
// Gives 25mV per ADC count with a 5.0V Vref
unsigned int16 display;
printf("%02u.%u",display/10,display%10); // Insert a decimal point

*Remember that the readings from an ADC actually represent the minimum of a range, not a discrete voltage. In this example, a unadjusted ADC reading of 0 represents a range from 0V to <25mV. ADC 1 represents the range 25mV to <50mV, etc.. So, the maximum reading of 1023 counts represents anything in the range 25.6V*1023/1024=25.575V to 25.6V, but the calculated value of 1023 is the 25.575V bottom of the range, unless you do the rounding or averaging trick I used.
 « Last Edit: August 12, 2019, 06:02:53 18:02 by Magnox » Logged
fpgaguy
Active Member

Offline

Posts: 138

Thank You
-Given: 154

 « Reply #7 on: August 12, 2019, 08:52:35 20:52 »

Don't forget to scale your 25V input to not supply more than VDD to your part!
 Logged
FTL
Junior Member

Offline

Posts: 83

Thank You
-Given: 170

 « Reply #8 on: August 12, 2019, 09:46:21 21:46 »

How will you be detecting the current signals? i.e. How will you be converting the current into a voltage that the ADC can read?

Quote
input 4 will read the actual drawn ampere 0-2.56A or 0-5.12A

The usual way is to have a series resistor, and then measure the voltage drop across it. That will be a relatively small voltage drop. For instance if you used a 0.1 Ohm resistor, it will will drop between 0 and 0.5V as it flows between 0 and 5A. You will not get a very accurate current reading if you feed that into the ADC with a range of 0-5V.

It is possible to use an op-amp to amplify the current signal before applying it to the ADC, but that is a bit tricky since the op amp must be true rail-to-rail at the low end since its power supply ground is presumably the same as the PIC.

 Logged
PICker
Active Member

Offline

Posts: 162

Thank You
-Given: 207

 « Reply #9 on: August 13, 2019, 07:24:04 07:24 »

You can use a transimpedance amplifier. With a simple resistor between the inverting input and the output of an OPA. If you want you can add a capacitor in parallel with the resistor for obtaining a low-pass filter.
I suggest you to read this amazing appnote from microchip about single-supply OPAs ( Figure 8 ).
I use the MCP6041 for making precision current-to voltage converters:
https://www.microchip.com/wwwproducts/en/MCP6041
 Logged
sphinx
Hero Member

Offline

Posts: 917

Thank You
-Given: 609

 « Reply #10 on: August 13, 2019, 08:09:12 08:09 »

@FTL there is a series resistor i will problably need to amplify that signal. There is only one current signal over a series resistor the other one is a 0-5v signal that sets the current limit.
 « Last Edit: August 13, 2019, 08:24:00 08:24 by sphinx » Logged

laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
fpgaguy
Active Member

Offline

Posts: 138

Thank You
-Given: 154

 « Reply #11 on: August 13, 2019, 10:16:18 22:16 »

try something like this to get better accuracy on current
https://www.diodes.com/products/analog/standard-linear-products/current-monitors/current-output/part/ZXCT1010
 Logged
Magnox
Senior Member

Offline

Posts: 250

Thank You
-Given: 983

Oink!

 « Reply #12 on: August 14, 2019, 12:45:51 00:45 »

That's an interesting current sensor, but I think it has a maximum of 20V above ground so no use on Sphinx's 25V.
 Logged
fpgaguy
Active Member

Offline

Posts: 138

Thank You
-Given: 154

 « Reply #13 on: August 14, 2019, 06:03:26 18:03 »

 Logged
sphinx
Hero Member

Offline

Posts: 917

Thank You
-Given: 609

 « Reply #14 on: August 15, 2019, 07:07:31 07:07 »

There is a slight problem with your suggestions of circuit for measuring current since the current resistor is on the low side.
 Logged

laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
fpgaguy
Active Member

Offline

Posts: 138

Thank You
-Given: 154

 « Reply #15 on: August 16, 2019, 05:14:37 17:14 »

perhaps post your circuit, so we can look
 Logged
sphinx
Hero Member

Offline

Posts: 917

Thank You
-Given: 609

 « Reply #16 on: August 16, 2019, 06:02:40 18:02 »

here is a pic of the schematic.
 Logged

laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
Checksum8
Active Member

Offline

Posts: 132

Thank You
-Given: 124

 « Reply #17 on: August 16, 2019, 11:28:05 23:28 »

 Logged
Wizpic
Global Moderator
Hero Member

Offline

Posts: 1200

Thank You
-Given: 549

 « Reply #18 on: August 17, 2019, 07:45:25 07:45 »

here is a pic of the schematic.

Does this power supply work ?
If I remember the pcb had a mod to, funny enough I was looking for another file the other day and I came across this design that we looked at many yers and was thinking shall I have a go again, the only thing g that I did not find was the photo of the pcb
 Logged

When you think, "I can't do anymore. I need a break," that is the time to challenge yourself to keep going another five minutes. Those who persevere for even an extra five minutes will win in life..
sphinx
Hero Member

Offline

Posts: 917

Thank You
-Given: 609

 « Reply #19 on: August 17, 2019, 08:07:12 08:07 »

Hey Wizpic it has never left my work bench and it still works great i am working on this project for it and later i am going to add add a constant volt indicator.
I still have the scans i did for you then. I think i will change all electrolytic capacitors i think its time for that.
 Logged

laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
Sideshow Bob
Cracking Team
Hero Member

Offline

Posts: 994

Thank You
-Given: 230

 « Reply #20 on: August 17, 2019, 04:34:53 16:34 »

Do you happen to have any functional description for this power supply link or document. I am not going to build it but as it looks it has a CL funtion built in it must have current sense resistor some place
 Logged

I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum
sphinx
Hero Member

Offline

Posts: 917

Thank You
-Given: 609

 « Reply #21 on: August 17, 2019, 06:36:47 18:36 »

Yes it has  current seinsing R17 R19.

I attached all pictures i scanned some time ago, sadly it is swedish, and there are t2 things i remember that needs to added and changed
the info this should be in the forum i think, i remember i told this to wizpic.
there is one capacitor to prevent oscillation
the other one is that there is + - on one op apms need to be switched the schematics on pcb is ok from what i remember
there should be a paper with correction but i can not find that single paper.

/sphinx
 Logged

laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
Wizpic
Global Moderator
Hero Member

Offline

Posts: 1200

Thank You
-Given: 549

 « Reply #22 on: August 17, 2019, 06:51:51 18:51 »

I think I made these corrections at the time we worked on it, I will dig the stuff out that I’ve got and done on it and compare the original against the one I did, it could be that I did not make  the change on the one I did nd that could be the reason why I could not get it to work, but I created the schematic from the photo’s of the pcb you sent me.

I will check tomorrow and update you

Wizpic
 Logged

When you think, "I can't do anymore. I need a break," that is the time to challenge yourself to keep going another five minutes. Those who persevere for even an extra five minutes will win in life..
Sideshow Bob
Cracking Team
Hero Member

Offline

Posts: 994

Thank You
-Given: 230

 « Reply #23 on: August 17, 2019, 07:18:26 19:18 »

Yes it has  current seinsing R17 R19.

I attached all pictures i scanned some time ago, sadly it is swedish, and there are t2 things i remember that needs to added and changed
the info this should be in the forum i think, i remember i told this to wizpic.
there is one capacitor to prevent oscillation
the other one is that there is + - on one op apms need to be switched the schematics on pcb is ok from what i remember
there should be a paper with correction but i can not find that single paper.

/sphinx
No problem, as I am from Norway. I can read swedish and dansish. I will look at it on sunday
 Logged

I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum
Wizpic
Global Moderator
Hero Member

Offline

Posts: 1200

Thank You
-Given: 549

 « Reply #24 on: August 18, 2019, 09:25:25 09:25 »

I've found this so far and by looking at the symbols are wrong on IC1A pins 2 and 3, The PCB layout is correct.

I have corrected this in my schematic but will go through the pictures of the PCB layout and check, The version I worked on was planning to upgrade to 5amp as the transistor are mounted on another PCB, But looking back it now this would not work as T3 is acting as a temperature control if I have understood it correctly been mounted by the heatsink.
I will see about changing this and redo the PCB and get new board made and bring back this project to life.

sphinx sorry don't mean to hi-jack your thread, means this was about reading ADC but guess its easier for others to see the schematics to get a better under standing of it. plus with the schematic redrawn it makes it easier to read I guess

Wizpic
 Logged

When you think, "I can't do anymore. I need a break," that is the time to challenge yourself to keep going another five minutes. Those who persevere for even an extra five minutes will win in life..
 Pages: [1] 2  All