Professional Documents
Culture Documents
DC-18 App-B Vivek Ver2 PDF
DC-18 App-B Vivek Ver2 PDF
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
void whatcrc(struct dat *d, int mode) // calculate a CRC given a dat structure
{
char *msg; // buffer to message
int i,j,len; // counters and length of message
union doub sck,byt;
{
printf("Sorry, but I couldn't allocate memory\n");
exit(1);
}
for (i=0;i<d->dcount;++i)
msg[i+2]=d->data[i];
if (mode)
{
msg[i+2]=(d->crc&0xFF00)>>8;
msg[i+3]=d->crc&0x00FF;
}
{
byt.c[0]=msg[i]; // put the current character at end of working byt
byt.c[1]=0; // set start of byt to 0
sck.i^=byt.i; // set sck to sck XOR byt (MOD-2 maths)
for (j=0;j<8;++j) // for each bit
{
if (sck.b.msb)
{
sck.i>>=1; // shift the remainder right 1 bit
(divide by 2)
sck.b.lsb=0; // and set the MSB to 0
sck.i^=CRC16; // set remainder = sck XOR the CRC16
polynomial
}
else
{
sck.i>>=1; // shift the remainder right 1 bit
(divide by 2)
sck.b.lsb=0; // and set the MSB to 0
}
}
}
d->crc = (sck.i)<<8; // update the CRC in the data structure
d->crc |= (sck.i&0xFF00)>>8;
free (msg); // free the temporary storage area
}