0

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); 
}
1
  • transfer sends a byte only. See SPI.transfer16(intval); if necessary Commented Mar 17, 2019 at 16:59

1 Answer 1

1

Not sure about the int valuex and using the SPI.transfer() function.

A couple of things to know:

  1. 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 be const.
  2. SPI.transfer() transfers only 8 bits of data. You are only sending the lowest byte of your valuex 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
4
  • 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. Commented Mar 20, 2019 at 14:57
  • Also, when I added 16 to SPI.transfer, transfer changed from blue to black color. Commented Mar 20, 2019 at 16:38
  • The code works perfect! Thank you and I wish I had sent this sooner. Thanks again! Commented Apr 16, 2019 at 17:37
  • @howdyrichard Accept the answer if it worked for you Commented Aug 14, 2019 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.