Go Down

Topic: SPI communication on arduino uno (Read 1 time) previous topic - next topic

Jan 10, 2013, 12:35 pm Last Edit: Jan 10, 2013, 07:58 pm by Coding Badly Reason: 1
Hi all
I am trying to communicate using SPI protocol between two arduino uno boards. I have writthen a very simple code to transfer a single character from master. The code is as following

Code: [Select]
#include <SPI.h>
#include "pins_arduino.h"

void setup (void)
{
}


void loop (void)
{

 
  digitalWrite(SS, HIGH);  // ensure SS stays high

  SPI.begin ();

  delay (5000);  // 5 seconds delay to start logic analyser.

  // enable Slave Select
  digitalWrite(SS, LOW);    // SS is pin 10
 
  // send test string
    SPI.transfer ( 'c');

// disable Slave Select
digitalWrite(SS, HIGH);

// turn SPI hardware off
SPI.end ();

while (1);  //loop
}


Please correct the code if its not right. And I really want to know that how to recieve that character on arduino uno in slave mode. There is a command SPI.transfer() to send data on SPI where as I haven't seen any command to recieve it.

Thanks and regards


Moderator edit: [code] [/code] tags added.

magagna

Nick Gammon has an excellent writeup on SPI, including master/slave data exchange, at:

http://www.gammon.com.au/forum/?id=10892

Good luck!

http://en.wiktionary.org/wiki/magagna <-- My last name.  Pretty apt.

Coding Badly


You cross-post one more time and I will ban you.

Thanks magagna...
One thing is clear to me that when using the command of SPI.transfer() both the MOSI and MISO are clocked out and in respectively. Now the question is how to grab the bit coming on MISO if my arduino is Master.

magagna

On Nick Gammon's page see the section "how to get a response from a slave". Specifically the code line

Code: [Select]
byte a = SPI.transfer (what);

On the master you call SPI.transfer() to send a byte to the slave, and the function returns the byte coming from the slave.
http://en.wiktionary.org/wiki/magagna <-- My last name.  Pretty apt.

Thanks magagna...
I have learnt how to communicate bi-directionally...
Regards

Go Up