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.
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)
STM32 HAL (e.g., USART2 on 115200-8N1)
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.
评论
发表评论