Sub topic 3
Reading a datasheet: commands, addresses and registers
This chapter assumes you know Arduino sketch structure (setup and loop), pinMode and digitalWrite, hexadecimal numbers, bitwise OR, and the four SPI signals from the previous chapter.
Finding the command format
A datasheet is the manufacturer's reference document for a chip. For SPI peripherals, look for a section named "SPI interface" or "Serial interface". It usually gives the command byte format, the register map, and the timing diagram that sets the SPI mode.
Many chips send a command byte first. The top bit often selects read or write, and the lower bits hold the register address. Confirm the bit positions in the register table, because they vary by chip.
Reading one register
This sketch reads register 0x0F from a device on pin 10. Replace the address and read flag with values from your datasheet.
#include <SPI.h>
const uint8_t CS_PIN = 10;
const uint8_t READ_FLAG = 0x80; // check your datasheet
uint8_t readRegister(uint8_t reg) {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
SPI.transfer(reg | READ_FLAG);
uint8_t value = SPI.transfer(0x00); // dummy byte clocks out the reply
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
return value;
}
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
Serial.println(readRegister(0x0F), HEX);
}
void loop() {}Several peripherals on one bus
Devices on a shared bus share SCLK, MOSI and MISO. Each device needs its own chip-select pin, and only one chip-select line should be LOW at a time. Call beginTransaction before each exchange so each device gets its own clock rate and mode.
Bulk transfers and throughput
SPI.transfer(buffer, length) sends a whole array in one call. This removes per-byte call overhead and keeps chip select LOW for the entire frame. Received bytes overwrite the buffer in place.
Wiring, signal integrity and voltage levels
Keep SCLK and MOSI wires short, and connect grounds between devices. Match logic levels: a 5 V output can damage a 3.3 V-only input, so use a level shifter.
> Tip: Pull each chip-select line high with a 10 kΩ resistor. A floating chip-select line can start random transfers.
Note: the callout above uses a block quote in this text only for display. In the chapter it is written as a callout fence, so the reader sees it as a tip.
Debugging with a logic analyzer
Probe SCLK, MOSI, MISO and CS. Check that CS falls before the first clock edge, that the clock idles at the level your mode expects, that data changes on the edge your mode specifies, and that the clock stays within the datasheet limit. A wrong mode shows bytes sampled on the wrong edge, which usually produces shifted or garbled values.
SPI compared with I2C and UART
SPI uses four lines, is full duplex and is usually the fastest of the three. I2C uses two shared lines with device addresses and needs pull-up resistors. UART uses two point-to-point lines with no shared clock, so both sides must agree on the baud rate.
Daisy chaining, dual SPI and quad SPI
Daisy chaining links devices in series so data passes through each one in turn, usually behind one chip select. Dual and quad SPI use two or four data lines to move more bits per clock. Check the datasheet before using them, because the standard Arduino SPI library handles only single-line transfers.
Exercise
A datasheet says the read flag is bit 7 and the register address is 0x0F. What command byte do you send first?
Show answer
0x8F, because 0x0F | 0x80 = 0x8F.