PDA

View Full Version : Darrel's latest 16 bit averaging routine?



jellis00
- 5th October 2009, 05:57
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.

'************************************************* ***************
'* 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

Darrel Taylor
- 6th October 2009, 03:46
Are you getting ... ?
Symbol not previously defined (WRITE)

jellis00
- 16th October 2009, 06:35
Are you getting ... ?
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?

Darrel Taylor
- 16th October 2009, 09:24
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>

Acetronics2
- 16th October 2009, 12:04
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

jellis00
- 16th October 2009, 18:56
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?

jellis00
- 16th October 2009, 20:27
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.

Darrel Taylor
- 16th October 2009, 22:08
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>

jellis00
- 16th October 2009, 23:17
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.

Darrel Taylor
- 17th October 2009, 01:57
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