// IF YOU ARE LOOKING AT THIS WITH A BROWSER, USE THE BACK BUTTON TO GET // BACK TO THE PREVIOUS PAGE. YOU CAN ALSO DOWNLOAD THIS FILE BY GOING // BACK AND RIGHT CLICKING ON THE LINK, THEN SELECTING THE APPROPRIATE // COMMAND. // Define Global variables // message buffer variables unsigned char msg_buf[20], nbytes, bit_point; unsigned char * byte_point; // Switch to select VPW PWM or ISO 0, 1, 2, respectively unsigned char protocol; // values to define protocol variable #define vpw 0 #define pwm 1 #define iso 2 /********************************************************************* CRC CALCULATION SUBROUTINE Calculates the crc as defined by SAE, or the checksum for ISO *********************************************************************/ // This routine assumes that all the data bytes are in the array msg_buf[] // Starting with msg_buf[0] as the first byte. // nbytes indicates how many bytes are in the array. // The subroutine calculates both the checksum and the CRC byte as // defined by SAE and ISO specifications. // Either the CRC or the Checksum is returned, depending on what // protocol is selected. unsigned char crc(void) { unsigned char crc_reg=0xff,poly,i,j, checksum=0; for (i=0, byte_point=msg_buf; i>=1) { if (bit_point & *byte_point) // case for new bit =1 { if (crc_reg & 0x80) poly=1; // define the polynomial else poly=0x1c; crc_reg= ( (crc_reg << 1) | 1) ^ poly; } else // case for new bit =0 { poly=0; if (crc_reg & 0x80) poly=0x1d; crc_reg= (crc_reg << 1) ^ poly; } } checksum += *byte_point; // Calculate checksum } if (protocol==iso) return checksum; // Iso uses checksum, return ~crc_reg; // Otherwise, use CRC }