SPI master-slave architecture helps devices talk to each other quickly using a simple wire connection. One device controls the conversation (master), and others listen and respond (slaves).
SPI master-slave architecture in Embedded C
Introduction
Syntax
Embedded C
/* SPI Master Initialization Example */ void SPI_MasterInit(void) { /* Set MOSI, SCK as Output, MISO as Input */ DDRB = (1 << DDB3) | (1 << DDB5); /* Enable SPI, Set as Master, Set clock rate fck/16 */ SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0); } /* SPI Slave Initialization Example */ void SPI_SlaveInit(void) { /* Set MISO as Output */ DDRB = (1 << DDB4); /* Enable SPI */ SPCR = (1 << SPE); }
Master controls clock and data flow.
Slave waits for master's clock to send/receive data.
Examples
Embedded C
/* Master sends a byte */ void SPI_MasterTransmit(char data) { SPDR = data; /* Load data into SPI data register */ while(!(SPSR & (1 << SPIF))) ; /* Wait until transmission complete */ }
Embedded C
/* Slave receives a byte */ char SPI_SlaveReceive(void) { while(!(SPSR & (1 << SPIF))) ; /* Wait for data reception */ return SPDR; /* Return received data */ }
Sample Program
This program sets up a device as SPI master and sends the letter 'A' repeatedly to a slave device.
Embedded C
#include <avr/io.h> void SPI_MasterInit(void) { DDRB = (1 << DDB3) | (1 << DDB5) | (1 << DDB2); /* MOSI, SCK, SS as output */ SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0); /* Enable SPI, Master, clk/16 */ } void SPI_MasterTransmit(char data) { SPDR = data; while(!(SPSR & (1 << SPIF))) ; } int main(void) { SPI_MasterInit(); while(1) { SPI_MasterTransmit('A'); /* Send character 'A' */ for (volatile int i = 0; i < 10000; i++); /* Simple delay */ } return 0; }
Important Notes
Always set the slave select (SS) pin correctly to choose which slave to talk to.
SPI is full-duplex: master and slave send data at the same time.
Clock polarity and phase must match on master and slave for correct communication.
Summary
SPI master controls the clock and starts communication.
Slaves respond when selected by the master.
SPI is fast and uses few wires for device communication.
Practice
1. In SPI master-slave architecture, who controls the clock signal?
easy
Solution
Step 1: Understand SPI roles
In SPI, the master initiates communication and controls timing.Step 2: Identify clock control
The master generates the clock signal to synchronize data transfer.Final Answer:
The master device controls the clock signal. -> Option AQuick Check:
Master controls clock = A [OK]
Hint: Master always controls clock in SPI [OK]
Common Mistakes:
- Thinking slave controls clock
- Assuming no clock is needed
- Believing clock is shared equally
2. Which of the following is the correct way to select a slave device in SPI using embedded C?
easy
Solution
Step 1: Recall SPI slave selection method
In SPI, the master selects a slave by pulling its SS pin LOW.Step 2: Understand pin logic
Setting SS LOW activates the slave; HIGH deselects it.Final Answer:
Set the slave select (SS) pin LOW to select the slave. -> Option DQuick Check:
SS LOW selects slave = A [OK]
Hint: SS pin LOW means slave selected [OK]
Common Mistakes:
- Setting SS HIGH to select slave
- Using clock pin to select slave
- Sending data to select slave
3. Given this embedded C code snippet for SPI master:
SPI_SS_PIN = 0; // Select slave SPI_Write(0xA5); // Send data SPI_SS_PIN = 1; // Deselect slaveWhat happens on the SPI bus during this code execution?
medium
Solution
Step 1: Analyze slave select pin changes
Setting SPI_SS_PIN to 0 selects the slave before sending data.Step 2: Understand data transmission
SPI_Write(0xA5) sends data from master to selected slave.Step 3: Deselect slave after sending
Setting SPI_SS_PIN to 1 ends communication by deselecting slave.Final Answer:
The master selects the slave, sends 0xA5, then deselects the slave. -> Option AQuick Check:
SS LOW + send data + SS HIGH = D [OK]
Hint: SS LOW before send, HIGH after send [OK]
Common Mistakes:
- Assuming slave sends data
- Ignoring slave select pin state
- Thinking data sends without slave selected
4. Identify the error in this SPI master code snippet:
SPI_SS_PIN = 1; // Deselect slave SPI_Write(0x3C); // Send data SPI_SS_PIN = 0; // Select slave
medium
Solution
Step 1: Check slave select pin timing
Slave is deselected (SS=1) before sending data, so slave ignores data.Step 2: Correct order for SPI communication
Slave must be selected (SS=0) before sending data to receive it.Final Answer:
Slave is deselected before sending data, so data won't be received. -> Option CQuick Check:
SS must be LOW before send = C [OK]
Hint: Select slave before sending data [OK]
Common Mistakes:
- Sending data before selecting slave
- Assuming data sends regardless of SS pin
- Misunderstanding SPI_Write timing
5. You want to communicate with two SPI slaves using one master. Which approach correctly manages slave selection in embedded C?
hard
Solution
Step 1: Understand multiple slave selection
Each slave needs its own slave select pin controlled by the master.Step 2: Select only one slave at a time
Set only one slave select pin LOW to activate that slave; others stay HIGH.Step 3: Avoid simultaneous selection
Selecting multiple slaves at once causes bus conflicts and data errors.Final Answer:
Use separate slave select pins for each slave and set only one LOW at a time. -> Option BQuick Check:
Separate SS pins, one LOW at a time = B [OK]
Hint: One SS pin LOW per slave communication [OK]
Common Mistakes:
- Using one SS pin for all slaves
- Trying to select slaves by clock toggling
- Selecting multiple slaves simultaneously
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
