0

I am newbie. In my application Arduino Due is configured as Master and Arduino Uno as Slave. I need to transfer 24-bit Hexadecimal data of about 79-registers to slave device. At the slave end in serial monitor window I would like to display the transmitted data. I have developed an array at slave side, and I am comparing the master data with slave one. I have divided the 24-bit data into 3 Bytes. I will attach the Master and Slave codes below. I am getting Invalid data in serial monitor window as the data (Hexa Decimal) transmitted by Master is converted into decimal format internally and I think this was the reason I am not able to print the Valid data in serial monitor window.

Any help will be greatly appreciated. Thanks in Advance.

MASTER Code

#include <SPI.h>

SPISettings mySettting(125000, MSBFIRST, SPI_MODE0);
const uint8_t ss = 10;

//pinMode(10, OUTPUT)
void setup() {
  pinMode(ss, OUTPUT);

  SPI.begin();
SPI.beginTransaction(mySettting);
 
 digitalWrite(ss, LOW); 
  SPI.transfer('@'); 
  SPI.transfer(0x1C);
  SPI.transfer(0x25);
  SPI.transfer(0x00);
  SPI.transfer('$');
  delay(100);
  digitalWrite(ss, HIGH);
  
digitalWrite(ss, LOW);
  SPI.transfer('@');  
  SPI.transfer(0x08);
  SPI.transfer(0x08);
  SPI.transfer(0x01);
  SPI.transfer('$');
  delay(100);
  digitalWrite(ss, HIGH);

digitalWrite(ss, LOW);  
  SPI.transfer('@');
  SPI.transfer(0x00);
  SPI.transfer(0x05);
  SPI.transfer(0x02);
  SPI.transfer('$');
  delay(100);
  digitalWrite(ss, HIGH);

digitalWrite(ss, LOW);  
  SPI.transfer('@');
  SPI.transfer(0x42);
  SPI.transfer(0x06);
  SPI.transfer(0x03);
  SPI.transfer('$');
  delay(100);
  digitalWrite(ss, HIGH);

digitalWrite(ss, LOW);  
  SPI.transfer('@');
  SPI.transfer(0x43);
  SPI.transfer(0x0A);
  SPI.transfer(0x04);
  SPI.transfer('$');
  delay(100);
  digitalWrite(ss, HIGH);

digitalWrite(ss, LOW);  
  SPI.transfer('@');
  SPI.transfer(0xC8);
  SPI.transfer(0x00);
  SPI.transfer(0x05);
  SPI.transfer('$');
  delay(100);
  digitalWrite(ss, HIGH);

digitalWrite(ss, LOW);  
  SPI.transfer('@');
  SPI.transfer(0x02);
  SPI.transfer(0xC8);
  SPI.transfer(0x6C);
  SPI.transfer('$');
  delay(100);
  digitalWrite(ss, HIGH);

digitalWrite(ss, LOW);  
  SPI.transfer('@');
  SPI.transfer(0xB2);
  SPI.transfer(0x40);
  SPI.transfer(0x07);
  SPI.transfer('$');
  SPI.transfer('&');
  delay(100);
  digitalWrite(ss, HIGH);  
SPI.endTransaction();
}

void loop() {
}

SLAVE Code

#include <SPI.h>
#include <stdio.h>
#include <string.h>
int buff[100] = {0x001C2500,0x00080801,0x00000502,0x00420603,0x430A04,0xC80005,0x02C806,0xB24007};


uint32_t buff_1 [100];
volatile byte indx;
volatile boolean process;
int i = 0;
uint32_t var_byte = 0x00000000;
uint32_t var_byte_1 = 0x00000000;
int byte_pos = 0;
short int status_1 = 0;



void setup (void) {
   Serial.begin (115200);
   pinMode(MISO, OUTPUT); // have to send on master in so it set as output
   SPCR |= _BV(SPE); // turn on SPI in slave mode
   indx = 0; // buffer empty
   process = false;
   SPI.attachInterrupt(); // turn on interrupt
   memset(buff_1,'\0',sizeof(buff_1));
}

ISR (SPI_STC_vect) // SPI interrupt routine 
{ 
   byte c = SPDR; // read byte from SPI Data Register

   if(c == '@')
   {
      var_byte = 0x00000000;
      byte_pos = 2;
   }
   else if(c == '$')
      buff_1 [indx++] = var_byte;
   else if(c == '&')
      process = true;
   else
   {
      var_byte_1 = 0x00000000;
      var_byte_1 = (var_byte_1 | c) << byte_pos;
      var_byte |= var_byte_1; 
      byte_pos--;
   }
   
}
void loop (void) {
   if (process) {
      process = false; //reset the process
      status_1 = 0;
      for(i=0;i<79;i++)
      {
        if (buff[i]==buff_1[i])
        {
          Serial.println ("Valid data_1");
        }
        else
        {
          status_1 = 1;
          break;
          //Serial.println ("Inavlid data");
        }
      }
      if (status_1 == 0)
        Serial.println ("Valid data");
      else
        Serial.println ("Inavlid data");
      indx= 0; //reset button to zero
      memset(buff_1,'\0',sizeof(buff_1));
   }
}
  • Try to print out the data, that you received. Then you might see, what the problem is, meaning how exactly the data is "invalid" – chrisl Jul 20 at 12:48
  • 24-bit Hexadecimal data ... do you want to transmit 6 text characters, or 3 bytes? – jsotola Jul 20 at 16:50
  • I want to transmit 3 bytes of hexadecimal data, I mean 0x1A, 0x25, 0x00 in this way I would like to send the data. – vamshi krishna Jul 21 at 4:41
  • Your process turn true for every receive of a "$", i.e. at end of receiving 3 bytes of data, but you are comparing it with 80-bytes of data, of course it won't match and will be "invalid". And BTW, where that 80 bytes come from? it looks that your data only consists of 32 bytes. – hcheung Jul 21 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.