User Tools

Site Tools


en:pfw:crc16

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
en:pfw:crc16 [2023-09-04 18:12] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1en:pfw:crc16 [2023-09-04 18:12] (current) – ↷ Seite von pfw:crc16 nach en:pfw:crc16 verschoben uho
Line 1: Line 1:
 +{{pfw:banner.png}}
 +===== CRC generation - 16bit =====
 +
 +Here some examples of 16 bit CRC-code generators.
 +
 +The start-value is important: start with 0xFFFF and you have the CCITT version of CRC-16, start with 0x0000 and you have the CRC-16 as used in the XMODEM data-transfer protocol.
 +
 +There are a lot of different CRC-standards. They all have the same principle but differ in details. This is not relevant when using it for yourself, but finding the correct version for existing protocol is not always easy.
 +
 +For an example of a very specific way of using a CRC-code, see the CRC16 for NRF24.
 +
 +==== Implementations ====
 +
 +The generic Forth version should run on all Forth implementations.
 +
 +===== Generic Forth =====
 +
 +<code forth>
 +hex
 +1021 CONSTANT crc-polynomial ( CCITT )
 +
 +: crc16 ( n ch--n)
 +    8 lshift XOR
 +    8 0 DO
 +        DUP 8000 AND IF                     \ msb = '1'?
 +            2*
 +            FFFF AND                        \ needed for systems with > 16 bits
 +            crc-polynomial XOR
 +        ELSE
 +            2*
 +        THEN
 +    LOOP ;
 +
 +: tst
 +    ffff                                    \ start-value
 +    f0 crc16
 +    f0 crc16
 +    f0 crc16
 +    f0 crc16
 +    05 crc16
 +    0d crc16
 +    15 crc16
 +    02 crc16
 +    84 crc16
 +    . ;
 +</code>
 +
 +===== Contributions =====
 +
 +<html><h2 style="background-color:yellow">Alternative Implementations</h2></html>