I am receiving a string of data via UART in PIC32, extracting and concatenating two of its bytes, dividing the result by 2, converting the resulting integer to a string and transmitting via i2c to another PIC32. My problem is that this conversion has a strange bug: for numbers greater than 1K, I get weird results like 32760 even though it should be like 1000, 1252, 1100, 1090 etc. but it is good for some specific numbers like 1050, 1080, 1085.
Any suggestions are welcome.
My code is as below:
'''
default: if (flag.bits.nopmeasure_received)
{
nopmeasure[Ptr_nopmeasure++] = c;
if(Ptr_nopmeasure >= 4)
{
flag.bits.LR_OK = true;
I2c_Data.Lr.flag.bits.mesure_received = true;
unsigned short result = 0;
char buff[2];
result = (nopmeasure[3]<<8)|nopmeasure[2];
result = result / 2;
itoa(buff, result, 10);
strcpy(I2c_Data.Lr.Mesure1, buff);
flag.bits.nopmeasure_received = false;
}
}
break;
'''