0
0
Embedded Cprogramming~10 mins

Receiving a byte over UART in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Receiving a byte over UART
Start UART Receive
Wait for Data Ready Flag
Read Byte from UART Data Register
Store Byte in Variable
Process or Use Byte
End Receive
This flow shows how a program waits for a byte to arrive via UART, reads it, stores it, and then uses it.
Execution Sample
Embedded C
while (!(UART_SR & (1 << RXNE_FLAG))) {}
uint8_t received_byte = UART_DR;
// Use received_byte
Waits until UART data is ready, then reads one byte from the UART data register.
Execution Table
StepUART_SR (Status Register)Condition (RXNE_FLAG set?)ActionVariable received_byte
10x00FalseWait (loop continues)undefined
20x00FalseWait (loop continues)undefined
30x20TrueExit loop, read UART_DR=0x410x41
40x20TrueUse received_byte=0x410x41
💡 RXNE_FLAG set in UART_SR, indicating data ready; loop exits and byte is read.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
UART_SR0x000x000x000x200x20
received_byteundefinedundefinedundefined0x410x41
Key Moments - 3 Insights
Why does the program keep looping before reading the byte?
Because the RXNE_FLAG in UART_SR is not set yet (see steps 1 and 2 in execution_table), so the program waits until data arrives.
What happens when RXNE_FLAG becomes true?
The loop condition becomes false, so the program exits the loop and reads the byte from UART_DR (step 3).
Why do we read UART_DR only after RXNE_FLAG is set?
Because RXNE_FLAG signals that new data is ready to read; reading UART_DR before that could give invalid data (see exit_note).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of received_byte at Step 2?
A0x41
B0x00
Cundefined
D0x20
💡 Hint
Check the 'Variable received_byte' column at Step 2 in execution_table.
At which step does the condition RXNE_FLAG become true, allowing the loop to exit?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Condition (RXNE_FLAG set?)' column in execution_table.
If UART_SR never sets RXNE_FLAG, what happens to the loop?
AIt exits immediately
BIt loops forever waiting
CIt reads garbage data
DIt skips reading UART_DR
💡 Hint
Refer to the loop condition and steps 1-2 in execution_table where RXNE_FLAG is false.
Concept Snapshot
Receiving a byte over UART:
- Wait until RXNE_FLAG in UART_SR is set
- Read byte from UART_DR
- Store byte in a variable
- Use the byte as needed
Always check RXNE_FLAG before reading to avoid invalid data.
Full Transcript
This example shows how to receive a byte over UART in embedded C. The program waits in a loop until the RXNE_FLAG bit in the UART status register (UART_SR) is set, indicating that a new byte has arrived. Once the flag is set, the program reads the byte from the UART data register (UART_DR) and stores it in a variable called received_byte. The program then can use this byte for further processing. The execution table traces each step, showing the status register, the condition check, the action taken, and the value of received_byte. Key moments clarify why the loop waits and when it exits. The visual quiz tests understanding of variable values and loop behavior. The snapshot summarizes the key steps to safely receive a byte over UART.