Shop OBEX P1 Docs P2 Docs Learn Events
Byte array to Word variables — Parallax Forums

Byte array to Word variables

w4gonw4gon Posts: 4
edited 2011-08-31 19:41 in BASIC Stamp
Hi,

I currently have a SERIN command taking in 17 bytes and putting them into a byte array. Right now I'm inputting the data via Hyperterminal, but later the data will be coming from an inclinometer. The incoming data looks like this: x=+03.30,y=-07.41. These are degree measurements on the x and y axis. And here is the BS2 code I'm using to fill the array:

serStr VAR Byte(18)
serStr(17)=0
SERIN PC, Baud, [STR serStr\17]

My question is how to process these raw bytes and put them into Word variables? Right now I really only need the x axis degree reading. I'm guessing the decimal might be a problem for the BS2 as well.

I'm working with the Basic Stamp Homework Board (BS2).

Any help greatly appreciated.
Joel

Comments

  • ZootZoot Posts: 2,227
    edited 2011-08-30 16:13
    I would define the work variables for serStr as Words, then use the "implied" byte array for the STR, then you'd have words ready:
    serStrW VAR Word(9)
    serStr VAR serStrW.LOWBYTE ' this also means an "implied" array of 18 bytes starting at serStr(0)
    serStr(17)=0
    SERIN PC, Baud, [STR serStr\17] 
    
    DEBUG CR, HEX4 serStrW(0) ' byte0 and byte1 of the "implied" array
    DEBUG CR, HEX4 serStrW(1) ' byte2 and byte3 of the "implied" array
    DEBUG CR, HEX2 serStr(4) ' byte4 of the "implied" array
    DEBUG CR, HEX2 serStr(5) ' byte5 of the "implied" array
    DEBUG CR, HEX4 serStrW(3) ' byte6 and byte7 of the "implied" array
    'etc
    
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2011-08-30 16:15
    w4gon,

    I can give you a hint. Pull up the basic stamp Help (F1), and do a search for 'Memory" and then select "Memory Organization". The table for "BASIC Stamp 2 Series RAM Organization" should give you the answer that you are looking for.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-08-30 18:47
    Joel, welcome to the forum!

    What is the baud rate?
    SERIN PC, Baud, [STR serStr\17]
    
    If it is low (say 2400 or lower), then may be possible to parse the data directly into word variables, using DEC modifiers instead of STR.

    However at higher baud rates, you'll stick with the STR array, and then pick out the numerical ascii values, subtract 48 to convert from ascii to number, and add them up with proper weighting. something like this for the x axis...
    x = (serStr(3) - 48) * 10 + (serStr(4) - 48)   ' digits to left of decimal
    x = x * 100 + ((serStr(6) - 48) * 10 + (serStr(7) - 48)) ' digits to right of decimal
    IF serStr(2)="-" THEN x = -x   ' if negative sign
    DEBUG SDEC x   ' prints 330 when x=+03.30
    
  • w4gonw4gon Posts: 4
    edited 2011-08-30 20:06
    Thanks Tracy, this is a great forum!

    The baud rate is 9600: SERIN PC, 84, [STR serStr\17]

    Your example code is precisely what I was looking for.

    My goal is to take the degree reading from the inclinometer, place it in a word variable and then turn this reading into a percent. Thank you for helping me get half way there. Quick question on the second half of my project. Can the BS2 do Tangents: percent = Tan(degree)x100? If so I might need to open another thread.

    Thanks again!
    Joel
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-08-31 09:01
    I'm glad that helped!

    As to TAN(angle) * 100.
    The Stamp has SIN(angle) and COS(angle). You can find them described in the manual.
    So you may be able to do tan = SIN/COS.
    The angle on the Stamp has to be expressed in units of brads, where there are 246 brads around a circle in analogy with 360 degrees or 2 pi radians. If you already have degrees, then
    brads = degrees ** 46603 ' 256/360
    Both SIN and COS will be 16 bit signed values, but nowhere near 16 bits of resolution, which is closer to 8 bits around the full circle.
  • ZootZoot Posts: 2,227
    edited 2011-08-31 09:08
    there are 246 brads

    I think you mean 256 brads in a circle.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-08-31 09:13
    Right, Zoot, thanks, 256 brads in a circle.
  • w4gonw4gon Posts: 4
    edited 2011-08-31 15:49
    Thanks Tracy. I did see the SIN and COS functions and as you say I think the resolution might be a problem. I'm thinking using a trig table might work for my purposes so I'll try that route first.

    Thanks all for your help.
    Joel
  • w4gonw4gon Posts: 4
    edited 2011-08-31 19:41
    Yep, a lookup table did the trick. I decided to just do the tangent math for the BS2 with my trusty calculator and put the results in the table. That's one way to do it. :lol:

    Cheers
    Joel
Sign In or Register to comment.