newbie question: SPI transfer

Hi everybody, After reading an example code for communicating with an SPI device, I came across something that seems weird for me.

  digitalWrite(10, LOW); // select the DS3234 that has its CS line on digital 10
  SPI.transfer(0x08); // tell the DS3234 device we're requesting data from the register at 08h
  data=SPI.transfer(0); // the DS3234 sends the data back and stores it in the byte data
  digitalWrite(10, HIGH);  // deselect the DS3234 if finished with it

(code from the SPI-Tutorial II of “Getting Started/Moving Forward with Arduino!” by John Boxall http://tronixstuff.wordpress.com/2011/06/15/tutorial-arduino-and-the-spi-bus-part-ii/)

My questions: - How does the SPI-slave device know that it should send data back, and not store the value 0 in register 08? It seems to be the same command. - What does the value 0 in data=SPI.transfer(0) mean at all?

Thanks, Andy

How does the SPI-slave device know that it should send data back, and not store the value 0 in register 08?

Because of the way the slave device is designed, that is what it is designed to do.

What does the value 0 in data=SPI.transfer(0) mean at all?

It is know as a dummy value, the slave device simply does not clock it in, you could have any value here.

Thanks for your quick replay.

Grumpy_Mike:

How does the SPI-slave device know that it should send data back, and not store the value 0 in register 08?

Because of the way the slave device is designed, that is what it is designed to do.

Yes, thats already clear to me, but how does the master inform the slave that it has to give back a value. Does the SPI-master perform some kind of request message or so?

Grumpy_Mike:

What does the value 0 in data=SPI.transfer(0) mean at all?

It is know as a dummy value, the slave device simply does not clock it in, you could have any value here.

Ah, I suppose something needs to be send to maintain the clock signal. Am I right?

but how does the master inform the slave that it has to give back a value.

It doesn't or rather the action of writing a value to it tells it. You need to look at the data sheet of what ever device we are talking about.

I suppose something needs to be send to maintain the clock signal. Am I right?

No it is not to maintain the clock signal. It is just that the SPI shift registers in and out are being clocked at the same time so something is always being clocked out just like something is always being clocked in.

Grumpy_Mike:

I suppose something needs to be send to maintain the clock signal. Am I right?

No it is not to maintain the clock signal. It is just that the SPI shift registers in and out are being clocked at the same time so something is always being clocked out just like something is always being clocked in.

Yes, I thought something like this.

Grumpy_Mike:

but how does the master inform the slave that it has to give back a value.

It doesn’t or rather the action of writing a value to it tells it. You need to look at the data sheet of what ever device we are talking about.

Sorry, but what is the difference between this:

SPI.transfer(0x08); // tell the DS3234 device we're requesting data from the register at 08h
data=SPI.transfer(0); // the DS3234 sends the data back and stores it in the byte data

and this:

SPI.transfer(0x08); // tells the device which address to write to
SPI.transfer(0);   // you can send any representation of a byte

Sorry, but what is the difference between this:

Code: SPI.transfer(0x08); // tell the DS3234 device we're requesting data from the register at 08h data=SPI.transfer(0); // the DS3234 sends the data back and stores it in the byte dataand this:

Code: SPI.transfer(0x08); // tells the device which address to write to SPI.transfer(0); // you can send any representation of a byte

The first sends out 2 bytes. The second also sends out 2 bytes, but it reads back in a byte during the 2nd send and stores that result in the variable "data" (which is probably not a very good name, looks like one the compiler may choke on, give back and "already defined" error kind of message)

The data sheet provides the details:

Address and data bytes are shifted MSB first into the serial data input (DIN) and out of the serial data output (DOUT). Any transfer requires the address of the byte to specify a write or read, followed by one or more bytes of data. Data is transferred out of the DOUT pin for a read operation and into the DIN for a write operation (Figures 3 and 4).

The address byte is always the first byte entered after CS is driven low. The most significant bit of this byte determines if a read or write takes place. If the MSB is 0, one or more read cycles occur. If the MSB is 1, one or more write cycles occur.

Data transfers can occur one byte at a time or in multiple- byte burst mode. After CS is driven low, an address is written to the DS3234. After the address, one or more data bytes can be written or read. For a single-byte transfer, one byte is read or written and then CS is driven high. For a multiple-byte transfer, however, multiple bytes can be read or written after the address has been written (Figure 5). Each read or write cycle causes the RTC register address to automatically increment, which continues until the device is disabled. The address wraps to 00h after incrementing to 13h (during a read) and wraps to 80h after incrementing to 93h (during a write). An updated copy of the time is loaded into the user buffers upon the falling edge of CS and each time the address pointer increments from 13h to 00h. Because the internal and user copies of the time are only synchronized on these two events, an alarm condition can occur internally and activate the INT/SQW pin independently of the user data. If the SRAM is accessed by reading (address 19h) or writing (address 99h) the SRAM data register, the contents of the SRAM address register are automatically incremented after the first access, and all data cycles will use the SRAM data register.

http://datasheets.maxim-ic.com/en/ds/DS3234.pdf pages 18/19 So in your example

Code:
SPI.transfer(0x08); // tell the DS3234 device we're requesting data from the register at 08h
data=SPI.transfer(0); // the DS3234 sends the data back and stores it in the byte dataand this:

Code:
SPI.transfer(0x08); // tells the device which address to write to
SPI.transfer(0);   // you can send any representation of a byte

the MSB is low (of 0x08) so that would mean two reads, with a byte being stored in the first example, and the same byte ignored in the 2nd.

Ah, now I understand: I have to use different adresses for writing and reading.

For example: To write to register 0 => use adress 0x80 (B10000000) and write a value To read from register 0 => call adress 0x00 (B00000000). Now, when you send a dummy-byte, the slave device will send the data along the MISO-line.

I hope this is correct. Thanks a lot

Correct, for the DS3234. This 2nd command during the read data=SPI.transfer(0); is what captures that data coming back in on MISO.