0
0
Embedded Cprogramming~20 mins

Receiving a byte over UART in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UART Receive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this UART receive code snippet?

Consider the following embedded C code snippet that reads a byte from UART data register and stores it in a variable. What value will received_byte hold after execution?

Embedded C
volatile uint8_t UART_DR = 0x5A; // UART Data Register mock
uint8_t received_byte = 0;

// Simulate reading a byte from UART
received_byte = UART_DR;
AUndefined behavior
B0x00
C0xFF
D0x5A
Attempts:
2 left
💡 Hint

Think about what value is stored in UART_DR and how assignment works.

Predict Output
intermediate
2:00remaining
What error occurs when reading UART without checking status?

What happens if you read the UART data register without checking if data is ready?

uint8_t data = UART_DR;
ACompilation error
BYou might read invalid or old data
CUART hardware resets
DProgram crashes immediately
Attempts:
2 left
💡 Hint

Think about hardware behavior when data is not ready.

🔧 Debug
advanced
2:00remaining
Why does this UART receive code cause a runtime error?

Identify the problem in this code snippet that reads a byte from UART:

uint8_t *ptr = NULL;
*ptr = UART_DR;
ADereferencing a NULL pointer causes a runtime error
BUART_DR is not initialized
CMissing semicolon causes syntax error
DUART_DR is not volatile
Attempts:
2 left
💡 Hint

What happens if you write to a NULL pointer?

📝 Syntax
advanced
2:00remaining
Which option correctly reads a byte from UART with status check?

Choose the correct code snippet that waits for UART data ready flag before reading the byte.

Awhile(!(UART_SR & (1 << RXNE))); uint8_t data = UART_DR;
Bif(UART_SR & (1 << RXNE)) uint8_t data = UART_DR;
Cuint8_t data = UART_DR; while(!(UART_SR & (1 << RXNE)));
Dwhile(UART_SR & (1 << RXNE)); uint8_t data = UART_DR;
Attempts:
2 left
💡 Hint

Remember to wait until the RXNE bit is set before reading.

🚀 Application
expert
3:00remaining
How many bytes are received after this UART interrupt handler runs?

Given this UART interrupt handler code, how many bytes will be stored in buffer after it runs once?

#define BUFFER_SIZE 4
volatile uint8_t buffer[BUFFER_SIZE];
volatile uint8_t index = 0;

void UART_IRQHandler(void) {
  if(UART_SR & (1 << RXNE)) {
    buffer[index++] = UART_DR;
    if(index >= BUFFER_SIZE) index = 0;
  }
}
A0
B4
C1
DUndefined
Attempts:
2 left
💡 Hint

How many times does the interrupt handler run and how many bytes does it read per run?