FFT
Joined: 07 Jul 2010 Posts: 92
|
ds18b20:CRC Function instead of CRC table |
Posted: Fri Nov 26, 2010 11:25 am |
|
|
Hi All,
I'm trying to replace the crc table with a function that calculates 8-bit crc for ds18b20 sensor because of limited ROM space, but no chance.
I'm using exactly that code in:
http://www.ccsinfo.com/forum/viewtopic.php?p=126649#126649
I've found this function: Code: | //calc_CRC - INTERNAL FUNCTION
//Purpose: To calculate an 8-bit CRC based on a polynomial and the series
// of data bytes
//Note: Polynomial used x^8 + x^5 + x^4 + 1 = 10001100
//Inputs: A pointer to an array of the data bytes and an int saying how many
// bytes there are in the data array
//Outputs: An int8 which is the calculated CRC
int8 ds18b20_calc_CRC(int8* data, int8 bytes)
{
#define CRC_POLY 0x8C
int8 shift_register = 0, i, datab, bits;
for(i = 0; i < bytes; ++i)
{
datab = *(data + i);
for(bits = 0; bits < 8; ++bits)
{
if(bit_test((shift_register ^ datab), 0))
{
shift_register = shift_register >> 1;
shift_register ^= CRC_POLY;
}
else
shift_register = shift_register >> 1;
datab = datab >> 1;
}
}
return shift_register;
} //calc_CRC |
Just I've changed the row in ds1820_read() function: Code: | if (scratch[8] == dowcrc) |
to Code: | if (scratch[8] == ds18b20_calc_CRC(scratch[0], 7)) // Calculate a 8-bit crc for all 7 scratch bytes | In the linked code, but it does not work.
Also I've tried it as:
Code: | // One wire crc
int8 ow_crc(int8 x)
{
// dowcrc = dscrc_table[dowcrc^x];
dowcrc = ds18b20_calc_CRC(x,8);
return dowcrc;
} |
As said in : http://www.ccsinfo.com/forum/viewtopic.php?t=41878
As another way I've found this link:
http://www.phanderson.com/PIC/16C84/crc.html
The C function on link should call for every byte I think, I tried some variants to import it into my code but could not make it works too...
I need help to replace crc table with a function for reducing rom usage.
Best regards |
|