• 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.
    Code:
    GrayToBin_Sub:
        xBinaryNumber = 0                               ' BinaryNumber result, clear first.
        nXOR_result = 0                                 ' Temporary variable for Carry Bit.
        xNumberBits = 16                                ' Number GRAY bits to decode.
      
    GrayToBin_Loop:
        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.
        EndIf
        Return
    GrayCode.zip
    Alberto
  • Recent Activity

    See_Mos-247

    Mysterious PORTB problem

    Thread Starter: xldaedalus

    I'm using Proton+ to develop firmware for a product with switches. The MCU is an 18F26K22. Most of the switches reside on PORTB. I am NOT using a...

    See_Mos Yesterday, 10:05 Go to last post
    towlerg-21522

    Pic16f18877 oread

    Thread Starter: evoortman

    Hi, On a PIC16F18877 the OREAD command doesn't seem to work. The code is working on a PIC16F1939. Both controllers use 32MHz int osc. If i...

    towlerg Yesterday, 13:23 Go to last post