0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Interface PC with Microcontroller Using UART in Embedded C

To interface a PC with a microcontroller using UART in Embedded C, configure the UART peripheral on the microcontroller with matching baud rate and settings, then use UART transmit and receive functions to send and receive data. On the PC side, use a serial terminal or program to communicate via the COM port matching the microcontroller's UART settings.
📐

Syntax

Basic UART interface involves initializing UART with baud rate, data bits, stop bits, and parity, then using transmit and receive functions to send or get data.

  • UART_Init(baudrate): Sets up UART speed and frame format.
  • UART_Transmit(data): Sends one byte over UART.
  • UART_Receive(): Waits and reads one byte from UART.
c
void UART_Init(unsigned int baudrate);
void UART_Transmit(unsigned char data);
unsigned char UART_Receive(void);
💻

Example

This example shows how to initialize UART at 9600 baud, send a string to the PC, and echo back any received characters.

c
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1

void UART_Init(unsigned int ubrr) {
    UBRR0H = (unsigned char)(ubrr>>8);
    UBRR0L = (unsigned char)ubrr;
    UCSR0B = (1<<RXEN0) | (1<<TXEN0); // Enable RX and TX
    UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); // 8-bit data
}

void UART_Transmit(unsigned char data) {
    while (!(UCSR0A & (1<<UDRE0))) ; // Wait for empty buffer
    UDR0 = data;
}

unsigned char UART_Receive(void) {
    while (!(UCSR0A & (1<<RXC0))) ; // Wait for data
    return UDR0;
}

void UART_SendString(const char* str) {
    while (*str) {
        UART_Transmit(*str++);
    }
}

int main(void) {
    UART_Init(MYUBRR);
    UART_SendString("UART Interface Ready\r\n");
    while (1) {
        unsigned char received = UART_Receive();
        UART_Transmit(received); // Echo back
    }
    return 0;
}
Output
UART Interface Ready [Characters typed on PC are echoed back]
⚠️

Common Pitfalls

Common mistakes include:

  • Mismatch in baud rate between PC and microcontroller causes garbled data.
  • Not enabling UART transmitter or receiver bits in control registers.
  • Ignoring hardware flow control if required by the PC or device.
  • Not waiting for transmit buffer to be empty before sending data.
  • Forgetting to configure UART frame format (data bits, parity, stop bits) consistently on both ends.
c
/* Wrong: Not waiting for buffer empty before transmit */
void UART_Transmit_Wrong(unsigned char data) {
    UDR0 = data; // May overwrite data if buffer not ready
}

/* Correct: Wait for buffer empty */
void UART_Transmit_Correct(unsigned char data) {
    while (!(UCSR0A & (1<<UDRE0))) ;
    UDR0 = data;
}
📊

Quick Reference

Tips for UART PC-Microcontroller communication:

  • Match baud rate and frame format on both sides.
  • Use a USB-to-serial adapter if PC lacks RS232 port.
  • Use terminal software like PuTTY or Tera Term on PC.
  • Test with simple echo programs before complex protocols.
  • Check wiring: TX of microcontroller to RX of PC adapter and vice versa.

Key Takeaways

Initialize UART with matching baud rate and frame format on microcontroller and PC.
Always wait for UART transmit buffer to be ready before sending data.
Use terminal software on PC to send and receive UART data.
Connect microcontroller TX to PC RX and microcontroller RX to PC TX correctly.
Test communication with simple echo programs to verify setup.