Go Down

Topic: SPI interface with other board_AVR_ESK1100 or among Arduino itself (Read 10 times) previous topic - next topic

ranjeetray


Quote
Master code on Arduino
...
Master code on Arduino Again
...
Other Slave codes are:


What?


Hi..!!

I mean to say that I have tried both the way on Arduino in Master , with delay and without delay also, but still does not respond properly.
I tried all the combination of master and slave but slave is not able to send back data for multiple if conditions or multiple SPDR = data.


And AVR EVK1100 master is reading FFFFFFFFFFFFF from slave Arduino board.

Nick Gammon

You are going to have to post code for a master, and a slave, and describe what is going wrong. Not lots of codes for each. And not "does not respond properly".

We are up to page 8 and your posts are as vague as a violet.

ranjeetray

#107
Nov 19, 2012, 10:56 am Last Edit: Nov 19, 2012, 11:00 am by ranjeetray Reason: 1

You are going to have to post code for a master, and a slave, and describe what is going wrong. Not lots of codes for each. And not "does not respond properly".

We are up to page 8 and your posts are as vague as a violet.

Ok. I apologize for this.


//Master sending data
Code: [Select]

#include <SPI.h>// include the SPI library:
#define SS_PIN   10

int command_array[] = {0x04, 0x06, 0x08, 0x0A};
int returned_data[4];

void setup() {
 
  pinMode (SS_PIN, OUTPUT);// set the spi_data_pin as an output:

  Serial.begin(115200);
 
  SPI.begin();
  SPI.setDataMode(SPI_MODE2);
  SPI.setClockDivider(SPI_CLOCK_DIV64) ;
  SPI.setBitOrder(LSBFIRST);

}

void loop() {
   
    digitalWrite(SS_PIN,LOW);
    SPI.transfer(command_array[0]);       // send command
    //delay(1);                             // give the slave some time
    returned_data[0] = SPI.transfer(0);   // get response
    digitalWrite(SS_PIN,HIGH);

    digitalWrite(SS_PIN,LOW);
    //delay(1);
    SPI.transfer(command_array[1]);
    returned_data[1] = SPI.transfer(0);
    digitalWrite(SS_PIN,HIGH);

    digitalWrite(SS_PIN,LOW);
    //delay(1);
    SPI.transfer(command_array[2]);
    returned_data[2] = SPI.transfer(0);
    digitalWrite(SS_PIN,HIGH);

    digitalWrite(SS_PIN,LOW);
    //delay(1);
    SPI.transfer(command_array[3]);
    returned_data[3] = SPI.transfer(0);
    digitalWrite(SS_PIN,HIGH);

    Serial.print (returned_data[0]);
    Serial.print (',');
    Serial.print (returned_data[1]);
    Serial.print (',');
    Serial.print (returned_data[2]);
    Serial.print (',');
    Serial.println (returned_data[3]);
 
}


Slave code for sending back data:
Code: [Select]

//Slave Code
#include "pins_arduino.h"
#include <SPI.h>
#define SS 10

byte c;
int m, n ;
void setup (void)
{
  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode (SPI_MODE2);
  SPI.setClockDivider(SPI_CLOCK_DIV64) ;

  // turn on SPI in slave mode
  SPCR |= _BV(SPE);

  // turn on interrupts
  SPCR |= _BV(SPIE);
 
// disable timer interrupts
  TIMSK0 = 0;
 
}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
  c = SPDR;
  if(c == 0x04)
{
  SPDR = 0x01;
}
  else if(c == 0x06)
{

  SPDR = 0x02;
}
  else if(c == 0x08)
{

  SPDR = 0x03;
}
   else if(c == 0x0A)
{

  SPDR = 0x07;
}

else
SPDR = 0;    // what to return to the master

}  // end of interrupt service routine (ISR) SPI_STC_vect

void loop (void)
{

}  // end of loop


This combination of Master and Slave code I have tried but Slave is not able to send back data to Master, I have observed in Oscilloscope MISO is not having data. What could be the reason.

Go Up