Peripheral Programming
Embedded & MCUEmbedded LinuxExplainerIoT HardwaresMicrocontrollersTutorials/DIY

Peripheral Programming (UART, GPIO,ADC, SPI, I2C, etc)

Introduction

Peripheral programming is an essential aspect of embedded systems development, enabling communication between a microcontroller (MCU) and external devices such as sensors, actuators, and memory modules. Understanding key peripheral interfaces like UART, GPIO, ADC, SPI, and I2C is critical for designing efficient and reliable embedded applications.

This article provides an in-depth exploration of peripheral programming, covering key communication protocols, their operation, and implementation strategies.

1. General-Purpose Input/Output (GPIO)

Overview

GPIOs are simple digital pins that can be configured as either inputs or outputs. They are fundamental in interfacing with external components such as LEDs, buttons, and sensors.

Characteristics:

  • Can be configured as input or output.
  • Often have internal pull-up or pull-down resistors.
  • Can generate interrupts for event-driven programming.

GPIO Programming Example (C):

#include <stdint.h>
#define GPIO_PIN 5
#define GPIO_PORT (*(volatile uint32_t *)0x40021000)

void gpio_init() {
    GPIO_PORT |= (1 << GPIO_PIN); // Set pin as output
}

void gpio_toggle() {
    GPIO_PORT ^= (1 << GPIO_PIN); // Toggle pin state
}

Use Cases:

  • LED control.
  • Button press detection.
  • External device triggering.

2. Universal Asynchronous Receiver-Transmitter (UART)

Overview

UART is a widely used serial communication protocol that transmits data asynchronously using two wires: TX (transmit) and RX (receive).

Characteristics:

  • Full-duplex communication.
  • Uses start and stop bits for synchronization.
  • Configurable baud rate (e.g., 9600, 115200 bps).

UART Frame Format:

Start Bit | Data Bits (5-9) | Parity Bit (Optional) | Stop Bit (1 or 2)

UART Programming Example (C – STM32 HAL):

UART_HandleTypeDef huart2;

void uart_init() {
    huart2.Instance = USART2;
    huart2.Init.BaudRate = 9600;
    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;
    HAL_UART_Init(&huart2);
}

void uart_send(char *msg) {
    HAL_UART_Transmit(&huart2, (uint8_t *)msg, strlen(msg), HAL_MAX_DELAY);
}

Use Cases:

  • Debugging via serial consoles.
  • Communication with GPS modules, Bluetooth, and GSM modules.
  • Bootloaders for firmware updates.

3. Analog-to-Digital Converter (ADC)

Overview

An ADC converts analog signals (e.g., sensor outputs) into digital values that a microcontroller can process.

Characteristics:

  • Resolution: 8-bit, 10-bit, 12-bit, or higher.
  • Sampling rate: Determines speed of conversion.
  • Reference voltage: Defines conversion range.

ADC Programming Example (C – STM32 HAL):

ADC_HandleTypeDef hadc1;

void adc_init() {
    hadc1.Instance = ADC1;
    hadc1.Init.Resolution = ADC_RESOLUTION_12B;
    hadc1.Init.ScanConvMode = DISABLE;
    hadc1.Init.ContinuousConvMode = ENABLE;
    hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    HAL_ADC_Init(&hadc1);
}

uint32_t adc_read() {
    HAL_ADC_Start(&hadc1);
    HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
    return HAL_ADC_GetValue(&hadc1);
}

Use Cases:

  • Reading temperature sensors (e.g., LM35, NTC thermistors).
  • Monitoring battery voltage.
  • Processing analog joystick signals.

4. Serial Peripheral Interface (SPI)

Overview

SPI is a high-speed synchronous serial communication protocol used for short-distance communication between microcontrollers and peripherals.

Characteristics:

  • Supports full-duplex data transfer.
  • Uses four lines: MOSI, MISO, SCLK, and CS.
  • Can operate in master or slave mode.

SPI Programming Example (C – STM32 HAL):

SPI_HandleTypeDef hspi1;

void spi_init() {
    hspi1.Instance = SPI1;
    hspi1.Init.Mode = SPI_MODE_MASTER;
    hspi1.Init.Direction = SPI_DIRECTION_2LINES;
    hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
    hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
    hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
    HAL_SPI_Init(&hspi1);
}

void spi_send(uint8_t *data, uint16_t size) {
    HAL_SPI_Transmit(&hspi1, data, size, HAL_MAX_DELAY);
}

Use Cases:

  • Communicating with SD cards and displays.
  • Interface with sensors like MPU6050 (IMU) and BME280 (environmental sensor).
  • Connecting with high-speed memory devices.

5. Inter-Integrated Circuit (I2C)

Overview

I2C is a two-wire serial communication protocol used for connecting multiple slave devices to a single master.

Characteristics:

  • Uses two lines: SDA (data) and SCL (clock).
  • Supports multiple slave devices on a single bus.
  • Provides clock stretching for synchronization.

I2C Programming Example (C – STM32 HAL):

I2C_HandleTypeDef hi2c1;

void i2c_init() {
    hi2c1.Instance = I2C1;
    hi2c1.Init.ClockSpeed = 100000; // 100 kHz standard mode
    hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
    HAL_I2C_Init(&hi2c1);
}

void i2c_write(uint16_t devAddr, uint8_t *data, uint16_t size) {
    HAL_I2C_Master_Transmit(&hi2c1, devAddr, data, size, HAL_MAX_DELAY);
}

Use Cases:

  • Interfacing with EEPROMs and real-time clocks (RTC).
  • Connecting to sensors like MPU6050 (accelerometer/gyroscope).
  • OLED display communication.

Conclusion

Peripheral programming is an essential skill for embedded system developers. By mastering interfaces such as GPIO, UART, ADC, SPI, and I2C, developers can build robust and efficient applications. Selecting the right communication protocol depends on factors like speed, distance, and power consumption.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *