Pages: [1]   Go Down
Author Topic: SPI communication on arduino uno  (Read 1163 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
« Last Edit: January 10, 2013, 01:58:11 pm by Coding Badly » Logged

Southern California
Offline Offline
God Member
*****
Karma: 5
Posts: 539
I like blinky lights
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Good luck!

Logged

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13085
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Southern California
Offline Offline
God Member
*****
Karma: 5
Posts: 539
I like blinky lights
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: