1

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;

'''

Ahmed
  • 171
  • 1
  • 6

1 Answers1

2

Isn't this the definition of itoa?

 #include <stdlib.h>

 char *itoa(int value, char *string, int radix);

If it is, your code should be:

 char buff[8];

 itoa(result, buff, 10);

If the maximum value of the MSByte is 127 since the argument is unsigned but the function receives an integer. If the MSBit of the number you build with the OR operation is 1, you will get negative numbers.

devnull
  • 8,447
  • 2
  • 15
  • 38
  • 1
    +1 for using the correct size for `buff` – Dave Tweed Mar 29 '21 at 10:58
  • @vangelo as per my stdlib, the format should be like this: [ extern char * itoa(char * buf, int val, int base); ] – Ahmed Mar 29 '21 at 11:08
  • Thanks for the confirmation (it is different in the reference I checked). This is the why I asked first. The comments regarding the range and the buffer size still stand. – devnull Mar 29 '21 at 11:15
  • I have adjusted the 'buff' size. Do I also need to change the type of 'result'? – Ahmed Mar 29 '21 at 11:23
  • The compiler is probably promoting (nopmeasure[3]<<8) to (int), otherwise you would always get zero for the MSByte. Depending on the range of both 8 bits values, it will not make a difference changing result to int. – devnull Mar 29 '21 at 11:27
  • Okay thanks. Can you suggest another approach to do this? – Ahmed Mar 29 '21 at 11:42
  • You are welcome @Ahmed. We are seeing just a small portion of the code, from 1 of the two uCs, and don't know more details about how the values are obtained and the communication itself. If it still does not work, please share more information otherwise we would be just guessing. – devnull Mar 29 '21 at 11:48
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/122400/discussion-between-ahmed-and-vangelo). – Ahmed Mar 29 '21 at 11:53