【STM32】ST7789屏幕驱动
目录
CubeMX配置
配置SPI
开DMA
时钟树
堆栈大小
Keil工程配置
添加两个group
添加文件包含路径
驱动编写
写单字节函数
写字函数
写多字节函数
初始化函数
设置窗口函数
情况一:正常的0度旋转
情况二:顺时针90度旋转
情况三:180度旋转
情况四:顺时针270度旋转
清屏函数
画单色水平线函数
画单色垂直线函数
区域填充函数
画彩色线函数
画图函数
附录
ST7789.c
ST7789.h
ST7789手册阅读,准备编写驱动-CSDN博客
看完手册之后就可以开始敲代码
先看看最终效果
笔者用的是淘宝买的裸屏,分辨率240*240,自己焊接外围电路。
CubeMX配置
配置晶振,调试口等等就不赘述了
配置SPI
笔者的单片机SPI时钟最大可以到50MHz,为了便于演示,这里只开到6.25MHz,后面会有不同时钟下速率的对比
开DMA
屏幕的数据量还是挺大的,用DMA可以让MCU的负荷轻一些
时钟树
堆栈大小
按默认的来也可以
至此,cubemx配置完毕,点击生成代码
Keil工程配置
添加两个group
ST7789用于存放即将编写的驱动,generate存放生成的文件
添加文件包含路径
驱动编写
写单字节函数
可以发命令或者数据,均为单字节
void ST7789_SendByte(uint8_t dat, ST7789_DCType DC)
{ST7789_CS_LOW();(DC == ST7789_DATA) ? ST7789_DC_HIGH() : ST7789_DC_LOW();HAL_SPI_Transmit_DMA(ST7789_SPI, &dat, 1);while (HAL_SPI_GetState(ST7789_SPI) == HAL_SPI_STATE_BUSY_TX);ST7789_CS_HIGH();
}
写字函数
用于发送一个字
void ST7789_SendHalfWord(uint16_t dat)
{uint8_t da[2];ST7789_CS_LOW();ST7789_DC_HIGH();da[0] = dat >> 8;da[1] = dat & 0xFF;HAL_SPI_Transmit_DMA(ST7789_SPI, da, 2);while (HAL_SPI_GetState(ST7789_SPI) == HAL_SPI_STATE_BUSY_TX);ST7789_CS_HIGH();
}
写多字节函数
void ST7789_SendMultiByte(uint8_t* dat, uint16_t len)
{ST7789_CS_LOW();ST7789_DC_HIGH();HAL_SPI_Transmit_DMA(ST7789_SPI, dat, len);while (HAL_SPI_GetState(ST7789_SPI) == HAL_SPI_STATE_BUSY_TX);ST7789_CS_HIGH();
}
初始化函数
复位低电平时间需要持续至少10us,然后拉高,接下来的最大120ms内是在reset
void ST7789_Init(void)
{ST7789_BL_HIGH();ST7789_CS_HIGH();ST7789_RST_HIGH();ST7789_RST_LOW();HAL_Delay(1);ST7789_RST_HIGH();HAL_Delay(120);ST7789_SendByte(0x11, ST7789_CMD); ST7789_SendByte(0x3A, ST7789_CMD); ST7789_SendByte(0x55, ST7789_DATA); // 5 565 6 666ST7789_SendByte(0xB2, ST7789_CMD); ST7789_SendByte(0x0C, ST7789_DATA);ST7789_SendByte(0x0C, ST7789_DATA); ST7789_SendByte(0x00, ST7789_DATA); ST7789_SendByte(0x33, ST7789_DATA); ST7789_SendByte(0x33, ST7789_DATA); ST7789_SendByte(0xB7, ST7789_CMD); ST7789_SendByte(0x35, ST7789_DATA);ST7789_SendByte(0xBB, ST7789_CMD); ST7789_SendByte(0x32, ST7789_DATA); //Vcom=1.35VST7789_SendByte(0xC2, ST7789_CMD); ST7789_SendByte(0x01, ST7789_DATA);ST7789_SendByte(0xC3, ST7789_CMD); ST7789_SendByte(0x19, ST7789_DATA); //GVDD=4.8V ST7789_SendByte(0xC4, ST7789_CMD); ST7789_SendByte(0x20, ST7789_DATA); //VDV, 0x20:0vST7789_SendByte(0xC6, ST7789_CMD); ST7789_SendByte(0x0F, ST7789_DATA); //0x0F:60Hz ST7789_SendByte(0xD0, ST7789_CMD); ST7789_SendByte(0xA4, ST7789_DATA);ST7789_SendByte(0xA1, ST7789_DATA); ST7789_SendByte(0xE0, ST7789_CMD); ST7789_SendByte(0xD0, ST7789_DATA); ST7789_SendByte(0x08, ST7789_DATA); ST7789_SendByte(0x0E, ST7789_DATA); ST7789_SendByte(0x09, ST7789_DATA); ST7789_SendByte(0x09, ST7789_DATA); ST7789_SendByte(0x05, ST7789_DATA); ST7789_SendByte(0x31, ST7789_DATA); ST7789_SendByte(0x33, ST7789_DATA); ST7789_SendByte(0x48, ST7789_DATA); ST7789_SendByte(0x17, ST7789_DATA); ST7789_SendByte(0x14, ST7789_DATA); ST7789_SendByte(0x15, ST7789_DATA); ST7789_SendByte(0x31, ST7789_DATA); ST7789_SendByte(0x34, ST7789_DATA); ST7789_SendByte(0xE1, ST7789_CMD); ST7789_SendByte(0xD0, ST7789_DATA); ST7789_SendByte(0x08, ST7789_DATA); ST7789_SendByte(0x0E, ST7789_DATA); ST7789_SendByte(0x09, ST7789_DATA); ST7789_SendByte(0x09, ST7789_DATA); ST7789_SendByte(0x15, ST7789_DATA); ST7789_SendByte(0x31, ST7789_DATA); ST7789_SendByte(0x33, ST7789_DATA); ST7789_SendByte(0x48, ST7789_DATA); ST7789_SendByte(0x17, ST7789_DATA); ST7789_SendByte(0x14, ST7789_DATA); ST7789_SendByte(0x15, ST7789_DATA); ST7789_SendByte(0x31, ST7789_DATA); ST7789_SendByte(0x34, ST7789_DATA); ST7789_SendByte(0x21, ST7789_CMD);ST7789_SendByte(0x36, ST7789_CMD); //MX, MY, RGB mode
#if (ST7789_ROTATION == 0)ST7789_SendByte(0x00, ST7789_DATA);
#elif (ST7789_ROTATION == 90)ST7789_SendByte(0x60, ST7789_DATA);//90
#elif (ST7789_ROTATION == 180)ST7789_SendByte(0xC0, ST7789_DATA);//180
#elif (ST7789_ROTATION == 270)ST7789_SendByte(0xA0, ST7789_DATA);//270
#endifST7789_SendByte(0x2A, ST7789_CMD); //Column Address SetST7789_SendByte(0x00, ST7789_DATA);ST7789_SendByte(0x00, ST7789_DATA); //0ST7789_SendByte(0x00, ST7789_DATA);ST7789_SendByte(0xEF, ST7789_DATA); //239ST7789_SendByte(0x2B, ST7789_CMD); //Row Address SetST7789_SendByte(0x00, ST7789_DATA);ST7789_SendByte(0x00, ST7789_DATA); //0ST7789_SendByte(0x00, ST7789_DATA);ST7789_SendByte(0xEF, ST7789_DATA); //239ST7789_SendByte(0x29, ST7789_CMD);
}
设置窗口函数
void ST7789_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)
{if(ST7789_ROTATION==0){ST7789_SendByte(0x2a, ST7789_CMD);//列地址设置ST7789_SendHalfWord(x1);ST7789_SendHalfWord(x2);ST7789_SendByte(0x2b, ST7789_CMD);//行地址设置ST7789_SendHalfWord(y1);ST7789_SendHalfWord(y2);ST7789_SendByte(0x2c, ST7789_CMD);//储存器写}else if(ST7789_ROTATION == 90){ST7789_SendByte(0x2a, ST7789_CMD);//列地址设置ST7789_SendHalfWord(x1);ST7789_SendHalfWord(x2);ST7789_SendByte(0x2b, ST7789_CMD);//行地址设置ST7789_SendHalfWord(y1);ST7789_SendHalfWord(y2);ST7789_SendByte(0x2c, ST7789_CMD);//储存器写}else if(ST7789_ROTATION == 180){y1 += 80;y2 += 80;ST7789_SendByte(0x2a, ST7789_CMD);//列地址设置ST7789_SendHalfWord(x1);ST7789_SendHalfWord(x2);ST7789_SendByte(0x2b, ST7789_CMD);//行地址设置ST7789_SendHalfWord(y1);ST7789_SendHalfWord(y2);ST7789_SendByte(0x2c, ST7789_CMD);//储存器写}else if(ST7789_ROTATION == 270){x1 += 80;x2 += 80;ST7789_SendByte(0x2a, ST7789_CMD);//列地址设置ST7789_SendHalfWord(x1);ST7789_SendHalfWord(x2);ST7789_SendByte(0x2b, ST7789_CMD);//行地址设置ST7789_SendHalfWord(y1);ST7789_SendHalfWord(y2);ST7789_SendByte(0x2c, ST7789_CMD);//储存器写}else{}
}
编写设置窗口函数,有些角度需要加80的偏移,这里简单解释一下
因为屏幕是240*240的但是芯片本身支持240*320
初始化的时候可以设置屏幕镜像旋转等参数,如下图所示
0度对应MV=0,MX=0,MY=0
90度对应MV=1,MX=1,MY=0
180度对应MV=0,MX=1,MY=1
270度对应MV=1,MX=0,MY=1
情况一:正常的0度旋转
黑色代表屏幕可见区域(240*240),红色是芯片RAM(240*320),蓝色是起始坐标,此时不需要偏移
情况二:顺时针90度旋转
黑色代表屏幕可见区域(240*240),红色是芯片RAM(240*320),蓝色是起始坐标,此时不需要偏移
情况三:180度旋转
黑色代表屏幕可见区域(240*240),红色是芯片RAM(240*320),蓝色是我们想要的起始坐标(0,0),紫色的是芯片的(0,0)坐标
此时如果setwindow(0,0),那么实际上是从紫色的地方开始的,这部分已经在屏幕外面,因此Y需要增加80
情况四:顺时针270度旋转
黑色代表屏幕可见区域(240*240),红色是芯片RAM(240*320),蓝色是我们想要的起始坐标(0,0),紫色的是芯片的(0,0)坐标
此时如果setwindow(0,0),那么实际上是从紫色的地方开始的,这部分已经在屏幕外面,因此X需要增加80
手册里镜像的操作读者可以自行编写,明白原理后都是相通的
清屏函数
void ST7789_Clear(uint16_t color)
{uint16_t i,j;ST7789_Address_Set(0,0,240-1,240-1);for ( i = 0; i < ST7789_BUF_SIZE; i += 2){ST7789_Buf[i] = color >> 8;ST7789_Buf[i + 1] = color & 0xFF;}for ( i = 0; i < 240 * 240 * 2 / ST7789_BUF_SIZE; i++){ST7789_SendMultiByte(ST7789_Buf, ST7789_BUF_SIZE);}
}
效果
ST7789_Clear(BLUE);
逻辑分析仪测试波形如下,清屏耗费150ms和21ms
void ST7789_DrawPixel(uint16_t x,uint16_t y,uint16_t color)
{ST7789_Address_Set(x,y,x,y);//设置光标位置 ST7789_SendHalfWord(color);
}
效果
ST7789_Clear(BLACK); ST7789_DrawPixel(0, 100, WHITE);ST7789_DrawPixel(239, 100, WHITE);
画单色水平线函数
void ST7789_DrawHLine(uint16_t xs, uint16_t xe,uint16_t y,uint16_t color)
{uint16_t i,j;ST7789_Address_Set(xs,y,xe,y);//设置光标位置 for ( i = 0; i < (xe - xs + 1) * 2; i += 2){ST7789_Buf[i] = color >> 8;ST7789_Buf[i + 1] = color & 0xFF;}ST7789_SendMultiByte(ST7789_Buf, (xe - xs + 1) * 2);
}
效果
ST7789_Clear(BLACK); ST7789_DrawPixel(0, 100, WHITE);ST7789_DrawPixel(239, 100, WHITE);ST7789_DrawHLine(1,238,100,RED);
画单色垂直线函数
void ST7789_DrawVLine(uint16_t ys, uint16_t ye, uint16_t x, uint16_t color)
{uint16_t i,j;ST7789_Address_Set(x,ys,x,ye);//设置光标位置 for ( i = 0; i < (ye - ys + 1) * 2; i += 2){ST7789_Buf[i] = color >> 8;ST7789_Buf[i + 1] = color & 0xFF;}ST7789_SendMultiByte(ST7789_Buf, (ye - ys + 1) * 2);
}
效果
ST7789_Clear(BLACK); ST7789_DrawPixel(0, 100, WHITE);ST7789_DrawPixel(239, 100, WHITE);ST7789_DrawHLine(1,238,100,RED);ST7789_DrawVLine(0,199,100,BLUE);
区域填充函数
void ST7789_FillRect(uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye, uint16_t color)
{uint16_t i,j;uint32_t depth;depth = (ye - ys + 1) * (xe - xs + 1) * 2;ST7789_Address_Set(xs, ys, xe, ye);//设置光标位置 if (depth < ST7789_BUF_SIZE){for ( i = 0; i < depth; i += 2){ST7789_Buf[i] = color >> 8;ST7789_Buf[i + 1] = color & 0xFF;}ST7789_SendMultiByte(ST7789_Buf, depth);}else{for ( i = 0; i < ST7789_BUF_SIZE; i += 2){ST7789_Buf[i] = color >> 8;ST7789_Buf[i + 1] = color & 0xFF;}for ( i = 0; i < depth / ST7789_BUF_SIZE; i++){ST7789_SendMultiByte(ST7789_Buf, ST7789_BUF_SIZE);}if (depth % ST7789_BUF_SIZE != 0){ST7789_SendMultiByte(ST7789_Buf, (depth % ST7789_BUF_SIZE));}else{}}
}
效果
ST7789_Clear(BLACK); ST7789_DrawPixel(0, 100, WHITE);ST7789_DrawPixel(239, 100, WHITE);ST7789_DrawHLine(1,238,100,RED);ST7789_DrawVLine(0,199,100,BLUE);ST7789_FillRect(101, 0, 200, 199, RED);
画彩色线函数
/*** xsize is in [0,239]*/
void ST7789_DrawBitLine16BPP(uint16_t xs, uint16_t y, uint8_t const * p, uint16_t xsize)
{uint16_t i,j;ST7789_Address_Set(xs, y, xs + xsize - 1, y);//设置光标位置 for ( i = 0; i < xsize * 2; i+=2){// ST7789_Buf[i] = *(p + i) >> 8;// ST7789_Buf[i + 1] = *(p + i + 1) & 0xFF;ST7789_Buf[i + 1] = *(p + i);ST7789_Buf[i] = *(p + i + 1);}ST7789_SendMultiByte(ST7789_Buf, xsize * 2);
}
画图函数
void ST7789_DrawBitmap(uint16_t xs, uint16_t ys, uint16_t xsize, uint16_t ysize, uint8_t *p)
{uint16_t i,j;for ( i = 0; i < ysize; i++){ST7789_DrawBitLine16BPP(xs, ys + i, p + i * xsize * 2, xsize);}
}
效果
ST7789_DrawBitmap(0, 0, bmphonk.XSize, bmphonk.YSize, bmphonk.pData);
逻辑分析仪测试波形如下,画一张240*240的图耗费195ms和67ms
附录
ST7789.c
#include "ST7789.h"/* at least 240*2 and can be divided wholely by 240*240*2 */
#define ST7789_BUF_SIZE (240 * 2)
uint8_t ST7789_Buf[ST7789_BUF_SIZE];void ST7789_SendByte(uint8_t dat, ST7789_DCType DC)
{ST7789_CS_LOW();(DC == ST7789_DATA) ? ST7789_DC_HIGH() : ST7789_DC_LOW();HAL_SPI_Transmit_DMA(ST7789_SPI, &dat, 1);while (HAL_SPI_GetState(ST7789_SPI) == HAL_SPI_STATE_BUSY_TX);ST7789_CS_HIGH();
}void ST7789_SendHalfWord(uint16_t dat)
{uint8_t da[2];ST7789_CS_LOW();ST7789_DC_HIGH();da[0] = dat >> 8;da[1] = dat & 0xFF;HAL_SPI_Transmit_DMA(ST7789_SPI, da, 2);while (HAL_SPI_GetState(ST7789_SPI) == HAL_SPI_STATE_BUSY_TX);ST7789_CS_HIGH();
}void ST7789_SendMultiByte(uint8_t* dat, uint16_t len)
{ST7789_CS_LOW();ST7789_DC_HIGH();HAL_SPI_Transmit_DMA(ST7789_SPI, dat, len);while (HAL_SPI_GetState(ST7789_SPI) == HAL_SPI_STATE_BUSY_TX);ST7789_CS_HIGH();
}void ST7789_Init(void)
{ST7789_BL_HIGH();ST7789_CS_HIGH();ST7789_RST_HIGH();ST7789_RST_LOW();HAL_Delay(1);ST7789_RST_HIGH();HAL_Delay(120);ST7789_SendByte(0x11, ST7789_CMD); ST7789_SendByte(0x3A, ST7789_CMD); ST7789_SendByte(0x55, ST7789_DATA); // 5 565 6 666ST7789_SendByte(0xB2, ST7789_CMD); ST7789_SendByte(0x0C, ST7789_DATA);ST7789_SendByte(0x0C, ST7789_DATA); ST7789_SendByte(0x00, ST7789_DATA); ST7789_SendByte(0x33, ST7789_DATA); ST7789_SendByte(0x33, ST7789_DATA); ST7789_SendByte(0xB7, ST7789_CMD); ST7789_SendByte(0x35, ST7789_DATA);ST7789_SendByte(0xBB, ST7789_CMD); ST7789_SendByte(0x32, ST7789_DATA); //Vcom=1.35VST7789_SendByte(0xC2, ST7789_CMD); ST7789_SendByte(0x01, ST7789_DATA);ST7789_SendByte(0xC3, ST7789_CMD); ST7789_SendByte(0x19, ST7789_DATA); //GVDD=4.8V ST7789_SendByte(0xC4, ST7789_CMD); ST7789_SendByte(0x20, ST7789_DATA); //VDV, 0x20:0vST7789_SendByte(0xC6, ST7789_CMD); ST7789_SendByte(0x0F, ST7789_DATA); //0x0F:60Hz ST7789_SendByte(0xD0, ST7789_CMD); ST7789_SendByte(0xA4, ST7789_DATA);ST7789_SendByte(0xA1, ST7789_DATA); ST7789_SendByte(0xE0, ST7789_CMD); ST7789_SendByte(0xD0, ST7789_DATA); ST7789_SendByte(0x08, ST7789_DATA); ST7789_SendByte(0x0E, ST7789_DATA); ST7789_SendByte(0x09, ST7789_DATA); ST7789_SendByte(0x09, ST7789_DATA); ST7789_SendByte(0x05, ST7789_DATA); ST7789_SendByte(0x31, ST7789_DATA); ST7789_SendByte(0x33, ST7789_DATA); ST7789_SendByte(0x48, ST7789_DATA); ST7789_SendByte(0x17, ST7789_DATA); ST7789_SendByte(0x14, ST7789_DATA); ST7789_SendByte(0x15, ST7789_DATA); ST7789_SendByte(0x31, ST7789_DATA); ST7789_SendByte(0x34, ST7789_DATA); ST7789_SendByte(0xE1, ST7789_CMD); ST7789_SendByte(0xD0, ST7789_DATA); ST7789_SendByte(0x08, ST7789_DATA); ST7789_SendByte(0x0E, ST7789_DATA); ST7789_SendByte(0x09, ST7789_DATA); ST7789_SendByte(0x09, ST7789_DATA); ST7789_SendByte(0x15, ST7789_DATA); ST7789_SendByte(0x31, ST7789_DATA); ST7789_SendByte(0x33, ST7789_DATA); ST7789_SendByte(0x48, ST7789_DATA); ST7789_SendByte(0x17, ST7789_DATA); ST7789_SendByte(0x14, ST7789_DATA); ST7789_SendByte(0x15, ST7789_DATA); ST7789_SendByte(0x31, ST7789_DATA); ST7789_SendByte(0x34, ST7789_DATA); ST7789_SendByte(0x21, ST7789_CMD);ST7789_SendByte(0x36, ST7789_CMD); //MX, MY, RGB mode
#if (ST7789_ROTATION == 0)ST7789_SendByte(0x00, ST7789_DATA);
#elif (ST7789_ROTATION == 90)ST7789_SendByte(0x60, ST7789_DATA);//90
#elif (ST7789_ROTATION == 180)ST7789_SendByte(0xC0, ST7789_DATA);//180
#elif (ST7789_ROTATION == 270)ST7789_SendByte(0xA0, ST7789_DATA);//270
#endifST7789_SendByte(0x2A, ST7789_CMD); //Column Address SetST7789_SendByte(0x00, ST7789_DATA);ST7789_SendByte(0x00, ST7789_DATA); //0ST7789_SendByte(0x00, ST7789_DATA);ST7789_SendByte(0xEF, ST7789_DATA); //239ST7789_SendByte(0x2B, ST7789_CMD); //Row Address SetST7789_SendByte(0x00, ST7789_DATA);ST7789_SendByte(0x00, ST7789_DATA); //0ST7789_SendByte(0x00, ST7789_DATA);ST7789_SendByte(0xEF, ST7789_DATA); //239ST7789_SendByte(0x29, ST7789_CMD);
}void ST7789_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)
{if(ST7789_ROTATION==0){ST7789_SendByte(0x2a, ST7789_CMD);//列地址设置ST7789_SendHalfWord(x1);ST7789_SendHalfWord(x2);ST7789_SendByte(0x2b, ST7789_CMD);//行地址设置ST7789_SendHalfWord(y1);ST7789_SendHalfWord(y2);ST7789_SendByte(0x2c, ST7789_CMD);//储存器写}else if(ST7789_ROTATION == 90){ST7789_SendByte(0x2a, ST7789_CMD);//列地址设置ST7789_SendHalfWord(x1);ST7789_SendHalfWord(x2);ST7789_SendByte(0x2b, ST7789_CMD);//行地址设置ST7789_SendHalfWord(y1);ST7789_SendHalfWord(y2);ST7789_SendByte(0x2c, ST7789_CMD);//储存器写}else if(ST7789_ROTATION == 180){y1 += 80;y2 += 80;ST7789_SendByte(0x2a, ST7789_CMD);//列地址设置ST7789_SendHalfWord(x1);ST7789_SendHalfWord(x2);ST7789_SendByte(0x2b, ST7789_CMD);//行地址设置ST7789_SendHalfWord(y1);ST7789_SendHalfWord(y2);ST7789_SendByte(0x2c, ST7789_CMD);//储存器写}else if(ST7789_ROTATION == 270){x1 += 80;x2 += 80;ST7789_SendByte(0x2a, ST7789_CMD);//列地址设置ST7789_SendHalfWord(x1);ST7789_SendHalfWord(x2);ST7789_SendByte(0x2b, ST7789_CMD);//行地址设置ST7789_SendHalfWord(y1);ST7789_SendHalfWord(y2);ST7789_SendByte(0x2c, ST7789_CMD);//储存器写}else{}
}void ST7789_Clear(uint16_t color)
{uint16_t i,j;ST7789_Address_Set(0,0,ST7789_HIGHT - 1,ST7789_WIDTH - 1);for ( i = 0; i < ST7789_BUF_SIZE; i += 2){ST7789_Buf[i] = color >> 8;ST7789_Buf[i + 1] = color & 0xFF;}for ( i = 0; i < ST7789_HIGHT * ST7789_WIDTH * 2 / ST7789_BUF_SIZE; i++){ST7789_SendMultiByte(ST7789_Buf, ST7789_BUF_SIZE);}
}void ST7789_DrawPixel(uint16_t x,uint16_t y,uint16_t color)
{ST7789_Address_Set(x,y,x,y);//设置光标位置 ST7789_SendHalfWord(color);
}void ST7789_DrawHLine(uint16_t xs, uint16_t xe,uint16_t y,uint16_t color)
{uint16_t i,j;ST7789_Address_Set(xs,y,xe,y);//设置光标位置 for ( i = 0; i < (xe - xs + 1) * 2; i += 2){ST7789_Buf[i] = color >> 8;ST7789_Buf[i + 1] = color & 0xFF;}ST7789_SendMultiByte(ST7789_Buf, (xe - xs + 1) * 2);
}void ST7789_DrawVLine(uint16_t ys, uint16_t ye, uint16_t x, uint16_t color)
{uint16_t i,j;ST7789_Address_Set(x,ys,x,ye);//设置光标位置 for ( i = 0; i < (ye - ys + 1) * 2; i += 2){ST7789_Buf[i] = color >> 8;ST7789_Buf[i + 1] = color & 0xFF;}ST7789_SendMultiByte(ST7789_Buf, (ye - ys + 1) * 2);
}void ST7789_FillRect(uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye, uint16_t color)
{uint16_t i,j;uint32_t depth;depth = (ye - ys + 1) * (xe - xs + 1) * 2;ST7789_Address_Set(xs, ys, xe, ye);//设置光标位置 if (depth < ST7789_BUF_SIZE){for ( i = 0; i < depth; i += 2){ST7789_Buf[i] = color >> 8;ST7789_Buf[i + 1] = color & 0xFF;}ST7789_SendMultiByte(ST7789_Buf, depth);}else{for ( i = 0; i < ST7789_BUF_SIZE; i += 2){ST7789_Buf[i] = color >> 8;ST7789_Buf[i + 1] = color & 0xFF;}for ( i = 0; i < depth / ST7789_BUF_SIZE; i++){ST7789_SendMultiByte(ST7789_Buf, ST7789_BUF_SIZE);}if (depth % ST7789_BUF_SIZE != 0){ST7789_SendMultiByte(ST7789_Buf, (depth % ST7789_BUF_SIZE));}else{}}
}/*** xsize is in [0,239]*/
void ST7789_DrawBitLine16BPP(uint16_t xs, uint16_t y, uint8_t const * p, uint16_t xsize)
{uint16_t i,j;ST7789_Address_Set(xs, y, xs + xsize - 1, y);//设置光标位置 for ( i = 0; i < xsize * 2; i+=2){// ST7789_Buf[i] = *(p + i) >> 8;// ST7789_Buf[i + 1] = *(p + i + 1) & 0xFF;ST7789_Buf[i + 1] = *(p + i);ST7789_Buf[i] = *(p + i + 1);}ST7789_SendMultiByte(ST7789_Buf, xsize * 2);
}void ST7789_DrawBitmap(uint16_t xs, uint16_t ys, uint16_t xsize, uint16_t ysize, uint8_t *p)
{uint16_t i,j;for ( i = 0; i < ysize; i++){ST7789_DrawBitLine16BPP(xs, ys + i, p + i * xsize * 2, xsize);}
}
ST7789.h
#ifndef ST7789_H
#define ST7789_H #include "main.h"
#include "gpio.h"
#include "spi.h"
#include "stdint.h"#define ST7789_ROTATION 0
#define ST7789_SPI &hspi1#define ST7789_BUFFER#define ST7789_HIGHT 240
#define ST7789_WIDTH 240#define ST7789_RST_LOW() HAL_GPIO_WritePin(RES_GPIO_Port,RES_Pin,GPIO_PIN_RESET)
#define ST7789_RST_HIGH() HAL_GPIO_WritePin(RES_GPIO_Port,RES_Pin,GPIO_PIN_SET)#define ST7789_CS_LOW() HAL_GPIO_WritePin(CS_GPIO_Port,CS_Pin,GPIO_PIN_RESET)
#define ST7789_CS_HIGH() HAL_GPIO_WritePin(CS_GPIO_Port,CS_Pin,GPIO_PIN_SET)#define ST7789_DC_LOW() HAL_GPIO_WritePin(DC_GPIO_Port,DC_Pin,GPIO_PIN_RESET)
#define ST7789_DC_HIGH() HAL_GPIO_WritePin(DC_GPIO_Port,DC_Pin,GPIO_PIN_SET)#define ST7789_BL_LOW() HAL_GPIO_WritePin(BL_GPIO_Port,BL_Pin,GPIO_PIN_RESET)
#define ST7789_BL_HIGH() HAL_GPIO_WritePin(BL_GPIO_Port,BL_Pin,GPIO_PIN_SET)#define WHITE 0xFFFF
#define BLACK 0x0000
#define BLUE 0x001F
#define BRED 0XF81F
#define GRED 0XFFE0
#define GBLUE 0X07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define GREEN 0x07E0
#define CYAN 0x7FFF
#define YELLOW 0xFFE0
#define BROWN 0XBC40
#define BRRED 0XFC07
#define GRAY 0X8430typedef enum
{ST7789_CMD,ST7789_DATA,
}ST7789_DCType;typedef struct
{ST7789_DCType DC;uint8_t data;
}ST7789_InitSequenceType;typedef struct {uint16_t XSize;uint16_t YSize;uint16_t BytesPerLine;uint16_t BitsPerPixel;const uint8_t * pData;} GUI_BITMAP;void ST7789_Clear(uint16_t Color);
void ST7789_SendByte(uint8_t dat, ST7789_DCType DC);
void ST7789_SendHalfWord(uint16_t dat);
void ST7789_SendMultiByte(uint8_t* dat, uint16_t len);
void ST7789_Init(void);
void ST7789_DrawPixel(uint16_t x,uint16_t y,uint16_t color);
void ST7789_DrawHLine(uint16_t xs, uint16_t xe,uint16_t y,uint16_t color);
void ST7789_DrawVLine(uint16_t ys, uint16_t ye,uint16_t x,uint16_t color);
void ST7789_FillRect(uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye, uint16_t color);
void ST7789_DrawBitmap(uint16_t xs, uint16_t ys, uint16_t xsize, uint16_t ysize, uint8_t *p);#endif