I am trying to let two uC talk to each other. Both chips are from dsPIC33EP family. The slave will respond according to what master has sent to it. My question is, since SPI is full-duplex, would it be too late to send data back to master in one communication cycle? Should I load one byte to transmit buffer before receiving one byte? Please see my code as I demonstrate my doubts.
void ISR() iv IVT_ADDR_SPIINTERRUPT{
char data;
SpiIntFlag=0;//clear interrupt flag
data=SPIBUF;//read the incoming byte
//send back one byte according to the received byte
if(data==0x01){
SPIBUF=0xF1;
}
else if(data==0x02){
SPIBUF=0xF2;
}
else if(data==0x03){
SPIBUF=0xF3;
}
else if(data==0x04){
SPIBUF=0xF4;
}
}
As you can see, a byte will be sent after the slave receives one. Does it mean receiving and sending do not happen at the same time? I have tried this code with ATmega32u4 at 5MHZ. But it looks like the master receives a corresponding byte according to the byte sent to the slave. Also, there is no additional bytes received so I think this communication process(read and send) happen at the same time. What about the delay that if() statements caused?
Is this the correct way to do SPI communication? All I want to achieve is master send one command byte and slave send back couple bytes back according to it.