0
0
Embedded Cprogramming~10 mins

Printf debugging over UART 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 send a character over UART using printf.

Embedded C
#include <stdio.h>

int main() {
    char c = 'A';
    printf([1]);
    return 0;
}
Drag options to blanks, or click blank then click option'
A"%c", c
B"%d", c
C"%s", c
D"%f", c
Attempts:
3 left
💡 Hint
Common Mistakes
Using %d prints the ASCII code, not the character.
Using %s expects a string, not a character.
2fill in blank
medium

Complete the code to initialize UART for printf debugging.

Embedded C
void UART_Init() {
    // Set baud rate
    UBRR0 = [1];
    // Enable transmitter
    UCSR0B = (1 << TXEN0);
    // Set frame format: 8 data bits, 1 stop bit
    UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}
Drag options to blanks, or click blank then click option'
A8
B9600
C16
D103
Attempts:
3 left
💡 Hint
Common Mistakes
Setting UBRR0 directly to baud rate value causes wrong speed.
Confusing baud rate with register value.
3fill in blank
hard

Fix the error in the UART transmit function to send a character correctly.

Embedded C
void UART_Transmit(char data) {
    while (!(UCSR0A & (1 << [1]))) {
        ; // wait for empty transmit buffer
    }
    UDR0 = data;
}
Drag options to blanks, or click blank then click option'
AUDRE0
BTXC0
CRXC0
DRXEN0
Attempts:
3 left
💡 Hint
Common Mistakes
Waiting for RXC0 waits for receive complete, not transmit buffer.
Using TXC0 waits for transmit complete, not buffer empty.
4fill in blank
hard

Fill both blanks to print a number in decimal and padded uppercase hex using printf for debugging.

Embedded C
int val = 255;
 printf("val = %d (0x%02[1])\n", val, [2]);
Drag options to blanks, or click blank then click option'
AX
Bval
Cx
D255
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'x' prints lowercase hex letters.
Passing literal value '255' instead of variable 'val'.
5fill in blank
hard

Fill all three blanks to create a FILE stream that redirects printf to UART.

Embedded C
[1] uartout = FDEV_SETUP_STREAM(UART_Transmit, NULL, [2]);

int main() {
    [3] = &uartout;
    printf("Debug: UART printf ready!\n");
    return 0;
}
Drag options to blanks, or click blank then click option'
Astatic FILE
B_FDEV_SETUP_WRITE
Cstdout
Dstdin
Attempts:
3 left
💡 Hint
Common Mistakes
Using stdin instead of stdout redirects input, not output.
Missing 'static' or wrong type for the stream declaration.