How to Send String Over UART in Embedded C: Simple Guide
To send a string over
UART in embedded C, use a loop to send each character one by one through the UART transmit register, waiting for the transmit buffer to be ready before sending the next character. Typically, you write each character to a UART data register and check a status flag to ensure the UART is ready for the next byte.Syntax
Sending a string over UART usually involves a function that loops through each character of the string and sends it using the UART transmit register. You wait for the transmit buffer to be empty before sending the next character to avoid data loss.
Key parts:
char *str: pointer to the string to sendwhile(*str): loop until the string endsUART_TX_REG: UART transmit data registerUART_TX_READY_FLAG: status flag indicating UART is ready to send
c
void UART_SendString(char *str) { while (*str) { while (!(UART_STATUS_REG & UART_TX_READY_FLAG)) { // wait until UART is ready to send } UART_TX_REG = *str; // send one character str++; } }
Example
This example shows how to send the string "Hello UART" over UART using a simple function. It assumes the UART hardware registers and flags are defined for your microcontroller.
c
#include <stdint.h> // Mock UART hardware registers and flags for demonstration volatile uint8_t UART_STATUS_REG = UART_TX_READY_FLAG; // TX ready flag set volatile uint8_t UART_TX_REG; #define UART_TX_READY_FLAG 0x01 void UART_SendString(char *str) { while (*str) { while (!(UART_STATUS_REG & UART_TX_READY_FLAG)) { // wait until UART is ready } UART_TX_REG = *str; // send character str++; } } int main() { char message[] = "Hello UART"; UART_SendString(message); return 0; }
Common Pitfalls
Common mistakes when sending strings over UART include:
- Not waiting for the UART transmit buffer to be ready before sending the next character, causing data loss.
- Forgetting to send the string terminator or not handling null-terminated strings properly.
- Not configuring UART baud rate and settings before sending data.
Example of a wrong approach and the correct fix:
c
// Wrong: Sending characters without checking if UART is ready void UART_SendString_Wrong(char *str) { while (*str) { UART_TX_REG = *str; // may overwrite data if UART not ready str++; } } // Correct: Wait for UART ready flag before sending void UART_SendString_Correct(char *str) { while (*str) { while (!(UART_STATUS_REG & UART_TX_READY_FLAG)) { // wait } UART_TX_REG = *str; str++; } }
Quick Reference
Tips for sending strings over UART in embedded C:
- Always wait for the UART transmit buffer to be ready before sending each byte.
- Use null-terminated strings and loop until the null character.
- Initialize UART hardware (baud rate, frame format) before sending data.
- Consider adding a function to send a newline or carriage return if needed.
Key Takeaways
Send strings by transmitting one character at a time over UART.
Always check UART transmit ready flag before sending each byte.
Use null-terminated strings and loop until the end character.
Initialize UART hardware settings before sending data.
Avoid sending data too fast to prevent buffer overruns.