Pages: [1] 2 3 ... 8   Go Down
Author Topic: SPI interface with other board_AVR_ESK1100 or among Arduino itself  (Read 9830 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


Hi...!!!

I want to read a data from another board using SPI(MOSI,MISO,SCK and SS) wires.
First I want to read some data as an acknowledgement then I want to send some data.Sending data part code is working fine but reading is happening. Please help me.


Sending data Code:-
Code:
#include <SPI.h>// include the SPI library:

const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot:
int mm =0;

void setup() {
  
  pinMode (spidata, OUTPUT);// set the spi_data_pin as an output:
  
  SPI.begin();// initialize SPI:
  Serial.begin(4800);
}

void loop() {
     int arry[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
      digitalWrite(spidata,LOW);
      Serial.println("If condition-1 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[0], BIN);
      SPI.transfer(arry[0]); //  send in the address and value via SPI:
      delay(1500);
      Serial.println("If condition-2 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[1], BIN);
      SPI.transfer(arry[1]);
     delay(1500);
     Serial.println("If condition-3 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[2],BIN);
      SPI.transfer(arry[2]);
      delay(1500);
      Serial.println("If condition-4 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[3], BIN);
      SPI.transfer(arry[3]);
     delay(1500);
     Serial.println("If condition-5 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[4], BIN);
      SPI.transfer(arry[4]);
      delay(1500);
      Serial.println("If condition-6 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[5], BIN);
      SPI.transfer(arry[5]);
      delay(1500);
      Serial.println("If condition-7 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[6], BIN);
      SPI.transfer(arry[6]);
      delay(1500);
      Serial.println("If condition-8 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[7], BIN);
      SPI.transfer(arry[7]);
     Serial.println("If condition-9 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[8], BIN);
      SPI.transfer(arry[8]);
      delay(1500);
      Serial.println("If condition-10 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[9], BIN);
      SPI.transfer(arry[9]);
     delay(1500);
     Serial.println("If condition-11 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[10], BIN);
      SPI.transfer(arry[10]);
     delay(1500);
     Serial.println("If condition-12 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[11], BIN);
      SPI.transfer(arry[11]);
      delay(1500);
      Serial.println("If condition-13 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[12], BIN);
      SPI.transfer(arry[12]);
      delay(1500);
      Serial.println("If condition-14 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[13], BIN);
      SPI.transfer(arry[13]);
      delay(1500);
      Serial.println("If condition-15 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[14], BIN);
      SPI.transfer(arry[14]);
      delay(1500);
    
      Serial.println("ALL THE CONDITIONS are over now::");
      Serial.println("\n");
      Serial.println("\n");
      digitalWrite(spidata,HIGH);// take the SS pin high to de-select the chip:
      delay(1500);
      
  

  
    }





Recieving data code:-
Code:
// Slave

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

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 = B00000000;
  SPCR = (1<<SPE);
}

byte ReadByte(void) {
  while(!(SPSR & (1<<SPIF))) ;
  return SPDR;
}

void WriteByte(byte value) {
  SPDR = value;
  while (!(SPSR & (1<<SPIF))) ;
  return;
}

unsigned long lastSent;

void setup() {
  Serial.begin(57600);
  digitalWrite(5, HIGH);
  SlaveInit();
  lastSent = millis();
  pinMode(5, OUTPUT);
}

void loop() {
  if (digitalRead(10) == LOW) {
    Serial.println("Pin 10 low...");
    byte rxData;
    rxData = ReadByte();
    Serial.print("Command: ");
    Serial.println(rxData, DEC);
    if (rxData == 17) {
      Serial.println("Sending data to master...");
      WriteByte(19);
      Serial.println("Done Sending data...");
    }
  }
  if (millis() > lastSent + 2000) {
    Serial.println("Pin 5 low...");
    digitalWrite(5, LOW);
    delay(10);
    digitalWrite(5, HIGH);
    lastSent = millis();
  }
}
 

« Last Edit: August 28, 2012, 04:54:33 am by ranjeetray » Logged

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


Hi...!!!

I want to read a data from another board using SPI(MOSI,MISO,SCK and SS) wires.
First I want to read some data as an acknowledgement then I want to send some data.Sending data part code is working fine but reading is happening. Please help me.


Sending data Code:-
Code:
#include <SPI.h>// include the SPI library:

const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot:
int mm =0;

void setup() {
  
  pinMode (spidata, OUTPUT);// set the spi_data_pin as an output:
  
  SPI.begin();// initialize SPI:
  Serial.begin(4800);
}

void loop() {
     int arry[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
      digitalWrite(spidata,LOW);
      Serial.println("If condition-1 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[0], BIN);
      SPI.transfer(arry[0]); //  send in the address and value via SPI:
      delay(1500);
      Serial.println("If condition-2 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[1], BIN);
      SPI.transfer(arry[1]);
     delay(1500);
     Serial.println("If condition-3 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[2],BIN);
      SPI.transfer(arry[2]);
      delay(1500);
      Serial.println("If condition-4 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[3], BIN);
      SPI.transfer(arry[3]);
     delay(1500);
     Serial.println("If condition-5 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[4], BIN);
      SPI.transfer(arry[4]);
      delay(1500);
      Serial.println("If condition-6 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[5], BIN);
      SPI.transfer(arry[5]);
      delay(1500);
      Serial.println("If condition-7 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[6], BIN);
      SPI.transfer(arry[6]);
      delay(1500);
      Serial.println("If condition-8 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[7], BIN);
      SPI.transfer(arry[7]);
     Serial.println("If condition-9 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[8], BIN);
      SPI.transfer(arry[8]);
      delay(1500);
      Serial.println("If condition-10 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[9], BIN);
      SPI.transfer(arry[9]);
     delay(1500);
     Serial.println("If condition-11 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[10], BIN);
      SPI.transfer(arry[10]);
     delay(1500);
     Serial.println("If condition-12 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[11], BIN);
      SPI.transfer(arry[11]);
      delay(1500);
      Serial.println("If condition-13 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[12], BIN);
      SPI.transfer(arry[12]);
      delay(1500);
      Serial.println("If condition-14 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[13], BIN);
      SPI.transfer(arry[13]);
      delay(1500);
      Serial.println("If condition-15 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[14], BIN);
      SPI.transfer(arry[14]);
      delay(1500);
    
      Serial.println("ALL THE CONDITIONS are over now::");
      Serial.println("\n");
      Serial.println("\n");
      digitalWrite(spidata,HIGH);// take the SS pin high to de-select the chip:
      delay(1500);
      
  

  
    }





Recieving data code:-
Code:
// Slave

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

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 = B00000000;
  SPCR = (1<<SPE);
}

byte ReadByte(void) {
  while(!(SPSR & (1<<SPIF))) ;
  return SPDR;
}

void WriteByte(byte value) {
  SPDR = value;
  while (!(SPSR & (1<<SPIF))) ;
  return;
}

unsigned long lastSent;

void setup() {
  Serial.begin(57600);
  digitalWrite(5, HIGH);
  SlaveInit();
  lastSent = millis();
  pinMode(5, OUTPUT);
}

void loop() {
  if (digitalRead(10) == LOW) {
    Serial.println("Pin 10 low...");
    byte rxData;
    rxData = ReadByte();
    Serial.print("Command: ");
    Serial.println(rxData, DEC);
    if (rxData == 17) {
      Serial.println("Sending data to master...");
      WriteByte(19);
      Serial.println("Done Sending data...");
    }
  }
  if (millis() > lastSent + 2000) {
    Serial.println("Pin 5 low...");
    digitalWrite(5, LOW);
    delay(10);
    digitalWrite(5, HIGH);
    lastSent = millis();
  }
}
 



Dear Sir @Nick Gammon

Please help me out.
Logged

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


Hi...!!!

I want to read a data from another board using SPI(MOSI,MISO,SCK and SS) wires.
First I want to read some data as an acknowledgement then I want to send some data.Sending data part code is working fine but reading is happening. Please help me.


Sending data Code:-
Code:
#include <SPI.h>// include the SPI library:

const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot:
int mm =0;

void setup() {
  
  pinMode (spidata, OUTPUT);// set the spi_data_pin as an output:
  
  SPI.begin();// initialize SPI:
  Serial.begin(4800);
}

void loop() {
     int arry[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
      digitalWrite(spidata,LOW);
      Serial.println("If condition-1 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[0], BIN);
      SPI.transfer(arry[0]); //  send in the address and value via SPI:
      delay(1500);
      Serial.println("If condition-2 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[1], BIN);
      SPI.transfer(arry[1]);
     delay(1500);
     Serial.println("If condition-3 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[2],BIN);
      SPI.transfer(arry[2]);
      delay(1500);
      Serial.println("If condition-4 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[3], BIN);
      SPI.transfer(arry[3]);
     delay(1500);
     Serial.println("If condition-5 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[4], BIN);
      SPI.transfer(arry[4]);
      delay(1500);
      Serial.println("If condition-6 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[5], BIN);
      SPI.transfer(arry[5]);
      delay(1500);
      Serial.println("If condition-7 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[6], BIN);
      SPI.transfer(arry[6]);
      delay(1500);
      Serial.println("If condition-8 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[7], BIN);
      SPI.transfer(arry[7]);
     Serial.println("If condition-9 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[8], BIN);
      SPI.transfer(arry[8]);
      delay(1500);
      Serial.println("If condition-10 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[9], BIN);
      SPI.transfer(arry[9]);
     delay(1500);
     Serial.println("If condition-11 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[10], BIN);
      SPI.transfer(arry[10]);
     delay(1500);
     Serial.println("If condition-12 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[11], BIN);
      SPI.transfer(arry[11]);
      delay(1500);
      Serial.println("If condition-13 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[12], BIN);
      SPI.transfer(arry[12]);
      delay(1500);
      Serial.println("If condition-14 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[13], BIN);
      SPI.transfer(arry[13]);
      delay(1500);
      Serial.println("If condition-15 Matches then:");
      Serial.println("I am writting on SPI");
      Serial.println(arry[14], BIN);
      SPI.transfer(arry[14]);
      delay(1500);
    
      Serial.println("ALL THE CONDITIONS are over now::");
      Serial.println("\n");
      Serial.println("\n");
      digitalWrite(spidata,HIGH);// take the SS pin high to de-select the chip:
      delay(1500);
      
  

  
    }





Recieving data code:-
Code:
// Slave

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

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 = B00000000;
  SPCR = (1<<SPE);
}

byte ReadByte(void) {
  while(!(SPSR & (1<<SPIF))) ;
  return SPDR;
}

void WriteByte(byte value) {
  SPDR = value;
  while (!(SPSR & (1<<SPIF))) ;
  return;
}

unsigned long lastSent;

void setup() {
  Serial.begin(57600);
  digitalWrite(5, HIGH);
  SlaveInit();
  lastSent = millis();
  pinMode(5, OUTPUT);
}

void loop() {
  if (digitalRead(10) == LOW) {
    Serial.println("Pin 10 low...");
    byte rxData;
    rxData = ReadByte();
    Serial.print("Command: ");
    Serial.println(rxData, DEC);
    if (rxData == 17) {
      Serial.println("Sending data to master...");
      WriteByte(19);
      Serial.println("Done Sending data...");
    }
  }
  if (millis() > lastSent + 2000) {
    Serial.println("Pin 5 low...");
    digitalWrite(5, LOW);
    delay(10);
    digitalWrite(5, HIGH);
    lastSent = millis();
  }
}
 



Dear Sir @Nick Gammon

Please help me out.

Dear Sir

Even this code is working fine .

Code:
#include <SPI.h>// include the SPI library:

const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot:
int mm =0;

void setup() {
 
  pinMode (spidata, OUTPUT);// set the spi_data_pin as an output:
 
  SPI.begin();// initialize SPI:
  Serial.begin(4800);
}

void loop() {
      for(mm=0; mm<256;mm++){
      digitalWrite(spidata,LOW);
      Serial.println("I am writting on SPI");
      Serial.println(mm, BIN);
      SPI.transfer(mm); //  send in the address and value via SPI:
      digitalWrite(spidata,HIGH);// take the SS pin high to de-select the chip:
      delay(1500);}
     
   

 
    }


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Even this code is working fine .
Then, what IS your problem?
Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12169
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think your WriteByte routine is failing to complete the process - it reads SPSR but doesn't read SPDR once there's a byte so the hardware never clears ready for the next byte?  To quote the datasheet:
Quote
the SPIF bit is cleared by first reading the SPI Status Register with SPIF set, then accessing the SPI Data Register (SPDR)

  How about rewriting WriteByte thus:
Code:

byte WriteByte (byte value) {
  SPDR = value ;
  while (!(SPSR & (1<<SPIF)))
  {}
  return SPDR ;
}
(So easy to miss a semicolon after a while, use a blank block instead, harder to misread)
Logged

[ I won't respond to messages, use the forum please ]

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

Quote
Even this code is working fine .
Then, what IS your problem?

Sir.

I am able to send/write data on the SPI but I am not able to read the data, Please help me out.
I tried following code also

Code:

#include <SPI.h>// include the SPI library:

const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot:
int mm =0;
//int ReadByte();

void setup() {
  
  pinMode (spidata, OUTPUT);// set the spi_data_pin as an output:
  SPI.setDataMode(SPI_MODE3);
  SPI.setClockDivider(SPI_CLOCK_DIV8);
  SPCR = B00000000;
  SPCR = (1<<SPE);
  
  SPI.begin();// initialize SPI:
  Serial.begin(4800);
  
}

byte ReadByte(void) {
  while(!(SPSR & (1<<SPIF)))
  return SPDR;
}


// int ReadByte(int value) {
//  SPDR = value;
//  while((!SPSR & (1<<SPIF)))
//  return 0;
//}
//byte WriteByte (byte value) {
//  SPDR = value ;
//  while (!(SPSR & (1<<SPIF)))
//  {}
//  return SPDR ;
//}

void loop() {
      int arry[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
      int rxData;
      digitalWrite(spidata,LOW);
      rxData = ReadByte();   // Sir, this part is not working, please help me.
      Serial.println("Read value ");
       Serial.println(rxData, DEC);
       Serial.println("Newline");
    //if (rxData == 59) {
      Serial.println("If condition-1 Matches and recieves 0x3B from SPI then:");
      Serial.println("I am Sending 1st data of 15 data on SPI");
      
      
      
      Serial.println(arry[0], BIN);
      SPI.transfer(arry[0]); //  send in the address and value via SPI:
      delay(1500);
 
      Serial.println("I am Sending 2nd data of 15 data on SPI");
      Serial.println(arry[1], BIN);
      SPI.transfer(arry[1]);
      delay(1500);

      Serial.println("I am Sending 3rd data of 15 data on SPI");
      Serial.println(arry[2],BIN);
      SPI.transfer(arry[2]);
      delay(1500);
    
      Serial.println("I am Sending 4th data of 15 data on SPI");
      Serial.println(arry[3], BIN);
      SPI.transfer(arry[3]);
      delay(1500);
    
      Serial.println("I am Sending 5th data of 15 data on SPI");
      Serial.println(arry[4], BIN);
      SPI.transfer(arry[4]);
      delay(1500);

      Serial.println("I am Sending 6th data of 15 data on SPI");
      Serial.println(arry[5], BIN);
      SPI.transfer(arry[5]);
      delay(1500);
 
      Serial.println("I am Sending 7th data of 15 data on SPI");
      Serial.println(arry[6], BIN);
      SPI.transfer(arry[6]);
      delay(1500);

      Serial.println("I am Sending 8th data of 15 data on SPI");
      Serial.println(arry[7], BIN);
      SPI.transfer(arry[7]);

      Serial.println("I am Sending 9th data of 15 data on SPI");
      Serial.println(arry[8], BIN);
      SPI.transfer(arry[8]);
      delay(1500);
  
      Serial.println("I am Sending 10th data of 15 data on SPI");
      Serial.println(arry[9], BIN);
      SPI.transfer(arry[9]);
      delay(1500);
    
      Serial.println("I am Sending 11th data of 15 data on SPI");
      Serial.println(arry[10], BIN);
      SPI.transfer(arry[10]);
      delay(1500);
  
      Serial.println("I am Sending 12th data of 15 data on SPI");
      Serial.println(arry[11], BIN);
      SPI.transfer(arry[11]);
      delay(1500);
    
      Serial.println("I am Sending 13th data of 15 data on SPI");
      Serial.println(arry[12], BIN);
      SPI.transfer(arry[12]);
      delay(1500);

      Serial.println("I am Sending 14th data of 15 data on SPI");
      Serial.println(arry[13], BIN);
      SPI.transfer(arry[13]);
      delay(1500);
  
      Serial.println("I am Sending 15th data of 15 data on SPI");
      Serial.println(arry[14], BIN);
      SPI.transfer(arry[14]);
      delay(1500);
    
      Serial.println("ALL 15 datas are sent on SPI, process completed ::");
      Serial.println("\n");
      Serial.println("\n");
      digitalWrite(spidata,HIGH);// take the SS pin high to de-select the chip:
      delay(1500);
   //}
  
    }




Output of the above code:

Code:
Read value
0
Newline
If condition-1 Matches and recieves 0x3B from SPI then:
I am Sending 1st data of 15 data on SPI
1
I am Sending 2nd data of 15 data on SPI
10
I am Sending 3rd data of 15 data on SPI
11
I am Sending 4th data of 15 data on SPI
100
I am Sending 5th data of 15 data on SPI
101
I am Sending 6th data of 15 data on SPI
110
I am Sending 7th data of 15 data on SPI
111
I am Sending 8th data of 15 data on SPI
1000
I am Sending 9th data of 15 data on SPI
1001
I am Sending 10th data of 15 data on SPI
1010
I am Sending 11th data of 15 data on SPI
1011
I am Sending 12th data of 15 data on SPI
1100
I am Sending 13th data of 15 data on SPI
1101
I am Sending 14th data of 15 data on SPI
1110
I am Sending 15th data of 15 data on SPI
1111
ALL 15 datas are sent on SPI, process completed ::


« Last Edit: August 29, 2012, 12:30:26 am by ranjeetray » Logged

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

I think your WriteByte routine is failing to complete the process - it reads SPSR but doesn't read SPDR once there's a byte so the hardware never clears ready for the next byte?  To quote the datasheet:
Quote
the SPIF bit is cleared by first reading the SPI Status Register with SPIF set, then accessing the SPI Data Register (SPDR)

  How about rewriting WriteByte thus:
Code:

byte WriteByte (byte value) {
  SPDR = value ;
  while (!(SPSR & (1<<SPIF)))
  {}
  return SPDR ;
}
(So easy to miss a semicolon after a while, use a blank block instead, harder to misread)

Sir,

I have tried with your suggestion also but it is not able to read from other board which is just keep on pumping the data and giving 0 as output, I have connected MOSI,MISO,CS,SCK wires across the two Arduino boards.
Tried code.

Code:

#include <SPI.h>// include the SPI library:

const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot:
int mm =0;
//int ReadByte();

void setup() {
 
  pinMode (spidata, OUTPUT);// set the spi_data_pin as an output:
  SPI.setDataMode(SPI_MODE3);
  SPI.setClockDivider(SPI_CLOCK_DIV8);
  SPCR = B00000000;
  SPCR = (1<<SPE);
 
  SPI.begin();// initialize SPI:
  Serial.begin(4800);
   
}

byte ReadByte(void) {
  while(!(SPSR & (1<<SPIF)))
  return SPDR;
}


// int ReadByte(int value) {
//  SPDR = value;
//  while((!SPSR & (1<<SPIF)))
//  return 0;
//}
//byte WriteByte (byte value) {
//  SPDR = value ;
//  while (!(SPSR & (1<<SPIF)))
//  {}
//  return SPDR ;
//}

void loop() {
      int arry[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
      int rxData;
      digitalWrite(spidata,LOW);
      rxData = ReadByte();
      Serial.println("Read value ");
       Serial.println(rxData, DEC);
       Serial.println("Newline");
    //if (rxData == 59) {
      Serial.println("If condition-1 Matches and recieves 0x3B from SPI then:");
      Serial.println("I am Sending 1st data of 15 data on SPI");
     
       
       
      Serial.println(arry[0], BIN);
      SPI.transfer(arry[0]); //  send in the address and value via SPI:
      delay(1500);
 
      Serial.println("I am Sending 2nd data of 15 data on SPI");
      Serial.println(arry[1], BIN);
      SPI.transfer(arry[1]);
      delay(1500);

      Serial.println("I am Sending 3rd data of 15 data on SPI");
      Serial.println(arry[2],BIN);
      SPI.transfer(arry[2]);
      delay(1500);
     
      Serial.println("I am Sending 4th data of 15 data on SPI");
      Serial.println(arry[3], BIN);
      SPI.transfer(arry[3]);
      delay(1500);
     
      Serial.println("I am Sending 5th data of 15 data on SPI");
      Serial.println(arry[4], BIN);
      SPI.transfer(arry[4]);
      delay(1500);

      Serial.println("I am Sending 6th data of 15 data on SPI");
      Serial.println(arry[5], BIN);
      SPI.transfer(arry[5]);
      delay(1500);
 
      Serial.println("I am Sending 7th data of 15 data on SPI");
      Serial.println(arry[6], BIN);
      SPI.transfer(arry[6]);
      delay(1500);

      Serial.println("I am Sending 8th data of 15 data on SPI");
      Serial.println(arry[7], BIN);
      SPI.transfer(arry[7]);

      Serial.println("I am Sending 9th data of 15 data on SPI");
      Serial.println(arry[8], BIN);
      SPI.transfer(arry[8]);
      delay(1500);
 
      Serial.println("I am Sending 10th data of 15 data on SPI");
      Serial.println(arry[9], BIN);
      SPI.transfer(arry[9]);
      delay(1500);
     
      Serial.println("I am Sending 11th data of 15 data on SPI");
      Serial.println(arry[10], BIN);
      SPI.transfer(arry[10]);
      delay(1500);
   
      Serial.println("I am Sending 12th data of 15 data on SPI");
      Serial.println(arry[11], BIN);
      SPI.transfer(arry[11]);
      delay(1500);
     
      Serial.println("I am Sending 13th data of 15 data on SPI");
      Serial.println(arry[12], BIN);
      SPI.transfer(arry[12]);
      delay(1500);

      Serial.println("I am Sending 14th data of 15 data on SPI");
      Serial.println(arry[13], BIN);
      SPI.transfer(arry[13]);
      delay(1500);
 
      Serial.println("I am Sending 15th data of 15 data on SPI");
      Serial.println(arry[14], BIN);
      SPI.transfer(arry[14]);
      delay(1500);
   
      Serial.println("ALL 15 datas are sent on SPI, process completed ::");
      Serial.println("\n");
      Serial.println("\n");
      digitalWrite(spidata,HIGH);// take the SS pin high to de-select the chip:
      delay(1500);
   //}
   

 
    }
Output of the above code which reads "0"
[code]
Read value
0
Newline
If condition-1 Matches and recieves 0x3B from SPI then:
I am Sending 1st data of 15 data on SPI
1
I am Sending 2nd data of 15 data on SPI
10
I am Sending 3rd data of 15 data on SPI
11
I am Sending 4th data of 15 data on SPI
100
I am Sending 5th data of 15 data on SPI
101
I am Sending 6th data of 15 data on SPI
110
I am Sending 7th data of 15 data on SPI
111
I am Sending 8th data of 15 data on SPI
1000
I am Sending 9th data of 15 data on SPI
1001
I am Sending 10th data of 15 data on SPI
1010
I am Sending 11th data of 15 data on SPI
1011
I am Sending 12th data of 15 data on SPI
1100
I am Sending 13th data of 15 data on SPI
1101
I am Sending 14th data of 15 data on SPI
1110
I am Sending 15th data of 15 data on SPI
1111
ALL 15 datas are sent on SPI, process completed ::



[/code]
Logged

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

Quote
Even this code is working fine .
Then, what IS your problem?

Sir,

Please help me out on this.
Logged

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

I think your WriteByte routine is failing to complete the process - it reads SPSR but doesn't read SPDR once there's a byte so the hardware never clears ready for the next byte?  To quote the datasheet:
Quote
the SPIF bit is cleared by first reading the SPI Status Register with SPIF set, then accessing the SPI Data Register (SPDR)

  How about rewriting WriteByte thus:
Code:

byte WriteByte (byte value) {
  SPDR = value ;
  while (!(SPSR & (1<<SPIF)))
  {}
  return SPDR ;
}
(So easy to miss a semicolon after a while, use a blank block instead, harder to misread)

Sir,

Please help me out on this.
Logged

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

If you are going to ignore the questions, and ignore the suggested corrections, please don't keep re-posting "Please help me out on this".
Logged


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

Quote
Even this code is working fine .
Then, what IS your problem?

Sir,

Please help me out on this.

That is not a meaningful answer to the question "what IS your problem?".
Logged


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

If you are going to ignore the questions, and ignore the suggested corrections, please don't keep re-posting "Please help me out on this".

Sir,

I am using two Arduino board, from one board I am able to send the data which is available on oscilloscope.
But when I read from another board, it is not able to read.

Code:
byte ReadByte(void) {
 
  while(!(SPSR & (1<<SPIF)))
    return SPDR;
}
void loop() {
  
      int rxData;
     digitalWrite(spidata,LOW);
      rxData = ReadByte();
      Serial.println("Read value ");
     Serial.println(rxData, DEC);
      digitalWrite(spidata,HIGH);

}
Sir, this read function is returning 0.
Please suggest something.


« Last Edit: August 29, 2012, 02:09:44 am by ranjeetray » Logged

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

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
Logged


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

If you are going to ignore the questions, and ignore the suggested corrections, please don't keep re-posting "Please help me out on this".

Dear Sir


I am using two arduino board for SPI communication. I am sending data from one board which is happening and data is available on oscilloscope. But when I try to read from with another board it is reading the data.
Data Sending Code
Code:

#include <SPI.h>// include the SPI library:

const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot:
int mm =0;

void setup() {
  
  pinMode (spidata, OUTPUT);// set the spi_data_pin as an output:
  
  SPI.begin();// initialize SPI:
  Serial.begin(9600);
}

void loop() {
    
      digitalWrite(spidata,LOW);
      SPI.transfer(1);
      digitalWrite(spidata,HIGH);
      delay(1500);
  
    }


Data Recieving code:

Code:
#include <SPI.h>// include the SPI library:

const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot:

void setup() {
  
  pinMode (spidata, INPUT);// set the spi_data_pin as an output:
  SPI.setDataMode(SPI_MODE3);
  SPI.setClockDivider(SPI_CLOCK_DIV8);
  SPI.begin();// initialize SPI:
  Serial.begin(9600);}
  
  byte ReadByte(void) {
  
  while(!(SPSR & (1<<SPIF)))
    return SPDR;
}

void loop() {
  
      int rxData;
  
    digitalWrite(spidata,LOW);
      rxData = ReadByte();
      Serial.println("Read value ");
     Serial.println(rxData, DEC);
    digitalWrite(spidata,HIGH);
   delay(1500);
}



Sir, please help me.
Sorry I had replied him like this only before then again I asked him to help me.

Read value is coming as "0".
Logged

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

OK, now re-read reply #4 and explain why you ignored his suggestion.
Logged


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