Ground Embedded KO Subscribe

3D Labs

SPI

One bit per clock, with master and slave exchanging data at the same instant. Chip select, the four modes, and how a mode mismatch shifts the value you read.

Send

Bus waveform CS · SCK · MOSI · MISO

Dashed vertical lines mark when the master samples. The yellow band is the current position.

Results

Value sent (MOSI)
Value the slave sent
Value the master read (MISO)
Time per byte

Try this

  1. Press Send. First the CS line goes LOW. Only that slave wakes up; the other slave leaves MISO floating (grey).
  2. On every clock one bit leaves on MOSI and, at the same moment, one bit arrives on MISO. Watch TX and RX fill together on the master's screen.
  3. Change the target to Slave B: CS1 goes LOW and B answers. Clock and data lines are shared, but each chip has its own CS.
  4. Switch the mode from 0 to 3. The clock now idles HIGH, yet the value reads the same, because both modes sample on the rising edge.
  5. Choose mode 1 or 2 and the master samples on the falling edge, so the received value is shifted by one bit. A mode error does not stop communication; it makes the value wrong.

How to read it

SPI is two shift registers, one in the master and one in the slave, joined in a ring. Eight clocks from the master swap the two bytes completely. That is why the master must send something even when it only wants to read, and why something always comes back when it writes.

Mode Clock idle (CPOL) Sampling edge (CPHA)
0 LOW first = rising
1 LOW second = falling
2 HIGH first = falling
3 HIGH second = rising

Both slaves in this lab are assumed to sample on the rising edge and change their output on the falling edge. Many sensors and flash memories work this way, which is why their datasheets list both mode 0 and mode 3.

FREE PDFGet the 3 free ebooksIf you want what you just ran written down: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →

This lab is also available in Korean. More labs: all 3D labs.