Home
Jingkai Ma 於 2 月之前 修改了此頁面

#include #include "pico/stdlib.h" #include "hardware/i2c.h"

// I2C 地址和 LCD 命令 #define I2C_PORT i2c0 #define LCD_ADDR 0x3C #define LCD_CMD 0x00 #define LCD_DATA 0x40

#define FUNCTION_SET 0x38 #define FUNCTION_SET_RE 0x3A #define FUNCTION_SET_IS 0x39 #define CLEAR_DISPLAY 0x01 #define DISPLAY_ON 0x0C #define DISPLAY_ROW_1 0x80 #define DISPLAY_ROW_2 0xA0 #define DISPLAY_ROW_3 0xC0 #define DISPLAY_ROW_4 0xE0

uint8_t lcd_setting_para[] = {

FUNCTION_SET_RE, 0x09, // Extended Function (4 line mode)
0x12,                 // Bias Setting (BS1 = 1)
FUNCTION_SET_IS, 0x1B, // Internal OSC (BS0 = 1, Bias = 1/6)
0x6E,                 // Follower Control (Devider on, Set Value)
FUNCTION_SET, DISPLAY_ON // Display on/off Control (Cursor off, Blink off)

};

uint8_t lcd_contrast_para[] = {

FUNCTION_SET_IS, 0x54, // Power Control (Booster on) and Contrast (C5, C4)
0x70                  // Contrast (C3 - C0)

};

void lcd_command(uint8_t cmd) {

uint8_t data[2] = {LCD_CMD, cmd};
i2c_write_blocking(I2C_PORT, LCD_ADDR, data, 2, false);

}

void lcd_data(uint8_t data) {

uint8_t buffer[2] = {LCD_DATA, data};
i2c_write_blocking(I2C_PORT, LCD_ADDR, buffer, 2, false);

}

void lcd_init() {

for (size_t i = 0; i < sizeof(lcd_setting_para); i++) {
    lcd_command(lcd_setting_para[i]);
    sleep_ms(10);
}
for (size_t i = 0; i < sizeof(lcd_contrast_para); i++) {
    lcd_command(lcd_contrast_para[i]);
    sleep_ms(10);
}
lcd_command(CLEAR_DISPLAY);

}

void lcd_clear() {

lcd_command(CLEAR_DISPLAY);

}

void lcd_print(const char *text, uint8_t row) {

uint8_t cmd;
switch (row) {
    case 1: cmd = DISPLAY_ROW_1; break;
    case 2: cmd = DISPLAY_ROW_2; break;
    case 3: cmd = DISPLAY_ROW_3; break;
    case 4: cmd = DISPLAY_ROW_4; break;
    default: cmd = DISPLAY_ROW_1; break;
}
lcd_command(cmd);
while (*text) {
    lcd_data(*text++);
}

}

int main() {

// 初始化 STDIO 和 I2C
stdio_init_all();
i2c_init(I2C_PORT, 100 * 1000);
gpio_set_function(4, GPIO_FUNC_I2C);
gpio_set_function(5, GPIO_FUNC_I2C);
gpio_pull_up(4);
gpio_pull_up(5);

// 初始化 LCD
lcd_init();
lcd_print("Hochschule", 1);
lcd_print("Anhalt", 2);

while (1) {
    // 主循环
    tight_loop_contents();
}

return 0;

}