SPI

hello,
for a project I have to read the current and the voltage values of an MPPT (STEVAL-ISV009v1) via SPI,
here is the code wich I have made, but the problem is that the miso no data send to the arduino, and the value of readspanning is 0
can someone help me with this problem?

/*
  * CS - to digital pin 10  (SS pin)
  * tdata - to digital pin 11 (MOSI pin)
  * Dout - to digital pin 12 (MISO pin)
  * tCLK - to digital pin 13 (SCK pin)
*/

#include <SPI.h> // include the SPI library:

 int slaveSelectPin = 10; // set pin 10 as the slave

void setup() 
{
  Serial.begin(9600);
  
  pinMode (slaveSelectPin, OUTPUT); // set the slaveSelectPin as an output:
  
  SPI.begin(); // initialize SPI:
  SPI.setBitOrder(MSBFIRST);
}

void loop() 
{
  SPI.transfer(0x02); // shut down
  
  SPI.transfer(0x03); // turn on
  
  delay(100);
  digitalWrite(slaveSelectPin,LOW);
  
  byte Readspanning = SPI.transfer(0x05); // read spanning
  
  SPI.transfer(0x01); // nop
 
  SPI.transfer(0x01); // nop
  
  digitalWrite(slaveSelectPin,HIGH); 
  
  Serial.print(" spanning = ");
  Serial.print(Readspanning); 
}

Moderator edit: [code][/code] tags added. (Nick Gammon)

SPI.transfer(0x02); // shut down
  
  SPI.transfer(0x03); // turn on
  
...

  digitalWrite(slaveSelectPin,LOW);

No point in sending commands until you select the slave.

ok, but it has no influence for the working of the program, I just wanna try to see the value of the readcurrent in the serail monitor

Take a look at this doc. It has the timing for the SPI read. Pages 24-26. http://www.mouser.com/catalog/specsheets/steval-isv009v1.pdf

Notice on page 25 the 16 bit data is apparently returned with the NOP sends.

  byte Readspanning = SPI.transfer(0x05); // read spanning
  
  // this should be byte 1 (high byte)
  byte Readspanning2 = SPI.transfer(0x01); // nop
  // this should be byte 2 (low byte)
  byte Readspanning3 = SPI.transfer(0x01); // nop

Then I would use the serial monitor to print all those values and see if they make sense to you.

stevieboy: ok, but it has no influence for the working of the program, I just wanna try to see the value of the readcurrent in the serail monitor

It doesn't work, but you are certain I am wrong, is that it?

http://www.gammon.com.au/spi

@Nick Gammon, no sorry I just tried it but it stays the same

stevieboy: @Nick Gammon, no sorry I just tried it but it stays the same

Did you read my response? The data is NOT returned with the initial call. It is returned in two bytes by the NOP calls.

yes I have read it, and now I have done this and I get a vary value:

byte Readspanning1 = SPI.transfer(0x05); // read spanning

byte Readspanning2 = SPI.transfer(0x01); // nop

byte Readspanning3 = SPI.transfer(0x01); // nop

digitalWrite(slaveSelectPin,HIGH);

Serial.print(" spanning = "); Serial.print(Readspanning2); Serial.print(" spanning1 = "); Serial.print(Readspanning3); }

Good deal. Sounds like you now have comm with it.

Convert the two values to an integer with something like this.

int Readspanning2 = SPI.transfer(0x01);
int Readspanning3 = SPI.transfer(0x01);

// shift the hi byte to the high byte
Readspanning2 = Readspanning2 << 8;
// clear the hi byte of the low byte
Readspanning3 = Readspanning3 & 0xff;
// put them together
int RSint = Readspanning2 | Readspanning3;

edit. Sorry. My bad. In my hurry, I reversed the bitwise operators. They are correct now.

BTW, thanks for the info on that MPPT charger. I use solar power frequently. That could prove to be a very useful tool. I mean, in some cases, why re-invent the wheel?

I think you should also be setting the MOSI and SCK pins as OUTPUTs since the SPI hardware doesn't do this automatically. It may be that the SPI library does this for you, but I think its wise to do this explicitly so that you can port to a different library without any surprises. It also documents in setup() which pins do what - so then you don't accidentally use the same pin for two different uses (surprising common error).

You can set those in your code, but remember to take into account there are different devices with different SPI pins. I feel it is best to call SPI.begin() and let it decide. Note the first three instructions.

SPIClass SPI;

void SPIClass::begin() {
  // Set direction register for SCK and MOSI pin.
  // MISO pin automatically overrides to INPUT.
  // When the SS pin is set as OUTPUT, it can be used as
  // a general purpose output port (it doesn't influence
  // SPI operations).

  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(SS, OUTPUT);
  
  digitalWrite(SCK, LOW);
  digitalWrite(MOSI, LOW);
  digitalWrite(SS, HIGH);

  // Warning: if the SS pin ever becomes a LOW INPUT then SPI 
  // automatically switches to Slave, so the data direction of 
  // the SS pin MUST be kept as OUTPUT.
  SPCR |= _BV(MSTR);
  SPCR |= _BV(SPE);
}

