一、接线图
二、实物
三、代码
#include <stdio.h>
#include <string.h>
#include "unity.h"
#include "driver/i2c_master.h"
#include "driver/gpio.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "esp_system.h"
#include "driver/i2c.h"#define SSD1306_LCD_H 128
#define SSD1306_LCD_V 64#define I2C_SDA_GPIO_PIN 1 // 连接 SSD1306 的 SDA
#define I2C_SCL_GPIO_PIN 2 // 连接 SSD1306 的 SCL#define I2C_BUS_PORT 0 // ESP32-S3有两个I2C端口,选择第一个
#define I2C_SSD1306_ADDR 0x3C //SSD1306 I2C 地址#define LCD_SCL_CLOCK_HZ (400 * 1000) // SSD1306 时钟速度 400Kconst uint8_t words [][16] = {
{0x00,0x00,0xF0,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x10,0xF0,0x00,0x00,0x00},
{0x00,0x00,0x0F,0x04,0x04,0x04,0x04,0xFF,0x04,0x04,0x04,0x04,0x0F,0x00,0x00,0x00},/*"中",0*/
{0x00,0xFE,0x02,0x12,0x92,0x92,0x92,0xF2,0x92,0x92,0x92,0x12,0x02,0xFE,0x00,0x00},
{0x00,0xFF,0x40,0x48,0x48,0x48,0x48,0x4F,0x48,0x4A,0x4C,0x48,0x40,0xFF,0x00,0x00},/*"国",1*/
};const uint8_t alpha[][8] ={{0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00},{0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00},//E 0{0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00},{0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00},//S 1{0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00},{0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00},//P 2{0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00},{0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00},//3 3{0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00},{0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},//2 4{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},{0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01},//- 5{0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00},{0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00},//S 6{0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00},{0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00},//3 7};const uint8_t blank[] ={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};void app_main(void)
{ //设置 I2C,使用新版本v2.0 I2C驱动i2c_master_bus_config_t i2c_bus_conf = {.clk_source = I2C_CLK_SRC_DEFAULT,.sda_io_num = I2C_SDA_GPIO_PIN,.scl_io_num = I2C_SCL_GPIO_PIN,.i2c_port = -1,.flags.enable_internal_pullup = 1,};i2c_master_bus_handle_t bus_handle;TEST_ESP_OK(i2c_new_master_bus(&i2c_bus_conf, &bus_handle));//设置 SSD1306esp_lcd_panel_io_handle_t io_handle = NULL;esp_lcd_panel_io_i2c_config_t io_config = {.dev_addr = I2C_SSD1306_ADDR,.scl_speed_hz = LCD_SCL_CLOCK_HZ,.control_phase_bytes = 1, .dc_bit_offset = 6, .lcd_cmd_bits = 8, .lcd_param_bits = 8, };TEST_ESP_OK(esp_lcd_new_panel_io_i2c(bus_handle, &io_config, &io_handle));esp_lcd_panel_handle_t panel_handle = NULL;esp_lcd_panel_dev_config_t panel_config = {.bits_per_pixel = 1,.reset_gpio_num = -1,};TEST_ESP_OK(esp_lcd_new_panel_ssd1306(io_handle, &panel_config, &panel_handle));TEST_ESP_OK(esp_lcd_panel_reset(panel_handle));TEST_ESP_OK(esp_lcd_panel_init(panel_handle));// 打开显示TEST_ESP_OK(esp_lcd_panel_disp_on_off(panel_handle, true));//清屏for (int i = 0; i < 120; i++) {for(int j = 0;j < 56;j++){TEST_ESP_OK(esp_lcd_panel_draw_bitmap(panel_handle, i * 8, j * 8, i * 8 + 8, j * 8 + 8, blank));}}//显示 8 x 8 字母和数字for (int i = 0; i < 8; i++) {for(int j = 0;j < 2;j++){TEST_ESP_OK(esp_lcd_panel_draw_bitmap(panel_handle, i * 8, j * 8, i * 8 + 8, j * 8 + 8, alpha[i*2 + j]));}}//显示 16 x 16 汉字int location_x_start = 90; int location_y_start = 40; for(int x = 0; x < 2; x++){for(int y = 0; y < 2; y++){TEST_ESP_OK(esp_lcd_panel_draw_bitmap(panel_handle, x * 16 + location_x_start, y * 8 + location_y_start , x * 16 + 16 + location_x_start ,y * 8 + 8 + location_y_start, words[x*2+y]));}}}
四、效果