Page 1 of 1
Question about communication between two mcu via spi
Posted: Wed Jul 30, 2025 7:07 am
by seokgi
Hello.
I want to communicate between MCUs (PIC24FJ512GA606, PIC18F45K40) via SPI.
I tried following the examples, but communication isn't working.
There are 12 data items.
If you could provide a simple example, I'll try to implement it.
Thank you.
Re: Question about communication between two mcu via spi
Posted: Wed Jul 30, 2025 8:12 am
by mnfisher
Can you post the code you have - or snippets thereof.
You'll need one device as master and one slave - and you'll need to use an interrupt on the slave to handle the transfer - master 'requests' data and slave clocks out bytes in reply.
The master 'controls' the transaction.
Martin
Re: Question about communication between two mcu via spi
Posted: Wed Jul 30, 2025 10:58 am
by seokgi
Here my project.
Thank you.
Re: Question about communication between two mcu via spi
Posted: Wed Jul 30, 2025 11:53 am
by mnfisher
The SPI_Slave ISR has a lot of code in it - remove the calls to UART etc - these may well rely and on interrupts and shouldn't be called in the interrupt handler. This includes the call to Monitor - but toggling the heartbeat pin should be okay...
Try reducing the speed of the SPI too - and for the return just return (SPI + value) rather than using a switch. The ISR needs to be as quick as possible - but depends on the SPI speed you are aiming for.
Martin
Re: Question about communication between two mcu via spi
Posted: Wed Jul 30, 2025 8:19 pm
by mnfisher
I wrote a very minimal demonstration....
I used a PIC16F18877 and as the master (since this is actually immaterial in this case) - an Arduino Uno.
The Arduino is set to send 0..255 and repeat and output to UART the number returned.
The PIC - which is responding to the Uno outputs the number of times that it has output a character ( MOD 255 - it's a byte) - note that the two values are not necessarily the same.
I also added a logic analyser - so I could 'see' what was happening (and nothing did at first

)
I needed to create a custom interrupt for the PIC - to call the ISR on SPI received - this might differ slightly on different PICs.
The wiring....
I connected (Uno -> Pic)
D11 (MOSI) -> RC5 (MISO on the PIC)
D12 (MISO) -> RC6 (MOSI)
D13 (CLK) -> C7 (Clk)
GND -> GND (Nothing happens without this!)
I didn't use CS/SS - but made this an output on the Arduino to stop SPI lockup.
This seems to work AOK - Initially I used FOsc/64 as SPI speed on the Arduino - but then tested at FOsc/4 which was a little 'flakey'. FOsc/16 seemed good though..
Martin
Re: Question about communication between two mcu via spi
Posted: Wed Jul 30, 2025 9:11 pm
by chipfryer27
Hi Martin
With the PIC, could you not use one of the Synchronous Serial Port Interrupts?
Regards
Re: Question about communication between two mcu via spi
Posted: Wed Jul 30, 2025 9:26 pm
by mnfisher
Yes - it looks like I could.....
There is an interesting 'gotcha' with SPI transmission like this - the rcv/send is synchronous - so the first byte is whatever is in the SSP1BUF - so might need to preload or ignore this... And also receive an extra byte at the end....
Martin