onewire:crc
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
onewire:crc [2012/05/15 14:08] – fdc | onewire:crc [2013/09/18 09:08] (current) – sdolt | ||
---|---|---|---|
Line 1: | Line 1: | ||
< | < | ||
+ | |||
+ | < | ||
====== DOW CRC (Dallas One Wire CRC) ====== | ====== DOW CRC (Dallas One Wire CRC) ====== | ||
+ | <note warning> | ||
===== DOW CRC-8 ===== | ===== DOW CRC-8 ===== | ||
Line 9: | Line 12: | ||
<file c> | <file c> | ||
#include < | #include < | ||
+ | #include < | ||
#define CRCPOLY8 0x18 // | #define CRCPOLY8 0x18 // | ||
void main(void) | void main(void) | ||
{ | { | ||
- | int crc = 0x00 , i = 0, bit_counter = 0, b = 0, feedback_bit = 0; // | + | int crc = 0x00 , i = 0, bit_counter = 0, b = 0, feedback_bit |
char string[]={" | char string[]={" | ||
- | for (i=0; i<=15; i++) // | + | |
+ | | ||
{ | { | ||
- | b = string[15-i]; // | + | b = string[length-i]; // |
bit_counter = 4; //si le caractère a déjà été décomposé en hexadécimal => 4 bits par valeur | bit_counter = 4; //si le caractère a déjà été décomposé en hexadécimal => 4 bits par valeur | ||
while (bit_counter> | while (bit_counter> | ||
Line 25: | Line 30: | ||
if (feedback_bit == 0x01) crc = crc ^ CRCPOLY8; | if (feedback_bit == 0x01) crc = crc ^ CRCPOLY8; | ||
crc = (crc >> 1) & 0x7F; // | crc = (crc >> 1) & 0x7F; // | ||
- | if (feedback_bit == 0x01) crc = crc | 0x80; // | + | if (feedback_bit == 0x01) crc = crc | 0x80; // |
b = b>> | b = b>> | ||
bit_counter--; | bit_counter--; | ||
} | } | ||
} | } | ||
- | printf(" | + | printf(" |
} | } | ||
</ | </ | ||
==== Exemple de fonctionnement ==== | ==== Exemple de fonctionnement ==== | ||
- | 1-Wire ROM: A2 00 00 00 01 B8 1C 02 \\ CRCPOLY8 = 0x18 | + | 1-Wire ROM: A2 00 00 00 01 B8 1C 02 \\ CRCPOLY8 = 0x18 \\ CRCinitial = 0x00 |
=== i=0 === | === i=0 === | ||
=== b = string [15] <=> ' | === b = string [15] <=> ' | ||
Line 405: | Line 410: | ||
=== i=2 === | === i=2 === | ||
=== b = string [13] <=> ' | === b = string [13] <=> ' | ||
+ | |||
+ | . \\ . \\ . | ||
+ | |||
+ | === i=13 === | ||
+ | === b = string [2] <=> ' | ||
+ | |||
+ | . \\ . \\ . | ||
+ | |||
+ | == bit_counter = 1 == | ||
+ | |||
+ | <code c> feedback_bit = (crc ^ b) & 0x01;</ | ||
+ | ^ crc XOR b ^^ | ||
+ | | crc | 0101' | ||
+ | | b | 0000' | ||
+ | ^ Résultat | 0101' | ||
+ | |||
+ | ^ (crc XOR b) & 0x01 ^^ | ||
+ | |crc XOR b | 0101' | ||
+ | | 0x01 | 0000' | ||
+ | ^ Résultat | ||
+ | |||
+ | **=> feedback_bit = 0x01** | ||
+ | |||
+ | <code c> if (feedback_bit == 0x01) crc = crc ^ CRCPOLY8; </ | ||
+ | |||
+ | ^ crc XOR CRCPOLY8 | ||
+ | | crc | 0101' | ||
+ | | CRCPOLY8 | ||
+ | ^ Résultat | ||
+ | |||
+ | **=> crc = 0x45** | ||
+ | |||
+ | <code c> crc = (crc >> 1) & 0x7F; </ | ||
+ | |||
+ | ^ crc>> | ||
+ | | crc | 0100' | ||
+ | ^ crc>> | ||
+ | |||
+ | ^ (crc>> | ||
+ | | crc>> | ||
+ | | 0x7F | 0111' | ||
+ | ^ Résultat | ||
+ | |||
+ | **=> crc = 0x22** | ||
+ | |||
+ | <code c> if (feedback_bit == 0x01) crc = crc | 0x80; </ | ||
+ | |||
+ | ^ crc OR 0x80 ^^ | ||
+ | | crc | 0010' | ||
+ | | 0x80 | 1000' | ||
+ | ^ Résultat | ||
+ | |||
+ | **=> crc = 0xA2 {C'est la valeur du CRC pour [00000001B81C (Numéro de série) + 02 (Family Code)], s'il est ajouté à la suite du numéro de série, le CRC final sera de 0x00!}** | ||
+ | |||
+ | . \\ . \\ . | ||
+ | |||
+ | === i=14 === | ||
+ | === b = string [1] <=> ' | ||
. \\ . \\ . | . \\ . \\ . | ||
Line 417: | Line 480: | ||
. \\ . \\ . | . \\ . \\ . | ||
- | **=> crc = 0x00** | + | **=> crc = 0x00 {Nous avons au final un 0x00, 0xA2 est bel et bien le CRC de [00000001B81C02]}** |
- | ===== CRC-16 ===== | + | ===== DOW CRC-16 ===== |
==== Fonction ==== | ==== Fonction ==== | ||
Line 425: | Line 488: | ||
<file c> | <file c> | ||
#include < | #include < | ||
- | #define CRCPOLY16 0x4002 | + | #include < |
+ | #define CRCPOLY16 0x4002 | ||
void main(void) | void main(void) | ||
{ | { | ||
- | int crc = 0x0000 , i = 0, bit_counter = 0, b = 0, feedback_bit = 0; // | + | int crc = 0x0000 , i = 0, bit_counter = 0, b = 0, feedback_bit |
char string[]={" | char string[]={" | ||
- | for (i=0; i<=1; i++) // | + | |
+ | | ||
{ | { | ||
- | b = string[1-i]; // | + | b = string[length-i]; // |
bit_counter = 4; //si le caractère a déjà été décomposé en hexadécimal => 4 bits par valeur | bit_counter = 4; //si le caractère a déjà été décomposé en hexadécimal => 4 bits par valeur | ||
while (bit_counter> | while (bit_counter> | ||
Line 441: | Line 506: | ||
if (feedback_bit == 0x0001) crc = crc ^ CRCPOLY16; | if (feedback_bit == 0x0001) crc = crc ^ CRCPOLY16; | ||
crc = (crc >> 1) & 0x7FFF; | crc = (crc >> 1) & 0x7FFF; | ||
- | if (feedback_bit == 0x0001) crc = crc | 0x8000; | + | if (feedback_bit == 0x0001) crc = crc | 0x8000; |
b = b>> | b = b>> | ||
bit_counter--; | bit_counter--; | ||
} | } | ||
} | } | ||
- | printf(" | + | printf(" |
} | } | ||
</ | </ | ||
==== Exemple de fonctionnement ==== | ==== Exemple de fonctionnement ==== | ||
- | 1-Wire ROM: 75 \\ CRCPOLY8 | + | 1-Wire ROM: 75 \\ CRCPOLY16 |
=== i=0 === | === i=0 === | ||
=== b = string [1] <=> ' | === b = string [1] <=> ' | ||
Line 828: | Line 893: | ||
**=> crc = 0x6390** | **=> crc = 0x6390** | ||
- | |||
- | <code c> b = b>>1; </ | ||
- | |||
- | ^ b>> | ||
- | | b | 0000' | ||
- | ^ b>> | ||
- | |||
- | **=> b = 0x0000** | ||
- | |||
- | === crc-16 de ' | ||
- | **=> b = 0x00** | ||
- | |||
- | === i=2 === | ||
- | === b = string [13] <=> ' | ||
- | |||
- | . \\ . \\ . | ||
- | |||
- | === i=15 === | ||
- | === b = string [0] <=> ' | ||
- | |||
- | . \\ . \\ . | ||
- | |||
- | == bit_counter = 1 == | ||
- | |||
- | . \\ . \\ . | ||
- | |||
- | **=> crc = 0x00** |
onewire/crc.1337090920.txt.gz · Last modified: 2012/05/15 14:08 by fdc