Sub topic 10
Reading a Datasheet: Commands, Addresses and Registers
Assume you know Arduino setup() and loop(), digital output, hexadecimal numbers, and bitwise operators. This chapter uses a generic SPI peripheral. Its command format is an example, so always verify the exact format in your device's datasheet.
An SPI device usually exposes registers. A register is a small storage location identified by an address. Registers hold configuration values, status flags, measurements, or commands. To access one, you send a sequence of bytes called a frame while chip select is low.
Find the SPI command format
Look for the datasheet sections named serial interface, SPI protocol, register map, or timing. Identify these details:
| Detail | Question to answer |
|---|---|
| Read command | Is there a read flag, separate opcode, or fixed command? |
| Address | How many address bytes are required? |
| Response delay | Does the reply begin immediately or after dummy bytes? |
| Write data | How many data bytes follow a write command? |
| Chip select | Must it remain low for the whole frame? |
A common one-byte format uses the highest address bit as a read flag:
- Send
register address | 0x80. - Send a dummy byte, often
0x00. - Receive the register value during that dummy byte.
SPI is full duplex, so every transmitted byte also receives a byte. The peripheral may transmit a meaningless value while it interprets the command. The useful response often appears on the next byte.
Implement a register read
The Arduino SPI.transfer() function sends a byte and returns the byte received at the same time. SPISettings selects the speed, bit order, and clock mode required by the peripheral.
#include <SPI.h>
const uint8_t CS_PIN = 10;
const uint8_t READ_FLAG = 0x80;
uint8_t readRegister(uint8_t address) {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
SPI.transfer(address | READ_FLAG);
uint8_t value = SPI.transfer(0x00);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
return value;
}
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
uint8_t status = readRegister(0x0F);
Serial.println(status, HEX);
}
void loop() {}0x0F is only an example address. The datasheet might require a different read flag, a separate opcode, least-significant-bit-first order, or extra dummy bytes. Some devices also require a delay between the command and response.
Check the register map
A register table tells you what each bit means. Read the reset value before changing a register. For a register containing independent options, change only the intended bits:
uint8_t updated = (oldValue & ~0x06) | 0x04;This clears bits 1 and 2, then sets bit 2. It preserves all other bits. Avoid writing a guessed value to a status or reserved register. Reserved bits may need to remain zero or unchanged.
Exercise
The peripheral returns 0x00 for every register read. What should you check first?
Show answer
Check chip-select framing, SPI mode, clock speed, read-command format, register address, and whether the response requires one or more dummy bytes. Use a logic analyzer to compare the transmitted frame with the datasheet timing diagram.