Wow... That is clean... --- James. > -----Original Message----- > From: piclist-bounces@mit.edu > [mailto:piclist-bounces@mit.edu] On Behalf Of Andrew Warren > Sent: 2005 Apr 06, Wed 19:15 > To: microcontroller discussion list - Public. > Subject: [PIC] Hamming ECC again... In C, this time > > Just wrote this for a friend who needed to do simple error- > correction. I've previously posted (4,7) Hamming > encode/decode routines in assembly language, but he needed it > in C, so... > > It's two small tables and two lines of code. Maybe someone > else will find it useful. > > const uint8 encode[16] = {0x00, 0x31, 0x52, 0x63, > 0x64, 0x55, 0x36, 0x07, > 0x78, 0x49, 0x2A, 0x1B, > 0x1C, 0x2D, 0x4E, 0x7F} > > const uint8 fixbits[8] = {0x00, 0x90, 0xA0, 0x81, > 0xC0, 0x82, 0x84, 0x88} > > uint8 transmit(uint8 x) > { > return encode[x]; > } > > uint8 receive(uint8 x) > { > return x ^ fixbits[(encode[x & 0x0F] ^ x) >> 4]; > } > > The transmit() function takes as input a 4-bit number > (0x00-0x0F) and returns a 7-bit codeword (original input > unchanged in bits 0-3, three check bits in bits 4-6, and bit > 7 clear). > > The receive() function takes as input a 7-bit codeword > (0x00-0x7F) with up to two errors. If the codeword contains > no errors, the function returns the codeword unchanged > (original data in bits 0-3, correct check bits in bits 4-6, > bit 7 clear). If the codeword contains one error, the > function returns a corrected codeword (original data in bits > 0-3, correct check bits in bits 4-6, but bit 7 set to > indicate that an error was detected). If the codeword > contains two errors, the function returns garbage in bits 0-6, but > with bit 7 set to indicate that an error was detected. > > -Andy > > === Andrew Warren - fastfwd@ix.netcom.com > > -- > http://www.piclist.com PIC/SX FAQ & list archive View/change > your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist