I'm trying to make a function to calculate the checksum of an array. I should add all values in the array and take two's complement. I've tested my code and the result is correct. As soon as i try to implement this code in a separate function the result is wrong. Can somebody help me solve this issue.
The function call is:
uint8_t Testframe[] = {0xAA,0x43,0x10,0x97,0x55,0xBE, 0x6A, 0xEA, 0xF3, 0xBE, 0xF4, 0x71, 0x9D, 0xB2, 0x4B, 0x0B, 0x45, 0x56, 0x55};
uint8_t result2 = 0;
result2 = CalculateChecksum(Testframe);
The function is:
uint8_t CalculateChecksum( uint8_t array[])
{
uint64_t result = 0;
uint8_t output = 0;
for(int i = 0; i < sizeof(array); i++)
{
result = result + array[i];
}
output = (uint8_t) result;
output = output ^ 0xFF;
output = output + 0x01;
return output;
}
The result should be 0x5A