Microcontroller Communication Protocols: UART, SPI, I2C

Today, let’s talk about the topic of microcontroller communication protocols. When microcontrollers interact with peripherals, there needs to be a common language, right? That’s what communication protocols are for! We will focus on three common protocols: UART, SPI, and I2C, and see how they allow devices to communicate with each other.

UART: The Simplest Serial Communication

What is UART? It stands for Universal Asynchronous Receiver-Transmitter. In simple terms, it is a straightforward point-to-point communication method.

UART communication only requires two wires:

  • One for transmitting data (TX)
  • One for receiving data (RX)

Data is transmitted one bit at a time in a serial manner. Each data frame includes:

  1. Start bit (indicates the beginning of transmission)
  2. Data bits (usually 8 bits)
  3. Optional parity bit
  4. Stop bit (indicates the end of transmission)

Here’s a simple example of sending data using UART:

void UART_SendByte(uint8_t data) {
    while(!(USART1->SR & USART_SR_TXE));  // Wait for the transmit buffer to be empty
    USART1->DR = data;  // Send data
}

This code sends data to the UART data register, and the UART hardware automatically transmits the data according to the protocol requirements.

Friendly reminder: The baud rate for UART communication must be configured to be the same; otherwise, the sender and receiver won’t be able to communicate effectively.

SPI: A Tool for High-Speed Synchronous Communication

What is SPI? It stands for Serial Peripheral Interface. It is much faster than UART and supports full-duplex communication.

SPI communication uses four wires:

  • MOSI (Master Out, Slave In)
  • MISO (Master In, Slave Out)
  • SCK (Clock Signal)
  • SS (Slave Select Signal, used to select the slave to communicate with)

Data transmission in SPI is synchronous, meaning the data transmission is synchronized with the clock signal. The master controls the clock, and the slave follows the rhythm.

Here’s an example of sending data using SPI:

void SPI_SendByte(uint8_t data) {
    while(!(SPI1->SR & SPI_SR_TXE));  // Wait for the transmit buffer to be empty
    SPI1->DR = data;  // Send data
    while(!(SPI1->SR & SPI_SR_RXNE));  // Wait for reception to complete
    (void)SPI1->DR;  // Read the received data (discarded)
}

This code sends data to the SPI data register and then waits for the transmission to complete. Note that SPI is full-duplex, so even if you only send data, you must also read the received data (even if you don’t use it).

I2C: A Great Helper for Multi-Device Communication

What is I2C? It stands for Inter-Integrated Circuit. It supports multiple devices communicating on the same bus and only requires two wires.

I2C uses two wires:

  • SDA (Serial Data Line)
  • SCL (Serial Clock Line)

I2C communication is also synchronous but a bit more complex than SPI. Each communication consists of:

  1. Start condition
  2. 7-bit device address + Read/Write bit
  3. Data transmission
  4. Stop condition

Here’s an example of writing data using I2C:

void I2C_WriteByte(uint8_t address, uint8_t reg, uint8_t data) {
    I2C_Start();  // Send start condition
    I2C_SendAddress(address << 1);  // Send device address (write mode)
    I2C_SendByte(reg);  // Send register address
    I2C_SendByte(data);  // Send data
    I2C_Stop();  // Send stop condition
}

This code demonstrates the complete I2C write operation process. First, it sends the start condition, then the device address, followed by the register address to be written, and finally the data, ending with the stop condition.

Friendly reminder: Each device on the I2C bus has a unique address so that the master can distinguish which device it wants to communicate with.

Alright, today we discussed three common microcontroller communication protocols. UART is simple and direct, SPI is fast, and I2C saves wires while allowing multi-device communication. Which one to use depends on your project requirements. Remember, communication protocols are like the rules of conversation between devices; mastering these rules will enable seamless communication between various devices!

Leave a Comment