How to Use Serial Print for Debugging in Embedded C
In embedded C, use
printf or UART functions to send debug messages over a serial connection. Initialize the UART peripheral, then use printf or a custom serial_print function to output text to a serial monitor for debugging.Syntax
To print debug messages via serial in embedded C, you typically use UART functions or printf redirected to UART. The basic steps are:
- Initialize UART: Set baud rate, data bits, stop bits, and enable transmitter.
- Send data: Use a function like
UART_SendChar()orprintf()to send characters. - Wait for transmission: Ensure the UART buffer is ready before sending next byte.
c
void UART_Init(void) { // Configure UART registers for baud rate, frame format // Enable UART transmitter } void UART_SendChar(char c) { while (!(UART_STATUS_REGISTER & TX_READY_FLAG)); // Wait until ready UART_DATA_REGISTER = c; // Send character } void serial_print(const char *str) { while (*str) { UART_SendChar(*str++); } }
Example
This example shows how to initialize UART and send a debug message using a simple serial_print function. It demonstrates printing a string over serial for debugging.
c
#include <stdint.h> #define UART_STATUS_REGISTER (*(volatile uint8_t*)0x4000) #define UART_DATA_REGISTER (*(volatile uint8_t*)0x4001) #define TX_READY_FLAG 0x20 void UART_Init(void) { // Example: configure UART for 9600 baud, 8N1 // This is hardware-specific and simplified here } void UART_SendChar(char c) { while (!(UART_STATUS_REGISTER & TX_READY_FLAG)); UART_DATA_REGISTER = c; } void serial_print(const char *str) { while (*str) { UART_SendChar(*str++); } } int main(void) { UART_Init(); serial_print("Debug: System started\r\n"); while (1) { // Main loop } return 0; }
Output
Debug: System started
Common Pitfalls
Common mistakes when using serial print for debugging include:
- Not initializing UART before sending data, causing no output.
- Forgetting to wait for UART transmit buffer to be ready, which can corrupt data.
- Using
printfwithout redirecting it to UART, so output goes nowhere. - Not adding line endings (
\r\n) which can make output hard to read on some terminals.
c
/* Wrong: Sending without UART init or waiting */ void UART_SendChar_Wrong(char c) { UART_DATA_REGISTER = c; // No wait, may lose data } /* Right: Wait for ready before sending */ void UART_SendChar_Right(char c) { while (!(UART_STATUS_REGISTER & TX_READY_FLAG)); UART_DATA_REGISTER = c; }
Quick Reference
Tips for effective serial debugging in embedded C:
- Always initialize UART before printing.
- Use a helper function like
serial_printto send strings. - Wait for UART transmit buffer ready before sending each byte.
- Include
\r\nfor new lines to improve readability. - Redirect
printfto UART if available for easier debugging.
Key Takeaways
Initialize UART properly before using serial print for debugging.
Use a function that waits for UART transmit buffer before sending each character.
Include carriage return and newline characters for clear output formatting.
Redirect printf to UART if your platform supports it for easier debug prints.
Avoid sending data without checking UART readiness to prevent data loss.