图片
下一步把ascii 8*8点阵输入程序,屏幕就可以显示ascii字符了,最终把此显示程序写成函数,输入参数为要显示字符的数组,这样就是st7735的显示驱动程序了。当然还要加入显示自动换行功能。
至于怎样显示ascii,我想到查表法,比如点阵字库a在数组中的序号为10,如要显示数组遇到a,则.直接去查找显示10号ascii就行了。
#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(0b01000000); //40
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 cls(){
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);
}
}
}
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 初始化后是花屏
cls();
uint8_t font[][8] ={
{0x1C, 0x22, 0x26, 0x2A, 0x32, 0x22, 0x1C, 0x00}, // '0'
{0x04, 0x0C, 0x04, 0x04, 0x04, 0x04, 0x0E, 0x00}, // 数字 '1'
{0x0E, 0x11, 0x01, 0x06, 0x08, 0x10, 0x1F, 0x00 }, // 2
{0x1C, 0x22, 0x02, 0x1C, 0x02, 0x22, 0x1C, 0x00}, // '3'
{0x08, 0x18, 0x28, 0x48, 0x7E, 0x08, 0x08, 0x00}, // '4'
{0x3E, 0x20, 0x3E, 0x02, 0x02, 0x22, 0x1C, 0x00}, // '5'
{0x1C, 0x22, 0x20, 0x3C, 0x22, 0x22, 0x1C, 0x00}, // '6'
{0x3E, 0x02, 0x04, 0x08, 0x10, 0x20, 0x20, 0x00}, // '7'
{0x1C, 0x22, 0x22, 0x1C, 0x22, 0x22, 0x1C, 0x00}, // '8'
{0x1C, 0x22, 0x22, 0x1E, 0x02, 0x22, 0x1C, 0x00}, // '9'
};
void xs(uint8_t ls,uint8_t le,uint8_t hs,uint8_t he,uint8_t *ci){
qp(127-le,127-ls,hs,he);
for (uint16_t a = 0; a < 8; a++) {
for(uint8_t b=0;b<8;b++){
if((int)(((ci[a]>>b)&0b00000001))==1){ //如果像素点为1
st7735_send_data(0xf8);
st7735_send_data(0x00);
}else{
st7735_send_data(0xff);
st7735_send_data(0xff);
}
}
}
}
uint8_t i[]={6,1,8,3,7,3,6,7,8,7,1,7};
uint16_t len=(int)sizeof(i);
uint8_t lc=0; //lcd 显示起始列
uint8_t hc=50; // lcd 显示行
for(uint8_t a=0;a<len;a++){
uint8_t ls=lc;
uint8_t le=ls+7;
uint8_t hs=hc;
uint8_t he=hs+7;
xs(ls,le,hs,he,&font[i[a]][0]);
lc=le; //列自动加7,相当于光标后移一个字符
}
}