Question about communication between two mcu via spi

For general Flowcode discussion that does not belong in the other sections.
Post Reply
seokgi
Posts: 212
http://meble-kuchenne.info.pl
Joined: Thu Dec 03, 2020 1:43 pm
Has thanked: 5 times
Been thanked: 7 times

Flowcode v10 Question about communication between two mcu via spi

Post 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.

mnfisher
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

Post 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

seokgi
Posts: 212
Joined: Thu Dec 03, 2020 1:43 pm
Has thanked: 5 times
Been thanked: 7 times

Re: Question about communication between two mcu via spi

Post by seokgi »

Here my project.

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 9 times

mnfisher
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

Post 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

mnfisher
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

Post 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
Attachments
SPI_Slave.fcfx
(11.57 KiB) Downloaded 10 times
SPI_Send.fcfx
(13.15 KiB) Downloaded 12 times

chipfryer27
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

Post by chipfryer27 »

Hi Martin

With the PIC, could you not use one of the Synchronous Serial Port Interrupts?

Regards

mnfisher
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

Post 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

Post Reply