Pages: [1]   Go Down
Author Topic: Arduino as SPI Master and Slave Demo Code  (Read 3852 times)
0 Members and 1 Guest are viewing this topic.
Keeping it weird in Portland
Offline Offline
Newbie
*
Karma: 1
Posts: 44
Arduino Noob, Veteran Programmer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What a fun project this turned out to be! I feel like I just finished a good video game or something. I hope I'm not the only one who found the documentation for Arduino SPI challenging. For example the SPI library does not support SPI slave mode. This doesn't present too much of a problem but does require you to do a bit more of the "dirty work" yourself when working on the SPI slave sketch. I've attached my sketches for an Arduino SPI Master and Slave. The code is heavily documented and loaded with Serial.println output for debugging. Don't forget to make a Gnd connection between each Arduino. I'm hoping to have more Arduinos to work with and would enjoy testing a SPI master with two or more SPI slaves. My demo video can be seen on YouTube at http://youtu.be/A9oSX_dwJKQ

If you found this fun or interesting please make a small donation to my PayPal account at https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=GTBD7T7BXPGQY. I have many more Arduino projects in mind and appreciate your support.

* SPI_Master_Demo.ino (4.55 KB - downloaded 178 times.)
* SPI_Slave_Demo.ino (3.66 KB - downloaded 210 times.)
« Last Edit: August 25, 2013, 07:46:06 pm by LinhartR » Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 481
Posts: 18741
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/spi

On that page:

http://www.gammon.com.au/forum/?id=10892&reply=1#reply1  ("How to make an SPI slave")
Logged


Keeping it weird in Portland
Offline Offline
Newbie
*
Karma: 1
Posts: 44
Arduino Noob, Veteran Programmer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey Nick! Great reference! Most of what I did was actually based on this article. You are even credited in the sketches I uploaded. I appreciated but chose not to use an Interrupt Service Routine in my solution. I must have been viewing your article from a mirror site since I didn't see any of the follow up posts. Looks like I've got some more reading to do.
One thing I feel that I feel is significant in my example is controlling the MISO pin. Many of the examples I found ignored this important requirement when dealing with multiple slaves. You've inspired me to try implementing the SS as an interrupt. The biggest challenge I found was ensuring the slave had written it's data to the DR before the master begins the SPI transfer. I thought about using SS to signal each transfer and this would be much easier as an ISR.
I'm looking forward to purchasing a sensor board and SPI based sensor modules to learn more about the communication protocols used by these devices.
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 481
Posts: 18741
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The biggest challenge I found was ensuring the slave had written it's data to the DR before the master begins the SPI transfer. I thought about using SS to signal each transfer and this would be much easier as an ISR.

I don't understand that bit. The master controls the timing (it sends the clock pulses). Once an SPI.transfer call completes the data is transferred. There is no waiting.
Logged


Keeping it weird in Portland
Offline Offline
Newbie
*
Karma: 1
Posts: 44
Arduino Noob, Veteran Programmer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The biggest challenge I found was ensuring the slave had written it's data to the DR before the master begins the SPI transfer. I thought about using SS to signal each transfer and this would be much easier as an ISR.

I don't understand that bit. The master controls the timing (it sends the clock pulses). Once an SPI.transfer call completes the data is transferred. There is no waiting.

If the master expects a reply from the slave, the slave has no way of knowing when the master is going start cycling the clock. It has to write each bit to the DR before the master tries to clock it out.
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 481
Posts: 18741
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, I see what you mean. From page 171 of the datasheet:



Basically you set up (get the data ready) on the opposite edge to the sample one. An interesting question is: "when does the first bit get set up?". I presume the answer would be:

  • After SS is asserted; or
  • As soon as possible after a response is requested
Logged


Offline Offline
Newbie
*
Karma: 2
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hy Guys,

First of all, thanks for all the info, very good work!

I'm trying to do the same, but with a Master Arduino Due, and Slave Arduino Uno.

Colud be a problem with the SS pin to set the logical state if Due works in 3.3v and Uno in 5v?

Regards
Logged

Pages: [1]   Go Up
Jump to: