Pages: 1 2 3 [4] 5 6 ... 8   Go Down
Author Topic: SPI interface with other board_AVR_ESK1100 or among Arduino itself  (Read 9004 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I found that once SPI_SlaveReceive(); function is initialized it is not able to generate the clock ...

I don't understand what you are saying here. The master generates the clock, not the slave.

Respected Sir

Actually once it receive a data in slave mode then SPI clock becomes dead from this board, I want that my both the board should work as master and both should receive the data and send the data. Can we do like this. Is multimaster concept is same as what I am trying to do. Please guide me Sir.


Thanks & Regards....  :-)


Respected Sir

It receives the data in slave mode after receiving the data I want to compare the data and want to send another data on SPI bus in master mode. Is it possible. Please guide me on this.



Code:
#include <SPI.h>


#define SCK_PIN   13
#define MISO_PIN  12
#define MOSI_PIN  11
#define SS_PIN    10

unsigned char data;
int array[10] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }, ii;

void SlaveInit(void) {
  // Set MISO output, all others input
  pinMode(SCK_PIN, INPUT);
  pinMode(MOSI_PIN, INPUT);
  pinMode(MISO_PIN, OUTPUT);
  pinMode(SS_PIN, INPUT);

  // Enable SPI
  SPCR = B00101100;
  SPCR = (1<<SPE);
}

void setup(){ Serial.begin(9600);
            SPI.begin();
          }

void loop()
{
  unsigned char hg = 0;
  Serial.println("Data Received from Master Board");
  SlaveInit();
  data = SPI_SlaveReceive();
   Serial.println(data, HEX);
   hg = data;
   data = 0;
   delay(1000);
  
   if(hg == 0x3B)
   {
     Serial.println("Writting Data on SPI bus");
     pinMode(SS_PIN, OUTPUT);
     digitalWrite(SS_PIN, LOW);
     delay(1000);
     for(ii=0;ii<10;ii++){
     SPI.transfer(array[ii]);
     Serial.println(array[ii], BIN);
       delay(1000);      
     }
     digitalWrite(SS_PIN, HIGH);
     delay(1000);
   Serial.println("Sending Done");
    delay(1000);  
   }

}

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



You can see in the image of Oscilloscope while transmitting data from slave after comparing 0x3b , it is sending even 0x3B also with each data. Why it is happening. Can we clear the buffer before transmission of data.


Thanks & Regards....  :-)


* TEK0000.BMP (76.05 KB, 320x240 - viewed 15 times.)
« Last Edit: October 04, 2012, 10:24:02 pm by ranjeetray » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I found that once SPI_SlaveReceive(); function is initialized it is not able to generate the clock ...

I don't understand what you are saying here. The master generates the clock, not the slave.

Respected Sir

Actually once it receive a data in slave mode then SPI clock becomes dead from this board, I want that my both the board should work as master and both should receive the data and send the data. Can we do like this. Is multimaster concept is same as what I am trying to do. Please guide me Sir.


Thanks & Regards....  :-)


Respected Sir

It receives the data in slave mode after receiving the data I want to compare the data and want to send another data on SPI bus in master mode. Is it possible. Please guide me on this.



Code:
#include <SPI.h>


#define SCK_PIN   13
#define MISO_PIN  12
#define MOSI_PIN  11
#define SS_PIN    10

unsigned char data;
int array[10] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }, ii;

void SlaveInit(void) {
  // Set MISO output, all others input
  pinMode(SCK_PIN, INPUT);
  pinMode(MOSI_PIN, INPUT);
  pinMode(MISO_PIN, OUTPUT);
  pinMode(SS_PIN, INPUT);

  // Enable SPI
  SPCR = B00101100;
  SPCR = (1<<SPE);
}

void setup(){ Serial.begin(9600);
            SPI.begin();
          }

void loop()
{
  unsigned char hg = 0;
  Serial.println("Data Received from Master Board");
  SlaveInit();
  data = SPI_SlaveReceive();
   Serial.println(data, HEX);
   hg = data;
   data = 0;
   delay(1000);
  
   if(hg == 0x3B)
   {
     Serial.println("Writting Data on SPI bus");
     pinMode(SS_PIN, OUTPUT);
     digitalWrite(SS_PIN, LOW);
     delay(1000);
     for(ii=0;ii<10;ii++){
     SPI.transfer(array[ii]);
     Serial.println(array[ii], BIN);
       delay(1000);      
     }
     digitalWrite(SS_PIN, HIGH);
     delay(1000);
   Serial.println("Sending Done");
    delay(1000);  
   }

}

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



You can see in the image of Oscilloscope while transmitting data from slave after comparing 0x3b , it is sending even 0x3B also with each data. Why it is happening. Can we clear the buffer before transmission of data.


Thanks & Regards....  :-)


Hi...!!!


Respected Sir,

Can we flush the buffer like SPDR, SPSR registers. How can we do this.

Thanks & Regards...  :-)
Logged

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

I don't understand what you are doing here:

Code:
  SlaveInit();
  data = SPI_SlaveReceive();
  Serial.println(data, HEX);
  hg = data;
  data = 0;
  delay(1000);

  if(hg == 0x3B)
  {
    Serial.println("Writting Data on SPI bus");
    pinMode(SS_PIN, OUTPUT);
    digitalWrite(SS_PIN, LOW);
    delay(1000);
    for(ii=0;ii<10;ii++){
      SPI.transfer(array[ii]);
      Serial.println(array[ii], BIN);
      delay(1000);     
    }
    digitalWrite(SS_PIN, HIGH);
    delay(1000);
    Serial.println("Sending Done");
    delay(1000); 
  }

