Skip to content

Instantly share code, notes, and snippets.

@hunternsk
Last active December 26, 2024 17:06
Show Gist options
  • Save hunternsk/aa561a5dcba9e3faa683f34f60d95559 to your computer and use it in GitHub Desktop.
Save hunternsk/aa561a5dcba9e3faa683f34f60d95559 to your computer and use it in GitHub Desktop.
CRC-8-SAE J1850 test
// 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;
}
}
@ecobra
Copy link

ecobra commented Jun 7, 2018

Worked like a charm. Thanks.

@ahamedalishaik
Copy link

Thanks !

@endland
Copy link

endland commented Dec 6, 2019

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