picbasic pro optimizations


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Mark,

    Hey, glad you like the averaging routine. I was wondering if you could do me a favor?   I made a modification to it a little while ago.   You know how the least sigificant digit will "wobble" back and forth between 2 numbers? It's half-way between 2 numbers and it can't quite figure out which one it should be.   Well, the modification adds hysterisis to the averaging routine to eliminate that.

    None of my current projects use the averaging, so I haven't had a chance to test it yet.   I gave it to someone else, but never heard back from him. I assume that means it worked.

    If you have some extra time, maybe you could try this out.
    Code:
    '****************************************************************
    '*  Name    : Average_Hyst.pbp                                  *
    '*  Author  : Darrel Taylor                                     *
    '*  Date    : 7/19/2005                                         *
    '*  Version : 3.0                                               *
    '*  Notes   : Modified to use Hysterisis                        *
    '*          : Use as an INCLUDE file                            *
    '****************************************************************
    ' This routine will keep a "Running Average" of 10-bit Analog Values 
    ' Instead of adding a bunch of values together and then dividing by the number 
    ' of samples, it averages each sample into the final result immediately.
    ' This eliminates the need for 32 bit math.
    ' To allow it to "Catch up" to large changes, set the FAspread to an 
    ' acceptable range.
    ' Simply place the new number in VALUE and GoSub Average 
    ' The Average (with Hysterisis) will be returned into the same variable VALUE
    
    '---- [ User Options ] ---------------------------------------------------------
    HystLevel   CON  7      ' 7 = .7 of the Least Significant Digit
                            '    Valid HystLevel is from 6 to 13
    AvgCount    CON  16     ' = Number of samples to average. For best response 
                            '    times, keep AvgCount as small as you can
    FAspread    CON  50     ' = Fast Average threshold +/-
                            '  FAspread should be larger than the worst possible
                            '  noise in the A/D conversion.
    '-------------------------------------------------------------------------------
    
    AVGchanged  VAR  BIT    ' 1 indicates that the Average Value has changed
                            ' you have to clear this bit after using it
    ADavg       VAR  WORD   ' Stores the current Average
    ADhyst      VAR  WORD   ' Stores the current Value for Hysterisis
    Value       VAR  WORD   ' Input / Output variable to be averaged
    spread      CON  FAspread * 10  ' adjust spread *10 for Hyst.
    
    GOTO OverAverage
    ' -=-=-=-=-=-=  Average Analog values -=-=-=-=-=-=-=-=-=-=
    Average:
        Value = Value * 10                      ' Scale value up for hysterisis
        IF Value = ADavg Then NoChange          ' if they're the same, nothing to do 
        IF ABS (Value - ADavg) > spread OR Value < AvgCount Then FastAvg
        IF ABS (Value - ADavg) < AvgCount Then RealClose
        ADavg = ADavg - (ADavg/AvgCount)         ' Subtract 1 samples worth
        ADavg = ADavg + (Value/AvgCount)         ' Add in the new sample portion 
        GoTo AVGok
      FastAvg:                                   ' Catch up to the changing value
        ADavg = Value                            ' Set Average to the current value
        GoTo AVGok
      RealClose:                                 ' Reduce the sample size when
        ADavg = ADavg - (ADavg/(AvgCount/4))     ' the average and the sample are
        ADavg = ADavg + (Value/(AvgCount/4))     ' "Real Close"
      AVGok:
        IF ABS (ADavg - ADhyst) > HystLevel then ' If it changed > HystLevel +/-
            ADhyst = ((ADavg + 5) / 10) * 10     ' Round ADavg to get new Value
            AVGchanged = 1                       ' Indicate that Average changed
        ENDIF   
      NoChange:
        Value = ADhyst / 10                      ' Scale the result down
    Return
    
    OverAverage:
    '---------------------------------------------------------------
    DT

  2. #2
    Join Date
    Jun 2005
    Posts
    53


    Did you find this post helpful? Yes | No

    Default

    Darrel,

    I will gladly test your new modifications and post the results here after
    I return from a business trip next week!

    Mark

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Cool, Thanks Mark!

    Have a nice trip,
    too bad it's for business.
    <br>
    DT

  4. #4
    Join Date
    Jun 2005
    Posts
    53


    Did you find this post helpful? Yes | No

    Default

    Darrel,

    I returned from my business trip and have been swamped with several customer changes, I hope to test your filter modifications this weekend!

    Mark

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Thanks Mark,

    No hurry. The customer's always come first.

    I apprecieate your efforts.
    <br>
    DT

  6. #6
    Join Date
    Apr 2007
    Location
    Pennsylvania, USA
    Posts
    158


    Did you find this post helpful? Yes | No

    Default

    Thanks Darrel! This routine was just what I needed to smooth out a slightly unstable temp reading on a hot tub project I was working on. Just for reference, the code as you have it works 100%
    Shawn

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Thanks for the feedback Shawn.

    DT

Similar Threads

  1. VB.net and Picbasic Pro
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th November 2007, 21:18
  2. How to configure SPI in PICBASIC PRO?
    By moogle in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 15th April 2007, 19:31
  3. Replies: 0
    Last Post: - 22nd November 2005, 14:16
  4. Question for all that use MELABS PICBASIC PRO
    By oskuro in forum Off Topic
    Replies: 2
    Last Post: - 24th March 2005, 18:15
  5. PicBasic Pro & PicBasic syntax different
    By Billyc in forum General
    Replies: 5
    Last Post: - 16th April 2004, 22:19

Members who have read this thread : 0

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