Sub topic 6
Bit Order and Word Size
Assume you can read binary and hexadecimal numbers, use Arduino setup() and loop(), and configure digital pins. This chapter focuses on how SPI orders bits and groups them into words.
Bit order: MSB first or LSB first
SPI sends one bit on each clock pulse. Bit order tells the controller which bit leaves the device first:
- MSB first sends the most significant bit first. In
0b10110010, the first bit is1. - LSB first sends the least significant bit first. In the same value, the first bit is
0.
The value itself does not change in memory. Only the order of its bits on the wire changes. The controller and peripheral must use the same order.
A datasheet usually states this as “MSB first,” “LSB first,” or a timing diagram with bit labels such as D7 through D0.
Word size and byte order
A word is the number of bits that a device treats as one data unit. Common word sizes are 8 and 16 bits. SPI itself does not require one particular word size. It simply produces clock pulses and shifts bits.
An 8-bit transfer uses eight clock pulses:
8-bit value: 1 0 1 1 0 0 1 0
MSB first: ^ first
LSB first: ^ firstA 16-bit value uses sixteen pulses. Some devices expect those pulses as one uninterrupted frame. Others describe the data as two separate bytes.
For a 16-bit value such as 0x1234, the device might require:
- high byte first:
0x12, then0x34 - low byte first:
0x34, then0x12
This byte order is separate from the bit order inside each byte. Read both requirements from the datasheet.
| Setting | Meaning |
|---|---|
| `MSBFIRST` | Send the highest-valued bit of each transferred unit first |
| `LSBFIRST` | Send the lowest-valued bit of each transferred unit first |
| 8-bit word | Eight clock pulses represent one data unit |
| 16-bit word | Sixteen clock pulses represent one data unit |
Configuring bit order on Arduino
SPISettings selects the clock speed, bit order, and clock mode for a transaction. The following example sends a 16-bit register value as two bytes, high byte first. The chip select stays active for both bytes, so the peripheral sees one continuous frame.
#include <SPI.h>
const uint8_t CS_PIN = 10;
void writeWord(uint16_t value) {
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
SPI.transfer(highByte(value)); // 0x12 for value 0x1234
SPI.transfer(lowByte(value)); // 0x34 for value 0x1234
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
}
void setup() {
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
writeWord(0x1234);
}
void loop() {}If the datasheet requires LSB-first transfers, replace MSBFIRST with LSBFIRST. If it requires the low byte first, reverse the two SPI.transfer() calls as well. Do not change either setting by guesswork.
Exercise
A device expects 0x1234 least significant byte first, with MSB-first bits inside each byte. Which bytes should Arduino send?
Show answer
Send 0x34 first, followed by 0x12, using MSBFIRST. Keep chip select active across both transfers if the datasheet defines them as one frame.