一根据下面程序可以知道,要显示什么图像,只需要把对应像素点数据输入显存。
显存大小必须与像素点多少一致
如要输入多个字符就必须要多个显存相对应
显存数据包括显示图像的像素列起点和终点,行起点和终点,和每个像素的颜色值。
图片
显示1,2两个数,显示的字符是反的,在0x36 中设置,先左右翻转,再设置显示像素坐标,列可以采用128减的方式,行不变
#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 列
st7735_send_cmd(0x2A);
st7735_send_data(0x00);
st7735_send_data(xs);
st7735_send_data(0x00);
st7735_send_data(xe);
// Set 行
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);
}
}
*/
const uint8_t font1[8] = {
0x04, 0x0C, 0x04, 0x04, 0x04, 0x04, 0x0E, 0x00
}; // 数字 '1'
const uint8_t font2[8] = {
0x0E, // 第1行
0x11, // 第2行
0x01, // 第3行
0x06, // 第4行
0x08, // 第5行
0x10, // 第6行
0x1F, // 第7行
0x00 // 第8行 2 的8*8点阵
};
qp(10,17,30,37); //从30行,10列显示8*8个像素点
for (uint16_t a = 0; a < 8; a++) {
for(uint8_t b=0;b<8;b++){
if((int)(((font2[a]>>b)&0b00000001))==1){ //如果像素点为1
st7735_send_data(0xf8);
st7735_send_data(0x00);
}else{
st7735_send_data(0xff);
st7735_send_data(0xff);
}
}
}
qp(18,25,30,37); //从30行,18列显示第二个8*8
for (uint16_t a = 0; a < 8; a++) {
for(uint8_t b=0;b<8;b++){
if((int)(((font1[a]>>b)&0b00000001))==1){
st7735_send_data(0xf8);
st7735_send_data(0x00);
}else{
st7735_send_data(0xff);
st7735_send_data(0xff);
}
}
}
}