Typical SPI communication

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

 

I become very confused when all three (sensor adc and spi )combined. I know sensor detect the physical property such as temperature and convert it into voltage signal. ADC is device that convert analog signal into digital signal. SPI is serial protocol. 

 

ADT7307 is temperature sensor, It has inbuilt adc it work on SPI protocol.     

 

datasheet https://www.analog.com/media/en/technical-documentation/data-sheets/ADT7311.pdf

I assume sensor store the temperature data at register 0x02   Microcontroller has one 8 bit register who can store 8 bit data 

 

0x02 - sensor register  X

0x09 - micro register   Y

 

I assume that sensor detect the physical property and gives analog signal and ADC convert it into digital signal we called it ADC value for example if it is X = 1100 1100, we are sending data via SPI so after 8 clock micro controller will hold same value so Y = 1100 1100

 

X = 1111 0000 ->  after 8 clock = Y = 1111 0000    // 32 * c

X = 0000 1111  ->  after 8 clock = Y = 0000 1111  // 36 *c

X = 0000 1111  ->  after 8 clock = Y = 0000 1111  // 42 *c

 

Assumed value is a temporary value Which is taken for example only,  I think this process happen  when we interface the ADT7307 sensor to the microcontroller 

 

Is this really happen I do  not  have so deep understanding about overall process?

 

 

This topic has a solution.
Last Edited: Tue. Feb 25, 2020 - 03:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


That post contains the word "assume" rather more times than are probably healthy. You know the old adage about "never assume anything - it only makes an ASS-of-U-and-ME?

 

The datasheet surely tells you the facts so you don't actually have to ASS-U-ME anything?

 

What I read in the datasheet there is:

 

 

and there's even a diagram showing a typical dialog:

 

 

By implication the first data sent on DIN is 0x08 then 0x20 in which the 0x08 is a "command byte" It is:

0
0 R/_W (low means this is a write)
0 reg addr
0 reg addr
1 reg addr
0
0
0

So clearly the 0x08 is saying "write to register 1". the 0x20 is the byte then written to register 1. Register 1 is the "configuration register" and we're told:

 

 

So the value 0x20 is writing 01 to the [6:5] operation mode bits. It tells us this is "one shot" and delivers a reading after 240ms and that is exactly what the previous timing diagram shows.

 

So no assumptions required here I don't think?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

That post contains the word "assume" rather more times than are probably healthy. You know the old adage about "never assume anything - 

 

So no assumptions required here I don't think?

I am assuming because  I am not able to fully understand the basic concepts

 

slave and master : SPI always sends one byte and receives one byte at a time 

 

 

 

 

but what happen when slave is adc

 

Does ADC slave sensor send one byte  and receives one byte at a time ?

 

Does  ADC slave sensor  only send one byte and doesn't receive any byte ?

Last Edited: Mon. Feb 24, 2020 - 04:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You put 0x08 into SPDR then wait until SPIF becomes set (to signal it's all gone to the ADC). Then you put 0x20 into SPDR and wait until SPIF. Then you wait a further 240ms (because the datasheet tells you that's how long the ADC takes) then you put 0xFF (or any byte you care to choose) into SPDR, not because you are sending further commands/data but simply to trigger an SPI transfer. this time when SPIF is set (to show transfer complete) it's what came back to your SPDR that matters as it is one 8 bit half of a 16 bit reading. Then you send 0xFF (or anything) a second time and this time when SPIF is set you now have the other half of the 16 bit reading in SPDR

 

This "tricky bit" that gets many folks using SPI is this need for you (the "master") to have to send something to get a byte back. The something you send doesn't matter so can be 0xFf or 0x00 or anything you like. It's sometimes called a "stuffing byte". It's only purpose is because the master is in control of generating SCK you have to write something to it starts to clock the bits. The other end is not interested in what you actually send at this time. But it needs the SCK pulses so it can send the 8 bit parts of the result on the MISO line.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ansh11 wrote:

slave and master : SPI always sends one byte and receives one byte at a time 

 

I normally think of it as the master and slave swapping a byte under control of the master. Either one can ignore what they get in exchange; that's determined by the devices themselves.

 

 

ansh11 wrote:

Does  ADC slave sensor  only send one byte and doesn't receive any byte ?

 

It receives a byte but ignores it.

 

Worth noting that chip returns a 16-bit value so you'll need to swap two bytes to get the full result back.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

Last Edited: Mon. Feb 24, 2020 - 05:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

 

This "tricky bit" that gets many folks using SPI is this need for you (the "master") to have to send something to get a byte back. The something you send doesn't matter so can be 0xFf or 0x00 or anything you like. It's sometimes called a "stuffing byte". It's only purpose is because the master is in control of generating SCK you have to write something to it starts to clock the bits. The other end is not interested in what you actually send at this time. But it needs the SCK pulses so it can send the 8 bit parts of the result on the MISO line.

I'm  sorry but still i don't understand so MCU sends a 0xFF or 0x00  or dummy write byte in order to read the sensor data. If the MCU sends 0xFF to the sensor, the slave will hold 0xFF after 8 clocks, While Slave should have hold the value of ADC but why it hol 0xFF?

Last Edited: Mon. Feb 24, 2020 - 05:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

ansh11 wrote:

If the MCU sends 0xFF to the sensor, the slave will hold 0xFF after 8 clocks, While Slave should have hold the value of ADC but why it hol 0xFF?

 

It will hold 0xFF BUT, while taking in the 0XFF, it will have shifted out the ADC value.

 

Look at the picture you posted back in #3. As the master shifts data into the slave, at the the same time data is shifted out of the slave and into the master. As I said, data is swapped between the two.

 

 

