Stable Adc Reading Routine


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Posts
    18

    Default Stable Adc Reading Routine

    Dear friends

    Recently I came across an AVR application note about an intelligent charger
    design based on their controller line.
    There was a so called STABLE ADC READING FUNCTION
    (not explained in detail ) which was used for getting valid ADC readings.
    The following is copy-paste from there.
    .................................................. ..................
    void stable_ADC(void)
    The stable_ADC function is used when measuring battery
    voltage or temperature. It makes sure the ADC values are
    stable inside a defined area. This is important for an accurate
    measurement. The function loops until it gets three
    ADC values where the highest is no more than one step
    higher than the lowest.
    .............................................
    There was a pseudo c flowchart but I was not able to follow it through.
    stable_ADC

    |----------------- V[0] > V[1]+1 No ----------------------------- > return
    | Yes
    | V[5] = V[4]
    | V[4] = V[3]
    | V[3] = V[2]
    | Start ADC
    | ADC done? No ------>-continue conversion
    | Yes
    | V[2] = ADC
    | V[1] = highest value of V[2] to V[5]
    | V[0] = lowest value of V[2] to V[5]
    | |
    | |
    -----------------------
    I wonder if someone can help in implementing this for picbasic as I would like to
    use it in order to improve ADC readings on a PIC12F675 .
    At the moment I am adding 64 consecutive values in order to approximate a 16
    bit result and by floating point math to end up to my final reading which is not
    very stable.

    Bill
    Last edited by gebillpap; - 4th February 2008 at 22:42.

  2. #2
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    You probably need to clean up your circuit if you are getting that much noise. For example, I was taking an a2d reading, and it was varying by about 8 counts on a 10-bit measurement. That is a lot. I put a 1uF cap on the power pin of the amp, and now it only varies by one or two counts.

  3. #3
    Join Date
    Apr 2006
    Location
    New Hampshire USA
    Posts
    298


    Did you find this post helpful? Yes | No

    Smile junk the top and bottom four readings

    Hi Bill,

    Melanie’s great idea:
    Quote Originally Posted by Melanie
    take say 16 ADC readings, sort them in sequential order of value, junk the top and bottom four readings as being 'out-of-range', sum and average the middle eight... this makes ADC readings in industrial applications pretty much bomb-proof...
    http://www.picbasic.co.uk/forum/showthread.php?p=41702

    It would be interesting to see a “real world” working example of this technique written for a PIC’s 10 bit ADC.

    -Adam-
    Ohm it's not just a good idea... it's the LAW !

  4. #4
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    One advantage to Melanie's technique is very obvious when you have a moving a/d value to read. For slow moving, if you have a quality circuit with low noise, you can usually just average. I used an a/d port to track the charging of a battery once, using 10 point averaging. I could almost get better resolution than the PIC advertised because with a steady and slow moving v-in, the readings changed very slowly... that is, on a 10-bit reading, I'd get 5 readings at, say, 480, and 5 at 481. The next time, it would be 4 at 480 and 6 at 481... then 3 & 7, etc.

    If you really want a "rolling average" then you can take a reading and add it to your average, and subtract your oldest rolling number. That saves time in computation if it matters. Otherwise, just adding all of your numbers every time may be easier to read and comprehend.

  5. #5
    Join Date
    Oct 2005
    Posts
    18


    Did you find this post helpful? Yes | No

    Default

    Thank you for responding
    I will try to describe the problem I face better in order to justify why I think that something like the stable ADC mentioned in my first post might solve it.
    A 12f675 chip gets ADC readings of a battery undergoing charging with currents 0,1-4A.
    The voltage is monitored and displayed every 1 second with resolution of two decimals (for example 8,17 V).
    Since the battery is under charge it's voltage is climping up steadily and the display should show (at 1 sec rate ) voltages like 8,17 - 8,17 -8,17-some time -8,17 -8,17 -8,18 -8,18-some time-8,18- 8,19- some time-etc.
    But what happens is that I get the voltage increase in "bursts" of three like 8,17 - 8,18 -8,19- 8,19 - 8,19 -8,19- some time-etc. every time there is a slight increase in voltage.
    This is probably due to my manipulation of ADC data with adding 64 readings to achieve a 16 bit result and so on .
    Í believe that with the stable adc routine ,mentioned in my first post, I can improve my display of voltages.

    Anyway can anyone propose some picbasic equivelant for the stable ADC routine???

    Bill

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    OK Adam, you asked for it... here it is... quickly thrown together for an 18F2420... (you can probably optimise it with a bit of thought, but it's a starter just to get going)...

    The variables...
    Code:
    	ADCValue var WORD			' Final ADC Result
    	CounterA var BYTE			' Just a BYTE Temporary working variable
    	DataW var WORD				' Just a WORD Temporary working variable
    	RawData var WORD [16]			' Array holding ADC Result
    Some Initialisation Set-up's for the PIC....
    Code:
    	ADCON1=%00001110			' ADC Control 1
    						' 	7=0 - 0 Unused
    						' 	6=0 - 0 Unused
    						' 	5=0 - VRef=Vss
    						' 	4=0 - VRef=Vdd
    						' 	3=1 )
    						' 	2=1 ) RA0 set to Analogue
    						' 	1=1 ) All others set to Digital
    						'	0=0 )
    	ADCON2=%10101001			' ADC Control 2
    						' 	7=1 - Right Justified - Read 10-Bits
    						' 	6=0 - 0 Unused
    						' 	5=1 )
    						' 	4=0 ) TAD
    						' 	3=1 )
    						' 	2=0 )
    						' 	1=0 ) Fosc/8
    						'	0=1 )
    Now for the real program... First TAKE your SAMPLES...
    Code:
    		'
    		'	Stuff 16 Element WORD Array full of ADC values
    		'	----------------------------------------------
    	For CounterA=0 to 15
    		ADCON0=%00000001		' Select Channel, Turn-On A/D
    						'	7=0 Unused
    						'	6=0 Unused
    						'	5=0 )
    						'	4=0 )
    						'	3=0 ) selects AN0
    						'	2=0 )
    						'	1=0 Go-done Bit
    						'	0=1 switch-On ADC Module
    		Pauseus 50			' Wait for channel to setup
    	        ADCON0.1 = 1			' Start conversion
    		While ADCON0.1=1:Wend		' Wait for conversion
    		DataW.HighByte=ADRESH		' Read variable from ADC and save
    		DataW.LowByte=ADRESL
    		RawData(CounterA)=DataW
    		Next CounterA
    Then SORT your RESULTS...
    Code:
    		'
    		'	Sort ADC Input
    		'	--------------
    	CounterA=0
    GetADCSortLoop:
    	If RawData(CounterA+1) < RawData(CounterA) then
    		DataW=RawData(CounterA)
    		RawData(CounterA)=RawData(CounterA+1)
    		RawData(CounterA+1)=DataW
    		If CounterA>0 then CounterA=CounterA-2
    		endif
    	CounterA=CounterA+1
    	If CounterA<15 then goto GetADCSortLoop
    Finally EXTRACT SOMETHING USEFUL from what you've got...
    Code:
    	'
    	'	Quanticise discarding top and bottom FOUR elements
    	'	----------------------------------------------------
    	DataW=0
    	For CounterA=4 to 11
    		DataW=DataW+RawData(CounterA)
    		Next CounterA
    	ADCValue=DataW>>3			' Divide Result by EIGHT
    Ain't so difficult when you break it down into little steps is it?

  7. #7
    Join Date
    Oct 2005
    Posts
    18


    Did you find this post helpful? Yes | No

    Default

    Melanie you are right that I already use this "fake 16 bit" method which in turn made me use the FP routines in order to have math results.The reason is that although I display 2 decimals I calculate using 3 (1mV prec.).I have to multiply the 16 bit result by 5000 and divide by 65535 as you understand of course.
    You are also correct in pointing out that it is a POTENTIAL divider and not, as I incorrectly put it, a resistive one.

    I had some time and read the mentioned threads by pic-user(thanks for your input) but unfortunatelly accuracy is not sufficient or guaranteed.

    I made a simple differential amplifier circuit using 1/4 of LM324 having a gain of 0,5 and the output was very very very close to half the input once I managed to get close in resistor values matching R and 2R (1% used).
    I hope that by using 0,1% resistors and suitable resistor values I can get rid of the inaccuracies of the potential divider.On the other hand I will be facing another similar problem having to closely match the already difficult to find resistor values(0,1%) in order to make the correct division.
    Did anyone followed the dif.amp route and with what results?

    Another interesting way to get around the problem would be something like the Microchip suggestion(in an older app.note I have seen) in using a digital(eeprom) potentiometer to apply apropriate voltage for subtruction on a dif-amp.
    Any further thoughts?
    Bill

  8. #8
    Join Date
    Nov 2005
    Posts
    51


    Did you find this post helpful? Yes | No

    Default

    Bill
    Using LM324 or LM358 op-amps are probably some of the worst choices to use for ADC voltage buffer/follower applications (uA741 is also a poor choice). The better op-amps would be rail-to-rail op-amps They give much better performance and far more stable in these applications.

    Something like MCP6022 C-MOS OP-OP-AMP or any that can supply rail-to-rail on the input/output, Well I found them to work great

  9. #9
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Lightbulb

    Bill, you mention that you had to use FP to calculate, that's not necessary. You can save a lot of time and codespace by using ** instead. This operator makes your multiplication and an "invisible" division by 65536. Replace all your calculations with ......

    ScaledResult = AccumulatedADvals ** 5000

    to get 0 to 5.000 volts or .......

    ScaledResult = AccumulatedADvals ** 15000

    .... if yo want it to go from 0 to 15.000V instead.

  10. #10
    Join Date
    Oct 2005
    Posts
    18


    Did you find this post helpful? Yes | No

    Default

    Ingvar I think you are absolutely RIGHT and I will test it as soon as I can.
    Since the ** multiplication places the result in the upper 16 bits it is I believe the same with divide by 65536 as you say so in fact I can get rid of the FP routines.
    I think the slight miscalculation of using 65536 instead of 65535 is not worth to mention.
    Am I right?
    This proves that although I read the manual I didn't fully understand it.(of course it is not the first time )

    Thank you for pointing out this for me.
    Bill

  11. #11
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Wink

    I'd say that it is 2^16(65536) that is the right value to use but that's not really important since the error we're talking about is 0,0015%. Your resistors and ADreference will produce a far greater error than that.

    Happy coding
    /Ingvar

  12. #12
    Join Date
    Oct 2005
    Posts
    18


    Did you find this post helpful? Yes | No

    Default

    Ingvar
    Thanks for the clever suggestion.Did you use it and with what results/observations?
    I suppose that a 2,5V stable voltage ref is the obvious need BUT maybe a volage divider would be better because it will follow the VDD ripple.Is it so?
    I think that having one AD channel with Vref 5V for measuring 0-15V and another AD channel configured with 2,5V Vref (following your trick with 2,5V bias in respect to ground) for measuring 15-30V is what I should try.

    Pic_User
    Thanks also.Maybe after giving up with the regular stuff and methods I might follow this route.
    Bill

  13. #13
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Cool

    Well, i haven't used it exactly like this in exactly this application, but yes, i have used an AD like this. This is one of the reasons why there is the possibility to use external Vrefs.
    It's always a good practice to use the same source for both Vrefs for exactly the reason you mentioned. However, in this application that's not really necessary since the voltage you're measuring is not dependant on your reference. This means that you need a very stable(ripple free) and accurate(known voltage) reference. Vdd is most likley not good enough. There are about a gazillion different ways of creating this Vref, easiest is to buy a chip from Maxim. Trimming of your potentialdividers is also(probably) necessary to get the precision you're after. If it's a "one off" i'd do that with potentiometers, if it's going to be made in big quantities i'd do the trimming in software.

    To finish this off, i'd like to say that you're shooting WAY over the target for a batterycharger. Millivolts is not important in this case, tenths of a volt would be enough.

    But that's just my humble opinion .......

    Good luck,
    /Ingvar

  14. #14
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    579


    Did you find this post helpful? Yes | No

    Default Re: Stable Adc Reading Routine

    I tried to use Melanie's great piece of code (Thanks !) for sorting the values I read with 12F675 ...
    But the results are ...strange ; the schematic wont work as I wish.
    It's something wrong in my code ? Thanks in advance !

    Code:
    CounterA var BYTE			' Just a BYTE Temporary working variable
    DataW var WORD				' Just a WORD Temporary working variable
    RawData var WORD [10]			' Array holding ADC Result
    ............
    Main:
    For CounterA=0 to 9
    ADCON0 = %10001001 
    		Pauseus 50			' Wait for channel to setup
    	    ADCON0.1 = 1			' Start conversion
    		While ADCON0.1=1:Wend		' Wait for conversion
    		DataW.HighByte=ADRESH		' Read variable from ADC and save
    		DataW.LowByte=ADRESL
    		RawData(CounterA)=DataW
    Next CounterA
    gosub getsort
    CounterA=0 	
    
        if (ADvalue < 650) then
    	IF (ADvalue > 450) AND (ADvalue < 640) THEN gosub do_1
    	IF (ADvalue > 320) AND (ADvalue < 440) THEN gosub do_2  
    	IF (ADvalue > 180) AND (ADvalue < 310) THEN gosub do_3
    	IF (ADvalue > 90)  AND (ADvalue < 170) THEN  gosub do_4
    	IF (ADvalue < 80)  THEN gosub do_5        
        endif
    
    Goto Main
    
    
    ;=========================
    ; here are the commands
    GetSort:
    	If RawData(CounterA+1) < RawData(CounterA) then
    		DataW=RawData(CounterA)
    		RawData(CounterA)=RawData(CounterA+1)
    		RawData(CounterA+1)=DataW
    		If CounterA>0 then CounterA=CounterA-2
    	endif
    	CounterA=CounterA+1
    If CounterA<9 then goto GetSort
            DataW=0
    	    For CounterA=2 to 9
    		DataW=DataW+RawData(CounterA)
    		Next CounterA
    Advalue=DataW>>3
    return

  15. #15
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    579


    Did you find this post helpful? Yes | No

    Default Re: Stable Adc Reading Routine

    Me ... again ...
    Tried to use Mr.Darell (RIP ) routine :
    Code:
    include "D:\PBP\average_DT.pbp"
    
    Main:
    ADCON0 = %10001001 
    		Pauseus 50			' Wait for channel to setup
    	        ADCON0.1 = 1			' Start conversion
    		While ADCON0.1=1:Wend		' Wait for conversion
    		value.HighByte=ADRESH		' Read variable from ADC and save
    		value.LowByte=ADRESL
    gosub Average
    advalue=value
        if (advalue < 650) then
    	IF (advalue > 450) AND (advalue < 640) THEN gosub do_1 ...
    	IF (advalue > 320) AND (advalue < 440) THEN gosub do_2 ...      
            ...   
        endif
    
    Goto Main
    ...but something is wrong - always is executed do_1 subroutine

    Note : the code in his "simple" variant, works just fine . Just I want more "accuracy" ...
    Code:
    advalue  var word
    advaloop var byte
    advaltot var word
    
    pause 200
    
    Main:
    advaltot=0
    advaloop=0  
    
    ADCON0 = %10001001 
            PAUSEuS 50                      	' Wait for A/D channel acquisition time
            ADCON0.1 = 1
    for advaloop = 1 to 20
            WHILE ADCON0.1 = 1 : WEND
            advalue.HighByte = ADRESH
            advalue.LowByte = ADRESL
            advaltot=advaltot + advalue
    next advaloop
    
    advalue=advaltot / 20
    
        if (ADvalue < 650) then
    	IF (ADvalue > 470) AND (ADvalue < 640) THEN gosub do_1
    	IF (ADvalue > 320) AND (ADvalue < 450) THEN gosub do_2    
            ...           
        endif
    Goto Main
    Last edited by fratello; - 10th May 2015 at 12:15.

  16. #16
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Stable Adc Reading Routine

    Your implementation of Mel's sort algorithm has a problem in it.

    After you read the ADC values in the "For CounterA = 0 to 9" loop, CounterA equals 10.

    You then issue the "Gosub GetSort" statement.
    Since CounterA = 10 the following line is pointing at memory locations beyond the RawData array.
    "If RawData(CounterA+1) < RawData(CounterA) then"
    This says If RawData(11) < RawData(10) then....
    You need to set CounterA to 0 before calling GetSort.
    Regards,
    TABSoft

  17. #17
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Stable Adc Reading Routine

    Also you did not declare "Advalue" as a variable either.
    Regards,
    TABSoft

Similar Threads

  1. Reading A Photo Resistor using ADC
    By jessey in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 25th January 2007, 09:28
  2. adc reading ac
    By Ruben Pena in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 20th December 2005, 21:57
  3. 12F675 ADC 'Issues'
    By harrisondp in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2005, 01:55
  4. Problem reading multiple ADC ports
    By jswayze in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 4th November 2004, 16:46
  5. Reading multiple ADC channels FAST
    By lwindridge in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st April 2004, 22:37

Members who have read this thread : 4

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts