Ask ESP32 S3 SPI driver screen problem

15207779673
Posts: 2
Joined: Tue Sep 26, 2023 10:26 am

Ask ESP32 S3 SPI driver screen problem

Postby 15207779673 » Thu Nov 02, 2023 8:45 am

#define st7735s_width 128
#define st7735s_hight 160

#define GPIO_PIN_CS GPIO_NUM_3
#define GPIO_PIN_RST GPIO_NUM_2
#define GPIO_PIN_DC GPIO_NUM_4
#define GPIO_PIN_MOSI GPIO_NUM_11
#define GPIO_PIN_CLK GPIO_NUM_12

#define GPIO_PIN_MASK_BIT ((1ull<<GPIO_PIN_CS)|(1ull<<GPIO_PIN_RST)|(1ull<<GPIO_PIN_DC))

spi_device_handle_t spi;

void st7735_io_Init(void)
{
gpio_config_t io_cnf = {
.pin_bit_mask = GPIO_PIN_MASK_BIT,
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = 0,
.pull_down_en =0
};
gpio_config(&io_cnf);
}

void st7735s_spi_Init(void)
{
esp_err_t ret;
spi_bus_config_t buscfg ={
.mosi_io_num = GPIO_PIN_MOSI,
.miso_io_num = -1,
.sclk_io_num =GPIO_PIN_CLK ,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4092,
//.intr_flags = 0
};
spi_device_interface_config_t devcfg = {
.mode = 0,
.clock_speed_hz = SPI_MASTER_FREQ_26M,
.queue_size = 7
};
ret = spi_bus_initialize(SPI2_HOST,&buscfg,0);
ESP_ERROR_CHECK(ret);
ret = spi_bus_add_device(SPI2_HOST,&devcfg, &spi);
ESP_ERROR_CHECK(ret);
}
void spi_send_cmd(const uint8_t cmd)
{
esp_err_t ret;
spi_transaction_t t;
gpio_set_level(GPIO_PIN_DC,0);
gpio_set_level(GPIO_PIN_CS,0);
memset(&t, 0, sizeof(t));

t.length = 8;
t.tx_buffer = &cmd;
t.user = (void*)0;
ret = spi_device_polling_transmit(spi,&t);
gpio_set_level(GPIO_PIN_CS,1);
assert(ret==ESP_OK);
}

void spi_send_data(const uint8_t data)
{
esp_err_t ret;
spi_transaction_t t;
gpio_set_level(GPIO_PIN_DC,1);
gpio_set_level(GPIO_PIN_CS,0);
memset(&t, 0, sizeof(t));

t.length = 8;
t.tx_buffer = &data;
t.user = (void*)1;
ret = spi_device_polling_transmit(spi,&t);
gpio_set_level(GPIO_PIN_CS,1);
assert(ret==ESP_OK);
}
void spi_send_data_u16(uint16_t color)
{
spi_send_data(color>>8);
spi_send_data(color);
}

void st7735s_reset(void)
{
gpio_set_level(GPIO_PIN_RST,0);
vTaskDelay(10/portTICK_PERIOD_MS);
gpio_set_level(GPIO_PIN_RST,1);
vTaskDelay(120/portTICK_PERIOD_MS);
}

