Jon Baker wrote: > Just wondered if anyone had written any pic .asm code for encoding / > error checking data encoded using the 'hamming' technique. Theres a few > docs on the piclist site about it but no code that I can see? Jon: Can't really write the code for you unless you tell us how many data bits, etc... But here's the basic idea; you can make this code faster, I'm sure, or extend it to larger codewords or whatever. To encode, just use a lookup table. For a 4,7 code (4-bits input, 7 bits output), the table looks like this: ENCODE: ADDWF PCL,W ; CCCDDDD C1 = D4 + D2 + D1 ; 3214321 C2 = D4 + D3 + D1 ; C3 = D4 + D3 + D2 RETLW 00000000B RETLW 00110001B RETLW 01010010B RETLW 01100011B RETLW 01100100B RETLW 01010101B RETLW 00110110B RETLW 00000111B I've only shown the first half of the table; I'm typing this directly into my email client, and it's a pain to do (so the above may have errors, too). Decoding is easy, too: DECODE: MOVF RCVDATA,W ;W = RECEIVED DATA. ANDLW 00001111B ;MASK OFF ALL THE CHECK BITS. CALL ENCODE ;ENCODE WHAT'S LEFT. XORWF RCVDATA ;COMPARE IT TO THE RECEIVED DATA. SWAPF RCVDATA,W ;W = BIT POSITION OF THE ERROR ANDLW 00000111B ; (1-7), W = 0 IF NO ERROR. CALL BITMASK ;CONVERT BIT POSITION TO BITMASK. XORWF RCVDATA ;RCVDATA = CORRECTED DATA. The DECODE routine needs this subroutine: BITMASK: ADDWF PCL,W RETLW 00000000B RETLW 00000001B RETLW 00000010B RETLW 00000100B RETLW 00001000B RETLW 00010000B RETLW 00100000B RETLW 01000000B All of the above assumes that RADIX=DEC, of course. -Andy P.S. Ah... Feels good to write code. I don't get to do this much, anymore. === Andrew Warren -- aiw@cypress.com === Principal Design Engineer === Cypress Semiconductor Corporation === === Opinions expressed above do not === necessarily represent those of === Cypress Semiconductor Corporation -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads