Skip to content

Instantly share code, notes, and snippets.

@hunternsk hunternsk/crc.c

Last active Jan 18, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

ecobra commented Jun 7, 2018

Worked like a charm. Thanks.

@ahamedalishaik

This comment has been minimized.

Copy link

ahamedalishaik commented Mar 22, 2019

Thanks !

@endland

This comment has been minimized.

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
You can’t perform that action at this time.