Re: HP82240B IR Printer Protocol Message #7 Posted by Steve (Australia) on 28 June 2001, 8:12 p.m., in response to message #6 by Andrés C. Rodríguez (Argentina)
Actually a combination of AND and XOR is used to calculate the ECC, as copied from my article...

How do we get the ECC? well it's like this...
The 4 bits (call them H1, H2, H3, and H4) are calculated individually
Firstly for each bit you apply a mask (AND) to the data
the masks are:
H1 01111000
H2 11100110
H3 11010101
H4 10001011
So for the code 01000001 the resulting data for these is
H1 01000000
H2 01000000
H3 01000001
H4 00000001
Then you calculate the EVEN parity bit required for these (i.e. the bit required so that there are an EVEN number of bits. Since H1, H2, and H4 have an odd number of bits, the parity bit for these would be 1, bit for H3 it is 0.
Then you string them together (the parity bits) this gives you:
1101 which is what we found above!
These parity bits can be used to detect and correct up to two missed bits (though how you do that is well beyond what I'm prepared to go into!)

The masks are ANDed with the byte so that each ECC bit represents only 4 or 5 bits of the original data byte.
Then the even parity is generated and used as the ECC bit.
Calculating parity can be seen as an XOR operation. If you XOR all the bits together you will end up with the even parity bit (inverting this gives you an odd parity bit)
One thing that I find very interesting about this protocol is that it can be viewed as having the information not carried in the signal, but in the gaps between the signals. And decoding it this way means that you do not have to worry too much with exact timings.
