SirNickity:
Try this:
void setup()
{
uint16_t crcval;
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
// *** Use a statically-defined array of bytes, like this:
char valor = { 0xAA, 0x05, 0x01 };
// *** You can divide the size of the array by the size of one array element
// to get the length of an array. (In this case, each element is one byte, so
// you're dividing by one, but it's better to use the correct formula unless
// the useless divide operation is performance-critical in your application.)
crcval = crc16_lsb(valor, sizeof(valor) / sizeof(valor[0]));
}
void loop() // run over and over
{
char c;
if (Serial.available())
c = Serial.read();
if (c == '1') digitalWrite(13, HIGH);
if (c == '0') digitalWrite(13, LOW);
if (c == 'A') digitalWrite(7, HIGH);
if (c == 'F') digitalWrite(7, LOW);
}
// *** Dropping all the word references and using std int types instead
uint16_t crc16_lsb (char *pData, uint16_t length)
{
// *** Using uint8_t instead of byte. They're equivalent, so it's a matter
// of preference, but I believe in using a type that defines the variable's
// purpose -- a byte would signify to me that its contents is probably
// binary, whereas uint8_t just tells me 'i' is an 8-bit unsigned integer.
uint8_t i;
uint16_t data, crc; // *** Again, dropping the word type
crc = 0xFFFF;
// *** I'm removing the "if length == 0" thing and using a loop where
// the while conditional is up top. This skips the loop when length == 0,
// so there's no need to handle that case specifically. The only difference
// in functionality is what gets returned when length == 0. Before, the
// code returned "0x0000". Now, it returns "0xFFFF" -- the crc seed
// value. I think this is actually what's supposed to happen, but if I'm
// wrong, undo that change.
while (length--) {
// *** You need to cast "*pData" to an 8-bit type here to operate on
// one byte at a time, otherwise the compiler will use the default int
// type -- which is 16-bit, since the pointers refers to a 16-bit type.
// You don't, however, need to cast the literal (0x00FF) to a 16-bit
// type. It already is. (32-bit/64-bit computers would be another
// matter though...)
data = 0x00FF & (uint8_t)*pData++;
crc = crc ^ data;
for (i = 8; i > 0; i--)
{
if (crc & 0x0001)
crc = (crc >> 1) ^ 0x8408;
else
crc >>= 1;
}
}
crc = ~crc;
Serial.println(crc, HEX);
return (crc);
}
XD XD XD XD XD XD XD XD XD XD XD
It was perfect!!
I did some testing and all were ok.
I will now check what has been changed to enteder operation.
Thank you !!!!!