Last active
December 26, 2024 17:06
-
-
Save hunternsk/aa561a5dcba9e3faa683f34f60d95559 to your computer and use it in GitHub Desktop.
CRC-8-SAE J1850 test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc crc.c -std=c99 -o crctest | |
#include <inttypes.h> | |
#include <stdio.h> | |
uint8_t crcTable[256]; | |
uint8_t CalcCRC(uint8_t * buf, uint8_t len); | |
void CRCInit(void); | |
uint8_t buf[] = {0x34, 0x28, 0x00, 0x02, 0x71}; | |
int main() { | |
CRCInit(); | |
printf("0x%2X\n", CalcCRC(buf,sizeof(buf))); | |
return 0; | |
} | |
uint8_t CalcCRC(uint8_t * buf, uint8_t len) { | |
const uint8_t * ptr = buf; | |
uint8_t _crc = 0xFF; | |
while(len--) _crc = crcTable[_crc ^ *ptr++]; | |
return ~_crc; | |
} | |
void CRCInit(void) { | |
uint8_t _crc; | |
for (int i = 0; i < 0x100; i++) { | |
_crc = i; | |
for (uint8_t bit = 0; bit < 8; bit++) _crc = (_crc & 0x80) ? ((_crc << 1) ^ 0x1D) : (_crc << 1); | |
crcTable[i] = _crc; | |
} | |
} |
Thanks !
Nice job! It's really helpful! Thanks for sharing!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked like a charm. Thanks.