Pages: [1]   Go Down
Author Topic: Trying to send and receive at both the end of Arduino boards on SPI  (Read 260 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 am trying to receive and send data both the side of Arduino board on SPI, but it is not getting synchronized properly. And not giving proper results.

Code:

#include <SPI.h>
#define DATAOUT 11//MOSI
#define DATAIN  12//MISO
#define SPICLOCK  13//sck
#define SLAVESELECT 10//ss
int data;
int count;

byte clr;

 void setup()
 {
  
   SPI.begin();
   SPCR = (1<<SPE)|(1<<MSTR);
   clr=SPSR;
   clr=SPDR;
   delay(10);
   Serial.begin(9600);
   SPI.setBitOrder(MSBFIRST);
   SPI.setClockDivider(SPI_CLOCK_DIV64);
   SPI.setDataMode( SPI_MODE2);
  
  
 }
 
 void loop()
 {
   count = 1;
   if(count ==1){
    
   mode1();
  count++;
   }
  
   delay(1000);
   if(count == 2){
   mode2();
   count = 1;  
 }
  
 }
 
 void mode1()
 {
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
 
   digitalWrite(SLAVESELECT,LOW);
   SPI.transfer(0xFF);
   delay(10);
   digitalWrite(SLAVESELECT,HIGH);
 }
  void mode2()
 {
  pinMode(DATAOUT, INPUT);
  pinMode(DATAIN, OUTPUT);
  pinMode(SPICLOCK,INPUT);
  pinMode(SLAVESELECT,INPUT);
  
   digitalWrite(SLAVESELECT,LOW);
   data = SPI.transfer(0xFF);
   delay(18);
   digitalWrite(SLAVESELECT,HIGH);
   Serial.println(data, HEX);
 }


//Output

Code:

3C
3C
3C
3C
3C
3C



Code:

#include <SPI.h>
#define DATAOUT 11//MOSI
#define DATAIN  12//MISO
#define SPICLOCK  13//sck
#define SLAVESELECT 10//ss
int data;

byte clr;
int count;

 void setup()
 {
  
   SPI.begin();
   SPCR = (1<<SPE)|(1<<MSTR);
   clr=SPSR;
   clr=SPDR;
   delay(10);
   Serial.begin(9600);
   SPI.setBitOrder(MSBFIRST);
   SPI.setClockDivider(SPI_CLOCK_DIV64);
   SPI.setDataMode( SPI_MODE2);
  
  
 }
 
 void loop()
 {
  
    count = 2;
   if(count ==1){
    
   mode2();
  count--;
   }
  
   delay(1000);
   if(count == 2){
   mode1();
   count = 2;  
 }
        
 }
 
 void mode2()
 {
  pinMode(DATAOUT, INPUT);
  pinMode(DATAIN, OUTPUT);
  pinMode(SPICLOCK,INPUT);
  pinMode(SLAVESELECT,INPUT);  
  
  
   digitalWrite(SLAVESELECT,LOW);
   data = SPI.transfer(byte(0x00));
   delay(8);
   digitalWrite(SLAVESELECT,HIGH);
   Serial.println(data, HEX);
  
   delay(17);
   delay(1000);
 }
  void mode1()
 {
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  
   digitalWrite(SLAVESELECT,LOW);
   SPI.transfer(0x3C);
   delay(20);
   digitalWrite(SLAVESELECT,HIGH);
      
 }


//output

Code:
0
0
0
0
0
0

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45996
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That second block of code looks very strange. When properly formatted, loop looks like:
Code:
void loop()
{
   count = 2;
   if(count ==1)
   {
     mode2();
     count--;
   }
   
   delay(1000);

   if(count == 2)
   {
      mode1();
      count = 2; 
   }
}
count is set to 1 every pass through loop. It is not volatile, so nothing is going to change it between when it is set and when it is tested. So, mode2() will never be called. If count is 2, and it can never be anything but 2, mode1() is called, and count is reset to 2. Which is what it was. What is the purpose of all this folderol? Get rid of the useless code, and just call delay() (ugh!) and mode1().
Logged

Pages: [1]   Go Up
Jump to: