Something like this:
ii = 29 '
FOR gcnt = 1 to 29 '
vmean[ii] = vmean[ii - 1] '
ii = ii - 1 '
NEXT gcnt '
'
vmean[0] = newvalue '
(I think is right)
Something like this:
ii = 29 '
FOR gcnt = 1 to 29 '
vmean[ii] = vmean[ii - 1] '
ii = ii - 1 '
NEXT gcnt '
'
vmean[0] = newvalue '
(I think is right)
Seems right. Out of curiosity, what are you using this for?
Adding a new voltage reading every second to calculate a new mean voltage over thirty seconds for a low voltage cutout.
Why not:
I don't have the manual nearby, but I'm pretty sure we can go backwards.Code:FOR ii = 29 TO 1 STEP -1 vmean[ii] = vmean[ii - 1] NEXT ii vmean[0] = newvalue
Robert
instead of a for/next shift, how about a in-place overwrite. (instead of shifting the data, shift the index variable.)
index = 0
Loop:
adResult = {...new voltage value}
arraySum = arraySum - arr[index]
arr[index] = adResult
arraySum = arraySum + adResult
if index = 29 then
index = 0
else
index = index + 1
endif
goto Loop
Mike -
Mike, is the compile size smaller?
I can't really check in my project because it appears code is being fragmented over several pages
after I crossed the first boundary.
Robert, Didn't know you could use a STEP -1 directive,
but if it works, it's shorter, and saves a useless byte.
It appears to be working nicely, but I will shorten it if possible.
It's for my car computer I've posted about here before.
With the vehicle running, the voltage reading is live.
With the engine stopped, but accessories relay on,
the stereo amplifiers tend to cause the voltage to jump around (understandably).
The thirty second average is then displayed and acted on to turn accessories off if the average is too low.
Last edited by Art; - 14th March 2012 at 03:51.
Art,
don't know if it's smaller. i was going for speed rather than code size.
Mike -
I was thinking along the lines of what instead of what Tachyon wroteAdding a new voltage reading every second to calculate a new mean voltage over thirty seconds
But, perhaps you want to look at rolling averages...a for/next shift, how about a in-place overwrite. (instead of shifting the data, shift the index variable.)
http://www.pbpgroup.com/modules/wfse...hp?articleid=7
http://www.picbasic.co.uk/forum/showthread.php?t=12183
http://www.picbasic.co.uk/forum/showthread.php?t=8046
http://www.picbasic.co.uk/forum/showthread.php?p=41702
http://www.picbasic.co.uk/forum/cont...ith-hysteresis
Guys,
May not be the solution for Art's project, but this post interested me because I was also stumped trying to shift bytes of data FIFO and ended up making something work for me.
I liked the FOR/NEXT approach given here and thought I would try it since it looked faster and smaller since I'm really collecting 32 bytes instead of 12 in this example.
My kludge: This scheme uses 24 words and takes 12.5us
Was surprised to to find that this scheme uses 43 words and takes 216us:Code:DEFINE OSC 8 RX VAR BYTE ' Buffer data XYZ_val VAR BYTE[12] ' Accelerometer XYZ data TC_OUT VAR PORTC.4 ' Test point to measure loop time Load_array: TC_OUT = 1 XYZ_val[0] = XYZ_val[1] ' Oldest data XYZ_val[1] = XYZ_val[2] XYZ_val[2] = XYZ_val[3] XYZ_val[3] = XYZ_val[4] XYZ_val[4] = XYZ_val[5] XYZ_val[5] = XYZ_val[6] XYZ_val[6] = XYZ_val[7] XYZ_val[7] = XYZ_val[8] XYZ_val[8] = XYZ_val[9] XYZ_val[9] = XYZ_val[10] XYZ_val[10] = XYZ_val[11] XYZ_val[11] = RX ' Newest data TC_OUT = 0 RETURN
It meant more typing but saved some space and time.Code:DEFINE OSC 8 RX VAR BYTE ' Buffer data XYZ_val VAR BYTE[12] ' Accelerometer XYZ data TC_OUT VAR PORTC.4 ' Test point to measure loop time Load_array: TC_OUT = 1 FOR z = 0 to 10 XYZ_val[Z] = XYZ_val[Z+1] NEXT z XYZ_val[11] = RX TC_OUT = 0 RETURN
Last edited by LinkMTech; - 14th March 2012 at 18:22. Reason: Added note
Louie
Bookmarks