0
0
Embedded Cprogramming~20 mins

SPI with external devices (sensors, displays) in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SPI Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
SPI Data Transmission Output
What will be the output on the SPI data register (SPDR) after executing this code snippet?
Embedded C
#define SPI_DATA_REG (*(volatile unsigned char*)0x2F)
#define SPI_STATUS_REG (*(volatile unsigned char*)0x2E)
#define SPI_ENABLE 0x40
#define SPI_INT_FLAG 0x80

void spi_init() {
    SPI_STATUS_REG = SPI_ENABLE;
}

void spi_send(unsigned char data) {
    SPI_DATA_REG = data;
    while (!(SPI_STATUS_REG & SPI_INT_FLAG));
}

int main() {
    spi_init();
    spi_send(0xA5);
    return SPI_DATA_REG;
}
A0xA5
B0x00
C0xFF
DProgram hangs in while loop
Attempts:
2 left
💡 Hint
Think about what value is written to the SPI data register and when the function returns.
🧠 Conceptual
intermediate
1:30remaining
SPI Clock Polarity and Phase
Which SPI mode configuration correctly sets clock polarity (CPOL) to 1 and clock phase (CPHA) to 0?
ACPOL=0, CPHA=1 means clock idles high, data sampled on leading edge
BCPOL=1, CPHA=0 means clock idles low, data sampled on trailing edge
CCPOL=1, CPHA=0 means clock idles high, data sampled on leading edge
DCPOL=0, CPHA=0 means clock idles high, data sampled on trailing edge
Attempts:
2 left
💡 Hint
Remember CPOL controls idle clock level, CPHA controls sampling edge.
🔧 Debug
advanced
2:30remaining
SPI Read from Sensor Fails
This code tries to read a byte from an SPI sensor but always returns 0x00. What is the most likely cause?
Embedded C
unsigned char spi_read() {
    SPI_DATA_REG = 0xFF; // send dummy byte
    while (!(SPI_STATUS_REG & SPI_INT_FLAG));
    return SPI_DATA_REG;
}

int main() {
    spi_init();
    unsigned char val = spi_read();
    return val;
}
ASPI_STATUS_REG is incorrectly masked, causing infinite loop
BSPI clock not enabled, so no data is clocked in
CSPI_INT_FLAG is never set because SPI is in slave mode
DSPI_DATA_REG should not be written before reading
Attempts:
2 left
💡 Hint
Check if SPI clock is active to shift data in and out.
📝 Syntax
advanced
1:30remaining
Correct SPI Interrupt Handler Syntax
Which option shows the correct syntax for an SPI interrupt service routine (ISR) in embedded C for AVR microcontrollers?
AISR(SPI_STC_vect) { /* handle SPI interrupt */ }
Bvoid ISR_SPI_STC_vect(void) { /* handle SPI interrupt */ }
Cinterrupt SPI_STC_vect() { /* handle SPI interrupt */ }
Dvoid SPI_STC_vect() { /* handle SPI interrupt */ }
Attempts:
2 left
💡 Hint
Look for the special ISR macro used in AVR GCC.
🚀 Application
expert
3:00remaining
SPI Display Initialization Sequence
Given these SPI commands to initialize a display, what is the correct order to send them to properly initialize the device?
A4,1,2,3
B2,1,4,3
C1,4,2,3
D1,2,4,3
Attempts:
2 left
💡 Hint
Reset first, then wake up, set pixel format, then turn display on.