Shop OBEX P1 Docs P2 Docs Learn Events
Using "LOWBYE" and "HIGHBYTE" with an array varible? — Parallax Forums

Using "LOWBYE" and "HIGHBYTE" with an array varible?

Vern GranerVern Graner Posts: 337
edited 2005-10-15 05:43 in BASIC Stamp
Ok, I'm working with some code to control a PSC (Parallax Serial Servo Controller) and it expects the Position Command to be sent in the following manner:

SEROUT PSCPin, Baud+$8000,[noparse][[/noparse]"!SC", Channel, Speed, Destination.LOWBYTE, Destination.HIGHBYTE, CR] 



The above works fine, but I need to be able to do this with an array of servos, so I tried to use array of word variables like so:

SEROUT PSCPin, Baud+$8000,[noparse][[/noparse]"!SC", Channel, Speed(SERVO), Dest(SERVO).LOWBYTE, Dest(SERVO).HIGHBYTE, CR]



Unfortunately, it will no longer tokenize. It fails with the error being indicated as the "period" character between "Dest(SERVO)" and "LOWBYTE"). The error message displayed is:

Error: Expected "]"




I've checked the help files for the LOWBYTE/HIGHBYTE commands but all the examples show it being used with a "regular" word variable, not a word variable array. Has anyone run into this issue before, and if so could maybe shed some light on how to solve it? TIA!

smile.gif

Vern

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Vern Graner CNE/CNA/SSE    | "If the network is down, then you're
Senior Systems Engineer    | obviously incompetent so why are we
Texas Information Services | paying you? Of course,if the network
http://www.txis.com        | is up, then we obviously don't need
Austin Office 512 328-8947 | you, so why are we paying you?" ©VLG


