Shop OBEX P1 Docs P2 Docs Learn Events
variable as a bit position of a variable in SX/B — Parallax Forums

variable as a bit position of a variable in SX/B

Chicago MikeChicago Mike Posts: 88
edited 2008-10-21 13:55 in General Discussion
I'm trying to change one bit of a byte in a subroutine, however the bit place may change. It doesn't seem like I can refer to the bit position with a variable.

Simple example below.
sub rtc_bit_change
' PARAM1 = Takes Byte address to change
' PARAM2 = bit position (0-7)
' PARAM3 = desired state
    temp4 = __PARAM1
    temp5 = __PARAM2
    temp6 = __PARAM3
    temp7 = rtc_read_byte temp4
    temp7.temp5 = temp6
    rtc_write_byte temp4 temp7
    endsub




all variables declared as BYTES. (Though I tried changing temp6 to a bit, because that's all it is, and it still gets the same results).

I get the error
'EXPECTED A VALUE BETWEEN 0 AND 7 "temp5"

I can't specify a hard value because I want to pass the bit postion as a variable so I can reuse this routine.

Any ideas?

Comments

  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2008-10-19 21:28
    I'm not sure if this is what you are looking for but JonnyMac wrote this·Function sometime back which helped me:
    ' Use: value = BITVAL someVal, position
    ' -- "someVal" can be a byte or word
    FUNC BITVAL
      IF __PARAMCNT = 2 THEN                        ' byte passed?
        tmpW1 = __PARAM1                            ' get byte value
        tmpB1 = __PARAM2                            ' get bit position 
      ELSE                                          ' word passed
        tmpW1 = __WPARAM12                          ' word was passed
        tmpB1 = __PARAM3                            ' get bit position
      ENDIF
      tmpB2 = 0                                     ' assume cleared
      IF tmpB1 >= 0 THEN                            ' position value legal?
        IF tmpB1 <= 15 THEN
          tmpW2 = 1 << tmpB1                        ' create bit mask
          tmpW2 = tmpW2 & tmpW1                     ' clear other bits
          IF tmpW2 > 0 THEN                         ' if not zero
            tmpB2 = 1                               '   bit was 1
          ENDIF
        ENDIF  
      ENDIF
      RETURN tmpB2
      ENDFUNC
    

    You use it like this:
    data1   VAR Word
    data2   VAR Word           
    data3   VAR Word
    

    value   VAR Byte
    

    I       VAR Byte
    

    
    
    BITVAL  FUNC    1, 2, 3  'Thanks to JonnyMac for these cool functions!
                             'It saved me alot of coding.
    

    CheckDataSin:
    For I = 0 to 15
     [b]value = BITVAL data1, I[/b]
     IF value = 1 THEN
        HIGH SIN_1
     ELSE
        LOW SIN_1
     ENDIF
     value = BITVAL data2, I
     IF value = 1 THEN
        HIGH SIN_2
     ELSE
        LOW SIN_2
     ENDIF
     value = BITVAL data3, I
     IF value = 1 THEN
        HIGH SIN_3
     ELSE
        LOW SIN_3
     ENDIF
     HIGH CLOCK
     LOW CLOCK
    NEXT 
    



    Also keep an eye out when SX/B 2.0 is released for something to help you on this also.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Smykowski: Well-well look. I already told you: I deal with the customers so the engineers don't have to. I have people skills; I am good at dealing with people. Can't you understand that? What is wrong with you people?
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-10-20 15:21
    Here's a simpler version that just works with bytes:

    ' Use: result = SET_BIT value, position, new_bit
    
    FUNC SET_BIT
      tmpB1 = __PARAM1
      tmpB2 = __PARAM2
      tmpB3 = __PARAM3
    
      tmpB4 = 1 << tmpB2
      IF tmpB3.0 = 1 THEN
        tmpB1 = tmpB1 | tmpB4    
      ELSE
        tmpB4 = ~tmpB4
        tmpB1 = tmpB1 & tmpB4
      ENDIF
      RETURN tmpB1
      ENDFUNC
    
  • Chicago MikeChicago Mike Posts: 88
    edited 2008-10-21 13:55
    Perfect. I wrote something like this last night but the method of making a bitmask for changing a bit to 1 was much for complicated. Thanks!
Sign In or Register to comment.