title: PIC16LF1824 SPI Slave Interface
body: <p>I am trying to receive multiple bytes over SPI. The aim is when the master starts the SPI transfer, slave MCU is interrupted, and it should read the data via SPI and store it in an array, which will be then used by my application for other operations such as determining the device ID and the contents of the packet.</p>
  
  <pre><code>void interrupt __high_priority my_isr_high(void) {
  
  if (PIR1bits.SSP1IF) { // Interrupt from SPI? 
      rx[buffer_pointer] = SSP1BUF; // Get data from MSSP and store in RX buffer
      buffer_pointer++; // Next data
      if (buffer_pointer &lt; FRAME_SIZE) // Ended?
          SSP1BUF = tx[buffer_pointer]; // Send next byte to SPI
      else
         buffer_pointer = FRAME_SIZE;
      PIR1bits.SSP1IF = 0; // Clear interrupt flag
     }
   }
  </code></pre>
  
  <p>However, I am not receiving the 3 bytes correctly. I am sending the following from the master:</p>
  
  <pre><code>dataPacket[0] = 0x43; // Decimal 67
  dataPacket[1] = 0x42; //66
  dataPacket[2] = 0x41; //65
  </code></pre>
  
  <p>While I am receiving as follows from the ISR():</p>
  
  <pre><code>rx[0]: 67 
  rx[1]: 65 
  rx[2]: 67 
  </code></pre>
  
  <p>Am I missing something or handling the SPI incorrectly?  </p>
  
  <p>This will really solve the issue that I am stuck with and maybe will also help others who what to rx multiple bytes. </p>
  
  <p>I am sharing my codes here so that it helps to find the solution quickly. Also included is a .zip file for compiling. <a href="https://www.dropbox.com/sh/4soikmrvcmoibn0/AAAvx6hj5eLVnwEpPHgBKQi6a?dl=0" rel="nofollow noreferrer">Check the Codes here</a></p>
  
  <p>So far the above code did not work for me properly. Therefore, after a little bit of digging online and other forums I found the following way to read multiple bytes:</p>
  
  <pre><code>uint8_t SPI_ExchangeHandler(uint8_t byte){
  static uint8_t i = 0;
  for(i=0; i&lt;3; i++){
      SSP1BUF =0x00;
      while(!SSP1STATbits.BF);
      rx_buff[i]=SSP1BUF;
   }
  State = SEND; 
  return byte;
  }
  </code></pre>
  
  <p>Although the above codes give me what expected (i.e, correct data packets in the ordered manner), however, it misses two SPI interrupts every time and then displays/captures the correct data. Hence, two sets of data are always lost and then the third one is received correctly.
  Is something wrongly configured or missing?</p>