• PicŪ Basic

  • GRAY code

    I propose a little routine for encoding and decoding the Gray code. When I found the solution and I realized that most nice short code is not the fastest. You will find 2 types of routines in the library.
    The routine can manage up to 16 bits Binary and Gray code.

    Example of decode the Gray format.
        xBinaryNumber = 0                               ' BinaryNumber result, clear first.
        nXOR_result = 0                                 ' Temporary variable for Carry Bit.
        xNumberBits = 16                                ' Number GRAY bits to decode.
        xGrayNumber = xGrayNumber << 1                  ' Shift 1 bit to the left to load the Carry.
        nXOR_result = nXOR_result ^ STATUS              ' XOR Carry with the previous bit of BinaryNumber output.
        xBinaryNumber = xBinaryNumber | nXOR_result.0   ' Load the Bit to the BinaryNumber.0 position.
        Dec xNumberBits                                 ' Look for the next bit.
        If xNumberBits > 0 Then
            xBinaryNumber = xBinaryNumber << 1          ' Shift 1 bit to the left to load the next Binary bit.
            GoTo GrayToBin_Loop                         ' Until all bits are done.