Functionally identical code, different results

I have 2 pieces of code that do the same thing, but actually it does not work. Can someone explain why?

The code sends data via spi to the FPGA, which starts the display. I'm almost from a code repository on a chip, so I tried to cut everything I could. The change below is broken for some reason, the rest of the program is exactly the same as it is.

//Looping to execute code twice doesnt work
for (byte i = 0; i < 3; i++)
      {
        temp2 = temp % 10;
        temp /= 10;
        temp2 |= 0x40;
        for (byte k = 0; k < 2; k++)
        {
          SPI.transfer(reg[j]);
          delayMicroseconds(10);
          SPI.transfer(temp2);
          delayMicroseconds(10);
        }
        reg[j] -= 1;
      }

.

//But copy-paste does
for (int i = 0; i < 3; i++)
          {
            temp2 = temp % 10;
            temp /= 10;
            temp2 |= 0x40;
            SPI.transfer(reg[j]);
            delayMicroseconds(10);
            SPI.transfer(temp2);
            delayMicroseconds(10);
            SPI.transfer(reg[j]);
            delayMicroseconds(10);
            SPI.transfer(temp2);
            delayMicroseconds(10);
            reg[j] -= 1;
          }
+4
source share
2 answers

The most likely explanation is that some other code relies on this loop to satisfy certain time constraints, and fails if it does not.

, , :

  • i byte, int. - int "" , . , byte . , byte , int, int).
  • , . (, , , ), . , ( k, ..) .

- , - . , (, ), (, int [ ] , ).

0

... - ...

, - "", .

reg[j] -= 1; 

, , , j , , k .

... , .;)

0

Source: https://habr.com/ru/post/1666045/


All Articles