Stable Adc Reading Routine


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    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

  2. #2
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    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.

  3. #3
    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

  4. #4
    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

  5. #5
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: Stable Adc Reading Routine

    Thanks !
    In post #6 of Mel's I see the same sort procedure as mine ... why mine is wrong ?!
    Advalue is var word ...

  6. #6
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: Stable Adc Reading Routine

    In post #6 of Mel's I see the same sort procedure as mine ... why mine is wrong ?!
    Simple because, ad mentioned by Tabsoft, you have forgotten to clear the variable 'CounterA' before entering the sorting routine.

    Cheers

    Al.
    All progress began with an idea

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 : 3

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