Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 20, 2019, 11:32:44 11:32


Login with username, password and session length


Pages: 1 2 [All]
Print
Author Topic: A question about adc converting  (Read 2061 times)
0 Members and 1 Guest are viewing this topic.
sphinx
Hero Member
*****
Offline Offline

Posts: 615

Thank You
-Given: 380
-Receive: 155



« on: August 11, 2019, 10:53:34 10: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

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
h0nk
Active Member
***
Offline Offline

Posts: 168

Thank You
-Given: 150
-Receive: 113



« Reply #1 on: August 11, 2019, 12:08:13 12: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
and add this conversions up.
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 Offline

Posts: 145

Thank You
-Given: 141
-Receive: 90


« Reply #2 on: August 11, 2019, 01:09:40 13: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
Active Member
***
Offline Offline

Posts: 243

Thank You
-Given: 919
-Receive: 266


Oink!


« Reply #3 on: August 11, 2019, 02:28:40 14: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 Offline

Posts: 75

Thank You
-Given: 134
-Receive: 32


« Reply #4 on: August 11, 2019, 02:37:38 14: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 Offline

Posts: 615

Thank You
-Given: 380
-Receive: 155



« Reply #5 on: August 12, 2019, 12:05:28 00: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

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
Magnox
Active Member
***
Offline Offline

Posts: 243

Thank You
-Given: 919
-Receive: 266


Oink!


« Reply #6 on: August 12, 2019, 11:54:44 11: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
// ADC reading already captured into unsigned int16 variable 'ADC'
unsigned int16 display;
display=(ADC+2)>>2; // Add 2 for rounding, then divide by 4
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, 12:02:53 12:02 by Magnox » Logged
fpgaguy
Active Member
***
Offline Offline

Posts: 126

Thank You
-Given: 139
-Receive: 128


« Reply #7 on: August 12, 2019, 02:52:35 14:52 »

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

Posts: 75

Thank You
-Given: 134
-Receive: 32


« Reply #8 on: August 12, 2019, 03:46:21 15: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 Offline

Posts: 145

Thank You
-Given: 141
-Receive: 90


« Reply #9 on: August 13, 2019, 01:24:04 01: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 ).
http://ww1.microchip.com/downloads/en/appnotes/00682c.pdf
I use the MCP6041 for making precision current-to voltage converters:
https://www.microchip.com/wwwproducts/en/MCP6041
Logged
sphinx
Hero Member
*****
Offline Offline

Posts: 615

Thank You
-Given: 380
-Receive: 155



« Reply #10 on: August 13, 2019, 02:09:12 02: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, 02:24:00 02:24 by sphinx » Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
fpgaguy
Active Member
***
Offline Offline

Posts: 126

Thank You
-Given: 139
-Receive: 128


« Reply #11 on: August 13, 2019, 04:16:18 16: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
Active Member
***
Offline Offline

Posts: 243

Thank You
-Given: 919
-Receive: 266


Oink!


« Reply #12 on: August 13, 2019, 06:45:51 18: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 Offline

Posts: 126

Thank You
-Given: 139
-Receive: 128


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

use its' related part,
https://www.diodes.com/products/analog/standard-linear-products/current-monitors/current-output/part/ZXCT1110
Logged
sphinx
Hero Member
*****
Offline Offline

Posts: 615

Thank You
-Given: 380
-Receive: 155



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

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

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
fpgaguy
Active Member
***
Offline Offline

Posts: 126

Thank You
-Given: 139
-Receive: 128


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

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

Posts: 615

Thank You
-Given: 380
-Receive: 155



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

here is a pic of the schematic.
Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
Checksum8
Junior Member
**
Offline Offline

Posts: 98

Thank You
-Given: 90
-Receive: 74


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

Have you seen these? Uses a integrated hall effect sensor. Can be used high or low side.

https://www.sparkfun.com/datasheets/BreakoutBoards/0712.pdf

https://www.ebay.com/itm/5A-20A-30A-Range-Current-Sensor-Module-ACS712-714-For-Arduino-Raspberry-Pi-UNO/112275500486?_trkparms=ispr%3D1&hash=item1a24242dc6:m:mrWdLLRNFRBddlnsFPnv9pw&enc=AQAEAAAB0BPxNw%2BVj6nta7CKEs3N0qXnSFbjzn8RGGbCqm8rRvs1mFiM%2FuISE6yrejBQ%2FV0Nza3SirWkf%2B1Tv6bYEv%2F7sjBSqyyojeKdIJ0aLyGmgFXtEGIdtj%2BVE4B8OVY%2FQELZQqAyhOTJaug6trXKGqECUD6EitGsIM00qCI6qFS%2FesU1lrXrKc2Po8eO4Bu7%2FhYg9g2FniROBkQbN%2BOw6wLsXF7fodIQXXjVqjkrq8fRUSt9hdQxT%2BYYGTqn%2BOhoi02D7LTVZvRj854kilsAXJK2h1bsb4hrGtydGISOhs4SDJYXfH6gF4ma46pzfLlRBBJnxfGWZumUA5U6Q7EtJ7zHDuCLHkUPYghOzjMp6aRMz%2BHoMXRy0q2sRbVMlupzDO%2B1TmjWPuLCAhNXqHOzBLWQdcG%2BzaR3qPRlB9NdV6JK8LGD7VqZaDQHkJeOMC28Es5X32BMIrMUE56409YehUBmk%2FRwHW%2BdRfeUUD9c6YJmOsLKxXTz6ttkkGe1iOwVrTXYxqghP2Q%2B7fYc8coK1%2BhJwePEGSp4Z%2Bl9EKctmgWJY7vgKD16eLmk12YusqhTzfxrXhVQKRMvNcQwVc7wvJJV5FLyluUsBLjgJ%2Bl7DgvqGlVy&checksum=112275500486096157cea0de40eda5da00a1816c02e9
Logged
Wizpic
Global Moderator
Hero Member
*****
Offline Offline

Posts: 1089

Thank You
-Given: 343
-Receive: 385



« Reply #18 on: August 17, 2019, 01:45:25 01: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 Offline

Posts: 615

Thank You
-Given: 380
-Receive: 155



« Reply #19 on: August 17, 2019, 02:07:12 02: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

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
Sideshow Bob
Cracking Team
Hero Member
****
Offline Offline

Posts: 632

Thank You
-Given: 204
-Receive: 649



« Reply #20 on: August 17, 2019, 10:34:53 10: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 Offline

Posts: 615

Thank You
-Given: 380
-Receive: 155



« Reply #21 on: August 17, 2019, 12:36:47 12: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

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
Wizpic
Global Moderator
Hero Member
*****
Offline Offline

Posts: 1089

Thank You
-Given: 343
-Receive: 385



« Reply #22 on: August 17, 2019, 12:51:51 12: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 Offline

Posts: 632

Thank You
-Given: 204
-Receive: 649



« Reply #23 on: August 17, 2019, 01:18:26 13: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 Offline

Posts: 1089

Thank You
-Given: 343
-Receive: 385



« Reply #24 on: August 18, 2019, 03:25:25 03: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..
sphinx
Hero Member
*****
Offline Offline

Posts: 615

Thank You
-Given: 380
-Receive: 155



« Reply #25 on: August 18, 2019, 04:23:25 04:23 »

@ sideshow bob i did desolder the pcb so you can do a reverse engineer it to the same pcb and i did some scans of that all i got is in that collection
i have use this psu since i built it and it was made with connecting to a computer/embedded when it was designed.
i have repaired it several times from what i can remember it was the output transistors that went byebye
last time i repaired it i lost a piece of wire in it while powered on and some tracks burnt but it was fixable with
output transistors some fixings of the tracks.

once i forgot it on for a week with load on so cooling was pretty darn hot, no worries it still worked after that.

there is a tiny capacitor on one opamp ic's  i think one missing caused oscilation

i have never measured how good it really is.

it is also buildabable in single rail version as well all info in the scans.

no worries wizpic more input is hopefully better.

/sphinx
Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
Wizpic
Global Moderator
Hero Member
*****
Offline Offline

Posts: 1089

Thank You
-Given: 343
-Receive: 385



« Reply #26 on: August 18, 2019, 05:44:01 05:44 »

Looking at the PCB where the extra cap is on IC1, My schematic I've already added that cap and it says 47pF ?
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 Offline

Posts: 632

Thank You
-Given: 204
-Receive: 649



« Reply #27 on: August 18, 2019, 06:12:49 06:12 »

What you can do is to measure the voltage over the current sense resistor as the this voltage will have a linear relationship with the output current. But you have to ground reference this voltage and apply some gain to this volage so it fit your ADC range. If you look at the datasheet for LM324 http://www.ti.com/lit/ds/symlink/lm324-n.pdf  
You can use figure 32 or 38 for this. Any single supply OPAMP can do the job here as long as you stay inide the input Common-Mode Voltage Range given in the datasheet and of course also the supply volage. That will be the same as IC1 and IC4 use. If you want to measure the negative rail with the same micro controller this voltage will need to be inverted
Logged

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

Posts: 615

Thank You
-Given: 380
-Receive: 155



« Reply #28 on: August 18, 2019, 06:17:57 06:17 »

yes that is right. it is a 47pF
Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
Pages: 1 2 [All]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC