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
Corrects one error...leaves one more
Quote:
Originally Posted by
Darrel Taylor
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?
.hex file not being created after Error[108]
Quote:
Originally Posted by
jellis00
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.