配置 PWM ADC LCD

This commit is contained in:
2026-07-14 17:51:39 +08:00
parent 0a5c478a42
commit 2d0d000a38
67 changed files with 33357 additions and 911 deletions

View File

@@ -1,11 +1,14 @@
/*
* 模块名称ST7735S LCD驱动
* 模块功能:提供128x160 RGB TFT屏幕的初始化、画点、画线、填充、显示字符串等功能
* 适用平台STM32F103C8T6 + SPI2
* 模块名称ST7735S LCD驱动(底层)
* 模块功能:提供ST7735S屏幕的硬件SPI通信、初始化、填充、画点、方向控制等底层操作
* 绘图函数见 lcd.h
* 适用平台STM32F103C8T6 + 硬件SPI2
* 作者:王建锋
* 创建日期2026-07-13
* 修改记录:
* 2026-07-13 王建锋 创建初始版本
* 2026-07-14 王建锋 软件SPI -> 硬件SPI2
* 2026-07-14 王建锋 拆分:绘图函数移入 lcd.h
*/
#ifndef __ST7735S_H
@@ -40,16 +43,13 @@ extern "C" {
#define RGB565(r, g, b) ((uint16_t)(((r) & 0xF8) << 8 | ((g) & 0xFC) << 3 | ((b) >> 3)))
/* LCD控制引脚定义 - 根据CubeMX配置修改 */
/* SCL(PB13)和SDA(PB15)由SPI2外设管理无需GPIO定义 */
#define LCD_CS_PORT LCD_CS_GPIO_Port
#define LCD_CS_PIN LCD_CS_Pin
#define LCD_DC_PORT LCD_DC_GPIO_Port
#define LCD_DC_PIN LCD_DC_Pin
#define LCD_RST_PORT LCD_RST_GPIO_Port
#define LCD_RST_PIN LCD_RST_Pin
#define LCD_SCL_PORT GPIOB
#define LCD_SCL_PIN GPIO_PIN_13
#define LCD_SDA_PORT GPIOB
#define LCD_SDA_PIN GPIO_PIN_15
/* LCD命令定义 */
#define LCD_CMD_SWRESET 0x01 /* 软件复位 */
@@ -74,17 +74,42 @@ extern "C" {
#define LCD_CMD_GMCTRP1 0xE0 /* Gamma正向校正 */
#define LCD_CMD_GMCTRN1 0xE1 /* Gamma反向校正 */
/* 字体定义 */
#define FONT_WIDTH_8 8 /* 8x16字体宽度含间距 */
#define FONT_HEIGHT_16 16 /* 8x16字体高度 */
/* 外部变量 - LCD宽高由旋转函数更新 */
extern uint16_t s_lcd_width;
extern uint16_t s_lcd_height;
/* === 底层SPI通信函数 === */
/*
* 函数功能:LCD初始化
* 入口参数:
* 函数功能:硬件SPI写一个字节
* 入口参数:data - 要发送的数据
* 返回值:无
* 函数说明初始化ST7735S屏幕包括SPI、GPIO、显示参数配置
*/
void st7735s_init(void);
void st7735s_spi_write_byte(uint8_t data);
/*
* 函数功能硬件SPI批量写数据
* 入口参数data - 数据缓冲区
* len - 字节数
* 返回值:无
*/
void st7735s_spi_write_bulk(const uint8_t *data, uint32_t len);
/*
* 函数功能写入命令CS+DC管理单字节
* 入口参数cmd - 命令字节
* 返回值:无
*/
void st7735s_write_cmd(uint8_t cmd);
/*
* 函数功能写入数据CS+DC管理单字节
* 入口参数data - 数据字节
* 返回值:无
*/
void st7735s_write_data(uint8_t data);
/* === 驱动控制函数 === */
/*
* 函数功能:设置显示窗口
@@ -93,10 +118,23 @@ void st7735s_init(void);
* x2 - 结束列
* y2 - 结束行
* 返回值:无
* 函数说明:设置后续写入操作的窗口区域
*/
void st7735s_set_window(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
/*
* 函数功能LCD初始化
* 入口参数:无
* 返回值:无
*/
void st7735s_init(void);
/*
* 函数功能:全屏填充
* 入口参数color - 填充颜色 (RGB565)
* 返回值:无
*/
void st7735s_fill_screen(uint16_t color);
/*
* 函数功能:填充矩形区域
* 入口参数x1 - 起始列
@@ -105,96 +143,22 @@ void st7735s_set_window(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
* y2 - 结束行
* color - 填充颜色 (RGB565)
* 返回值:无
* 函数说明:用指定颜色填充矩形区域
*/
void st7735s_fill_rect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
/*
* 函数功能:全屏填充
* 入口参数color - 填充颜色 (RGB565)
* 返回值:无
* 函数说明:用指定颜色填充整个屏幕
*/
void st7735s_fill_screen(uint16_t color);
/*
* 函数功能:画点
* 入口参数x - 列坐标
* y - 行坐标
* color - 颜色 (RGB565)
* 返回值:无
* 函数说明:在指定位置画一个点
*/
void st7735s_draw_pixel(uint16_t x, uint16_t y, uint16_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);
/*
* 函数功能:画矩形
* 入口参数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);
/*
* 函数功能:画圆
* 入口参数x0 - 圆心列
* y0 - 圆心行
* radius - 半径
* color - 颜色 (RGB565)
* 返回值:无
* 函数说明画一个空心圆Bresenham算法
*/
void st7735s_draw_circle(uint16_t x0, uint16_t y0, uint16_t radius, uint16_t 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);
/*
* 函数功能:显示字符串
* 入口参数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);
/*
* 函数功能:设置屏幕方向
* 入口参数rotation - 方向 (0-3)
* 返回值:无
* 函数说明:设置屏幕显示方向
* 0: 竖屏
* 1: 横屏
* 2: 竖屏反转
* 3: 横屏反转
*/
void st7735s_set_rotation(uint8_t rotation);
@@ -202,7 +166,6 @@ void st7735s_set_rotation(uint8_t rotation);
* 函数功能:设置背光
* 入口参数state - 1:开 0:关
* 返回值:无
* 函数说明控制LCD背光如有硬件背光控制
*/
void st7735s_backlight(uint8_t state);