>-----Original Message----- >From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] >Sent: 07 April 2005 03: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 That's been added to my usefull code snippets collection, thanks for posting! Maybe a stupid question, but if bit 7 is set after calling receive(), how would you tell if the data had been corrected or was garbage? Regards Mike ======================================================================= This e-mail is intended for the person it is addressed to only. The information contained in it may be confidential and/or protected by law. If you are not the intended recipient of this message, you must not make any use of this information, or copy or show it to any person. Please contact us immediately to tell us that you have received this e-mail, and return the original to us. Any use, forwarding, printing or copying of this message is strictly prohibited. No part of this message can be considered a request for goods or services. ======================================================================= -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist