ESP-IDFESP32-S3 DevKitC-1Lab virtual

ESP-IDF: I2C LCD, two files

The I2C master driver and an LCD driver of its own (lcd1602.c, lcd1602.h) on the S3.

ESP-IDF: I2C LCD, two files in Argynix Lab

The code

main.cC
/* An I2C LCD with ESP-IDF's I2C master driver (v5.2 and later): the bus on GPIO8 (SDA)
   and GPIO9 (SCL), the display at 0x27, its driver in lcd1602.c. A task counts up. */
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c_master.h"
#include "esp_log.h"
#include "lcd1602.h"

static const char *TAG = "lcd";

void app_main(void)
{
    i2c_master_bus_config_t bus_config = {
        .i2c_port = I2C_NUM_0,
        .sda_io_num = GPIO_NUM_8,
        .scl_io_num = GPIO_NUM_9,
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = true,
    };
    i2c_master_bus_handle_t bus;
    ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &bus));
    ESP_ERROR_CHECK(i2c_master_probe(bus, 0x27, 100));
    ESP_LOGI(TAG, "LCD found at 0x27");

    lcd1602_t lcd;
    ESP_ERROR_CHECK(lcd1602_init(&lcd, bus, 0x27));
    lcd1602_print(&lcd, "ESP-IDF on S3");

    for (int seconds = 0;; seconds++) {
        char line[17];
        snprintf(line, sizeof line, "Uptime %5d s", seconds);
        lcd1602_move(&lcd, 0, 1);
        lcd1602_print(&lcd, line);
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}
lcd1602.hC
/* A 16x2 character LCD on a PCF8574 I2C backpack, for ESP-IDF's I2C master driver. */
#pragma once
#include "driver/i2c_master.h"

typedef struct {
    i2c_master_dev_handle_t dev;
} lcd1602_t;

esp_err_t lcd1602_init(lcd1602_t *lcd, i2c_master_bus_handle_t bus, uint16_t address);
void lcd1602_move(lcd1602_t *lcd, int col, int row);
void lcd1602_print(lcd1602_t *lcd, const char *text);
lcd1602.cC
/* The PCF8574 backpack: P0 RS, P1 RW, P2 E, P3 backlight, P4-P7 D4-D7. The LCD runs in
   4-bit mode; each nibble is latched by pulsing E. */
#include "lcd1602.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_rom_sys.h"

#define RS 0x01
#define EN 0x04
#define BACKLIGHT 0x08

static esp_err_t nibble(lcd1602_t *lcd, uint8_t bits, uint8_t mode)
{
    uint8_t pulse[2] = { bits | mode | EN | BACKLIGHT, bits | mode | BACKLIGHT };
    return i2c_master_transmit(lcd->dev, pulse, sizeof pulse, 100);
}

static esp_err_t send(lcd1602_t *lcd, uint8_t value, uint8_t mode)
{
    esp_err_t err = nibble(lcd, value & 0xF0, mode);
    if (err == ESP_OK) err = nibble(lcd, (value << 4) & 0xF0, mode);
    return err;
}

esp_err_t lcd1602_init(lcd1602_t *lcd, i2c_master_bus_handle_t bus, uint16_t address)
{
    i2c_device_config_t cfg = {
        .dev_addr_length = I2C_ADDR_BIT_LEN_7,
        .device_address = address,
        .scl_speed_hz = 100000,
    };
    esp_err_t err = i2c_master_bus_add_device(bus, &cfg, &lcd->dev);
    if (err != ESP_OK) return err;
    vTaskDelay(pdMS_TO_TICKS(50));
    nibble(lcd, 0x30, 0);
    vTaskDelay(pdMS_TO_TICKS(5));
    nibble(lcd, 0x30, 0);
    esp_rom_delay_us(150);
    nibble(lcd, 0x30, 0);
    nibble(lcd, 0x20, 0); /* 4-bit mode */
    send(lcd, 0x28, 0);   /* two lines, 5x8 */
    send(lcd, 0x0C, 0);   /* display on */
    send(lcd, 0x01, 0);   /* clear */
    vTaskDelay(pdMS_TO_TICKS(2));
    return send(lcd, 0x06, 0); /* left to right */
}

void lcd1602_move(lcd1602_t *lcd, int col, int row)
{
    send(lcd, 0x80 | (col + (row ? 0x40 : 0)), 0);
}

void lcd1602_print(lcd1602_t *lcd, const char *text)
{
    while (*text) send(lcd, (uint8_t)*text++, RS);
}