0
0
Embedded Cprogramming~5 mins

UART protocol fundamentals in Embedded C

Choose your learning style9 modes available
Introduction

UART helps two devices talk to each other by sending data one bit at a time. It is simple and common for communication.

Connecting a microcontroller to a GPS module to get location data.
Sending sensor readings from a device to a computer for logging.
Communicating between two microcontrollers in a robot.
Debugging code by sending messages from a microcontroller to a PC.
Controlling a device like a motor driver from a microcontroller.
Syntax
Embedded C
void UART_Init(unsigned int baud_rate);
void UART_SendChar(char data);
char UART_ReceiveChar(void);

UART_Init sets up the communication speed (baud rate).

UART_SendChar sends one character at a time.

Examples
Start UART communication at 9600 bits per second.
Embedded C
UART_Init(9600);
Sends the letter 'A' over UART.
Embedded C
UART_SendChar('A');
Waits and reads one character received via UART.
Embedded C
char received = UART_ReceiveChar();
Sample Program

This program sets up UART at 9600 baud, sends 'H' and 'i', then receives a character and prints it.

Embedded C
#include <stdio.h>
#include <stdlib.h>

// Simulated UART functions for demonstration
void UART_Init(unsigned int baud_rate) {
    printf("UART initialized at %u baud\n", baud_rate);
}

void UART_SendChar(char data) {
    printf("Sent: %c\n", data);
}

char UART_ReceiveChar(void) {
    // Simulate receiving character 'X'
    return 'X';
}

int main() {
    UART_Init(9600);
    UART_SendChar('H');
    UART_SendChar('i');
    char received = UART_ReceiveChar();
    printf("Received: %c\n", received);
    return 0;
}
OutputSuccess
Important Notes

UART sends data one bit at a time, so speed and timing must match on both devices.

Common baud rates are 9600, 19200, 38400, 115200.

UART uses start and stop bits to mark the beginning and end of each byte.

Summary

UART is a simple way for devices to send data one bit at a time.

Set baud rate to match both devices for correct communication.

Use UART functions to send and receive characters easily.