1

I am getting this and it is annoying. is there anyone can see the mistake? and know how to fix it?

warning: (373) implicit signed to unsigned conversion

unsigned char read_soft_SPI(void) {
  unsigned char value = 0;
  unsigned char i = 0;
  unsigned char x = 0x80;

  SPI_SCK = 0;
  __delay_us(1);

  for (i = 0; i < 8; i++) {
    __delay_us(1);
    if (SPI_MISO == 1) {
      value = value | (x >> i);
    }
    SPI_SCK = 1;
    __delay_us(1);
    SPI_SCK = 0;

  }
  return value;
}
Sagar V
  • 12,158
  • 7
  • 41
  • 68
Oguzhanreis
  • 33
  • 1
  • 7
  • for proper formatting of code, open the snippet, paste your code in the JavaScript section, click on the `Tidy` in the left pane, and then copy it and paste it in the question. – Sagar V Jun 08 '17 at 11:59
  • 3
    What is the line of the warning ? I don't see anything that will produce this warning in this code. Should be close as "can't reproduce" or provide a read [mcve]. – Stargateur Jun 08 '17 at 12:01
  • What is `SPI_SCK`, `SPI_MISO` and `__delay_us`? At which line exactly in the code you posted do you get the warning? – Jabberwocky Jun 08 '17 at 12:13

1 Answers1

1

I'm having just now similar "problems". I've solved as follows : value = (unsigned char) (value | (x >> i)); I believe that basically the compiler does not know that the result of your operation will remain inside a 8 bit size and without sign, so it asks to you to specify it.

The strange thing that's happening to me is that the solution is not yet enough good for 8 bit to 16 bit operation as this :

unsigned int mul;
unsigned char a,b;
mul=(unsigned int) ((a+CONSTANT_VALUE*2/3)*b);
  • "basically the compiler does not know that the result of your operation will remain inside a 8 bit size" It can't know that, but it can know that adding two unsigned char together and then truncate the result of that into another unsigned char is perfectly well-defined. An 8 bitter compiler might even chose to carry out the addition on 8 bit types. MPLAB always had a nasty repution though, so what it does and doesn't is anyone's guess. – Lundin Feb 13 '23 at 07:54