rotate bytes in array


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Aug 2003
    Posts
    985

    Default rotate bytes in array

    Hi Guys,
    I want to rotate all bytes (bytewise) in an array of 30 bytes so that the oldest value in the array is discarded
    from one end, and a new value is placed in the other end, and the new value should be the last to be discarded.

    A seemingly simple problem driving me nuts!
    Something like this would just copy the new value to all bytes:

    FOR gcnt = 0 TO 29 '
    vmean[gcnt+1] = vmean[gcnt] '
    NEXT gcnt '

    Any help appreciated.
    Cheers, Brek.

  2. #2
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: rotate bytes in array

    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)

  3. #3
    Join Date
    Jul 2003
    Location
    USA - Arizona
    Posts
    156


    Did you find this post helpful? Yes | No

    Default Re: rotate bytes in array

    Seems right. Out of curiosity, what are you using this for?

  4. #4
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: rotate bytes in array

    Adding a new voltage reading every second to calculate a new mean voltage over thirty seconds for a low voltage cutout.

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,159


    Did you find this post helpful? Yes | No

    Default Re: rotate bytes in array

    Why not:

    Code:
    FOR ii = 29 TO 1 STEP -1
    vmean[ii] = vmean[ii - 1]
    NEXT ii
    
    vmean[0] = newvalue
    I don't have the manual nearby, but I'm pretty sure we can go backwards.

    Robert

  6. #6
    Join Date
    Feb 2011
    Location
    Michigan, USA
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: rotate bytes in array

    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 -

  7. #7
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: rotate bytes in array

    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 02:51.

  8. #8
    Join Date
    Feb 2011
    Location
    Michigan, USA
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: rotate bytes in array

    Art,
    don't know if it's smaller. i was going for speed rather than code size.

    Mike -

  9. #9
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,159


    Did you find this post helpful? Yes | No

    Default Re: rotate bytes in array

    Quote Originally Posted by Art View Post
    ...Robert, Didn't know you could use a STEP -1 directive,
    but if it works, it's shorter, and saves a useless byte...

    Yup, got off my lazy butt and got the manual, it's in there.

    So far our score in PBP knowledge:
    Robert 1 .... Art 56,743


Members who have read this thread : 0

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