What is UART in microcontroller?

UART = Universal Asynchronous Receiver/Transmitter. It’s the hardware block inside a microcontroller that turns bytes in memory into a serial bitstream on a pin (TX) and turns a bitstream on another pin (RX) back into bytes—without a shared clock line.



What it does (in plain terms)

  • Sends: shifts out bits LSB-first with a start bit (low), data bits (usually 8), optional parity, then stop bit(s) (high).

  • Receives: watches RX for a falling edge (start), samples the line (often 16× oversampling), reconstructs the byte, checks parity/stop, raises flags/interrupts.

Idle=1 ──┐ Start Data bits (LSB→MSB) Parity Stop └─0─────b0─b1─b2─b3─b4─b5─b6─b7─(P)──────1──

Typical pins & wiring

  • TX (transmit), RX (receive), GND (ground). Cross them: TX ↔ RX and share ground.

  • Optional RTS/CTS for hardware flow control.

  • Some MCUs support single-wire half-duplex (TX/RX on one pin).

Electrical levels (important!)

  • TTL/CMOS UART: 0–3.3 V or 0–5 V logic. For short PCB/short cable only.

  • RS-232: ±3…±12 V and inverted logic → needs a level shifter (e.g., MAX232).

  • RS-485/422: differential, long cables, multi-drop → needs an RS-485/422 transceiver.

Key configuration terms

  • Baud rate: bits/s (9 600, 115 200, etc.). Both sides must match closely.

  • Frame: data bits (7/8/9), parity (None/Even/Odd), stop bits (1/2).

  • Flow control: none, RTS/CTS (hardware), or XON/XOFF (software).

  • Errors you may see: framing (bad stop bit), parity, overrun (RX FIFO full).

Common uses

  • Debug logs/printf, bootloaders, firmware updates.

  • Modules: GPS/GNSS, Bluetooth (HC-05/06), Wi-Fi/Cell modems, motor drives, sensors.

  • Inter-MCU communication when speed/length is moderate.

UART vs. USART (and vs. SPI/I²C)

  • UART: asynchronous (no clock pin).

  • USART: can do UART and synchronous serial (with a clock pin).

  • SPI/I²C: synchronous buses with shared clock (higher speed, multi-device support).

Minimal code snippets

Arduino (ATmega328P, 115200-8N1)

void setup() { Serial.begin(115200); // 8 data, No parity, 1 stop by default Serial.println("Hello UART"); } void loop() { if (Serial.available()) { int c = Serial.read(); Serial.write(c); // echo } }

STM32 HAL (e.g., USART2 on 115200-8N1)

UART_HandleTypeDef huart2; void MX_USART2_UART_Init(void) { huart2.Instance = USART2; huart2.Init.BaudRate = 115200; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; HAL_UART_Init(&huart2); } void app(void){ const char *msg = "Hello\r\n"; HAL_UART_Transmit(&huart2,(uint8_t*)msg,strlen(msg),100); }

Practical tips

  • Match baud/parity/stop on both ends; start with 115200-8N1 if unsure.

  • Share ground; cross TX/RX; avoid long flying leads at TTL levels.

  • For long/noisy runs use RS-485 with twisted pair and termination.

  • Use interrupts or DMA and a ring buffer for reliable reception at higher baud.

  • If text looks garbled, check baud mismatch, inverted logic, or wrong voltage level.

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How do you set up ADC (Analog-to-Digital Converter) in STM32?

How To Connect Stm32 To PC?