Files
stc32g128k/Docs/60_coding/mod-lcd.md

45 lines
1.7 KiB
Markdown
Raw Normal View History

# 编码实现 - LCD 显示与字模驱动 (docs/60_coding/mod-lcd.md)
本文件对应 LCD 驱动与字模渲染的具体编码实现细节。
## 1. 对应代码源文件
* [Drivers/lcd.c](file:///c:/workfile/105/stc32g12k128/Drivers/lcd.c) (LCD 初始化、SPI 数据写、画图画字函数)
* [Drivers/lcd.h](file:///c:/workfile/105/stc32g12k128/Drivers/lcd.h) (管脚接口与画图画字声明)
* [Drivers/lcd_font.h](file:///c:/workfile/105/stc32g12k128/Drivers/lcd_font.h) (ASCII 8x16 点阵字模与图标数据)
## 2. 关键代码片段与逻辑实现
### 2.1 局部窗口设置 (X轴偏移量修正)
`lcd.c` 中配置窗口时,需要向 X 轴坐标加上 4 像素偏差:
```c
void LCD_SetWindow(u16 x1, u16 y1, u16 x2, u16 y2) {
x1 += 4;
x2 += 4;
LCD_WriteCmd(0x2A); // Column Address Set
LCD_WriteData(x1 >> 8);
LCD_WriteData(x1 & 0xFF);
LCD_WriteData(x2 >> 8);
LCD_WriteData(x2 & 0xFF);
LCD_WriteCmd(0x2B); // Row Address Set
LCD_WriteData(y1 >> 8);
LCD_WriteData(y1 & 0xFF);
LCD_WriteData(y2 >> 8);
LCD_WriteData(y2 & 0xFF);
}
```
### 2.2 电池图标与百分比坐标对齐
`main.c` 的时钟重绘函数中,通过以下坐标绘制电量:
```c
// 绘制百分比字符
LCD_ShowString(62, 24, "85%", WHITE, BLACK);
// 绘制电池位图
LCD_ShowImage(92, 27, 24, 12, BATTERY_ICON, RED, BLACK);
```
两者 Y 轴保持 24 与 27 对齐X 轴保持 30 像素的安全边距62 + 3 * 8 = 86与 92 之间有 6 像素的安全间距),防止文字错乱或重合。
### 2.3 字模百分号偏置
`lcd_font.h` 的 ASCII 字符数组中,百分号 `%`(十进制 37十六进制 0x25的字模通过在字模绘制时向上平移 2 像素对齐普通数字的下基准线。