0
0
Embedded Cprogramming~10 mins

SPI with external devices (sensors, displays) in Embedded C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize SPI communication with the correct clock polarity.

Embedded C
SPI_InitTypeDef SPI_Config;
SPI_Config.CLKPolarity = [1];
Drag options to blanks, or click blank then click option'
ASPI_POLARITY_LOW
BSPI_POLARITY_HIGH
CSPI_MODE_MASTER
DSPI_FIRSTBIT_MSB
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing clock polarity with clock phase.
Using SPI_MODE_MASTER instead of polarity setting.
2fill in blank
medium

Complete the code to send a byte of data over SPI.

Embedded C
uint8_t data = 0xA5;
SPI_Transmit([1], 1);
Drag options to blanks, or click blank then click option'
ANULL
B0xA5
C&data
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the data value directly instead of its address.
Passing NULL which causes no data to send.
3fill in blank
hard

Fix the error in the SPI receive code to correctly read one byte.

Embedded C
uint8_t received;
SPI_Receive([1], 1);
Drag options to blanks, or click blank then click option'
ANULL
Breceived
C*received
D&received
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the variable directly instead of its address.
Using NULL which causes no storage location.
4fill in blank
hard

Complete the sensor reading loop by filling both blanks to use the sensor address for SPI read and check if the value is above 100.

Embedded C
for (int i = 0; i < sensor_count; i++) {
    uint16_t value = SPI_Read([1]);
    if (value [2] 100) {
        sensor_values[i] = value;
    }
}
Drag options to blanks, or click blank then click option'
Asensor_addresses[i]
B>
C<
Dsensor_ids[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Using sensor IDs instead of addresses for SPI read.
Using less than instead of greater than in the condition.
5fill in blank
hard

Fill all three blanks to correctly configure SPI mode, data size, and first bit order.

Embedded C
SPI_Config.Mode = [1];
SPI_Config.DataSize = [2];
SPI_Config.FirstBit = [3];
Drag options to blanks, or click blank then click option'
ASPI_MODE_MASTER
BSPI_DATASIZE_8BIT
CSPI_FIRSTBIT_MSB
DSPI_MODE_SLAVE
Attempts:
3 left
💡 Hint
Common Mistakes
Setting slave mode when master is needed.
Using wrong data size constant.
Confusing first bit order.