Look at the animation on this page...

 

https://electrosome.com/spi/

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

Last Edited: Mon. Feb 24, 2020 - 07:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:

ansh11 wrote:

If the MCU sends 0xFF to the sensor, the slave will hold 0xFF after 8 clocks, While Slave should have hold the value of ADC but why it hol 0xFF?

 

It will hold 0xFF BUT, while taking in the 0XFF, it will have shifted out the ADC value.

 

Look at the picture you posted back in #3. As the master shifts data into the slave, at the the same time data is shifted out of the slave and into the master. As I said, data is swapped between the two.

 

master and slave have a shift register  Both device writes a byte into the register.  after 8 pulses they will have  exchanged bytes. The master now holds the byte that was in the shift register of the slave and the slave holds the byte that was in the shift register of the master.

 

 

if  master  first send a byte  0x00 to the slave what will in the slave's register at this time ?

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Lemme see if I can simplify what the others are saying....

 

In SPI, when the Master wants to read data back from the slave it must SEND something...ANYTHING to the slave to be able to read back the data.  this 'Dummy byte' as we call it is a single byte with any number of one's and zero's.  IT DOES NOT MATTER IF THEY ARE ALL ZEROS, OR ALL ONES, OR SOME COLLECTION OF BOTH.  The reason why it does not matter what is in the 'Dummy Byte' is because the Slave will ignore the byte as it is shifting out the data from it's TX shifter.  In the case of your ADC, the ADC knows that on a READ, it should ignore TWO "Dummy Bytes" as it needs to shift out 14 bits of data...Or two BYTES with the Byte that holds the UPPER MSB's having zero's int eh D6 and D7 locations(or some status information...I have not read the datasheet)

 

 

The part is 'smart' enough to know to flush the byte(s) received from the master on a Read Function, and pay attention to them on a Write Function.

 

Long story short....you're 'reading' into this too much wink

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

 

 

The part is 'smart' enough to know to flush the byte(s) received from the master on a Read Function, and pay attention to them on a Write Function.

 

Long story short....you're 'reading' into this too much wink

 

Jim

 

 You can see that I left Slave register empty I have only one question, if  master  first send a byte  0x00 to the slave what will in the slave's register at this time ? 

 

I'm trying to understand by giving some value for examples at starting  Can you take a value in that place, for example so that It would be very easy to understand basics  

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What happens in the chip may not be the same as your diagram. If you want to delve further, then get yourself a logic analyser to observe what goes in and comes out.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
What happens in the chip may not be the same as your diagram. If you want to delve further, then get yourself a logic analyser to observe what goes in and comes out.

I don't have logic analyser. 

 

I do not understand why experts are not responding with ADC values with example. Like I have in my post for some example. I have shown my best attempt in post #10  But i am lost now  my Doubt is not clear even after more reading. I am sorry but the question I asked in post 10 has not been answered yet.

Last Edited: Tue. Feb 25, 2020 - 11:16 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The quality of the answer is based on the quality of the question. In this instance you need to think about what you are asking. It could be what you're asking is not sensible.

 

The ADC datasheet is clear as to the sequence it expects. Follow this and it will work.

 

No logic analyser? I bet your PC has a microphone input. You can use this as an oscilloscope. Google soundcard oscilloscope.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Perhaps a shorter answer?

 

What matters on an SPI system is the clock. Data is transferred in both directions at any appropriate clock edge (bearing in mind that there may be a chip enable involved).

 

Whether the data that is transferred is relevant is entirely down to the transfer protocol for the device in question. It may be that it reads and writes simultaneously (perhaps setting up a future mode while delivering data) or it may be strictly one way at a time. Doesn't matter: if the protocol says send a dummy byte it's because it wants the clock pulses either for its internal processing or to return data to you. Or perhaps a memory chip might require a byte of control data, two bytes of address, and then depending on the control data, either successive bytes to write or it will deliver bytes you want to read.

 

The point is that for every transfer you need to send clock pulses. Most microcontrollers have an SPI engine that generates the clock pulses after writing to the send buffer. If you want clocks to get out, you have to write something to the send buffer... but if the external device isn't listening - say it's sending you data - then it doesn't matter what that data is. It may be latched internally, it may be discarded, but it isn't acted upon. Equally, when you're (say) sending the address and the read/write flag to an SPI memory chip, it will still probably send you data while you send those bits, but they almost certainly don't mean anything.

 

It is possible that you send a byte and receive a byte at the same time - the protocol for the chip is what decides.

 

Neil

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you so much for all your valuable time. In conclusion, if any device supports SPI protocol then  SPI send a byte and receive a byte at the same time. I think When I write the code, everything will be cleared. But for now I am thinking that the answer to my question has been found

 

once again thanks 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ansh11 wrote:
 SPI send a byte and receive a byte at the same time.
This is exactly why it doesn't usually make a lot of sensce to have SPI_read() and SPI_write() routines because there's always a read and a write every time. So normally you have a single SPI_exchange() or SPI_tranfser() or sometimes just spi() which is a routine that already sens and received. Something like:

uint8_t SPI_exchange(uint8_t data) {
    SPDR = data; // trigger transfer
    while (!(SPSR & (1 << SPIF))); // wait for completion
    return SPDR;
}

then you can just send:

SPI_exchange(important_byte);

or you can send 0xFF as stuffing just because you want the reply:

received_byte = SPI_exchange(0xFF); // send "stuffing"

or you can both send and receive important things at the same time:

important_received_byte = SPI_exchange(important_byte_to_send);