#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c_master.h"
#include "esp_err.h"
#include "esp_log.h"
// OLED I2C設定
#define OLED_I2C_PORT I2C_NUM_0
#define OLED_SDA_PIN 12
#define OLED_SCL_PIN 14
#define OLED_I2C_ADDR 0x3C
#define OLED_I2C_HZ 100000
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
static const char *TAG = "OLED";
static i2c_master_bus_handle_t i2c_bus;
static i2c_master_dev_handle_t oled_dev;
// 5x7字型,只保留 "hello world!" 需要的字元
typedef struct {
char character;
uint8_t data[5];
} font_char_t;
static const font_char_t font[] = {
{' ', {0x00, 0x00, 0x00, 0x00, 0x00}},
{'!', {0x00, 0x00, 0x5F, 0x00, 0x00}},
{'d', {0x38, 0x44, 0x44, 0x48, 0x7F}},
{'e', {0x38, 0x54, 0x54, 0x54, 0x18}},
{'h', {0x7F, 0x08, 0x04, 0x04, 0x78}},
{'l', {0x00, 0x41, 0x7F, 0x40, 0x00}},
{'o', {0x38, 0x44, 0x44, 0x44, 0x38}},
{'r', {0x7C, 0x08, 0x04, 0x04, 0x08}},
{'w', {0x3C, 0x40, 0x30, 0x40, 0x3C}},
};
// 傳送SSD1306命令
static esp_err_t oled_write_command(uint8_t command)
{
uint8_t buffer[2] = {
0x00, // Control byte:後續資料為命令
command
};
return i2c_master_transmit(oled_dev, buffer,
sizeof(buffer), 1000);
}
// 傳送顯示資料
static esp_err_t oled_write_data(const uint8_t *data, size_t length)
{
// 第一個byte為Control byte,後面才是顯示資料
uint8_t buffer[OLED_WIDTH + 1];
if (length > OLED_WIDTH) {
return ESP_ERR_INVALID_SIZE;
}
buffer[0] = 0x40; // Control byte:後續資料為顯示資料
memcpy(&buffer[1], data, length);
return i2c_master_transmit(oled_dev, buffer,
length + 1, 1000);
}
// 初始化I2C
static void oled_i2c_init(void)
{
i2c_master_bus_config_t bus_config = {
.i2c_port = OLED_I2C_PORT,
.sda_io_num = OLED_SDA_PIN,
.scl_io_num = OLED_SCL_PIN,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
// 啟用ESP32內部上拉
.flags.enable_internal_pullup = true,
};
ESP_ERROR_CHECK(
i2c_new_master_bus(&bus_config, &i2c_bus)
);
i2c_device_config_t device_config = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = OLED_I2C_ADDR,
.scl_speed_hz = OLED_I2C_HZ,
};
ESP_ERROR_CHECK(
i2c_master_bus_add_device(i2c_bus,
&device_config,
&oled_dev)
);
}
// 初始化SSD1306
static void oled_init(void)
{
const uint8_t init_commands[] = {
0xAE, // 關閉顯示
0xD5, 0x80, // 設定顯示時鐘
0xA8, 0x3F, // Multiplex ratio:64列
0xD3, 0x00, // 顯示位移
0x40, // 顯示起始列
0x8D, 0x14, // 啟用Charge Pump
0x20, 0x02, // Page addressing mode
0xA1, // 水平翻轉
0xC8, // 垂直翻轉
0xDA, 0x12, // COM硬體設定
0x81, 0x7F, // 對比度
0xD9, 0xF1, // Pre-charge period
0xDB, 0x40, // VCOMH deselect level
0xA4, // 顯示RAM內容
0xA6, // 正常顯示,非反白
0xAF // 開啟顯示
};
for (size_t i = 0; i < sizeof(init_commands); i++) {
ESP_ERROR_CHECK(oled_write_command(init_commands[i]));
}
}
// 清除整個OLED
static void oled_clear(void)
{
uint8_t empty_line[OLED_WIDTH] = {0};
for (uint8_t page = 0; page < 8; page++) {
ESP_ERROR_CHECK(oled_write_command(0xB0 + page));
ESP_ERROR_CHECK(oled_write_command(0x00));
ESP_ERROR_CHECK(oled_write_command(0x10));
ESP_ERROR_CHECK(oled_write_data(empty_line, OLED_WIDTH));
}
}
// 設定文字位置
static void oled_set_cursor(uint8_t column, uint8_t page)
{
ESP_ERROR_CHECK(oled_write_command(0xB0 + page));
ESP_ERROR_CHECK(oled_write_command(0x00 | (column & 0x0F)));
ESP_ERROR_CHECK(oled_write_command(0x10 | (column >> 4)));
}
// 查找字型資料
static const uint8_t *font_get(char character)
{
for (size_t i = 0; i < sizeof(font) / sizeof(font[0]); i++) {
if (font[i].character == character) {
return font[i].data;
}
}
return font[0].data; // 找不到時顯示空白
}
// 顯示字串
static void oled_write_string(const char *text)
{
uint8_t character_data[6];
while (*text != '\0') {
const uint8_t *glyph = font_get(*text);
memcpy(character_data, glyph, 5);
character_data[5] = 0x00; // 字元間隔一個像素
ESP_ERROR_CHECK(
oled_write_data(character_data,
sizeof(character_data))
);
text++;
}
}
void app_main(void)
{
ESP_LOGI(TAG, "初始化I2C");
oled_i2c_init();
ESP_LOGI(TAG, "初始化SSD1306 OLED");
oled_init();
// 清除舊畫面
oled_clear();
/*
* "hello world!"共有12個字元。
* 每個字元寬6像素,因此總寬度為:
* 12 × 6 = 72像素
*
* 水平置中的起始位置:
* (128 - 72) / 2 = 28
*
* Page 3大約位於64像素高度的中央。
*/
oled_set_cursor(28, 3);
oled_write_string("hello world!");
ESP_LOGI(TAG, "已顯示:hello world!");
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306