Shop OBEX P1 Docs P2 Docs Learn Events
Setting the BITS of a BYTE variable — Parallax Forums

Setting the BITS of a BYTE variable

fiatluxfiatlux Posts: 1
edited 2011-08-08 07:17 in Propeller 1
I'm fairly new to spin programming, but not to microcontrollers.
I am trying to individually set the bits in a byte variable and I can only seem to set the LSB.
I have the Propellor hooked up to an addressable 16 input mux chip (74150), and I want to compile the information into 2 BYTE variables so I can send them out serially.

Here is the code that I am using:
repeat
    repeat address from 0 to 7
        outa[d_pin..a_pin] := address
        waitcnt(10000 + cnt)
        if ina[w0_pin]==1
            status[address] := 1
        else
            status[address] := 0
status and address are both BYTE variables.
Can anyone give me any insight into what I am doing wrong?

Thanks.

Thanks,

Comments

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-08-04 10:56
    to set a bit high, do this:
    varb |= |< 5 ' set bit 5 high

    to set a bit low, do this:
    varb &= !|< 3 ' set bit 3 low
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2011-08-04 10:57
    In order to set/clear individual bits within a variable you need to use logical operators. You can use OR to set bits and AND to turn them off. For example, if you want to turn on bits 0 and 2 in a variable named status you would use:
    status := status | %101
    

    This is breaking it down into simplest terms. I hope this helps.

    Edit, I see Bob replied, you can use his method as well. If you have additional questions about how he is doing that please ask.
  • kuronekokuroneko Posts: 3,623
    edited 2011-08-04 17:54
    @fiatlux: If you feel adventurous later you can also use the bit-index-ability of the special purpose registers (SPR, like you used for outa). This only works for SPRs.
    repeat
        [COLOR="orange"]repeat address from 0 to 7[/COLOR]
          [COLOR="blue"]outa[/COLOR][d_pin..a_pin] := address
          waitcnt(10000 + [COLOR="blue"]cnt[/COLOR])
          [COLOR="blue"]dirb[/COLOR][address] := [COLOR="blue"]ina[/COLOR][w0_pin]
    
    dirb is unused in the current propeller revision so can be (mis)used for whatever you want. After you leave the bit loop dirb[7..0] holds your assembled byte value. HTH
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-08-08 07:17
    I think this is what you're looking for. Note that I changed the repeat order to simplify the MSB first input of the bits.
    repeat
        status := 0
        repeat address from 7 to 0
          outa[d_pin..a_pin] := address
          waitcnt(10000 + cnt)
          status := (status << 1) | ina[w0_pin]
    
Sign In or Register to comment.