16 bit data transfer between two arduino uno's

Hi, i wrote a code for 8 bit data transfer as Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino ,transfer one data and adding some value and returing it .my question is hw to transfer 16 bit data to slave from master arduino uno

transfer 8 bits twice ?

in the link you shared, you have code sending multiple bytes

  for (const char * p = "Fab" ; c = *p; p++)
    SPI.transfer (c);

yeah, i tried sending 2 byte one after other,by changing code as below but it didn't worked for me and can i know why we are sending 0xFF . Anyhelp!!

#include <SPI.h>

void setup (void)
{
Serial.begin (115200);
Serial.println ();

digitalWrite(SS, HIGH); // ensure SS stays high for now

// Put SCK, MOSI, SS pins into output mode
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
SPI.begin ();

// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);

} // end of setup

uint16_t transferAndWait (const byte what)
{
uint16_t a = SPI.transfer (what &0xFF);
while(!(SPDR&(1<<SPIF)));
delayMicroseconds (20);
return a;
} // end of transferAndWait

void loop (void)
{

uint16_t a, b, c, d;

// enable Slave Select
digitalWrite(SS, LOW);

transferAndWait ('a'); // add command
transferAndWait (10);
a = transferAndWait (17);
b = transferAndWait (33);
c = transferAndWait (42);
d = transferAndWait (0);

// disable Slave Select
digitalWrite(SS, HIGH);

Serial.println ("Adding results:");
Serial.println (a, DEC);
Serial.println (b, DEC);
Serial.println (c, DEC);
Serial.println (d, DEC);

// enable Slave Select
digitalWrite(SS, LOW);

transferAndWait ('s'); // subtract command
transferAndWait (10);
a = transferAndWait (17);
b = transferAndWait (33);
c = transferAndWait (42);
d = transferAndWait (0);

// disable Slave Select
digitalWrite(SS, HIGH);

Serial.println ("Subtracting results:");
Serial.println (a, DEC);
Serial.println (b, DEC);
Serial.println (c, DEC);
Serial.println (d, DEC);

delay (1000); // 1 second delay
} // end of loop

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please correct your post above and add code tags around your code

(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

yeah sure!! i am new to forum.. can you help me to transfer 16 bit data

#include <SPI.h>

void setup (void)
{
Serial.begin (115200);
Serial.println ();

digitalWrite(SS, HIGH); // ensure SS stays high for now

// Put SCK, MOSI, SS pins into output mode
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
SPI.begin ();

// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);

} // end of setup

uint16_t transferAndWait (const byte what)
{
uint16_t a = SPI.transfer (what &0xFF);
while(!(SPDR&(1<<SPIF)));
delayMicroseconds (20);
return a;
} // end of transferAndWait

void loop (void)
{

uint16_t a, b, c, d;

// enable Slave Select
digitalWrite(SS, LOW);

transferAndWait ('a'); // add command
transferAndWait (10);
a = transferAndWait (17);
b = transferAndWait (33);
c = transferAndWait (42);
d = transferAndWait (0);

// disable Slave Select
digitalWrite(SS, HIGH);

Serial.println ("Adding results:");
Serial.println (a, DEC);
Serial.println (b, DEC);
Serial.println (c, DEC);
Serial.println (d, DEC);

// enable Slave Select
digitalWrite(SS, LOW);

transferAndWait ('s'); // subtract command
transferAndWait (10);
a = transferAndWait (17);
b = transferAndWait (33);
c = transferAndWait (42);
d = transferAndWait (0);

// disable Slave Select
digitalWrite(SS, HIGH);

Serial.println ("Subtracting results:");
Serial.println (a, DEC);
Serial.println (b, DEC);
Serial.println (c, DEC);
Serial.println (d, DEC);

delay (1000); // 1 second delay
} // end of loop

Spi always sent and receive the same number of bytes. You try to receive two bytes while sending only one. It is not works that

can i know how to transmit 16 bit data code , i tried in different ways but it didnt worked..Anyhelp!!

please show your efforts

Please, note that the SPI Port of Arduino UNO is capable to send/receive one-byte data at a time (in one bus cycle). This is because, the SPDR Register of the MCU is of 8-bit size (Fig-1).

In order to sed 16-bit data, you have to break it into two bytes and then execute these two instructions: SPI.transfer(highByte(y)) and SPI.transfer(lowByte)).


Figure-1:

Exercise: Create sketch to send 0x1234 from Master-UNO to Slave-UNO using SPI Port at 1-sec interval. The Slave will show the received data on it Serial Monitor.

Solution Hints:
Master Sketch:

#include<SPI.h>
int y = 0x1234;  //data item to send

void setup()
{
     SPI.begin();
     digitalWrite(SS, LOW);   //Slave is selected; SS is the software name for DPin-10
}

void loop()
{
     byte y1 = SPI.transfer(highByte(y));   //12 is being sent
     delayMicroseconds(100);     //give time to Slve to process received byte
     byte y2 = SPI.transfer(lowByte(y));   //34 is being sent
     delayMicroseconds(100);
     //================================
     delay(1000);   //test interval
}

Slave Sketch:

#include<SPI.h>
byte myData[2];
volatile bool flag = false;
int i = 0;

void setup()
{
     Serial.begin(9600);
     //--- consult data sheets to configure NANO (UNO-2) as Slave--
     SPI.attachInterrupt();  //after arraival of each byte data, the Slave is interrupted
}

void loop()
{
    //--write codes to process receive data to recreate 1234 
    //and show on Serial Monitor
    flag = false;
}

void ISR(SPI_STC_vect)
{
     myData[i] = SPDR;
     i++;
     if(i == 2)
     {
           i = 0;
           flag = true;
     }
}

``uint16_t transferAndWait (const byte what)
{
uint16_t a = SPI.transfer (what &0xFF);
while(!(SPDR&(1<<SPIF)));
delayMicroseconds (20);
return a;`

MCU is a machine with artificial intelligence. It will follow exactly what you tell it to follow. So, it is imporatant for the man (the user) to figure out how he is going to solve the problem (data send and data receive) and accordingly code the idea using Arduino/HLL Language.

If you trying to send a uint16_t, why the parameter of your function is of type byte?

have a look at the SerialCom library

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.