STM32 HAL: I2C LCD, its own driver
On the Black Pill, HAL_I2C_Master_Transmit() drives a 16x2 LCD through a driver in lcd.c and lcd.h.

The code
/* An I2C LCD on the Black Pill (STM32F411), with the HAL's I2C driver: I2C1 on PB6
(SCL) and PB7 (SDA) at 100 kHz, an LCD driver of its own in lcd.c and lcd.h, and
the 25 MHz crystal through the PLL to 100 MHz. HAL_I2C_IsDeviceReady() finds the
backpack first. */
#include "main.h"
#include <stdio.h>
#include "lcd.h"
I2C_HandleTypeDef hi2c1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
if (HAL_I2C_IsDeviceReady(&hi2c1, LCD_ADDRESS, 3, 100) != HAL_OK)
{
printf("No LCD at 0x%02X: check SDA, SCL and its power\n", LCD_ADDRESS >> 1);
Error_Handler();
}
printf("LCD found at 0x%02X\n", LCD_ADDRESS >> 1);
lcd_init(&hi2c1);
char line[17];
snprintf(line, sizeof line, "F411 at %luMHz", SystemCoreClock / 1000000);
lcd_print_at(0, 0, line);
/* USER CODE END 2 */
while (1)
{
/* USER CODE BEGIN 3 */
snprintf(line, sizeof line, "Up %lu s ", HAL_GetTick() / 1000);
lcd_print_at(0, 1, line);
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
HAL_Delay(500);
}
/* USER CODE END 3 */
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* HSE 25 MHz / 25 x 200 / 2 = 100 MHz */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 200;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}
}
static void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
}
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
#include "stm32f4xx_hal.h"
void Error_Handler(void);
/* Private defines */
#define LED_Pin GPIO_PIN_13
#define LED_GPIO_Port GPIOC
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
/* A 16x2 character LCD on a PCF8574 I2C backpack, for the STM32 HAL's I2C driver. */
#ifndef LCD_H
#define LCD_H
#include "main.h"
/* The HAL takes the address shifted left: 0x27 is 0x4E. */
#define LCD_ADDRESS (0x27 << 1)
void lcd_init(I2C_HandleTypeDef *hi2c);
void lcd_clear(void);
void lcd_print_at(uint8_t col, uint8_t row, const char *text);
#endif
/* The PCF8574 backpack: P0 RS, P1 RW, P2 E, P3 backlight, P4-P7 D4-D7. The LCD runs in
4-bit mode, each half byte latched by a pulse on E. */
#include "lcd.h"
#define RS 0x01
#define EN 0x04
#define BACKLIGHT 0x08
static I2C_HandleTypeDef *bus;
static void send_half(uint8_t half, uint8_t mode)
{
uint8_t data = (uint8_t)((half & 0xF0) | BACKLIGHT | mode);
uint8_t pulse[2] = {(uint8_t)(data | EN), data};
HAL_I2C_Master_Transmit(bus, LCD_ADDRESS, pulse, 2, 10);
}
static void send(uint8_t value, uint8_t mode)
{
send_half(value & 0xF0, mode);
send_half((uint8_t)(value << 4), mode);
}
void lcd_init(I2C_HandleTypeDef *hi2c)
{
bus = hi2c;
HAL_Delay(50);
send_half(0x30, 0);
HAL_Delay(5);
send_half(0x30, 0);
HAL_Delay(1);
send_half(0x30, 0);
send_half(0x20, 0); /* 4-bit */
send(0x28, 0); /* two lines, 5x8 dots */
send(0x0C, 0); /* display on, no cursor */
send(0x06, 0); /* the cursor moves right */
lcd_clear();
}
void lcd_clear(void)
{
send(0x01, 0);
HAL_Delay(2);
}
void lcd_print_at(uint8_t col, uint8_t row, const char *text)
{
send((uint8_t)(0x80 | (col + (row ? 0x40 : 0x00))), 0);
while (*text) send((uint8_t)*text++, RS);
}
/* Interrupt handlers: the Cortex-M's own, then the peripherals'. */
#include "main.h"
#include "stm32f4xx_it.h"
void NMI_Handler(void) { while (1) {} }
void HardFault_Handler(void) { while (1) {} }
void MemManage_Handler(void) { while (1) {} }
void BusFault_Handler(void) { while (1) {} }
void UsageFault_Handler(void) { while (1) {} }
void SVC_Handler(void) {}
void DebugMon_Handler(void) {}
void PendSV_Handler(void) {}
void SysTick_Handler(void)
{
HAL_IncTick();
}
#ifndef __STM32F4xx_IT_H
#define __STM32F4xx_IT_H
void NMI_Handler(void);
void HardFault_Handler(void);
void SysTick_Handler(void);
#endif
/* Each peripheral's pins and clocks, as CubeMX writes them. */
#include "main.h"
void HAL_MspInit(void)
{
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
}
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if (hi2c->Instance == I2C1)
{
__HAL_RCC_GPIOB_CLK_ENABLE();
/* PB6 I2C1_SCL, PB7 I2C1_SDA */
GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
__HAL_RCC_I2C1_CLK_ENABLE();
}
}
More like this

Nucleo-F401RE: knob on an LCD
An I2C LCD on D14/D15 shows the knob on A0 in volts; LD2 blinks by its STM32 name, PA5.
ArduinoSTM32 Nucleo-F401RE

Weather station
A DHT22 read every 2 s, shown on an I2C LCD and the serial port.
ArduinoArduino Uno

ESP32-H2: LCD, no Wi-Fi
The H2 has BLE and Thread, no Wi-Fi: an I2C LCD on SDA 12, SCL 22 counts up.
ArduinoESP32-H2 DevKitM-1