Communication Protocols
Microcontrollers need to talk to sensors, displays, and other microcontrollers. Standard communication protocols make this possible.
[Diagram: Master-Slave connection topologies for SPI and I2C]
UART (Universal Asynchronous Receiver-Transmitter)
Simple, point-to-point communication. Uses two wires: TX (Transmit) and RX (Receive). No clock line is used (asynchronous), so both sides must agree on a baud rate.
SPI (Serial Peripheral Interface)
Synchronous, full-duplex communication. Uses 4 wires: SCK (Clock), MOSI (Master Out Slave In), MISO (Master In Slave Out), and SS (Slave Select). Very fast.
I2C (Inter-Integrated Circuit)
Synchronous, half-duplex, multi-master bus. Uses 2 wires: SDA (Data) and SCL (Clock). Devices have unique addresses. Slower than SPI but requires fewer pins.
Comparison
| Protocol | Wires | Speed | Type |
|---|---|---|---|
| UART | 2 (TX, RX) | Slow | Asynchronous |
| SPI | 4 (SCK, MOSI, MISO, SS) | Fast | Synchronous |
| I2C | 2 (SDA, SCL) | Medium | Synchronous |
Example: UART Transmit
void uart_init(unsigned int baud) {
unsigned int ubrr = F_CPU/16/baud - 1;
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
UCSR0B = (1<// Enable RX and TX
}
void uart_send(unsigned char data) {
while (!(UCSR0A & (1<// Wait for buffer to be empty
UDR0 = data;
}