Menu

Access single bit in a word variable

Help
jackjames
2021-06-03
2021-09-01
<< < 1 2 (Page 2 of 2)
  • Anobium

    Anobium - 2021-06-06

    The root cause was that the compiler was not checking whether the variable being used was a bit value.

     
  • William Roth

    William Roth - 2021-09-01

    Here is another way I did a while back. Just for reference.
    Change the setbit routines to Macros and they will run somewhat faster.

      #Chip 18F27K40, 32
      #CONFIG MCLRE = ON, LVP = ON, CLKOUTEN = OFF
      #OPTION EXPLICIT
      #Startup INITPPS, 85
    
      #DEFINE USART_BAUD_RATE 19200
      #DEFINE USART_TX_BLOCKING
    
      HserSend 12
      Wait 600 ms
      HSERPRINT ChipNamestr
      Hserprint " Test Setbit Program"
      HserprintCRLF 2
    
      DIM BIT_NUM as BYTE
      DIM TEST_WORD AS WORD
      DIM TEST_BYTE AS BYTE
    
      TEST_WORD = 0  ' All BITS SET to "0"
      TEST_BYTE = 0
    
      'Test Byte Routine
      FOR BIT_NUM = 0 to 7
    
          SETBIT(TEST_BYTE, BIT_NUM, ON)
    
          HSERPRINT "TEST_BYTE = "
          HSERPRINT BYTEtoBin(TEST_BYTE)
          HSERPRINTCRLF
      NEXT
    
      For BIT_NUM = 7 to 0 step -1
    
          SETBIT(TEST_BYTE, BIT_NUM, OFF)
    
          HSERPRINT "TEST_BYTE = "
          HSERPRINT BYTETOBIN(TEST_Byte)
          HSERPRINTCRLF
    
      NEXT
    
      HSERPRINTCRLF
    
         'Test WORD Routine
      FOR BIT_NUM = 0 to 15
    
          SETBIT(TEST_WORD, BIT_NUM, ON)
    
          HSERPRINT "TEST_WORD = "
          HSERPRINT WORDtoBin(TEST_WORD)
          HSERPRINTCRLF
      NEXT
    
      FOR BIT_NUM = 15 to 0 step -1
    
          SETBIT(TEST_WORD, BIT_NUM, OFF)
    
          HSERPRINT "TEST_WORD = "
          HSERPRINT WORDTOBIN(TEST_WORD)
          HSERPRINTCRLF
      NEXT
    
      Do
      Loop
    
      '---------------------------------------------------------
      SUB SETBIT(_WORD_VAR_NAME as WORD, IN  _BIT_POSN as Byte, _BIT_STATE as Bit)
           Dim _BIT_MASK AS WORD
           _BIT_MASK = 1
    
           IF _Bit_State = 1 Then
               _WORD_VAR_NAME = _WORD_VAR_NAME OR (FnLSL(_BIT_MASK,_BIT_POSN))
           ELSE
             _WORD_VAR_NAME = _WORD_VAR_NAME AND !(FnLSL(_BIT_MASK,_BIT_POSN))
           END IF
    
      End SUB
    
      SUB SETBIT(_BYTE_VAR_NAME as BYTE, IN  _BIT_POSN as Byte, _BIT_STATE as Bit)
           Dim _BIT_MASK AS BYTE
           _BIT_MASK = 1
    
           IF _Bit_State = 1 Then
               _BYTE_VAR_NAME = _BYTE_VAR_NAME OR (FnLSL(_BIT_MASK,_BIT_POSN))
           ELSE
             _BYTE_VAR_NAME = _BYTE_VAR_NAME AND !(FnLSL(_BIT_MASK,_BIT_POSN))
           END IF
    
      End SUB
    
      SUB INITPPS
            'Module: EUSART1
             RX1PPS = 0x0017    'RC7 > RX1
             RC6PPS = 0x0009    'TX1 > RC6
      End SUB
    
     

    Last edit: William Roth 2021-09-01
  • Anobium

    Anobium - 2021-09-01

    The issue was bug in the compiler. And, therefore under very specific circumstances the compiler would not handle a bit as a bit, but a byte. As a bug it needed to be resolved. :-)

     
  • William Roth

    William Roth - 2021-09-01

    Nevertheless,

    The attached code works and demonstrates a way to set individual bits using a variable for the bit position and the bit state.

     
<< < 1 2 (Page 2 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.