Lcd初始化后全屏为花屏,必须再把整个屏幕转成全底白色消除花屏后再显示图片,字符。
我理解为什么是花屏,因为只是初始化各个参数,显示内存现在还是为空,还没有执行0x2c命令。
图片
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "string.h"
#define PIN_NUM_MOSI 12
#define PIN_NUM_CLK 13
#define PIN_NUM_CS 5
#define PIN_NUM_DC 19
#define PIN_NUM_RST 18
spi_device_handle_t spi;
void spi_init() {
spi_bus_config_t buscfg = {
.mosi_io_num = PIN_NUM_MOSI,
.miso_io_num = -1,
.sclk_io_num = PIN_NUM_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4096,
};
spi_bus_initialize(HSPI_HOST, &buscfg, 1);
spi_device_interface_config_t devcfg = {
.clock_speed_hz = 25 * 1000 * 1000, // 10 MHz
.mode = 0, // SPI mode 0
.spics_io_num = PIN_NUM_CS, // CS pin
.queue_size = 7,
};
spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
}
void st7735_send_cmd(uint8_t cmd) {
gpio_set_level(PIN_NUM_DC, 0); // Command mode
spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.length = 8;
t.tx_buffer = &cmd;
spi_device_transmit(spi, &t);
}
void st7735_send_data(uint8_t data) {
gpio_set_level(PIN_NUM_DC, 1); // Data mode
spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.length = 8;
t.tx_buffer = &data;
spi_device_transmit(spi, &t);
}
void st7735_reset() {
gpio_set_level(PIN_NUM_RST, 0);
vTaskDelay(1 / portTICK_PERIOD_MS);
gpio_set_level(PIN_NUM_RST, 1);
vTaskDelay(200 / portTICK_PERIOD_MS);
}
void st7735_init() {
// Reset the display
st7735_reset();
// Initialization commands for ST7735S
st7735_send_cmd(0x01); // Software reset
vTaskDelay(150 / portTICK_PERIOD_MS);
st7735_send_cmd(0x11); // Sleep out
vTaskDelay(120 / portTICK_PERIOD_MS);
st7735_send_cmd(0xB1);
st7735_send_data(0x01);
st7735_send_data(0x2c);
st7735_send_data(0x2D);
st7735_send_cmd(0xB2);
st7735_send_data(0x01);
st7735_send_data(0x2c);
st7735_send_data(0x2D);
st7735_send_cmd(0xB3);
st7735_send_data(0x01);
st7735_send_data(0x2c);
st7735_send_data(0x2D);
st7735_send_data(0x01);
st7735_send_data(0x2c);
st7735_send_data(0x2D);
st7735_send_cmd(0xB4);
st7735_send_data(0x00);
st7735_send_cmd(0xc0);
st7735_send_data(0xa2);
st7735_send_data(0x02);
st7735_send_data(0x84);
st7735_send_cmd(0xc1);
st7735_send_data(0xc5);
st7735_send_cmd(0xc2);
st7735_send_data(0x0d);
st7735_send_cmd(0xc3);
st7735_send_data(0x8d);
st7735_send_data(0x2a);
st7735_send_cmd(0xc4);
st7735_send_data(0x8d);
st7735_send_data(0xee);
st7735_send_cmd(0xc5);
st7735_send_data(0x0e);
st7735_send_cmd(0x36);
st7735_send_data(0x00);
st7735_send_cmd(0xe0);
st7735_send_data(0x0f);
st7735_send_data(0x1a);
st7735_send_data(0x0f);
st7735_send_data(0x18);
st7735_send_data(0x2f);
st7735_send_data(0x28);
st7735_send_data(0x20);
st7735_send_data(0x22);
st7735_send_data(0x1f);
st7735_send_data(0x1b);
st7735_send_data(0x23);
st7735_send_data(0x37);
st7735_send_data(0x00);
st7735_send_data(0x07);
st7735_send_data(0x02);
st7735_send_data(0x10);
st7735_send_cmd(0xe1);
st7735_send_data(0x0f);
st7735_send_data(0x1b);
st7735_send_data(0x0f);
st7735_send_data(0x17);
st7735_send_data(0x33);
st7735_send_data(0x2c);
st7735_send_data(0x29);
st7735_send_data(0x2e);
st7735_send_data(0x30);
st7735_send_data(0x30);
st7735_send_data(0x39);
st7735_send_data(0x3f);
st7735_send_data(0x00);
st7735_send_data(0x07);
st7735_send_data(0x03);
st7735_send_data(0x10);
st7735_send_cmd(0x2a);
st7735_send_data(0x00);
st7735_send_data(0x00);
st7735_send_data(0x00);
st7735_send_data(127);
st7735_send_cmd(0x2b);
st7735_send_data(0x00);
st7735_send_data(0x00);
st7735_send_data(0x00);
st7735_send_data(160);
st7735_send_cmd(0xf0);
st7735_send_data(0x01);
st7735_send_cmd(0xf6);
st7735_send_data(0x00);
st7735_send_cmd(0x3a);
st7735_send_data(0x05);
st7735_send_cmd(0x29); // Display on
vTaskDelay(100 / portTICK_PERIOD_MS);
}
void qp(uint8_t xs, uint8_t xe,uint8_t ys,uint8_t ye) {
// Set column address
st7735_send_cmd(0x2A);
st7735_send_data(0x00);
st7735_send_data(xs);
st7735_send_data(0x00);
st7735_send_data(xe);
// Set row address
st7735_send_cmd(0x2B);
st7735_send_data(0x00);
st7735_send_data(ys);
st7735_send_data(0x00);
st7735_send_data(ye);
// Write memory
st7735_send_cmd(0x2C);
}
void app_main() {
gpio_set_direction(PIN_NUM_DC, GPIO_MODE_OUTPUT);
gpio_set_direction(PIN_NUM_RST, GPIO_MODE_OUTPUT);
spi_init();
st7735_init(); //lcd 初始化后是花屏
qp(0,127,0,159); // lcd 全屏刷成白屏
for (uint16_t a = 0; a < 159; a++) {
for(uint8_t b=0;b<127;b++){
st7735_send_data(0xff);
st7735_send_data(0xff);
}
}
qp(0,10,0,10); // 显示10*10 的红色
for (uint16_t a = 0; a < 10; a++) {
for(uint8_t b=0;b<10;b++){
st7735_send_data(0xf8);
st7735_send_data(0x00);
}
}
}