Go Down

Topic: Trying to send and receive at both the end of Arduino boards on SPI (Read 311 times) previous topic - next topic

ranjeetray

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: [Select]


#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: [Select]


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




Code: [Select]


#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: [Select]

0
0
0
0
0
0


PaulS

That second block of code looks very strange. When properly formatted, loop looks like:
Code: [Select]
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().

Go Up