I am writing code to receive SPI data in an interrupt service routine on a PIC18F2680 microcontroller that is running at 40MHz that needs to receive 2 bytes (16 bits) of data from another microcontroller. The PIC only receives data (passively listening), and does not send anything back to the sender. The two data lines that are used MISO, and SCLK on the device. There was no slave select used in the SPI communication, and MOSI is not necessary for listening to the commands, only the slaves responses.
I did not realize at the time of design that the SPI data packets were sent 16 bits at a time, otherwise I would have used a different microcontroller.
I wanted to see if there was a way to read in two consecutive bytes in a SPI ISR without losing any data. My current implementation:
OpenSPI(SLV_SSOFF,MODE_00,SMPMID);
//***********************************SPI ISR*********************************
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
_asm
goto InterruptHandlerHigh //jump to interrupt routine
_endasm
}
//----------------------------------------------------------------------------
// High priority interrupt routine
#pragma code
#pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh () {
unsigned int next;
//the interrupt flag is set for SPI
if(PIR1bits.SSPIF ==1){
spiByte1 = SSPBUF;
while(SSPSTATbits.BF != 0);
spiByte2 = SSPBUF;
}
PIR1bits.SSPIF = 0;
}
However, this seems to be getting some correct data, but losing a lot of other bytes. Is there a better way to accomplish this, or am I SOL using an 8-bit MCU?
Thank you,
John