Shop OBEX P1 Docs P2 Docs Learn Events
Is this possible? — Parallax Forums

Is this possible?

GC4130GC4130 Posts: 13
edited 2005-06-01 02:48 in General Discussion
I have some code in SX/B that works very well for a particular sensor. I need to address multiple sensors of the same type and I am trying to figure out a way to make the original code more dynamic, all I need to do is change the I/O pin definition. Any help would be greatly appreciated. Thanks

'example

A var rb.1
B·var rb.2
temp var·byte

Test sub

Test:
...............
......
return

main:
··· temp=A
··· Test
··· temp=B
··· Test
goto main


·

Comments

  • BeanBean Posts: 8,129
    edited 2005-05-30 16:44
    George,
    SX/B does not have anything like PIN variables. That is because the compiler "hard codes" the pin into the assembly instructions. A different pin requires different instructions.
    Depending on what you are doing in your TEST routine you might be able to pass a bit mask to the routine.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012


    Product web site: www.sxvm.com

    "It's not getting what you want, it's wanting what you've got."
    ·
  • GC4130GC4130 Posts: 13
    edited 2005-05-31 14:21
    Bean,

    Could you explain a "bit mask rountine". My "Test" rountine is not complicated at all, it just allows me to measure the pulse width from an accelerometer. Problem is I have 4 of them and I want to·save code space by dynamically changing·the pin through the temp variable. If this is not possible, I guess I just have to have·4 individual "Test" rountines for·my 4 sensors with the specific pin designations in each.

    thanks

    George
  • BeanBean Posts: 8,129
    edited 2005-05-31 14:34
    George,
    Can you post your program ? Or at least the "test" subroutine. It's easier to show you the code, than to explain how to do it.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012


    Product web site: www.sxvm.com

    "It's not getting what you want, it's wanting what you've got."
    ·
  • KenMKenM Posts: 657
    edited 2005-05-31 15:07
    George,
    Bit masking is essentially performing an AND function to determine the state of one bit.

    check  ds 1         ;define a one byte space for status
    checkm ds 1         ;define a one byte space for memory of check
     
     
     
     
     
    ;later in your code
     
    and    check,#%00000001
     
    ;the value of check now indicates status of bit0
     
    mov    check, #checkm
    and    check,#%00000010
    ;the value of check now indicates status of bit1
    

    There are other ways to address a single bit of a byte
    Could you explain a "bit mask rountine".

  • GC4130GC4130 Posts: 13
    edited 2005-05-31 15:33
    The code I have has alot of irrelavant information for this particular problem so I will try to explain with an abreviated version.

    "This is what I would like to have"

    A var rb.1
    B·var rb.2
    temp var·byte

    Test sub

    Test:
    ···· if temp=1 then check1
    ···· if temp=0 then check2
    ·····check1:
    ········· ...........
    ···· return
    ···· check2:
    ··········...........
    ···· return

    main:
    ··· temp=A
    ··· Test
    ··· temp=B
    ··· Test
    goto main






    "This is what I Think I will be forced to do"

    A var rb.1
    B·var rb.2


    Test1 sub
    Test2 sub

    Test1:
    ···· if A=1 then check1
    ···· if A=0 then check2
    ·····check1:
    ········· ...........
    ···· return
    ···· check2:
    ··········...........
    ···· return

    Test2:
    ···· if B=1 then check3
    ···· if B=0 then check4
    ·····check3:
    ········· ...........
    ···· return
    ···· check4:
    ··········...........
    ·····return

    main:
    ··· Test1
    ··· Test2
    goto main
  • BeanBean Posts: 8,129
    edited 2005-05-31 16:35
    George,
    I need to know what is inside the "check" routine.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012


    Product web site: www.sxvm.com

    "It's not getting what you want, it's wanting what you've got."
    ·
  • GC4130GC4130 Posts: 13
    edited 2005-05-31 16:48
    Does it really matter what is inside check?, my problem is defined outside of this function Do you think there is some way I can rewrite my code to be more generalized like what I first posted.

    "This is what I would like to have"

    A var rb.1
    B·var rb.2
    temp var·byte

    Test sub

    Test:
    ···· if temp=1 then check1
    ···· if temp=0 then check2
    ·····check1:
    ········· ...........
    ···· return
    ···· check2:
    ··········...........
    ···· return

    main:
    ··· temp=A
    ··· Test
    ··· temp=B
    ··· Test
    goto main
  • BeanBean Posts: 8,129
    edited 2005-05-31 17:26
    George,
    To answer your original question with what you have provided...

    Yes it is possible.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012


    Product web site: www.sxvm.com

    "It's not getting what you want, it's wanting what you've got."
    ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-05-31 17:48
    Here is some code I banged out to show it is possible, I haven't tested it so there maybe bugs or syntax errors. It consists of two subroutines Fetch2bit and Store2bit, the first takes a single argument whose value is 0-3 (4 two bit values can be stored in each byte) and returns the value located at the specified position (into __Param1), the second takes two arguments the first is the storage position (0-3) and the second is the value to store (0-3). These routines do not perform any check on the bounds of the arguments so if you·use values outside of 0-3·you will get·unpredicitable·behaviour. You could also modify this to perfom on arrays if you need more than 4 two bit variables, but I leave that to you.
    TwoBitMask CON %00000011
     
    ByteVar  VAR byte
    TempVar  VAR byte
    TwoBit  VAR byte
    ShiftMask VAR byte
     
    Fetch2bit sub 1
    Store2bit sub 2
     
    Fetch2bit:
      TempVar = __Param1              'store 2 bit var position
      TwoBit = ByteVar                'make a copy of data
      DO WHILE TempVar > 0
        TwoBit >> 2                   'shift data down
        DEC TempVar
      LOOP
      __Param1 = TempVar & TwoBitMask 'store two bits into return value
      RETURN
     
    Store2bit:
      TempVar = __Param1              'store 2 bit var position
      TwoBit = __Param2               'store 2 bit value
      ShiftMask = !TwoBitMask         'create masker (to clear both bits)
      DO WHILE TempVar > 0
        TwoBit << 2                   'shift value and mask to next position
        ShiftMask << 2
        DEC TempVar
      LOOP
      ByteVar = ByteVar & ShiftMask   'zero out both bits of variable
      ByteVar = ByteVar | TwoBit      'store bits
      RETURN
    

    Post Edited (Paul Baker) : 5/31/2005 5:53:15 PM GMT
  • BeanBean Posts: 8,129
    edited 2005-05-31 18:40
    Paul,
    You can use variables for the shift count.
    Instead of your loop in fetch you can do:

    TempVar = __PARAM1
    TempVar = TempVar·* 2
    TwoBit = ByteVar >> TempVar

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012


    Product web site: www.sxvm.com

    "It's not getting what you want, it's wanting what you've got."


    Post Edited (Bean (Hitt Consulting)) : 5/31/2005 6:47:01 PM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-05-31 18:55
    Thanks Bean, my mind still thinks in assembly, plus the docs only being in the distro and me being at work, I was winging the code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • GC4130GC4130 Posts: 13
    edited 2005-05-31 20:50
    Maybe this is because I am a newbie to the SX but I can't see how I could implement this bit masking routine to the Original code I posted?? How does this relate to changing the pin designations? ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-05-31 21:43
    My code was for illustration purposes, I couched it in general subroutines that it could be used generically with a program. To distill it even further you do the following steps:

    To read:
    Figure out which position you need (0-3) (we'll call it P)
    make a copy of the variable you want the two bits from (this can be a port register)
    Right shift the copy 2*P times.
    AND the result with the constant 3
    what's left is the two bits you wanted in the lowest two bits of your copy of the data.

    To write:
    Figure out which position you need (0-3) (we'll call it P)
    Make a copy of the bits that you want store (or use the original if you do not care if its destroyed)
    Create a variable and store $FC into it (this is your mask which is the inverse of the above mask)
    Left shift the data to be store and the mask 2*P times.
    AND the mask with the location the data is to be stored
    OR the data to be stored with the location it is to be stored.
    Now those two bits are inserted into the location without messing with the other bits.

    Either create a subroutine as I did above or just embed it into your code.
  • GC4130GC4130 Posts: 13
    edited 2005-06-01 02:48
    Paul,

    Thanks for the explaination I think I get it now. I'll try to implement it in my code.

    George



    ·
Sign In or Register to comment.