Darrel's latest 16 bit averaging routine?


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378

    Default Darrel's latest 16 bit averaging routine?

    I have an application in which I need to average some sequential measurements from an ultrasonic transceiver that outputs a pulse whose width is a range measurement. Each pulse width can be converted to range in inches by applying a scale factor of 147 microsecs/inch. I am using a PULSEIN statement for the input pin to my PIC 16F690 and therefore need to running average of a series of pulse measurements ( a dozen or so) into one measurement to filter some of the noise and then convert the final avg. measurement into range with the scale factor.
    I immediately thought of Darrel's averaging routine that doesn't require 32 bit math. However, I want to make sure I find the latest version. Does anyone know how/where I can find the code for his latest version? I did find the one embedded in this below code, but I can't get it to compile. As you can see, I wrote this as a test of the averaging code with a test setup and call of the averaging routine from the Main code. If this is using the latest subroutine of Darrel's I may have a problem in the Main: code. If so, can anyone tell me what they see wrong with it that would prevent it from compiling. I get assembler errors.
    Code:
    '****************************************************************
    '*  Name    : TEST_AVERAGE.BAS                                                               *
    '*  Author  : John R. Ellis                                                                               
    '****************************************************************
    i          VAR Byte          ' Index for measurment loop
    pw      VAR WORD       ' Stores each pw measurment
    range   VAR WORD      ' Stores calculated range value
    us_inch con  147        ' Scale factor converts sensor microsecs to inches
    Value   VAR WORD      ' Stores average of measurements
     
    ' -----------------{ Subroutines }----------------------------------------
    Average_Single:
        'Embedded use of Darrel Taylors 16 bit averaging routine
        ' USE INTRUCTIONS: First, Select the sensor. ie.  Sensor = Temperature
        ' then place the new number in VALUE and then GoSub AVERAGE_SINGLE.
        ' The Average will be returned into the same variable VALUE.
        AvgCount       CON  6            ' = Number of samples to average
        FAspread        CON  1000      ' = Fast Average threshold +/-
        ADavg            VAR  WORD
     
        IF Value = ADavg Then NoChange
        IF ABS (Value - ADavg) > FAspread OR Value < AvgCount Then FastAvg
        IF ABS (Value - ADavg) < AvgCount Then RealClose
        ADavg = ADavg - (ADavg/AvgCount)
        ADavg = ADavg + (Value/AvgCount)
        GoTo AVGok
     
      FastAvg:
        ADavg = Value
        GoTo AVGok
     
      RealClose:
        ADavg = ADavg - (ADavg/(AvgCount/4))
        ADavg = ADavg + (Value/(AvgCount/4))
     
      AVGok:
        Value = ADavg   ' Put Average back into Value
     
      NoChange:
    Return
    '--------------------------------------
    Main:
            pw = 0
            For i = 0 to 5
                'PULSIN PORTA.4,1,pw   ' Make 6 measurements from PW pin 
                                                     ' of transceiver into array pw(i)
                                                     ' with running average in Value
                pw = pw +1                  ' Simulates pw measurement
                Value = pw
                'Running average in Value  ' Max pw = 20' = 240" = 35280 usec
                GOSUB Average_Single
            NEXT
            range = Value / us_inch   ' Convert Value to range in inches
            WRITE 19,range              ' Store day's range in EEPROM location
                                                  ' for this day
    END

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


    Did you find this post helpful? Yes | No

    Default

    Are you getting ... ?
    Code:
    Symbol not previously defined (WRITE)
    DT

  3. #3
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Default Poss?ibly a 2.6 bug

    Quote Originally Posted by Darrel Taylor View Post
    Are you getting ... ?
    Code:
    Symbol not previously defined (WRITE)
    No, I am getting the following error on attempts to compile:
    "Error[111] c:\program files\mechanique\mcs\test_average.mac 296 : Missing symbol"

    Could this be due to the bug in PICBASIC Pro version 2.6 which is the version I am using?

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


    Did you find this post helpful? Yes | No

    Default

    Using the code from post #1 with PBP 2.60, the only error I get is ...
    Symbol not previously defined (WRITE)

    If I add the line ...
    DEFINE WRITE_USED 1

    Then it compiles without error.

    To find where the problem is, open the
    c:\program files\mechanique\mcs\test_average.LST file with notepad.

    Search for error[111].
    The line immediately following the error should indicate where the problem is.
    <br>
    DT

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Default

    Hi, Darrel

    Without opening files, but the manual ... should make it too !

    ( range var ??? )

    did I miss something here ???

    BTW: MPLAB does not show any errors ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  6. #6
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Default Corrects one error...leaves one more

    Quote Originally Posted by Darrel Taylor View Post
    Using the code from post #1 with PBP 2.60, the only error I get is ...
    Symbol not previously defined (WRITE)

    If I add the line ...
    DEFINE WRITE_USED 1

    Then it compiles without error.
    Adding the DEFINE WRITE_USED 1 statement to my code eliminated the error I was seeing and it appears the compile was declared "Successful", but I still get this additional error after making that correction:
    "Error[108] : Illegal character (1)" which points in the .lst file to a LIST statement in the assembly listing. I don't know what this error is trying to tell me, and it must not be a fatal error because it still compiled and assembled. Can you tell me what this error is telling me to fix?

  7. #7
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Default .hex file not being created after Error[108]

    Quote Originally Posted by jellis00 View Post
    Adding the DEFINE WRITE_USED 1 statement to my code eliminated the error I was seeing and it appears the compile was declared "Successful", but I still get this additional error after making that correction:
    "Error[108] : Illegal character (1)" which points in the .lst file to a LIST statement in the assembly listing.
    I notice now that although it appeared the file compiled, it did not assemble correctly because I see that the .hex file details didn't update the time stamp of the file after the compile. Therefore, the Error[108] was fatal to the creation of a new .hex file. Any ideas why this might be happening?

    It appears to me that most of these problems are due to the WRITE bug in 2.6, which is driving me crazy because none of my write statement I typically insert in my code for test purposes are actually writing values to EEPROOM...even with the DEFINE WRITE_USED 1 statement included.

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


    Did you find this post helpful? Yes | No

    Default

    Apparently, you have modified the code from what was in post #1.

    First off, don't worry about the WRITE bug in 2.60.
    It is a very minor issue that only happens if you ONLY have WORD variables in your WRITE statements.

    If you also write from a BYTE variable anywhere in your program, then the problem goes away. It's not normal for people to only write from words, so I think the error slipped thru.

    If you do have WRITE's with BYTE's, then the DEFINE WRITE_USED 1 will generate the err[108] that you now see. Which is why I say that the program is not the same as before.

    If you are now using BYTE's in your WRITE's, delete the DEFINE, and compile again.

    If you get the old error[111] back, please send me the .LST file to darrel att pbpgroup.com.
    <br>
    DT

  9. #9
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Default Now I understand

    Thanks, Darrell! I finally understand the symptoms and true workarounds of the WRITE WORD bug. I had only WORD variables being written in the first version, hence the ERROR[111]. Then, you were right..I inserted a WRITE Byte statement without removing the DEFINE ...hence the ERROR[108]. Now I have a superfolous WRITE Byte statement just to avoid the bug, and have removed the DEFINE statement and everything works. Thanks again.

    Any word as to when their will be a patch for the v2.6 WRITE bug? I work with a lot of WORD variables, and would like to see it soon so I don't have to insert a superfolous WRITE byte statement.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jellis00 View Post
    Thanks, Darrell! I finally understand the symptoms and true workarounds of the WRITE WORD bug.
    Excellent!

    Any word as to when their will be a patch for the v2.6 WRITE bug?
    I know there is a patch in the works, but I do not know when it will be.

    Keep an eye on this page for news on the issues.
    http://melabs.com/support/pbpissues.htm
    DT

Similar Threads

  1. decoding quadrature encoders
    By ice in forum mel PIC BASIC Pro
    Replies: 93
    Last Post: - 28th February 2017, 10:02
  2. Darrel's Averaging Routine
    By Andy Wood in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 28th June 2008, 00:24
  3. PICBasic newbie problem
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 12th February 2008, 01:55
  4. Averaging 16 bit values without using 32 bit math
    By sirvo in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th October 2007, 23:18
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 02:07

Members who have read this thread : 1

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