Shop OBEX P1 Docs P2 Docs Learn Events
simple SX/B question — Parallax Forums

simple SX/B question

Alex HunterAlex Hunter Posts: 18
edited 2006-08-18 17:45 in General Discussion
Hello, i'm new to the SX, and i have a simple question that i can't figure out.

I would like to use code similar to the following:
for idx = 0 to 7
     HIGH RA.idx
next



I would like to use a variable to identify a pin. But this code doesn't work. Is there some way i can get something like this to work, without using if-then statements?

Thanks,
Alex Hunter

Comments

  • John CoutureJohn Couture Posts: 370
    edited 2006-08-02 23:14
    Edited after I realized I goofed on my answer.

    My apologies gang.· I just realized that my answer was so far off that it was flat out wrong ... thus I deleted it so that it doesn't get used.· Bean gave much better advice in the next message.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John J. Couture

    San Diego Miramar College

    Post Edited (John Couture) : 8/3/2006 5:36:29 AM GMT
  • BeanBean Posts: 8,129
    edited 2006-08-03 00:31
    Alex,
    · You cannot use a variable for the bit position. This is because of the way the SX instructions are formed.
    · Probably the easiest way is to do this:

    OUTPUT RB             ' Make all bits of RB outputs
    FOR idx = 0 to 7
      tempByte = 1 << idx ' Get a 1 in the position to turn on
      RB = RB OR tempByte ' Make pin high
    NEXT
    

    Bean.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
    ·
  • John BondJohn Bond Posts: 369
    edited 2006-08-16 12:54
    Hey Bean

    I found your trick the hard way last week. I thought I was real stupid finding no other way but by using the OR function. it makes me feel a little better seeing you're using it.

    John Bond
  • BeanBean Posts: 8,129
    edited 2006-08-16 13:49
    Here is a way to do it without needing the tempByte variable.

    idx VAR Byte
     
    OUTPUT RB             ' Make all bits of RB outputs
    idx = 1
    DO
      RB = RB OR idx
      idx = idx << 1
    LOOP UNTIL idx = 0 ' 128 << 1 = 0 for byte variables
     
    


    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
    ·
  • Alex HunterAlex Hunter Posts: 18
    edited 2006-08-16 20:38
    thanks bean, that is very helpful.
  • YendorYendor Posts: 288
    edited 2006-08-18 15:50
    I had a similar situation.

    Using the 'Bean' strategy, is there a similar way we can access the individual bits using an index, instead of shifting a byte?

    For example if I wanted to test individual bits of a byte:

    idx VAR BYTE
    mybyte VAR BYTE

    FOR idx = 0 TO 7
    IF mybyte.idx = 1 THEN ' which won't work
    'do somethin'
    ENDIF
    NEXT

    An array of bits was another idea, but the compiler didn't like it.

    Kinda like mybyte.LowBit(idx)) for the BS2 code.

    Thanks!
  • BeanBean Posts: 8,129
    edited 2006-08-18 16:09
    Sure you can do this:
    idx VAR BYTE
    mybyte VAR BYTE
    tempByte VAR Byte
    
    FOR idx = 0 TO 7
      tempByte = 1 << idx
      tempByte = tempByte AND myByte
      IF tempByte <> 0 THEN
        'do somethin'
      ENDIF
    NEXT
    
    

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
    ·
  • YendorYendor Posts: 288
    edited 2006-08-18 17:20
    Thanks Bean.

    Ah yes, being tricky with bytes! I was thinking that there were some functions using or accessing bits like in PBASIC other than mybyte.1.

    many thanks!
  • BeanBean Posts: 8,129
    edited 2006-08-18 17:27
    You can use __PARAM1 for those tempByte variables too. That way it doesn't require extra variable space.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
    ·
  • YendorYendor Posts: 288
    edited 2006-08-18 17:45
    Nice tip. However, I'm probably a way's away from touching any limitations in RAM.
Sign In or Register to comment.