I am assuming the default pin configuration is pin 10(SS), 11(MOSI), 12(MISO), 13(CLK) from a default setup, SPI.begin(). The code compiles OK. Not sure about the int valuex and using the SPI.transfer() function.
#include <SPI.h>
int value1 = 0x8026;
int value2 = 0x0801;
int value3 = 0x8027;
int value4 = 0x0000;
void setup (void)
{
Serial.begin (9600);
Serial.println ("Starting");
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_DIV4);
} // end of setup
void loop (void)
{
// enable Slave Select
digitalWrite(SS, LOW);
SPI.transfer (1); // initiate transmission
SPI.transfer (value1);
SPI.transfer (value2);
SPI.transfer (value3);
SPI.transfer (value4);
// disable Slave Select
digitalWrite(SS, HIGH);
delay (1000);
}
DataFiddler
1,0456 silver badges14 bronze badges
asked Mar 17, 2019 at 16:50
1 Answer 1
Not sure about the int valuex and using the SPI.transfer() function.
A couple of things to know:
- You are storing an unsigned 16-bit literal into a signed 16-bit variable. You should be using
uint16_t value1 = 0x8026;
etc. Also, unless you are planning to change the values, they should beconst
. SPI.transfer()
transfers only 8 bits of data. You are only sending the lowest byte of yourvaluex
variables.SPI.transfer16()
sends an unsigned 16-bit value.
To illustrate, this is how your program should look:
#include <SPI.h>
const uint16_t value1 = 0x8026;
const uint16_t value2 = 0x0801;
const uint16_t value3 = 0x8027;
const uint16_t value4 = 0x0000;
void setup(void) {
Serial.begin(9600);
Serial.println("Starting");
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_DIV4);
} // end of setup
void loop(void) {
// enable Slave Select
digitalWrite(SS, LOW);
SPI.transfer(1); // initiate transmission
SPI.transfer16(value1);
SPI.transfer16(value2);
SPI.transfer16(value3);
SPI.transfer16(value4);
// disable Slave Select
digitalWrite(SS, HIGH);
Delay(1000);
}
That will transfer the stream of 8-bit values:
0x01 0x80 0x26 0x08 0x01 0x80 0x27 0x00 0x00
answered Mar 17, 2019 at 17:20
-
Hi, and thank you. I believe the code is right but I do not get any signaling out of MOSI pin 11; it does go from high to low. I can 'see' (with an oscilloscope) SS pin 10 go low, I can see only 3 clock pulses on pin 13. I am not concerned with any incoming data.howdyrichard– howdyrichard2019年03月20日 14:57:37 +00:00Commented Mar 20, 2019 at 14:57
-
Also, when I added 16 to SPI.transfer, transfer changed from blue to black color.howdyrichard– howdyrichard2019年03月20日 16:38:19 +00:00Commented Mar 20, 2019 at 16:38
-
The code works perfect! Thank you and I wish I had sent this sooner. Thanks again!howdyrichard– howdyrichard2019年04月16日 17:37:25 +00:00Commented Apr 16, 2019 at 17:37
-
@howdyrichard Accept the answer if it worked for youSoreDakeNoKoto– SoreDakeNoKoto2019年08月14日 21:00:11 +00:00Commented Aug 14, 2019 at 21:00
lang-cpp
SPI.transfer16(intval);
if necessary