0
0
Embedded Cprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UART Transmission 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 transmit function?
Consider the following embedded C code snippet that transmits a byte over UART. What will be the value of the UART data register (UDR) after calling uart_transmit(0x55);?
Embedded C
volatile unsigned char UDR = 0;
volatile unsigned char UCSRA = 0x20; // UDRE bit set (ready)

void uart_transmit(unsigned char data) {
    while (!(UCSRA & 0x20)) {
        // wait until UDRE bit is set
    }
    UDR = data;
}

int main() {
    uart_transmit(0x55);
    return 0;
}
AUDR will be 0xFF
BUDR will be 0x00
CUDR will be 0x55
DUDR will be 0x20
Attempts:
2 left
💡 Hint
Check what the function assigns to UDR after the ready bit is confirmed.
🧠 Conceptual
intermediate
1:30remaining
Which statement correctly describes the UDRE bit in UART communication?
In UART communication, what does the UDRE (UART Data Register Empty) bit indicate?
AThe UART data register is full and cannot accept new data
BThe UART data register is empty and ready to accept new data
CThe UART has received a byte successfully
DThe UART transmitter is disabled
Attempts:
2 left
💡 Hint
Think about when you can safely write a new byte to the UART data register.
🔧 Debug
advanced
2:30remaining
Why does this UART transmit code cause a hang?
This code is intended to transmit a byte over UART, but it causes the program to hang (infinite loop). What is the reason?
Embedded C
volatile unsigned char UCSRA = 0x00; // UDRE bit is 0 (not ready)
volatile unsigned char UDR = 0;

void uart_transmit(unsigned char data) {
    while (!(UCSRA & 0x20)) {
        // wait until UDRE bit is set
    }
    UDR = data;
}

int main() {
    uart_transmit(0xAA);
    return 0;
}
AThe function does not clear the UDRE bit after transmission
BUDR register is not volatile, causing optimization issues
CData is assigned before checking UDRE bit
DUDRE bit is never set, so the while loop never ends
Attempts:
2 left
💡 Hint
Look at the initial value of UCSRA and the condition in the while loop.
📝 Syntax
advanced
2:00remaining
Which option correctly transmits a byte over UART?
Select the code snippet that correctly waits for the UART data register to be ready and then transmits the byte data.
A
while (!(UCSRA & (1 << 5))) {}
UDR = data;
B
while (UCSRA & (1 << 5)) {}
UDR = data;
C
if (UCSRA & (1 << 5)) {
  UDR = data;
}
D
while (!(UCSRA & 0x10)) {}
UDR = data;
Attempts:
2 left
💡 Hint
UDRE bit is bit 5 (value 0x20). You want to wait until it is set.
🚀 Application
expert
3:00remaining
How many bytes are transmitted by this UART send function call?
Given the following code, how many bytes will be transmitted when uart_send_string("Hi"); is called?
Embedded C
volatile unsigned char UCSRA = 0x20; // UDRE bit set
volatile unsigned char UDR = 0;

void uart_transmit(unsigned char data) {
    while (!(UCSRA & 0x20)) {}
    UDR = data;
}

void uart_send_string(const char *str) {
    while (*str) {
        uart_transmit(*str++);
    }
}

int main() {
    uart_send_string("Hi");
    return 0;
}
A2 bytes
B3 bytes
C1 byte
D0 bytes
Attempts:
2 left
💡 Hint
Count the characters in the string and how the loop processes them.