配置 PWM ADC LCD
This commit is contained in:
@@ -1,59 +1,62 @@
|
||||
/*
|
||||
* 模块名称:ST7735S LCD驱动
|
||||
* 模块功能:提供128x160 RGB TFT屏幕的初始化、画点、画线、填充、显示字符串等功能
|
||||
* 适用平台:STM32F103C8T6 + SPI2
|
||||
* 模块名称:ST7735S LCD驱动(底层)
|
||||
* 模块功能:提供ST7735S屏幕的硬件SPI通信、初始化、填充、画点、方向控制等底层操作
|
||||
* 绘图函数见 lcd.c
|
||||
* 适用平台:STM32F103C8T6 + 硬件SPI2
|
||||
* 作者:王建锋
|
||||
* 创建日期:2026-07-13
|
||||
* 修改记录:
|
||||
* 2026-07-13 王建锋 创建初始版本
|
||||
* 2026-07-14 王建锋 软件SPI -> 硬件SPI2
|
||||
* 2026-07-14 王建锋 拆分:绘图函数移入 lcd.c
|
||||
*/
|
||||
|
||||
#include "st7735s.h"
|
||||
#include "lcd_data.h"
|
||||
#include "spi.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static uint16_t s_lcd_width = LCD_WIDTH;
|
||||
static uint16_t s_lcd_height = LCD_HEIGHT;
|
||||
|
||||
extern DMA_HandleTypeDef hdma_spi2_tx;
|
||||
|
||||
/* 8x16 ASCII字体(ASCII 32-126),每字符16字节 - 定义在 lcd_data.c 中 */
|
||||
|
||||
/*
|
||||
* 函数功能:软件SPI写一个字节
|
||||
* 部分屏幕厂家虽然标称128x160,但实际使用132x162驱动晶片,
|
||||
* 左右各偏移2列,上下偏移1行才能正确显示。
|
||||
* 根据自己的屏幕实测调整这两个值。
|
||||
*/
|
||||
#define X_OFFSET 2
|
||||
#define Y_OFFSET 1
|
||||
|
||||
uint16_t s_lcd_width = LCD_WIDTH;
|
||||
uint16_t s_lcd_height = LCD_HEIGHT;
|
||||
|
||||
/*
|
||||
* 函数功能:硬件SPI写一个字节
|
||||
* 入口参数:data - 要发送的数据
|
||||
* 返回值:无
|
||||
* 函数说明:模拟SPI时序发送一个字节,MSB先
|
||||
* 函数说明:使用SPI2外设发送一个字节
|
||||
*/
|
||||
static void st7735s_spi_write_byte(uint8_t data)
|
||||
void st7735s_spi_write_byte(uint8_t data)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
HAL_GPIO_WritePin(LCD_SCL_PORT, LCD_SCL_PIN, GPIO_PIN_RESET);
|
||||
if (data & 0x80) {
|
||||
HAL_GPIO_WritePin(LCD_SDA_PORT, LCD_SDA_PIN, GPIO_PIN_SET);
|
||||
} else {
|
||||
HAL_GPIO_WritePin(LCD_SDA_PORT, LCD_SDA_PIN, GPIO_PIN_RESET);
|
||||
}
|
||||
__NOP();
|
||||
__NOP();
|
||||
HAL_GPIO_WritePin(LCD_SCL_PORT, LCD_SCL_PIN, GPIO_PIN_SET);
|
||||
__NOP();
|
||||
__NOP();
|
||||
data <<= 1;
|
||||
}
|
||||
HAL_SPI_Transmit(&hspi2, &data, 1, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:写入命令
|
||||
* 函数功能:硬件SPI批量写数据
|
||||
* 入口参数:data - 数据缓冲区
|
||||
* len - 字节数
|
||||
* 返回值:无
|
||||
* 函数说明:使用DMA批量发送
|
||||
*/
|
||||
void st7735s_spi_write_bulk(const uint8_t *data, uint32_t len)
|
||||
{
|
||||
HAL_SPI_Transmit_DMA(&hspi2, (uint8_t *)data, len);
|
||||
while (HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:写入命令(CS+DC管理,单字节)
|
||||
* 入口参数:cmd - 命令字节
|
||||
* 返回值:无
|
||||
* 函数说明:拉低DC,选择芯片,发送命令,释放芯片
|
||||
* 函数说明:拉低DC,发送命令
|
||||
*/
|
||||
static void st7735s_write_cmd(uint8_t cmd)
|
||||
void st7735s_write_cmd(uint8_t cmd)
|
||||
{
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, GPIO_PIN_RESET);
|
||||
@@ -62,12 +65,12 @@ static void st7735s_write_cmd(uint8_t cmd)
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:写入数据
|
||||
* 函数功能:写入数据(CS+DC管理,单字节)
|
||||
* 入口参数:data - 数据字节
|
||||
* 返回值:无
|
||||
* 函数说明:拉高DC,选择芯片,发送数据,释放芯片
|
||||
* 函数说明:拉高DC,发送数据
|
||||
*/
|
||||
static void st7735s_write_data(uint8_t data)
|
||||
void st7735s_write_data(uint8_t data)
|
||||
{
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, GPIO_PIN_SET);
|
||||
@@ -76,119 +79,83 @@ static void st7735s_write_data(uint8_t data)
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:写入多字节数据(CS保持低)
|
||||
* 入口参数:p_data - 数据缓冲区指针
|
||||
* len - 字节数
|
||||
* 函数功能:设置显示窗口(框选区域)
|
||||
* 入口参数:x1 - 起始列(屏幕坐标,含偏移处理)
|
||||
* y1 - 起始行
|
||||
* x2 - 结束列
|
||||
* y2 - 结束行
|
||||
* 返回值:无
|
||||
* 函数说明:用于连续写入多个数据字节,省去中间CS跳变
|
||||
* 函数说明:发送CASET(2Ah)+RASET(2Bh)+RAMWR(2Ch),
|
||||
* 后续写入的数据将填充在此区域内
|
||||
*/
|
||||
static void st7735s_write_data_multi(const uint8_t *p_data, uint16_t len)
|
||||
void st7735s_set_window(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
|
||||
{
|
||||
uint16_t i;
|
||||
st7735s_write_cmd(LCD_CMD_CASET);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data(x1 + X_OFFSET);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data(x2 + X_OFFSET);
|
||||
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, GPIO_PIN_SET);
|
||||
for (i = 0; i < len; i++) {
|
||||
st7735s_spi_write_byte(p_data[i]);
|
||||
}
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_SET);
|
||||
}
|
||||
st7735s_write_cmd(LCD_CMD_RASET);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data(y1 + Y_OFFSET);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data(y2 + Y_OFFSET);
|
||||
|
||||
/*
|
||||
* 函数功能:写入16位颜色数据
|
||||
* 入口参数:p_color - 16位颜色缓冲区指针
|
||||
* len - 像素个数
|
||||
* 返回值:无
|
||||
* 函数说明:MSB先发送RGB565数据
|
||||
*/
|
||||
static void st7735s_write_colors(uint16_t *p_color, uint16_t len)
|
||||
{
|
||||
uint16_t i;
|
||||
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, GPIO_PIN_SET);
|
||||
for (i = 0; i < len; i++) {
|
||||
st7735s_spi_write_byte((uint8_t)(p_color[i] >> 8));
|
||||
st7735s_spi_write_byte((uint8_t)(p_color[i] & 0xFF));
|
||||
}
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_SET);
|
||||
st7735s_write_cmd(LCD_CMD_RAMWR);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:LCD初始化
|
||||
* 入口参数:无
|
||||
* 返回值:无
|
||||
* 函数说明:执行ST7735S标准初始化序列
|
||||
* 函数说明:初始化ST7735S屏幕,依赖MX_SPI2_Init()提前配置好SPI2
|
||||
*/
|
||||
void st7735s_init(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
printf("LCD init start (HW SPI2)...\r\n");
|
||||
|
||||
printf("LCD init start (soft SPI)...\r\n");
|
||||
|
||||
/* 将SCL和SDA引脚切换为GPIO输出(软件SPI) */
|
||||
GPIO_InitStruct.Pin = LCD_SCL_PIN | LCD_SDA_PIN;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
HAL_GPIO_WritePin(LCD_SCL_PORT, LCD_SCL_PIN, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(LCD_SDA_PORT, LCD_SDA_PIN, GPIO_PIN_SET);
|
||||
|
||||
/* 确保CS和RST初始为高,避免LCD在初始化期间被错误选中 */
|
||||
/* CS初始高电平 */
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(LCD_RST_PORT, LCD_RST_PIN, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, GPIO_PIN_RESET);
|
||||
HAL_Delay(10);
|
||||
|
||||
printf("IO: RST=%d DC=%d CS=%d\r\n",
|
||||
(int)HAL_GPIO_ReadPin(LCD_RST_PORT, LCD_RST_PIN),
|
||||
(int)HAL_GPIO_ReadPin(LCD_DC_PORT, LCD_DC_PIN),
|
||||
(int)HAL_GPIO_ReadPin(LCD_CS_PORT, LCD_CS_PIN));
|
||||
|
||||
/* 硬件复位 */
|
||||
printf("HW reset...\r\n");
|
||||
HAL_GPIO_WritePin(LCD_RST_PORT, LCD_RST_PIN, GPIO_PIN_RESET);
|
||||
HAL_Delay(10);
|
||||
HAL_Delay(20);
|
||||
HAL_GPIO_WritePin(LCD_RST_PORT, LCD_RST_PIN, GPIO_PIN_SET);
|
||||
HAL_Delay(120);
|
||||
HAL_Delay(150);
|
||||
|
||||
/* 软件复位 */
|
||||
printf("SW reset...\r\n");
|
||||
st7735s_write_cmd(LCD_CMD_SWRESET);
|
||||
HAL_Delay(150);
|
||||
|
||||
/* 退出睡眠模式 */
|
||||
printf("Sleep out...\r\n");
|
||||
st7735s_write_cmd(LCD_CMD_SLPOUT);
|
||||
HAL_Delay(500);
|
||||
HAL_Delay(120);
|
||||
|
||||
/* 帧率控制 */
|
||||
printf("Frame rate...\r\n");
|
||||
st7735s_write_cmd(LCD_CMD_FRMCTR1);
|
||||
st7735s_write_data(0x01);
|
||||
st7735s_write_data(0x2C);
|
||||
st7735s_write_data(0x2D);
|
||||
st7735s_write_data(0x05);
|
||||
st7735s_write_data(0x3C);
|
||||
st7735s_write_data(0x3C);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_FRMCTR2);
|
||||
st7735s_write_data(0x01);
|
||||
st7735s_write_data(0x2C);
|
||||
st7735s_write_data(0x2D);
|
||||
st7735s_write_data(0x05);
|
||||
st7735s_write_data(0x3C);
|
||||
st7735s_write_data(0x3C);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_FRMCTR3);
|
||||
st7735s_write_data(0x01);
|
||||
st7735s_write_data(0x2C);
|
||||
st7735s_write_data(0x2D);
|
||||
st7735s_write_data(0x01);
|
||||
st7735s_write_data(0x2C);
|
||||
st7735s_write_data(0x2D);
|
||||
st7735s_write_data(0x05);
|
||||
st7735s_write_data(0x3C);
|
||||
st7735s_write_data(0x3C);
|
||||
st7735s_write_data(0x05);
|
||||
st7735s_write_data(0x3C);
|
||||
st7735s_write_data(0x3C);
|
||||
|
||||
/* 显示反转控制 */
|
||||
/* 显示反转控制(列反转) */
|
||||
st7735s_write_cmd(LCD_CMD_INVCTR);
|
||||
st7735s_write_data(0x07);
|
||||
st7735s_write_data(0x03);
|
||||
|
||||
/* 功率控制 */
|
||||
printf("Power ctrl...\r\n");
|
||||
st7735s_write_cmd(LCD_CMD_PWCTR1);
|
||||
st7735s_write_data(0xA2);
|
||||
st7735s_write_data(0x02);
|
||||
@@ -198,108 +165,113 @@ void st7735s_init(void)
|
||||
st7735s_write_data(0xC5);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_PWCTR3);
|
||||
st7735s_write_data(0x0A);
|
||||
st7735s_write_data(0x0D);
|
||||
st7735s_write_data(0x00);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_PWCTR4);
|
||||
st7735s_write_data(0x8A);
|
||||
st7735s_write_data(0x8D);
|
||||
st7735s_write_data(0x2A);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_PWCTR5);
|
||||
st7735s_write_data(0x8A);
|
||||
st7735s_write_data(0x8D);
|
||||
st7735s_write_data(0xEE);
|
||||
|
||||
/* VCOM控制 */
|
||||
st7735s_write_cmd(LCD_CMD_VMCTR1);
|
||||
st7735s_write_data(0x0E);
|
||||
|
||||
/* 竖屏模式 */
|
||||
printf("MADCTL...\r\n");
|
||||
st7735s_write_cmd(LCD_CMD_MADCTL);
|
||||
st7735s_write_data(0xC8);
|
||||
/* 关闭反转显示 */
|
||||
st7735s_write_cmd(0x20);
|
||||
/* 关闭空闲模式 */
|
||||
st7735s_write_cmd(0x38);
|
||||
/* 正常模式(非部分模式) */
|
||||
st7735s_write_cmd(0x13);
|
||||
|
||||
/* 像素格式: 16位RGB565 */
|
||||
printf("COLMOD...\r\n");
|
||||
/* 存储器访问控制 (MADCTL)
|
||||
* MY=0 MX=0 MV=0 ML=0 RGB=0 MH=0 → 0x00
|
||||
* 左上角为坐标原点,列从左到右,行从上到下
|
||||
*/
|
||||
st7735s_write_cmd(LCD_CMD_MADCTL);
|
||||
st7735s_write_data(0x00);
|
||||
|
||||
/* 像素格式:16位 RGB565 */
|
||||
st7735s_write_cmd(LCD_CMD_COLMOD);
|
||||
st7735s_write_data(0x05);
|
||||
|
||||
/* Gamma校正 */
|
||||
printf("Gamma...\r\n");
|
||||
st7735s_write_cmd(LCD_CMD_GMCTRP1);
|
||||
st7735s_write_data(0x02);
|
||||
st7735s_write_data(0x1C);
|
||||
st7735s_write_data(0x07);
|
||||
st7735s_write_data(0x12);
|
||||
st7735s_write_data(0x37);
|
||||
st7735s_write_data(0x32);
|
||||
st7735s_write_data(0x29);
|
||||
st7735s_write_data(0x2D);
|
||||
st7735s_write_data(0x29);
|
||||
st7735s_write_data(0x25);
|
||||
st7735s_write_data(0x2B);
|
||||
st7735s_write_data(0x39);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data(0x01);
|
||||
st7735s_write_data(0x03);
|
||||
st7735s_write_data(0x10);
|
||||
st7735s_write_data(0x0F);
|
||||
st7735s_write_data(0x1A);
|
||||
st7735s_write_data(0x0F);
|
||||
st7735s_write_data(0x18);
|
||||
st7735s_write_data(0x2F);
|
||||
st7735s_write_data(0x28);
|
||||
st7735s_write_data(0x22);
|
||||
st7735s_write_data(0x26);
|
||||
st7735s_write_data(0x31);
|
||||
st7735s_write_data(0x1B);
|
||||
st7735s_write_data(0x13);
|
||||
st7735s_write_data(0x1E);
|
||||
st7735s_write_data(0x13);
|
||||
st7735s_write_data(0x11);
|
||||
st7735s_write_data(0x06);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_GMCTRN1);
|
||||
st7735s_write_data(0x03);
|
||||
st7735s_write_data(0x1D);
|
||||
st7735s_write_data(0x07);
|
||||
st7735s_write_data(0x06);
|
||||
st7735s_write_data(0x2E);
|
||||
st7735s_write_data(0x0F);
|
||||
st7735s_write_data(0x1B);
|
||||
st7735s_write_data(0x0F);
|
||||
st7735s_write_data(0x17);
|
||||
st7735s_write_data(0x33);
|
||||
st7735s_write_data(0x2C);
|
||||
st7735s_write_data(0x29);
|
||||
st7735s_write_data(0x2D);
|
||||
st7735s_write_data(0x2E);
|
||||
st7735s_write_data(0x2E);
|
||||
st7735s_write_data(0x37);
|
||||
st7735s_write_data(0x30);
|
||||
st7735s_write_data(0x30);
|
||||
st7735s_write_data(0x39);
|
||||
st7735s_write_data(0x3F);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data(0x02);
|
||||
st7735s_write_data(0x10);
|
||||
st7735s_write_data(0x34);
|
||||
st7735s_write_data(0x14);
|
||||
st7735s_write_data(0x06);
|
||||
|
||||
/* 关闭反转 */
|
||||
st7735s_write_cmd(0x20);
|
||||
|
||||
/* 进入正常显示模式(关键!) */
|
||||
printf("NORON...\r\n");
|
||||
st7735s_write_cmd(0x13);
|
||||
HAL_Delay(10);
|
||||
|
||||
/* 开启显示 */
|
||||
printf("DISPON...\r\n");
|
||||
/* 显示开启 */
|
||||
st7735s_write_cmd(LCD_CMD_DISPON);
|
||||
HAL_Delay(100);
|
||||
printf("LCD init done\r\n");
|
||||
|
||||
printf("LCD init OK\r\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:设置显示窗口
|
||||
* 入口参数:x1 - 起始列
|
||||
* y1 - 起始行
|
||||
* x2 - 结束列
|
||||
* y2 - 结束行
|
||||
* 函数功能:全屏填充
|
||||
* 入口参数:color - 填充颜色 (RGB565)
|
||||
* 返回值:无
|
||||
* 函数说明:设置后续写入操作的窗口区域
|
||||
* 函数说明:用指定颜色填充整个屏幕
|
||||
*/
|
||||
void st7735s_set_window(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
|
||||
void st7735s_fill_screen(uint16_t color)
|
||||
{
|
||||
st7735s_write_cmd(LCD_CMD_CASET);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data((uint8_t)x1);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data((uint8_t)x2);
|
||||
uint32_t i;
|
||||
uint32_t total = (uint32_t)s_lcd_width * s_lcd_height;
|
||||
uint8_t buf[32];
|
||||
uint8_t hi = (uint8_t)(color >> 8);
|
||||
uint8_t lo = (uint8_t)(color & 0xFF);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_RASET);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data((uint8_t)y1);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data((uint8_t)y2);
|
||||
for (i = 0; i < sizeof(buf) / 2; i++) {
|
||||
buf[i * 2] = hi;
|
||||
buf[i * 2 + 1] = lo;
|
||||
}
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_RAMWR);
|
||||
st7735s_set_window(0, 0, s_lcd_width - 1, s_lcd_height - 1);
|
||||
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, GPIO_PIN_SET);
|
||||
i = total * 2;
|
||||
while (i > 0) {
|
||||
uint32_t n = (i > sizeof(buf)) ? sizeof(buf) : i;
|
||||
st7735s_spi_write_bulk(buf, n);
|
||||
i -= n;
|
||||
}
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -314,50 +286,33 @@ void st7735s_set_window(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
|
||||
*/
|
||||
void st7735s_fill_rect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
|
||||
{
|
||||
uint32_t pixel_count;
|
||||
uint16_t i;
|
||||
uint16_t buf[64];
|
||||
uint32_t i;
|
||||
uint32_t total;
|
||||
uint8_t buf[32];
|
||||
uint8_t hi = (uint8_t)(color >> 8);
|
||||
uint8_t lo = (uint8_t)(color & 0xFF);
|
||||
|
||||
if (x1 > x2) {
|
||||
uint16_t t = x1;
|
||||
x1 = x2;
|
||||
x2 = t;
|
||||
}
|
||||
if (y1 > y2) {
|
||||
uint16_t t = y1;
|
||||
y1 = y2;
|
||||
y2 = t;
|
||||
if (x1 > x2) { uint16_t t = x1; x1 = x2; x2 = t; }
|
||||
if (y1 > y2) { uint16_t t = y1; y1 = y2; y2 = t; }
|
||||
|
||||
for (i = 0; i < sizeof(buf) / 2; i++) {
|
||||
buf[i * 2] = hi;
|
||||
buf[i * 2 + 1] = lo;
|
||||
}
|
||||
|
||||
pixel_count = (uint32_t)(x2 - x1 + 1) * (y2 - y1 + 1);
|
||||
|
||||
/* 预填充颜色缓冲区 */
|
||||
for (i = 0; i < 64; i++) {
|
||||
buf[i] = color;
|
||||
}
|
||||
total = (uint32_t)(x2 - x1 + 1) * (y2 - y1 + 1);
|
||||
|
||||
st7735s_set_window(x1, y1, x2, y2);
|
||||
|
||||
/* 分块发送 */
|
||||
while (pixel_count >= 64) {
|
||||
st7735s_write_colors(buf, 64);
|
||||
pixel_count -= 64;
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, GPIO_PIN_SET);
|
||||
i = total * 2;
|
||||
while (i > 0) {
|
||||
uint32_t n = (i > sizeof(buf)) ? sizeof(buf) : i;
|
||||
st7735s_spi_write_bulk(buf, n);
|
||||
i -= n;
|
||||
}
|
||||
if (pixel_count > 0) {
|
||||
st7735s_write_colors(buf, (uint16_t)pixel_count);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:全屏填充
|
||||
* 入口参数:color - 填充颜色 (RGB565)
|
||||
* 返回值:无
|
||||
* 函数说明:用指定颜色填充整个屏幕
|
||||
*/
|
||||
void st7735s_fill_screen(uint16_t color)
|
||||
{
|
||||
printf("fill_screen 0x%04X\r\n", color);
|
||||
st7735s_fill_rect(0, 0, s_lcd_width - 1, s_lcd_height - 1, color);
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -370,252 +325,65 @@ void st7735s_fill_screen(uint16_t color)
|
||||
*/
|
||||
void st7735s_draw_pixel(uint16_t x, uint16_t y, uint16_t color)
|
||||
{
|
||||
if (x >= s_lcd_width || y >= s_lcd_height) {
|
||||
return;
|
||||
}
|
||||
st7735s_set_window(x, y, x, y);
|
||||
st7735s_write_data((uint8_t)(color >> 8));
|
||||
st7735s_write_data((uint8_t)color);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:画线
|
||||
* 入口参数:x1 - 起始列
|
||||
* y1 - 起始行
|
||||
* x2 - 结束列
|
||||
* y2 - 结束行
|
||||
* color - 颜色 (RGB565)
|
||||
* 返回值:无
|
||||
* 函数说明:画一条直线(Bresenham算法)
|
||||
*/
|
||||
void st7735s_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
|
||||
{
|
||||
int16_t dx;
|
||||
int16_t dy;
|
||||
int16_t sx;
|
||||
int16_t sy;
|
||||
int16_t err;
|
||||
int16_t e2;
|
||||
uint16_t x;
|
||||
uint16_t y;
|
||||
|
||||
if (x1 == x2) {
|
||||
/* 垂直线 */
|
||||
if (y1 > y2) {
|
||||
y = y1;
|
||||
y1 = y2;
|
||||
y2 = y;
|
||||
}
|
||||
st7735s_fill_rect(x1, y1, x1, y2, color);
|
||||
return;
|
||||
}
|
||||
|
||||
if (y1 == y2) {
|
||||
/* 水平线 */
|
||||
if (x1 > x2) {
|
||||
x = x1;
|
||||
x1 = x2;
|
||||
x2 = x;
|
||||
}
|
||||
st7735s_fill_rect(x1, y1, x2, y1, color);
|
||||
return;
|
||||
}
|
||||
|
||||
dx = (int16_t)x2 - (int16_t)x1;
|
||||
if (dx < 0) {
|
||||
dx = -dx;
|
||||
}
|
||||
sx = x1 < x2 ? 1 : -1;
|
||||
dy = (int16_t)y2 - (int16_t)y1;
|
||||
if (dy < 0) {
|
||||
dy = -dy;
|
||||
}
|
||||
sy = y1 < y2 ? 1 : -1;
|
||||
err = (dx > dy ? dx : -dy) / 2;
|
||||
|
||||
x = x1;
|
||||
y = y1;
|
||||
|
||||
while (1) {
|
||||
st7735s_draw_pixel(x, y, color);
|
||||
if (x == x2 && y == y2) {
|
||||
break;
|
||||
}
|
||||
e2 = err;
|
||||
if (e2 > -dx) {
|
||||
err -= dy;
|
||||
x += sx;
|
||||
}
|
||||
if (e2 < dy) {
|
||||
err += dx;
|
||||
y += sy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:画矩形
|
||||
* 入口参数:x - 左上角列
|
||||
* y - 左上角行
|
||||
* width - 宽度
|
||||
* height - 高度
|
||||
* color - 颜色 (RGB565)
|
||||
* 返回值:无
|
||||
* 函数说明:画一个空心矩形
|
||||
*/
|
||||
void st7735s_draw_rect(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t color)
|
||||
{
|
||||
st7735s_draw_line(x, y, x + width - 1, y, color);
|
||||
st7735s_draw_line(x, y + height - 1, x + width - 1, y + height - 1, color);
|
||||
st7735s_draw_line(x, y, x, y + height - 1, color);
|
||||
st7735s_draw_line(x + width - 1, y, x + width - 1, y + height - 1, color);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:画圆
|
||||
* 入口参数:x0 - 圆心列
|
||||
* y0 - 圆心行
|
||||
* radius - 半径
|
||||
* color - 颜色 (RGB565)
|
||||
* 返回值:无
|
||||
* 函数说明:画一个空心圆(Bresenham算法)
|
||||
*/
|
||||
void st7735s_draw_circle(uint16_t x0, uint16_t y0, uint16_t radius, uint16_t color)
|
||||
{
|
||||
int16_t f = 1 - radius;
|
||||
int16_t ddf_x = 1;
|
||||
int16_t ddf_y = -2 * radius;
|
||||
int16_t x = 0;
|
||||
int16_t y = radius;
|
||||
|
||||
st7735s_draw_pixel(x0, y0 + radius, color);
|
||||
st7735s_draw_pixel(x0, y0 - radius, color);
|
||||
st7735s_draw_pixel(x0 + radius, y0, color);
|
||||
st7735s_draw_pixel(x0 - radius, y0, color);
|
||||
|
||||
while (x < y) {
|
||||
if (f >= 0) {
|
||||
y--;
|
||||
ddf_y += 2;
|
||||
f += ddf_y;
|
||||
}
|
||||
x++;
|
||||
ddf_x += 2;
|
||||
f += ddf_x;
|
||||
|
||||
st7735s_draw_pixel(x0 + x, y0 + y, color);
|
||||
st7735s_draw_pixel(x0 - x, y0 + y, color);
|
||||
st7735s_draw_pixel(x0 + x, y0 - y, color);
|
||||
st7735s_draw_pixel(x0 - x, y0 - y, color);
|
||||
st7735s_draw_pixel(x0 + y, y0 + x, color);
|
||||
st7735s_draw_pixel(x0 - y, y0 + x, color);
|
||||
st7735s_draw_pixel(x0 + y, y0 - x, color);
|
||||
st7735s_draw_pixel(x0 - y, y0 - x, color);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:显示字符
|
||||
* 入口参数:x - 列坐标
|
||||
* y - 行坐标
|
||||
* ch - 字符 (ASCII 32-126)
|
||||
* color - 字符颜色 (RGB565)
|
||||
* bg_color - 背景颜色 (RGB565)
|
||||
* 返回值:无
|
||||
* 函数说明:在指定位置显示一个8x16 ASCII字符
|
||||
*/
|
||||
void st7735s_draw_char(uint16_t x, uint16_t y, char ch, uint16_t color, uint16_t bg_color)
|
||||
{
|
||||
uint16_t i;
|
||||
uint16_t j;
|
||||
uint8_t line;
|
||||
|
||||
if (ch < 32 || ch > 126) {
|
||||
ch = ' ';
|
||||
}
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
line = LCD_F8x16[ch - 32][i];
|
||||
for (j = 0; j < 8; j++) {
|
||||
if (line & 0x80) {
|
||||
st7735s_draw_pixel(x + j, y + i, color);
|
||||
} else {
|
||||
st7735s_draw_pixel(x + j, y + i, bg_color);
|
||||
}
|
||||
line <<= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:显示字符串
|
||||
* 入口参数:x - 列坐标
|
||||
* y - 行坐标
|
||||
* str - 字符串指针
|
||||
* color - 字符颜色 (RGB565)
|
||||
* bg_color - 背景颜色 (RGB565)
|
||||
* 返回值:无
|
||||
* 函数说明:在指定位置显示字符串
|
||||
*/
|
||||
void st7735s_draw_string(uint16_t x, uint16_t y, const char *str, uint16_t color, uint16_t bg_color)
|
||||
{
|
||||
while (*str) {
|
||||
if (x + 8 > s_lcd_width) {
|
||||
x = 0;
|
||||
y += 16;
|
||||
}
|
||||
if (y + 16 > s_lcd_height) {
|
||||
break;
|
||||
}
|
||||
st7735s_draw_char(x, y, *str, color, bg_color);
|
||||
x += FONT_WIDTH_8;
|
||||
str++;
|
||||
}
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, GPIO_PIN_SET);
|
||||
st7735s_spi_write_byte((uint8_t)(color >> 8));
|
||||
st7735s_spi_write_byte((uint8_t)(color & 0xFF));
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:设置屏幕方向
|
||||
* 入口参数:rotation - 方向 (0-3)
|
||||
* 返回值:无
|
||||
* 函数说明:0:竖屏 1:横屏 2:竖屏反转 3:横屏反转
|
||||
* 函数说明:通过修改MADCTL(36h)寄存器控制显示方向
|
||||
* 0: 竖屏 1: 横屏 2: 竖屏反转 3: 横屏反转
|
||||
*/
|
||||
void st7735s_set_rotation(uint8_t rotation)
|
||||
{
|
||||
st7735s_write_cmd(LCD_CMD_MADCTL);
|
||||
uint8_t madctl = 0xC0;
|
||||
|
||||
switch (rotation) {
|
||||
case 0:
|
||||
st7735s_write_data(0xC8);
|
||||
s_lcd_width = 128;
|
||||
s_lcd_height = 160;
|
||||
break;
|
||||
case 1:
|
||||
st7735s_write_data(0x78);
|
||||
s_lcd_width = 160;
|
||||
s_lcd_height = 128;
|
||||
break;
|
||||
case 2:
|
||||
st7735s_write_data(0x08);
|
||||
s_lcd_width = 128;
|
||||
s_lcd_height = 160;
|
||||
break;
|
||||
case 3:
|
||||
st7735s_write_data(0xA8);
|
||||
s_lcd_width = 160;
|
||||
s_lcd_height = 128;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
case 0:
|
||||
madctl = 0xC0;
|
||||
s_lcd_width = LCD_WIDTH;
|
||||
s_lcd_height = LCD_HEIGHT;
|
||||
break;
|
||||
case 1:
|
||||
madctl = 0xA0;
|
||||
s_lcd_width = LCD_HEIGHT;
|
||||
s_lcd_height = LCD_WIDTH;
|
||||
break;
|
||||
case 2:
|
||||
madctl = 0x00;
|
||||
s_lcd_width = LCD_WIDTH;
|
||||
s_lcd_height = LCD_HEIGHT;
|
||||
break;
|
||||
case 3:
|
||||
madctl = 0x60;
|
||||
s_lcd_width = LCD_HEIGHT;
|
||||
s_lcd_height = LCD_WIDTH;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_MADCTL);
|
||||
st7735s_write_data(madctl);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:设置背光
|
||||
* 入口参数:state - 1:开 0:关
|
||||
* 返回值:无
|
||||
* 函数说明:控制LCD背光(如有硬件背光控制)
|
||||
* 函数说明:通过PC13控制背光(如果有背光控制引脚则修改)
|
||||
*/
|
||||
void st7735s_backlight(uint8_t state)
|
||||
{
|
||||
(void)state;
|
||||
/* 当前硬件未连接背光控制引脚,预留接口 */
|
||||
if (state) {
|
||||
printf("backlight on\r\n");
|
||||
} else {
|
||||
printf("backlight off\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user