Skip to content

Instantly share code, notes, and snippets.

@gamaral
Created December 10, 2017 08:39
Show Gist options
  • Save gamaral/40418155dc615f4df42e70c0008d820d to your computer and use it in GitHub Desktop.
Save gamaral/40418155dc615f4df42e70c0008d820d to your computer and use it in GitHub Desktop.
CRC-8 for J1850
uint8_t
calculate_crc(const uint8_t *data, const size_t n)
{
const uint8_t poly = 0b00011101;
int datai, biti;
uint8_t crc = ~0;
for (datai = 0; datai < n; ++datai, ++data) {
crc ^= *data;
for (biti = 0; biti < 8; ++biti)
crc = (crc << 1) ^ (crc & 0x80 ? poly : 0);
}
return ~crc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment