Challenge - 5 Problems
SPI Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Think about what value is written to the SPI data register and when the function returns.
✗ Incorrect
The function spi_send writes 0xA5 to SPI_DATA_REG and waits for SPI_INT_FLAG to be set. After that, the data register still holds 0xA5, so main returns 0xA5.
🧠 Conceptual
intermediate1:30remaining
SPI Clock Polarity and Phase
Which SPI mode configuration correctly sets clock polarity (CPOL) to 1 and clock phase (CPHA) to 0?
Attempts:
2 left
💡 Hint
Remember CPOL controls idle clock level, CPHA controls sampling edge.
✗ Incorrect
CPOL=1 means clock line is high when idle. CPHA=0 means data is sampled on the first (leading) clock edge after the clock starts.
🔧 Debug
advanced2: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;
}Attempts:
2 left
💡 Hint
Check if SPI clock is active to shift data in and out.
✗ Incorrect
If SPI clock is not enabled or not running, no data is shifted in, so SPI_INT_FLAG never sets and data register remains 0x00.
📝 Syntax
advanced1: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?
Attempts:
2 left
💡 Hint
Look for the special ISR macro used in AVR GCC.
✗ Incorrect
AVR GCC uses ISR(vector_name) macro to define interrupt handlers. Option A uses this correctly.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Reset first, then wake up, set pixel format, then turn display on.
✗ Incorrect
The display must be reset first (0x01), then woken from sleep (0x11), then pixel format set (0x3A), and finally display turned on (0x29).