byte comparison problem in if statement

hi,everyone

I am working on a project that reads data through spi communication and prints it to a serial monitor.

When a specific byte is read, it tries to output to the serial monitor. Oddly, some bytes are read and some bytes are not read.

For example, if(buf[0] == 0x41) works fine, but if(buf[0] == 0x3F) does not.

Here is my code.

#include <stdio.h>
#include <SPI.h>


byte buf[50];

volatile byte pos; 

volatile boolean process_it;
 
void setup (void)
{  
  
  //pinMode(MISO, OUTPUT); //12  mega50
  pinMode(MOSI, INPUT); //11  mega51
  pinMode(SCK, INPUT); //13 mega52
  pinMode(SS, INPUT);  //10 mega53


  SPCR |= _BV(SPE);
  SPCR &= ~_BV(MSTR);
  SPCR |= _BV(SPIE);


  pos=0; 
  process_it = false;
 
  SPI.setBitOrder(LSBFIRST);
  SPI.setDataMode(SPI_MODE0);


  Serial.begin(57600);
  Serial.println("Serial Start!");

}


ISR (SPI_STC_vect)
{
    byte c = SPDR; 
    if (pos < sizeof(buf))
    { 
     buf[pos++] = c;
     
      if (c == 0x90)
      { 
        process_it = true;
      }
    }
}


void loop (void)
{
  
   if (process_it)
   { 

      if(buf[0] == 0x3F)  <--Here is the problem.
      {
        Serial.println("OK");
      }
      
      pos = 0; 
      process_it = false;

   }
}


 
constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                           '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 
void printHex(unsigned char * data, int len){
    char tempByte[3] = {0,0,0};
 
    for (int i = 0; i < len; i++) {
        Serial.print((char*)hexChar(tempByte, data[i]));

        if(data[i] == 0x90){
          Serial.print('\n');
          break;
        }
    }
    //Serial.print('\n');
}
 
char *hexChar(char *destBuf, unsigned char srcData){
    destBuf[0] = hexmap[(srcData & 0xF0) >> 4];
    destBuf[1] = hexmap[srcData & 0x0F];
 
    return destBuf;
}

Actually, the data received by spi is this format.
10:39:59.478 -> 41 50 3F 3F 3F 3F 90
10:39:59.478 -> 3F 50 11 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 90

However, ok is output only when buf[0] is 0x41, and ok is not output when 0x3f.

You keep talking about I2C, but that code is clearly using SPI, NOT I2C!

Also, process_it is ONLY set true when 0x97 is received, yet the data you show does NOT contain 0x97.

Regards,
Ray L.

Oh i am sorry.
yes i used spi.
I modifed question

As well as those problems, your code to do the processing in main is naive in that it assumes that...
a) the byte you are interested is at index 0 of the buffer
b) you have enough time to process all of the incoming data without any more bytes being received, before setting pos = 0 ready for the next packet

Your code is little better than polling the SPI when you rely on the latency of your mainline code in this way.

  1. Use a circular buffer with head and tail pointers, rather than setting pos back to zero from mainline. That way the ISR can be looking for and receiving subsequent packets while your mainline code processes the previous packet(s). This is how the Serial buffer works on Arduino.
  2. If all you are doing/interested in is waiting for a packet that starts 0x3F and ends 0x97 then filter for these in the ISR. That way your mainline code can just wait for a byte available in the buffer.

Whatever the issue is, its nothing to do with byte comparisons in if statements going wrong,
that would make nearly every program out there fail.

Trust the compiler more! Its very very rare to see a compiler bug these days, not unknown,
but not in something this basic, as large testsuites are run against nightly builds.