void st7735s_Init(void)
{
st7735_io_Init();
st7735s_spi_Init();
st7735s_reset();
// vTaskDelay(100/portTICK_PERIOD_MS);
//set softreset
spi_send_cmd(0x01);
vTaskDelay(150/portTICK_PERIOD_MS);

spi_send_cmd(0x11);//Sleep exit
vTaskDelay(120/portTICK_PERIOD_MS);
//ST7735R Frame Rate
spi_send_cmd(0xB1);
spi_send_data(0x01);
spi_send_data(0x2C);
spi_send_data(0x2D);
spi_send_cmd(0xB2);
spi_send_data(0x01);
spi_send_data(0x2C);
spi_send_data(0x2D);

spi_send_cmd(0xB3);
spi_send_data(0x01);
spi_send_data(0x2C);
spi_send_data(0x2D);
spi_send_data(0x01);
spi_send_data(0x2C);
spi_send_data(0x2D);

spi_send_cmd(0xB4); //Column inversion
spi_send_data(0x07);
//ST7735R Power Sequence
spi_send_cmd(0xC0);
spi_send_data(0xA2);
spi_send_data(0x02);
spi_send_data(0x84);
spi_send_cmd(0xC1);
spi_send_data(0xC5);
spi_send_cmd(0xC2);
spi_send_data(0x0A);
spi_send_data(0x00);
spi_send_cmd(0xC3);
spi_send_data(0x8A);
spi_send_data(0x2A);
spi_send_cmd(0xC4);
spi_send_data(0x8A);
spi_send_data(0xEE);
spi_send_cmd(0xC5); //VCOM
spi_send_data(0x23); //0x0E
spi_send_cmd(0x36); //MX, MY, RGB mode
spi_send_data(0xC0);
//ST7735R Gamma Sequence
spi_send_cmd(0xe0);
spi_send_data(0x0f);
spi_send_data(0x1a);
spi_send_data(0x0f);
spi_send_data(0x18);
spi_send_data(0x2f);

spi_send_data(0x28);
spi_send_data(0x20);
spi_send_data(0x22);
spi_send_data(0x1f);
spi_send_data(0x1b);

spi_send_data(0x23);
spi_send_data(0x37);
spi_send_data(0x00);
spi_send_data(0x07);
spi_send_data(0x02);

spi_send_data(0x10);

spi_send_cmd(0xe1);
spi_send_data(0x0f);
spi_send_data(0x1b);
spi_send_data(0x0f);
spi_send_data(0x17);
spi_send_data(0x33);

spi_send_data(0x2c);
spi_send_data(0x29);
spi_send_data(0x2e);
spi_send_data(0x30);
spi_send_data(0x30);

spi_send_data(0x39);
spi_send_data(0x3f);
spi_send_data(0x00);
spi_send_data(0x07);
spi_send_data(0x03);
spi_send_data(0x10);

spi_send_cmd(0x2a);
spi_send_data(0x00);
spi_send_data(0x00);
spi_send_data(0x00);
spi_send_data(0x7f);
spi_send_cmd(0x2b);
spi_send_data(0x00);
spi_send_data(0x00);
spi_send_data(0x00);
spi_send_data(0x9f);
spi_send_cmd(0xF0); //Enable test command
spi_send_data(0x01);
spi_send_cmd(0xF6); //Disable ram power save mode
spi_send_data(0x00);
spi_send_cmd(0x3A); //65k mode
spi_send_data(0);

spi_send_cmd(0x29);//Display on
}

void st7735s_setAddrWindow(uint16_t x_start,uint16_t y_start ,uint16_t x_end , uint16_t y_end) {
//控制列
spi_send_cmd(0x2A);
spi_send_data(x_start >> 8); // 高8列
spi_send_data(x_start & 0xFF); //低8列
spi_send_data(x_end >> 8);
spi_send_data(x_end & 0xFF);

//页地址设置
spi_send_cmd(0x2B);
spi_send_data(y_start >> 8);
spi_send_data(y_start & 0xFF);
spi_send_data(y_end >> 8);
spi_send_data(y_end & 0xFF);

spi_send_cmd(0x2C);
}

void st7735_clear(uint16_t color)
{
st7735s_setAddrWindow(0,0,st7735s_width,st7735s_hight);
for(unsigned int i = 0; i < st7735s_width;i++)
{
for(unsigned int j = 0; j<st7735s_hight;j++)
{
spi_send_data_u16(color);
}
}
}
What is the problem of driving the screen display color to become a flower screen?

Who is online

Users browsing this forum: Bing [Bot], MicroController and 95 guests