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