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?
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;
Think about what value is stored in UART_DR and how assignment works.
The variable UART_DR is set to 0x5A. Assigning it to received_byte copies that value, so received_byte becomes 0x5A.
What happens if you read the UART data register without checking if data is ready?
uint8_t data = UART_DR;
Think about hardware behavior when data is not ready.
Reading UART data register without checking the status may return stale or invalid data because no new byte has arrived yet.
Identify the problem in this code snippet that reads a byte from UART:
uint8_t *ptr = NULL; *ptr = UART_DR;
What happens if you write to a NULL pointer?
Dereferencing a NULL pointer leads to undefined behavior and usually a runtime crash or fault.
Choose the correct code snippet that waits for UART data ready flag before reading the byte.
Remember to wait until the RXNE bit is set before reading.
Option A waits in a loop until the RXNE (Receive Not Empty) bit is set, then reads the data. Others either read too early or wait incorrectly.
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;
}
}How many times does the interrupt handler run and how many bytes does it read per run?
The interrupt handler runs once per interrupt and reads one byte if RXNE is set, so it stores exactly one byte per call.