C7 · Lesson 55 · Project
Protecting a data link with a shift register and a lookup table
A temperature sensor sends readings over a long cable to a microcontroller, and every so often a burst of noise flips a bit. The fix used on the sensor's bus, as on Ethernet, USB and every disk, is a cyclic redundancy check: the sender divides its message by a fixed polynomial and appends the remainder, and the receiver repeats the division to see whether anything changed. In this project you'll watch the hardware version run in a 3-bit shift register, then implement the standard 8-bit CRC two ways in Python, bit by bit as the hardware does it and a byte at a time with a lookup table, and verify both against the published check values.
- A serial CRC register for the generator (1011). The message 1101 enters one bit per clock on D; EN stops the register after the fourth bit.
- Each clock, the top bit XORed with the incoming bit is fed back into the positions where the generator has terms ( and ).
- After four clocks = 001: the remainder of lesson 31's long division, computed with three flip-flops and two XOR gates.
Predict
The sender appends a message's CRC to it. What does the receiver get when it computes the CRC of the message plus that appended CRC, if nothing was corrupted?
Zero. Appending the remainder makes the whole frame exactly divisible by the generator, so dividing it again leaves nothing. Any single error, any two errors, and any burst shorter than the CRC change the remainder, so a nonzero result means corruption.
Name it
The bitwise CRC
The hardware register, in software: keep an n-bit value c (starting from an initial value, often 0). For each data byte, XOR it into the top of c; then, for each of its 8 bits, shift c left one place, and if the bit shifted out was 1, XOR in the generator polynomial (written without its top term: is 0x07).
The table-driven CRC
The 8 shift-and-XOR steps for a byte depend only on the 8 bits involved, so precompute them once for all 256 possible values: table[i] is the CRC of the single byte i. Then each byte costs one lookup:
for an 8-bit CRC (wider CRCs shift c and XOR in the table entry). That's eight times fewer steps, and it's how CRCs run in software.
Standard CRCs
A CRC is fully specified by its width, generator, initial value and a few bit-order options, and each standard publishes a check value: the CRC of the ASCII string "123456789".
| Name | Width | Generator | Initial | Check |
|---|---|---|---|---|
| CRC-8 (SMBus) | 8 | 0x07 | 0x00 | 0xF4 |
| CRC-16 (CCITT-FALSE) | 16 | 0x1021 | 0xFFFF | 0x29B1 |
An n-bit CRC detects every error burst of n bits or fewer, and misses a random corruption with probability .
Worked example
Step the serial CRC register through the message 1101.
Show the solution
- Set up. Start at 000; each clock, the feedback is , then , , .
- Clock in the message.
| D | f | |
|---|---|---|
| 1 | 1 | 011 |
| 1 | 1 | 101 |
| 0 | 1 | 001 |
| 1 | 1 | 001 |
- Read the remainder. The remainder is 001.
Answer the remainder is 001.
Worked example
What is table[1] for CRC-8 with generator 0x07?
Show the solution
- Shift seven times. The byte 00000001 shifts left seven times without anything falling out, reaching 10000000.
- The eighth shift. It drops that 1 and XORs in 0x07, so
table[1]= 0x07. - In general.
table[i]is the remainder of .
Answer table[1] = 0x07.
Check
What is the longest error burst a CRC with a 16-bit remainder is guaranteed to detect?
Check
For random corruption, a 16-bit CRC misses an error with probability . What is that as a power of ten (the exponent)?
Check
Write crc8(data, poly=0x07, init=0x00) for a bytes object, bitwise, and check it gives 0xF4 for b"123456789".
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write make_table(poly=0x07) returning the 256-entry CRC-8 table, and crc8_fast(data, table, init=0x00) that uses one lookup per byte.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write crc16_ccitt(data): the 16-bit CRC with generator 0x1021 and initial value 0xFFFF, bitwise, which gives 0x29B1 for b"123456789".
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a CRC catch errors that a simple sum of the bytes would miss?
A sum only accumulates the bytes' values, so many different corruptions leave it unchanged: two errors that cancel arithmetically, bytes swapped in order, or a bit set in one byte and cleared in the same place in another. A CRC is the remainder of dividing the whole message, as one long polynomial, by a carefully chosen generator, so every bit's position matters: a change anywhere shifts the remainder unless the error pattern is itself a multiple of the generator. Generators are chosen so that no short burst, and no small number of scattered bit errors, is such a multiple.
Practice
Keep practicing
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.