Shop OBEX P1 Docs P2 Docs Learn Events
converting BCD question — Parallax Forums

converting BCD question

KC8DKTKC8DKT Posts: 76
edited 2006-03-22 15:15 in BASIC Stamp
why does this not work
temp(1) = temp.NIB1(1)*10+temp.NIB0(1)


but this does
Temp = Temp(1)
Temp(1) = Temp.NIB1*10+Temp.NIB0


When converting BCD to DEC?· Not a big deal I know but its bugging me not knowing.

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-18 00:07
    You can't use variable modifiers with array elements -- but you can do this:

    · temp(1) = (temp(1) >> 4 * 10) + (temp(1) & $0F)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • KC8DKTKC8DKT Posts: 76
    edited 2006-03-18 08:23
    debug myBytes.HIGHNIB(0)
    debug myBytes.HIGHNIB(1)

    I seen this in the help file and got the idea it would work with math, my bad, Thanks for the info and new formula.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-18 16:12
    There is more than one way to skin this cat....· As it turns out, all of the RAM in the BASIC Stamp is considered an array and, usually, declaring an array is not the best thing to do.· Instead of:

    time··· VAR···· Byte(3)

    you can do this:

    time··· VAR···· Byte
    secs··· VAR···· time
    mins··· VAR···· Byte
    hrs···· VAR···· Byte

    Using this strategy you can access secs as time(0), mins as time(1), and hrs as time(2).· But now you can do direct access as well.· So now you can use the BCD conversion:

    · secs = (secs.NIB1 * 10) + secs.NIB0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • KC8DKTKC8DKT Posts: 76
    edited 2006-03-19 04:02
    Now that is sweat! Thanks...
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-19 04:11
    What I neglected to tell you is that you must declare your array elements in order with no breaks; that may be obvious but I think it's important to emphasize.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • KC8DKTKC8DKT Posts: 76
    edited 2006-03-19 10:19
    Yes it is.
    After messing with this I rememberd reading about an array bug some where. Keep in mind that PBasic saves Large to small as well!

    temp1 VAR Byte
    temp2 var byte
    temp3 var word

    temp1(0) = 1
    temp1(1) = 1
    temp1(2) = 1
    temp2 = 2
    temp3 = 3

    debug temp1(0) = 1
    temp1(1) = 2
    temp1(2) = 1 Word saved before byte. This would be the problem the bug could lead too.

    No idea how this helps anyone but it was fun.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-03-19 12:40
    KC8DTK -

    Quite regardless of whether there is an actual "bug" or not (which I rather doubt), your example leaves a bit of unfairness behind in its wake. Here's what I mean.

    In total, your example works with FOUR bytes, TWO BYTES represented by temp1 and temp2 (one byte each) as well as TWO BYTES from temp3 (a WORD or two bytes), so it would make a good deal more sense to VIEW all FOUR bytes, after the fact, to examine their COMPLETE contents like this:

    debug temp1(0), temp1(1), temp1(2), temp1(3)

    or

    debug temp1, temp2, temp3

    Anything less than that hides some of the contents. Personally, in terms of proper and complete examination, I'd much rather see:

    DEBUG IHEX? temp1, IHEX? temp2, IHEX4? temp3

    but that's just the inherent bit-wacker in me talking smile.gif

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-19 15:00
    I'm aware of no bugs in variable assignmet, KC,·and as you mention·-- by design -- the BASIC Stamp lays variables into memory largest to smallest:

    Word
    Byte
    Nib
    Bit

    So your declaration:

    temp1·· VAR··· ·Byte
    temp2·· VAR··· ·Byte
    temp3·· VAR··· ·Word

    ... would actually be stored in memory (as bytes) in this order:

    temp3.LOWBYTE
    temp3.HIGHBYTE
    temp1
    temp2

    I guess another point I should have made absolutely clear is that implicit arrays only work with variables of the same type.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • KC8DKTKC8DKT Posts: 76
    edited 2006-03-19 16:30
    I said bug because I read somthing when I was working with the new BCD line. And after looking around I found it again.

    A word of caution about arrays: If you're familiar with other BASICs and have used their arrays, you have probably run into the "subscript out of range" error. Subscript is another term for the index value. It is out-of-range when it exceeds the maximum value for the size of the array. For instance, in the example above, myBytes is a 10-cell array. Allowable index numbers are 0 through 9. If your program exceeds this range, PBASIC will not respond with an error message. Instead, it will access the next RAM location past the end of the array. If you are not careful about this, it can cause ALL SORTS OF BUGS.

    Its in the new Help File for PBasic 2.2.5

    Anyways, thanks for the new BCD line. Your new array(idx) fix worked out great for one small loop doing input & output converts too and from the RTC...
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-22 15:15
    To be clear, what you're referring to are programmer-induced errors, not bugs in the BASIC Stamp; let's just be very clear about that. And once one understands the operation of the BASIC Stamp memory, clever programmers can do a lot of neat tricks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.