Post Edited (Vern) : 10/15/2005 12:39:53 AM GMT

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-14 22:42
    The program I've attached should help -- it shows how you can·use arrays without actually defining them.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • JLBSheckyJLBShecky Posts: 6
    edited 2005-10-14 23:00
    Vern-

    The reason that you can't get the code to compile is the placement of the array index. By moving the index to after the '.HIGHBYTE/.LOWBYTE' The code will compile and should run. One thing to watch out for though is that each time that you increment the variable 'SERVO' by one it will look at the next byte in the array, not the next byte of the next word. To get get arround this you could multiply the value of servo by 2 and then you would be accessing the proper ellement, ofcourse that only matters if I understood the intent of your original code correctly. Here is the new line of code that I came up with, I can't test this my self, I don't have a PSC, but it should work correctly.

    SEROUT PSCPin, Baud+$8000,[noparse][[/noparse]"!SC", Channel, Speed(SERVO), Dest.LOWBYTE(SERVO*2), Dest.HIGHBYTE(SERVO*2), CR]
    


    -JLBShecky
  • Vern GranerVern Graner Posts: 337
    edited 2005-10-15 00:37
    JLBShecky said...
    Vern-

    The reason that you can't get the code to compile is the placement of the array index. By moving the index to after the '.HIGHBYTE/.LOWBYTE' The code will compile and should run. One thing to watch out for though is that each time that you increment the variable 'SERVO' by one it will look at the next byte in the array, not the next byte of the next word. To get get arround this you could multiply the value of servo by 2 and then you would be accessing the proper ellement, ofcourse that only matters if I understood the intent of your original code correctly. Here is the new line of code that I came up with, I can't test this my self, I don't have a PSC, but it should work correctly.

    SEROUT PSCPin, Baud+$8000,[noparse][[/noparse]"!SC", Channel, Speed(SERVO), Dest.LOWBYTE(SERVO*2), Dest.HIGHBYTE(SERVO*2), CR]
    


    -JLBShecky

    Thank you for the code. I can see where that would work, but I'm unclear on the "multiply by two" portion. I've gotten the code to "sorta" work, but now it doesn't seek the servos to new locations all the time. It tends to get "stuck" seeking between two static locations. I'm cross-eyed looking at it and was hoping you might be able to give it a look and tell me if you see what I might be doing wrong. TIA!

    [noparse]:)[/noparse]

    Vern

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Vern Graner CNE/CNA/SSE    | "If the network is down, then you're
    Senior Systems Engineer    | obviously incompetent so why are we
    Texas Information Services | paying you? Of course,if the network
    http://www.txis.com        | is up, then we obviously don't need
    Austin Office 512 328-8947 | you, so why are we paying you?" ©VLG
    
    
  • Vern GranerVern Graner Posts: 337
    edited 2005-10-15 00:38
    Jon Williams (Parallax) said...
    The program I've attached should help -- it shows how you can use arrays without actually defining them.

    Thanks for the code Jon. smile.gif I'll have to print it out and have a sit-down as at first glance it didn't "snap" with me. I'm too tired.. these 12 hr days before coding sessions really suk! smile.gif

    Vern

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Vern Graner CNE/CNA/SSE    | "If the network is down, then you're
    Senior Systems Engineer    | obviously incompetent so why are we
    Texas Information Services | paying you? Of course,if the network
    http://www.txis.com        | is up, then we obviously don't need
    Austin Office 512 328-8947 | you, so why are we paying you?" ©VLG
    
    
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-10-15 05:43
    Vern -

    The following comments will either make it clear as day, or possibly confuse you all the more. I HOPE it provides the former, and not the latter! I will attempt to describe the problem that you're presently faced with, in terms of another similar problem you've probably seen before. It must be said that the problem referenced herein is NOT the exact same problem you are facing, only one that's similar in nature.

    In many computer system architectures, boundary alignment of data and/or instructions is necessary. Usually the compiler/assembler that one uses is able to automatically determine the appropriate boundary by instruction, WORD, or DOUBLE WORD addresses, and if necessary, it adds the necessary and appropriate dummy alignment instructions, or it emits "slack data" bytes to guarantee that required boundary alignment is met.

    Think of this·LIKE it was a typical hidden boundary alignment issue, wherein, in the general case, the assembler/compiler is·NOT able to instantaneously determine the proper boundary alignment, and the programmer is burdened with the responsibilty of FORCING the proper boundary alignment, usually at the beginning of the data definitions by manually adding any necessary "slack bytes".

    Stop here and think about that for a second. All I'm trying to point out here is that MANUAL consideration of integral boundaries is sometimes REQUIRED, and that duty, when necessary,·falls to the programmer.

    With that in mind, the PBASIC addressing construct ".highbyte/.lowbyte" actually addresses BYTES by its very nature, and in truth you want to address only WORDS (aligned BYTE pairs in truth). If you address the BYTE array members as 0, 2, 4, 6, 8, etc you will always be looking at a WORD boundary, and in turn, be looking at the array in terms of WORDs, not BYTEs. Thus,·any INDEX value used must guarantee that only even numbered BYTE pairs are used. So, multiplying·any BYTE INDEX value by 2 guarantees that you will be looking at ONLY WORD boundaries, and not BYTE boundaries.

    /code

    BYTE number···· 0· 1···2· 3··4· 5·· 6· 7··· 8· 9·· also BYTE index

    data·············· ·· 0 1 |· 2 3 | 4 5 |· 6 7· | 8 9

    WORD number···0······ 1····· 2······ 3······ 4······ also WORD index

    code/

    Thus, as you can see, BYTE INDEX * 2 = WORD INDEX. Also note that any reference to .HIGHBYTE, when referenced with the WORD INDEX,·will always result in a even numbered address, presuming you START with an even numbered boundary.

    Although your present problem is·NOT actually integral boundary alignment per se, you DO want to address every BYTE pair in the arrray, and thus the problems are similar in nature, if viewed that way.

    I hope that helps.

    Regards,

    Bruce Bates
Sign In or Register to comment.