We send data one byte at a time using UART to communicate between devices like microcontrollers and computers.
Transmitting a byte over UART in Embedded C
void UART_Transmit(unsigned char data) {
while (!(UART_STATUS_REGISTER & TRANSMIT_READY_FLAG)) {
// wait until UART is ready to send
}
UART_DATA_REGISTER = data;
}The function waits until the UART hardware is ready before sending the byte.
Replace UART_STATUS_REGISTER, TRANSMIT_READY_FLAG, and UART_DATA_REGISTER with your device's specific register names.
UART_Transmit('A');unsigned char value = 0x55;
UART_Transmit(value);This program simulates sending two characters 'H' and 'i' over UART. It waits for UART to be ready before sending each byte.
#include <stdint.h> #include <stdbool.h> // Mock UART registers for example volatile uint8_t UART_STATUS_REGISTER = 0x02; // Bit 1 means ready volatile uint8_t UART_DATA_REGISTER = 0; #define TRANSMIT_READY_FLAG 0x02 void UART_Transmit(uint8_t data) { while (!(UART_STATUS_REGISTER & TRANSMIT_READY_FLAG)) { // wait until UART is ready } UART_DATA_REGISTER = data; // Simulate UART busy after sending UART_STATUS_REGISTER &= ~TRANSMIT_READY_FLAG; } int main() { // Simulate UART ready UART_STATUS_REGISTER = TRANSMIT_READY_FLAG; UART_Transmit('H'); UART_STATUS_REGISTER = TRANSMIT_READY_FLAG; // Ready again UART_Transmit('i'); // For demonstration, print what was sent // In real embedded, you can't print like this // Here we just show the last byte sent return UART_DATA_REGISTER; }
UART transmission is usually slow compared to CPU speed, so waiting for ready is important.
Registers and flags differ by microcontroller, always check your device datasheet.
In real embedded systems, you often use interrupts or DMA for efficient UART transmission.
UART sends data one byte at a time using hardware registers.
Always wait until UART is ready before sending a byte.
Use your microcontroller's datasheet to find correct register names and flags.