New software filter for adc reading


Closed Thread
Results 1 to 30 of 30

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    968


    Did you find this post helpful? Yes | No

    Default Re: New software filter for adc reading

    Another running average that may be useful to all. This one will work even if you do not have floating point. However, you need to be able to handle large numbers or size the buffer appropriately that it doesn't overflow 'sum' while summing. Of course, you need way more ram than the Avg = (Avg*(n-1)+adcval)/n formula which is good for Ram Constrained systems.

    Code:
    Pseudo code.
    
    NumOfSamples   equ   16
    
    AdcVal             Word                               ' adc reading is placed here
    Samples           Word [NumOfSamples]     ' rolling buffer having NumOfSamples of AdcReading (must retain value between calls to R_Average)
    Index              var     byte                       ' next position where the sample will be put (must retain value between calls to R_Average)
    Sum                var     long                      ' gross sum of all the samples (must retain value between calls to R_Average)
    
    RA_Init:  ' initialize the running average
          Index = 0
          Sum  =0
          return
    
    ' enter with A
    R_Average:
         sum = sum - Samples[Index]                    ' remove the oldest sample from the sum
         sum = sum + AdcVal                                ' add in the latest sample             to sum
         Samples[Index] = AdcVal                          ' and save it in the buffer too
    
         Index = Index+1                                     ' move the index
         If Index > NumOfSamples then Index = 0  ' wrap around if needed
    
         return sum / NumOfSamples                     ' the running average
    Caller has to initiate with RA_Init before using the R_Average routine. I hope you will excuse my pseudo code since I am a bit rusty on my PBP at this moment.
    Last edited by Jerson; - 15th January 2013 at 15:31.

Similar Threads

  1. Help with multiple ADC inputs
    By wdmagic in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 4th January 2013, 00:27
  2. Need help on ADC : Software R/C filter
    By luminas in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 27th November 2010, 14:33
  3. Filter for PWM
    By Tobias in forum General
    Replies: 1
    Last Post: - 24th August 2008, 10:26
  4. Replies: 3
    Last Post: - 26th November 2006, 21:47
  5. ADC filter
    By leonel in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th May 2005, 17:46

Members who have read this thread : 8

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