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
-
- Posts: 212
- http://meble-kuchenne.info.pl
- Joined: Thu Dec 03, 2020 1:43 pm
- Has thanked: 5 times
- Been thanked: 7 times
-
- Valued Contributor
- Posts: 1581
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 140 times
- Been thanked: 744 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 10 times
-
- 250730-18F45K40-SPI_Slave_Forum.fcfx
- (33.61 KiB) Downloaded 10 times
-
- Valued Contributor
- Posts: 1581
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 140 times
- Been thanked: 744 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: 1581
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 140 times
- Been thanked: 744 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 10 times
-
- SPI_Send.fcfx
- (13.15 KiB) Downloaded 12 times
-
- Valued Contributor
- Posts: 1640
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 362 times
- Been thanked: 575 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: 1581
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 140 times
- Been thanked: 744 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