Pages: [1]   Go Down
Author Topic: Communication the 2 arduinos to Spi  (Read 817 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying  communicate between two Arduino to Spi .. I can not send to more than 8 bits at a time I have to make a delay. then I have an arduino the Master and one Slave when the Slave responds Master can not receive anything.
 I want somebody who could ever help..

code Master
#include <avr/io.h>
#include <util/delay.h>   // delay eg _delay_ms(6);
#include <stdio.h>

void setup()
{
   Serial.begin(9600);
   /* SETTING CLOCK PRESCALER TO 1 */
   CLKPR = 0x80;    // Enable change to prescaler
   CLKPR = 0x00;    // Set clock prescaler to 1

   /* SETTING UP PORT DIRECTIONS */
   DDRB = 0b11101111;   // All output except MISO (PB4)

   /* SETTING UP SPI */
   SPCR = 0x50;   // Enable SPI in MSB-first mode, disable SPI interrupt and set SPI clock prescaler to 4
//   SPSR |= 0x01;   // Enable double speed (f_spi-clk = f_clk/2)
   SPSR &= 0xFE;   // Disable double speed
}
   

void SPI_MasterTransmit(unsigned char data_byte)
{
   SPDR = data_byte;
   PORTB &= 0xFB;   // Pull SS low to enable the slave

   while(!(SPSR & (1<<SPIF)));
   PORTB |= 0x04;   // Pull SS high to disable the slave
}


void loop()
{
  int data;
   //setup();   // Set registers for interrupts to run etc
   delay(5000);
   
   SPI_MasterTransmit('l');
    delay(10);
   SPI_MasterTransmit('c');
   SPI_MasterTransmit('d');
   
   
 
 
   
   data= SPI_SlaveReceive();
  Serial.println(data);
   
}
unsigned char SPI_SlaveReceive()
{
  while(!(SPSR & (1<<SPIF)));
  return SPDR;
}

 code Slave
#include <stdio.h>
#include <avr/io.h>
#include <SPI.h>
#define MOSI 11
#define MISO  12
#define SCK  13
#define SS 10

int array [3];
int array2[] = {108,99,100};
unsigned char data = 2;
unsigned char scrap;
int counter = 0;

void setup()
{

  int i=0;
  pinMode(MOSI, INPUT);
  pinMode(MISO, OUTPUT);
  pinMode(SCK,INPUT);
  pinMode(SS,INPUT);
    SPI.begin();
  Serial.begin(9600);
  Serial.println("Enabling SPI in slave mode");
  SPCR = 0x40;  // Enable SPI in slave mode
  delay(500);
  Serial.println("Initialisation complete.");

  for(i=0;i<3;i++){ smiley-roll-sweat smiley-roll-sweat
  array= SPI_SlaveReceive();
   Serial.println(array);
 
  //Serial.println(data);
  }
}
 int i;
void loop()
{
 
int  da;
  /*i++;
  data=0;
  Serial.print("loop");
 
 
 
  Serial.println(i);
  data = SPI_SlaveReceive();
  Serial.println(data);
 
 for(i=0;i<11;i++){
 da=array;
 Serial.println(data,DEC);
 delay(1000);
 }
  */
  //delay(1000);

 if (array_cmp(array, array2, 3, 3) == true){
   // do this if they are equal
   da=5;
   
   digitalWrite(SS,LOW);
   SPI.transfer(da);
   digitalWrite(SS,HIGH);
      Serial.println("Entrou7777777");
       Serial.println(da,HEX);
   }else{
      // do this if they are different
   }
 
}

unsigned char SPI_SlaveReceive()
{
  while(!(SPSR & (1<<SPIF)));
  return SPDR;
}


void SPI_SlaveTransmit(unsigned char data_byte)
{
   SPDR = data_byte;
   PORTB &= 0xFB;   // Pull SS low to enable the slave

   while(!(SPSR & (1<<SPIF)));
   PORTB |= 0x04;   // Pull SS high to disable the slave
}

boolean array_cmp(int *a, int *b, int len_a, int len_b){
   int n;
          Serial.println("Entrou");
   // if their lengths are different, return false
   if (len_a != len_b) return false;

   // test each element to be the same. if not, return false
   for (n=0;n<len_a;n++) if (a[n]!=b[n]) return false;

   //ok, if we have not returned yet, they are equal smiley
   return true;
}
 

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8066
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SPI does simultaneous send and receive.  As 8 bits go out, 8 bits come in and are found in SPDR.

For many devices you would send TWO bytes from the Master to the Slave.  The first byte tells the Slave what to do and the second byte clocks in the reply.  The Slave Select line is kept low for both bytes.

Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

I'm not able to send more than one byte without a delay ... I am the first byte to be transmitted to the second I have to do a delay, so if I want to send 24 bits can not follow

thank you
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

There are no (relevant) delays there in the example code.
Logged

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

thanks I'll try to see this site.....
Logged

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

I'm trying to have communication between two arduino's to understand SPI protocol. The objective is then communicate with an energy metter.

You have some time worked with CS5460A? Because i need to communicate whit them and i dont find libraries about SPI communication with CS5460A.


If you can help me i will be greatfull.

Thank You again
Logged

Pages: [1]   Go Up
Jump to: