| Purpose: | Generate a CCITT 16-bit CRC over a block of SRAM memory. | ||||
| Assumes: | The CRC generator polynomial is x15 + x13 + x0 (which is the value 0xA001 or 1010000000000001.) | ||||
| Passed: | R25:R24 | BYTE * | bPtrBuffer | the pointer to the start of the SRAM buffer | |
| R22 | BYTE | bCount | the number of bytes to include in the CRC | ||
| R20 | BYTE | bInit | 0 specifies the initial CRC of 0x0000, anything else 0xFFFF | ||
| Returns: | R25:R24 | WORD | wCRC | the CRC value over the SRAM range | |
| Alters: | Only R20, in addition to R24, R25 and the FLAGS | ||||
| Example: |
|
| Test it: |
|
| Notes: | The term "CRC" is an acronym, meaning Cyclic Redundancy Check. A CRC provides a way of detecting, but not correcting, bit errors in a block of data. Consider a data block to be simply a very long number. If that number is divided that by some known constant both before and after some event (e.g., data transmission and reception or storage and retrieval) and the remainders are identical, it approximates what a CRC is doing. Given a 16-bit "remainder", only one in 65,536 values will match. This is not precisely correct, mathematically, but it's a good mental model of how a CRC detects an error. There is much more information about CRCs on the web. |
| The RunningCRC system functions is able to calculate a 16-bit CRC over a range of SRAM, FLASH or EEPROM, as it does so one (1) BYTE at a time. This one only works on SRAM. A block could be copied from FLASH or EEPROM to SRAM and the CRC then calculated over the SRAM buffer using this function, but the source of any error could either be in copying or in storage. | |
| When a CRC WORD is initialized to 0xFFFF, changes in blocks that begin with a series of zeros can be detected. When initialized to 0x0000, they cannot. | |
| See the notes in the msTimerBYTE and SecTimerBYTE functions. | |
| Dropin: (setup code) |
; Setup for the CRC call: ; Passed: R25:R24 BYTE * bPtrBuffer to the start of the buffer in SRAM ; R22 BYTE bCount is the number of BYTEs to include in the CRC ; R20 BYTE bInit: 0 to initialize CRC to 0, else init to 0xFFFF ; Returns: R25:R24 WORD wCRC ; Alters: R0, R24, R25 and the FLAGS (R25:R24 has the return value) |
| Also see: | The millis and SystemSeconds variables, the msTimerBYTE, SecTimerBYTE, msTimerWORD, and SecTimerWORD timers. |