Hello all,
I am a new arduino programmer and I’ve found the post about STEVAL-ISV009v1 via SPI

I copy the code but I don’t understand somethings at Reply #8 and Reply #10 ,
Why do we need to convert the two values to an integer and we must to put the code at Reply #8 in the program?
Where do the code at Reply #10 put in?
Is the code right as below?
I hope to hear your reply soon.

Thanks,
Teechan

The code:
#include <SPI.h> // include the SPI library:

int slaveSelectPin = 10; // set pin 10 as the slave

void setup()
{
Serial.begin(9600);

pinMode (slaveSelectPin, OUTPUT); // set the slaveSelectPin as an output:
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
digitalWrite(SCK, LOW);
digitalWrite(MOSI, LOW);
digitalWrite(slaveSelectPin, HIGH);
SPI.begin(); // initialize SPI:
SPI.setBitOrder(MSBFIRST);
}

void loop()
{
SPI.transfer(0x02); // shut down

SPI.transfer(0x03); // turn on

delay(100);
digitalWrite(slaveSelectPin,LOW);

byte Readspanning1 = SPI.transfer(0x05); // read spanning

byte Readspanning2 = SPI.transfer(0x01); // nop

byte Readspanning3 = SPI.transfer(0x01); // nop

digitalWrite(slaveSelectPin,HIGH);

Serial.print(" spanning = “);
Serial.print(Readspanning2);
Serial.print(” spanning1 = ");
Serial.print(Readspanning3);
}

I haven’t reviewed the datasheet for that particular part (is there a link?), but my SPI experience would suggest the following changes:

#include <SPI.h> // include the SPI library:

 int slaveSelectPin = 10; // set pin 10 as the slave

void setup() 
{
  Serial.begin(9600);
  
  pinMode (slaveSelectPin, OUTPUT); // set the slaveSelectPin as an output:
  pinMode(SCK, OUTPUT); // delete this line
  pinMode(MOSI, OUTPUT); // delete this line
  digitalWrite(SCK, LOW); // delete this line
  digitalWrite(MOSI, LOW); // delete this line
  digitalWrite(slaveSelectPin, HIGH);
  SPI.begin(); // initialize SPI:
  SPI.setBitOrder(MSBFIRST); // not needed - this is the order used by default
}

void loop() 
{
digitalWrite (slaveSelectPin, LOW); // add this line

  SPI.transfer(0x02); // shut down

  digitalWrite (slaveSelectPin, HIGH); // add this line

digitalWrite (slaveSelectPin, LOW); // add this line

  SPI.transfer(0x03); // turn on
  
digitalWrite (slaveSelectPin, HIGH); // add this line

  delay(100);
  digitalWrite(slaveSelectPin,LOW);
  
  byte Readspanning1 = SPI.transfer(0x05); // read spanning
  
  byte Readspanning2 = SPI.transfer(0x01); // nop
 
  byte Readspanning3 = SPI.transfer(0x01); // nop
  
  digitalWrite(slaveSelectPin,HIGH); 
  
  Serial.print(" spanning1 = ");
  Serial.print(Readspanning1); 
  Serial.print(" spanning2 = ");
  Serial.print(Readspanning2);  
  Serial.print(" spanning3 = ");
  Serial.print(Readspanning3);

}

Thank your help,
the datasheet link:
http://www.st.com/web/catalog/tools/FM116/SC1078/PF251965
The SPI interface is in P.23-25

DC-DC converter - how does SPI play into this?

The board has 4 pins for SPI communication http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00026751.pdf

Ok, Go with what I suggested then. These lines can be deleted:

Serial.print(" spanning1 = "); Serial.print(Readspanning1);

as the byte will not contain useful info.

After this in setup: SPI.begin(); add this: SPI.setDataMode(3); // 3, or SPI_MODE3 ??

Read the article here http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus and confirm the mode that supports this:

The idle state of the serial clock for the SPV1020 is high, while data pins are driven on the falling edges of the serial clock and are sampled on its rising edges (SPI control bits CPOL = 1, and CPHA = 1).

Thanks CrossRoads, I've read the article about CPOL and CPHA. Also, I want to ask you one more things, What can i do if i want to measure the frequency and Vout?

From the spv1020 datasheet, i found the PWM frequency (default value) is 100kHz, Is it relevant to the command list of the code 06- read PWM?

SPV1020 datasheet http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/CD00275733.pdf

I can’t tell from the datasheet what the 9 and 10 bit values represent that you can read back. Suggest sending email to tech support at the manufacturer.