' ========================================================================= ' ' File....... Gray_To_Binary.BS2 ' Purpose.... ' Author..... Jon Williams -- Parallax, Inc. ' E-mail..... jwilliams@parallax.com ' Started.... ' Updated.... 17 DEC 2004 ' ' {$STAMP BS2} ' {$PBASIC 2.5} ' ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' -----[ Revision History ]------------------------------------------------ ' -----[ I/O Definitions ]------------------------------------------------- ' -----[ Constants ]------------------------------------------------------- ' -----[ Variables ]------------------------------------------------------- bVal VAR Nib gVal VAR Nib idx VAR Nib idx2 VAR Nib ' -----[ EEPROM Data ]----------------------------------------------------- GTable DATA %0000, %0001, %0011, %0010, %0110, %0111, %0101, %0100, %1100, %1101, %1111, %1110, %1010, %1011, %1001, %1000 ' -----[ Initialization ]-------------------------------------------------- Reset: DEBUG CLS ' -----[ Program Code ]---------------------------------------------------- Main: DEBUG "Gray to Binary", CR, CR FOR idx = 0 TO 15 READ (GTable + idx), gVal GOSUB Gray_To_Bin DEBUG BIN4 gVal, TAB, BIN4 bVal, TAB, DEC bVal, CR NEXT DEBUG CR, CR, "Binary to Gray", CR, CR FOR bVal = 0 TO 15 GOSUB Bin_To_Gray DEBUG BIN4 bVal, TAB, BIN4 gVal, CR NEXT END ' -----[ Subroutines ]----------------------------------------------------- Gray_To_Bin: bVal = gVal ' copy high bit FOR idx2 = 2 TO 0 ' process other bits bVal.LOWBIT(idx2) = bVal.LOWBIT(idx2+1) + gVal.LOWBIT(idx2) NEXT RETURN Bin_To_Gray: gVal = (bVal >> 1) ^ bVal ' convert to Gray code RETURN