You are receiving a byte from a SPI master (in slave mode) and then sending it back? Why bother? Plus, you are not changing to master mode. So you won't be sending anything back.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't understand what you are doing here:

Code:
  SlaveInit();
  data = SPI_SlaveReceive();
  Serial.println(data, HEX);
  hg = data;
  data = 0;
  delay(1000);

  if(hg == 0x3B)
  {
    Serial.println("Writting Data on SPI bus");
    pinMode(SS_PIN, OUTPUT);
    digitalWrite(SS_PIN, LOW);
    delay(1000);
    for(ii=0;ii<10;ii++){
      SPI.transfer(array[ii]);
      Serial.println(array[ii], BIN);
      delay(1000);     
    }
    digitalWrite(SS_PIN, HIGH);
    delay(1000);
    Serial.println("Sending Done");
    delay(1000); 
  }

You are receiving a byte from a SPI master (in slave mode) and then sending it back? Why bother? Plus, you are not changing to master mode. So you won't be sending anything back.

Respected Sir

Thanks a lot for your kind reply. I am reading data on SPI bus and trying to send back data on the same bus to other board from which I have received a byte of data. But I am not able to change the mode, please suggest me how to change mode from slave to master.
And I am thinking that once it comes out the slave mode loop it should act as a master by default. Can it happen like this.


Thanks & Regards...  :-)
Logged

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

What other board? How is everything wired up?

Quote
And I am thinking that once it comes out the slave mode loop it should act as a master by default. Can it happen like this.

By default? I don't think so.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What other board? How is everything wired up?

Quote
And I am thinking that once it comes out the slave mode loop it should act as a master by default. Can it happen like this.

By default? I don't think so.

Respected Sir

Please suggest how to do it and can we clear the buffer also.

Please guide me Sir.

Because received one byte data is going back on to SPI bus again when I send back data from slave.

Thanks & Regards...  :-)
Logged

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

You've chosen to not answer my question:

Quote
What other board? How is everything wired up?

Why?
Logged

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

Quote
Because received one byte data is going back on to SPI bus again when I send back data from slave.

I don't understand that.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Because received one byte data is going back on to SPI bus again when I send back data from slave.

I don't understand that.
When I send data from slave to master, data is not available on SPI bus.

* SPI_document.docx (68.86 KB - downloaded 12 times.)
« Last Edit: October 09, 2012, 04:46:43 am by ranjeetray » Logged

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

For the benefit of people who don't have MS Word, your connection diagram is:



Can you explain why you want to send data from one to the other, and then back again? Why not leave it where it started in the first place?

And assuming you have a good reason, on my page here:

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

I describe how to send data between two Arduinos using SPI. Scroll down to "How to get a response from a slave".

I don't understand this sort of talk, to be honest:

Quote
Because received one byte data is going back on to SPI bus again when I send back data from slave.
Logged

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

What has your diagram above got to do with "SPI interface with other board_AVR_ESK1100"?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What has your diagram above got to do with "SPI interface with other board_AVR_ESK1100"?
Respected sir

Thanks a lot for your kind reply.
Actually we are going test IMU(Inertial Measurement Unit) of a satellite, so before testing the actual IMU, I want to create a dummy IMU which will be Arduino board or EVK1100(which will act as Master and will send command for data) and other slave board will be Arduino board which will receive command from IMU master board(Arduino or EVK1100) and then send back required data on SPI bus.

I am able to receive command from Master(Arduino board) as 0x3E and when I send back data from Slave(Arduino board) this 0x3E is going with each data, I mean on oscilloscope it appears data(0x12 for example) and 0x3E together, So I want to remove this 0x3E while sending back data from Slave.
Is it possible by clearing any buffer.
Please guide me Sir.

Thanks & Regards... :-)


* 1.bmp (2304.05 KB, 1024x768 - viewed 19 times.)
Logged

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

Please don't send a 2 Mb image of the data. Just copy and paste the text into a message, inside code blocks.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8471
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I suspect that your 0x3E is still in the SPDR register although it should be overwritten by SPI.transfer(array[ii]) I would think.

Quote
So I want to remove this 0x3E while sending back data from Slave.Is it possible by clearing any buffer.
If 0x12 is written into the SPDR register that's the value that will go out, I don't know why SPI.transfer() would not do that.

Have I got this right...

Arduino sends 0x3E to IMU.
Line turns around.
IMU sends data to Arduino.

Does this IMU really expect the SPI line to be turned around? Have you got a data sheet?

Quote
so before testing the actual IMU, I want to create a dummy IMU which will be Arduino board or EVK1100(which will act as Master and will send command for data) and other slave board will be Arduino board which will receive command from IMU master board(Arduino or EVK1100)
BTW, All this talk of IMU/Arduino/EVK1100 is really confusing the issue IMO, can we just say Arduino and IMU and assume that they are both SPI-capable devices.

______
Rob


Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

Every transfer sends one byte and receives one. You don't "clear the buffer". Sending something, and getting a response to what was just sent, involves at least two transfers. You can't get a response at the same time you are sending because each bit is transferred at the same time.

Perhaps read this:

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

Quote
I want to create a dummy IMU which will be Arduino board or EVK1100(which will act as Master and will send command for data) and other slave board will be Arduino board which will receive command from IMU master board(Arduino or EVK1100) and then send back required data on SPI bus.

I really don't understand what you are doing. Normally SPI is two-way. You don't flip backwards and forwards from slave to master just to get something back. Please read the link above carefully. Then ask specific questions.
Logged

Pages: 1 2 3 [4] 5 6 ... 8   Go Up
Jump to: