Sigh... This is so embarrassing. The Hamming encoder/decoder I posted earlier in the week (which was itself a correction of some code I'd hastily posted to the list six months ago) contains at least one mistake. I apologize for posting, without a disclaimer, code that hadn't been tested or even assembled... I guess it's implied here on the list, but I should probably be more explicit about the disclaimer: You get, at most, what you pay for. Additionally... None of the code below has been tested or even assembled; I typed it directly into my email client as it came to me. I haven't written commercial PIC code in over two years, and I hardly even write code for ANY microcontroller these days, so my basic competence is probably suspect anyway. My PIC emulator's been on loan to a friend for over a year, so even if I did get motivated enough to assemble this code, I wouldn't test it very well. So here's the entire thing once again. The correction is the addition of an ANDLW instruction in the DECODE routine, between the CALL ENCODE and the XORWF RCVDATA: RADIX = DEC ; Encode 4 input bits to 7 output bits. Enter with 4 bits ; in W's lo-nibble (all zeroes in W's hi-nibble). Exits ; with a 7-bit codeword in the low 7 bits of W (W's MSB = 0). 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 RETLW 01111000B RETLW 01001001B RETLW 00101010B RETLW 00011011B RETLW 00011100B RETLW 00101101B RETLW 01001110B RETLW 01111111B ; Decode a 7-bit codeword into a 4-bit output, correcting any ; one-bit error if necessary. Enter with the 7-bit codeword ; in the low 7 bits of RCVDATA. Exits with the 4-bit corrected ; output in RCVDATA's lo-nibble. DECODE: MOVF RCVDATA,W ;W = RECEIVED DATA. ANDLW 00001111B ;MASK OFF ALL THE CHECK BITS. CALL ENCODE ;ENCODE WHAT'S LEFT. ANDLW 01110000B ;COMPARE THE CALCULATED CHECK BITS XORWF RCVDATA ;(BITS 4-6) TO THE RECEIVED CHECK ;BITS, WITHOUT MODIFYING THE ;RECEIVED LOW-NIBBLE. 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 ;LOW NIBBLE OF RCVDATA = CORRECTED ;DATA. RETURN ;RETURN. ; The DECODE routine needs this lookup table. BITMASK: ADDWF PCL,W RETLW 00000000B ;NO ERROR. RETLW 00010000B ;ERROR IN C1. RETLW 00100000B ;ERROR IN C2. RETLW 00000001B ;ERROR IN D1. RETLW 01000000B ;ERROR IN C3. RETLW 00000010B ;ERROR IN D2. RETLW 00000100B ;ERROR IN D3. RETLW 00001000B ;ERROR IN D4. By the way... As written, the DECODE routine gives no indication to its caller that any bits have been corrected. If you want to flag error-detections, use a BITMASK table that looks like this: BITMASK: ADDWF PCL,W RETLW 00000000B ;NO ERROR. RETLW 10010000B ;ERROR IN C1. RETLW 10100000B ;ERROR IN C2. RETLW 10000001B ;ERROR IN D1. RETLW 11000000B ;ERROR IN C3. RETLW 10000010B ;ERROR IN D2. RETLW 10000100B ;ERROR IN D3. RETLW 10001000B ;ERROR IN D4. With this modified table, DECODE will return with a 1 in RCVDATA's MSB if an error was detected and corrected, or a 0 in RCVDATA's MSB if no error was detected. Either way, RCVDATA's low-nibble will contain the decoded data. -Andrew === Andrew Warren -- aiw@cypress.com === Principal Design Engineer === Cypress Semiconductor Corporation -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics