Search code examples
microcontrollerpicspimicrochip

Microcontroller to Microcontroller SPI communication


I was doing some reading and have even gotten a "master" SPI working on my microcontroller. Here is my question, basically if the master wants to initialize a write to the slave we write to the SSPBUF, how do we control what the slave responds with? The datasheet doesn't seem really clear to me the order of events in that case.

I.E. Master puts a char into the SSPBUF, this initiates the SPI module to send data to the slave, during the shift, the slave returns a byte.

In the slave side, is there something that tells you you have incoming data, and you can write to your SSPBUF first, THEN accept the data?

OR

Do you have to write to the SSPBUF the first "return value" you want sent back before the master can have an opportunity to initiate a transfer?


Solution

  • You want to use an interrupt on the GPIO line which handles your slave's chip-select. Ensure there is enough time for the slave to process this interrupt and load the outgoing data register before the clock starts.

    Some SPI modules preclude the use of GPIO - you will need to check if your SPI module has a function to handle chip select, or only enable the module based on the GPIO activity.