Pages: [1] 2   Go Down
Author Topic: SPI Between 'Arduinos'  (Read 3238 times)
0 Members and 1 Guest are viewing this topic.
NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

Never used SPI before between Arduino's before, have only used I2C.

Basically I have a atMega2560 talking over SPI to 3x atTiny84's.
Ignore the type of micros, I just am trying to get my head around the code to do what I want.

Basically, I want the 2560 to be the master, and to request data from each of the atTinys.
Each atTiny has a seperate Chip Select going to it from the Master.

Looking at the SPI library, http://arduino.cc/en/Reference/SPI , there doesnt seem to be much in there....
So basically after I set up who is master and who is slave, set the speed etc, is it just a case of doing the transfer() ?

I am searching more, but thought I would post incase someone knows how to do this. I am just wanting to set up a quick test program to prove the point. atTiny does stuff and then when requested from the 2560, it will give up a value. Job done.

Any help would be very handy.

Thanks
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Example code here:

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

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Nick

Code doesnt compile in the atTiny84 (I found another post of yours I stole some test code from).

I put the core for the atTiny84 that I found on the forum from CodingBadly, however I am not sure if SPI is supported yet or not.

Damn.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The AtTiny25/45/85 seems to support SPI. Not sure about the 84.
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I get these errors

In file included from Brick_Tiny_Functions.cpp:1:
D:\Projects\Arduino-0022\libraries\SPI/SPI.h: In static member function 'static byte SPIClass::transfer(byte)':
D:\Projects\Arduino-0022\libraries\SPI/SPI.h:56: error: 'SPDR' was not declared in this scope
D:\Projects\Arduino-0022\libraries\SPI/SPI.h:57: error: 'SPSR' was not declared in this scope
D:\Projects\Arduino-0022\libraries\SPI/SPI.h:57: error: 'SPIF' was not declared in this scope
D:\Projects\Arduino-0022\libraries\SPI/SPI.h: In static member function 'static void SPIClass::attachInterrupt()':
D:\Projects\Arduino-0022\libraries\SPI/SPI.h:63: error: 'SPCR' was not declared in this scope
D:\Projects\Arduino-0022\libraries\SPI/SPI.h:63: error: 'SPIE' was not declared in this scope
D:\Projects\Arduino-0022\libraries\SPI/SPI.h: In static member function 'static void SPIClass::detachInterrupt()':
D:\Projects\Arduino-0022\libraries\SPI/SPI.h:67: error: 'SPCR' was not declared in this scope
D:\Projects\Arduino-0022\libraries\SPI/SPI.h:67: error: 'SPIE' was not declared in this scope
Brick_Tiny_Functions.cpp: In function 'void setup()':
Brick_Tiny_Functions:8: error: 'SPCR' was not declared in this scope
Brick_Tiny_Functions:8: error: 'SPE' was not declared in this scope
Brick_Tiny_Functions.cpp: In function 'void loop()':
Brick_Tiny_Functions:16: error: 'SPDR' was not declared in this scope
Brick_Tiny_Functions.cpp: In function 'byte slave()':
Brick_Tiny_Functions:22: error: 'SPSR' was not declared in this scope
Brick_Tiny_Functions:22: error: 'SPIF' was not declared in this scope
Brick_Tiny_Functions:23: error: 'SPDR' was not declared in this scope
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm. Looks like you might have to do a bit more work. I don't have an AtTiny84 to test on. Try reading this:

http://atmel.com/dyn/resources/prod_documents/doc2582.pdf
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Success smiley

Just some test code, here is the slave.

Code:
#include "pins_arduino.h"

byte Command;

void setup()
{
  pinMode(MISO, OUTPUT);
  USICR = (1 << USIWM0) | (1 << USICS1);
}

void loop()
{
  Command = slave();  //receive data from Master
  if(Command == 10)
  {
    USIDR = 5;  //Generic reply of 5
    USISR = (1 << USIOIF);
  }
  else
  {  
    USIDR = 7;  //Generic reply of 7
    USISR = (1 << USIOIF);
  }
}

byte slave()
{
  while (!(USISR & (1 << USIOIF)));
  return USIDR;
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool!
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ha - yeah.
Now I just need to fully understand why it is working and how to make it better!

These are going to be doing high speed counting so I need to make sure that what I have there isnt blocking, which I am thinking it might be with that while loop...
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes it looks like it would block. Now you need to get an interrupt out of it. smiley-wink
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your example you posted above in the link does interrupts does it... I havent looked again.

hmm
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It mentions interrupts. I don't see any code there.
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Care of GrayNomad - Thanks Rob!

Code:
#include "pins_arduino.h"

byte Command;

void setup()
{
  pinMode(MISO, OUTPUT);
   USICR = (1 << USIWM0) | (1 << USICS1) | (1 << USIOIE);
}

ISR (USI_OVF_vect)

 
  Command = USIDR;

  if(Command == 10)
  {
    USIDR = 5; //Generic reply of 5
    USISR = (1 << USIOIF);
  }
  else
  { 
    USIDR = 7; //Generic reply of 7
    USISR = (1 << USIOIF);
  } 
}
 
void loop()
{
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Better would be:

Code:
ISR (USI_OVF_vect)
{  
 
  Command = USIDR;

  if(Command == 10)
  {
    USIDR = 5; //Generic reply of 5
  }
  else
  {  
    USIDR = 7; //Generic reply of 7
  }  

  USISR = 1 << USIOIF;

}

Otherwise you don't clear the interrupt bit if you don't match the "if". (Although in this case you would, of course).

Also make "byte Command;" volatile, otherwise move it into the ISR.
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good thinking, thanks!
Logged

Pages: [1] 2   Go Up
Jump to: