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.
Question about communication between two mcu via spi[Solved]
-
- Posts: 217
- http://meble-kuchenne.info.pl
- Joined: Thu Dec 03, 2020 1:43 pm
- Has thanked: 6 times
- Been thanked: 7 times
Question about communication between two mcu via spi[Solved]
Last edited by seokgi on Tue Aug 12, 2025 2:37 am, edited 1 time in total.
-
- Valued Contributor
- Posts: 1626
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 142 times
- Been thanked: 757 times
Re: Question about communication between two mcu via spi
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
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
Here my project.
Thank you.
Thank you.
- Attachments
-
- 250729-Main-PIC24FJ512GA606_SPI_Master_Forum.fcfx
- (59.14 KiB) Downloaded 83 times
-
- 250730-18F45K40-SPI_Slave_Forum.fcfx
- (33.61 KiB) Downloaded 67 times
-
- Valued Contributor
- Posts: 1626
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 142 times
- Been thanked: 757 times
Re: Question about communication between two mcu via spi
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
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
-
- Valued Contributor
- Posts: 1626
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 142 times
- Been thanked: 757 times
Re: Question about communication between two mcu via spi
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
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
- Attachments
-
- SPI_Slave.fcfx
- (11.57 KiB) Downloaded 70 times
-
- SPI_Send.fcfx
- (13.15 KiB) Downloaded 53 times
-
- Valued Contributor
- Posts: 1678
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 373 times
- Been thanked: 581 times
Re: Question about communication between two mcu via spi
Hi Martin
With the PIC, could you not use one of the Synchronous Serial Port Interrupts?
Regards
With the PIC, could you not use one of the Synchronous Serial Port Interrupts?
Regards
-
- Valued Contributor
- Posts: 1626
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 142 times
- Been thanked: 757 times
Re: Question about communication between two mcu via spi
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
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
-
- Valued Contributor
- Posts: 1626
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 142 times
- Been thanked: 757 times
Re: Question about communication between two mcu via spi
Can you post your code (just for the comms) - and I'll look...
See above about the preloading of the transmitted data - it's somewhat difficult to work around....
Martin
See above about the preloading of the transmitted data - it's somewhat difficult to work around....
Martin
Re: Question about communication between two mcu via spi
When I send each variable to serial, the data does not come out correctly.
The order of each variable is .
123 124 125 126 127 128
This is the order in which the values of the variables are printed.
128 123 124 125 126 127
Thank you.
The order of each variable is .
123 124 125 126 127 128
This is the order in which the values of the variables are printed.
128 123 124 125 126 127
Thank you.
- Attachments
-
- 250811-18F45K40-SPI_Slave_Forum.fcfx
- (39.3 KiB) Downloaded 55 times
-
- 250811-Main-PIC24FJ512GA606_SPI_Master_Forum.fcfx
- (64.41 KiB) Downloaded 49 times