软件SPI可以驱动屏幕
This commit is contained in:
297
STM32F103C8T6/Drivers/BSP/LCD/lcd_data.c
Normal file
297
STM32F103C8T6/Drivers/BSP/LCD/lcd_data.c
Normal file
@@ -0,0 +1,297 @@
|
||||
#include "lcd_data.h"
|
||||
/*ASCII字模数据*********************/
|
||||
|
||||
/*宽8像素,高16像素*/
|
||||
const uint8_t LCD_F8x16[][16] =
|
||||
{
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
|
||||
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,// ! 1
|
||||
0x00,0x16,0x0E,0x00,0x16,0x0E,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// " 2
|
||||
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
|
||||
0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,// # 3
|
||||
0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
|
||||
0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,// $ 4
|
||||
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
|
||||
0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,// % 5
|
||||
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
|
||||
0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,// & 6
|
||||
0x00,0x00,0x00,0x16,0x0E,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// ' 7
|
||||
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
|
||||
0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,// ( 8
|
||||
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
|
||||
0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,// ) 9
|
||||
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
|
||||
0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,// * 10
|
||||
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
|
||||
0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,// + 11
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,// , 12
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,// - 13
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,// . 14
|
||||
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
|
||||
0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,// / 15
|
||||
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
|
||||
0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,// 0 16
|
||||
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
|
||||
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// 1 17
|
||||
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
|
||||
0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,// 2 18
|
||||
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
|
||||
0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,// 3 19
|
||||
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
|
||||
0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,// 4 20
|
||||
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
|
||||
0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,// 5 21
|
||||
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
|
||||
0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,// 6 22
|
||||
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
|
||||
0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,// 7 23
|
||||
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
|
||||
0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,// 8 24
|
||||
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
|
||||
0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,// 9 25
|
||||
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,// : 26
|
||||
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
|
||||
0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,// ; 27
|
||||
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
|
||||
0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,// < 28
|
||||
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
|
||||
0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,// = 29
|
||||
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
|
||||
0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,// > 30
|
||||
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
|
||||
0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,// ? 31
|
||||
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
|
||||
0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,// @ 32
|
||||
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
|
||||
0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,// A 33
|
||||
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
|
||||
0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,// B 34
|
||||
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
|
||||
0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,// C 35
|
||||
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
|
||||
0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,// D 36
|
||||
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
|
||||
0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,// E 37
|
||||
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
|
||||
0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,// F 38
|
||||
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
|
||||
0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,// G 39
|
||||
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
|
||||
0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,// H 40
|
||||
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
|
||||
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// I 41
|
||||
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
|
||||
0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,// J 42
|
||||
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
|
||||
0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,// K 43
|
||||
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
|
||||
0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,// L 44
|
||||
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
|
||||
0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,// M 45
|
||||
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
|
||||
0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,// N 46
|
||||
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
|
||||
0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,// O 47
|
||||
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
|
||||
0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,// P 48
|
||||
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
|
||||
0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,// Q 49
|
||||
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
|
||||
0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,// R 50
|
||||
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
|
||||
0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,// S 51
|
||||
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
|
||||
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,// T 52
|
||||
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
|
||||
0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,// U 53
|
||||
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
|
||||
0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,// V 54
|
||||
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
|
||||
0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,// W 55
|
||||
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
|
||||
0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,// X 56
|
||||
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
|
||||
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,// Y 57
|
||||
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
|
||||
0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,// Z 58
|
||||
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
|
||||
0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,// [ 59
|
||||
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,// \ 60
|
||||
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
|
||||
0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,// ] 61
|
||||
0x00,0x20,0x10,0x08,0x04,0x08,0x10,0x20,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// ^ 62
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,// _ 63
|
||||
0x00,0x02,0x04,0x08,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// ` 64
|
||||
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
|
||||
0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,// a 65
|
||||
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
|
||||
0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,// b 66
|
||||
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
|
||||
0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,// c 67
|
||||
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
|
||||
0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,// d 68
|
||||
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
|
||||
0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,// e 69
|
||||
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
|
||||
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// f 70
|
||||
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
|
||||
0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,// g 71
|
||||
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
|
||||
0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,// h 72
|
||||
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
|
||||
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// i 73
|
||||
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
|
||||
0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,// j 74
|
||||
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
|
||||
0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,// k 75
|
||||
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
|
||||
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// l 76
|
||||
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
|
||||
0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,// m 77
|
||||
0x00,0x80,0x80,0x00,0x80,0x80,0x00,0x00,
|
||||
0x00,0x20,0x3F,0x21,0x00,0x20,0x3F,0x20,// n 78
|
||||
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
|
||||
0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,// o 79
|
||||
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
|
||||
0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,// p 80
|
||||
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
|
||||
0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,// q 81
|
||||
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
|
||||
0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,// r 82
|
||||
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
|
||||
0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,// s 83
|
||||
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
|
||||
0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,// t 84
|
||||
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
|
||||
0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,// u 85
|
||||
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
|
||||
0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,// v 86
|
||||
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
|
||||
0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,// w 87
|
||||
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
|
||||
0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,// x 88
|
||||
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
|
||||
0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,// y 89
|
||||
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
|
||||
0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,// z 90
|
||||
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
|
||||
0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,// { 91
|
||||
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,// | 92
|
||||
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
|
||||
0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,// } 93
|
||||
0x00,0x80,0x40,0x40,0x80,0x00,0x00,0x80,
|
||||
0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,// ~ 94
|
||||
};
|
||||
|
||||
/*宽6像素,高8像素*/
|
||||
const uint8_t LCD_F6x8[][6] =
|
||||
{
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,// 0
|
||||
0x00,0x00,0x00,0x2F,0x00,0x00,// ! 1
|
||||
0x00,0x00,0x07,0x00,0x07,0x00,// " 2
|
||||
0x00,0x14,0x7F,0x14,0x7F,0x14,// # 3
|
||||
0x00,0x24,0x2A,0x7F,0x2A,0x12,// $ 4
|
||||
0x00,0x23,0x13,0x08,0x64,0x62,// % 5
|
||||
0x00,0x36,0x49,0x55,0x22,0x50,// & 6
|
||||
0x00,0x00,0x00,0x07,0x00,0x00,// ' 7
|
||||
0x00,0x00,0x1C,0x22,0x41,0x00,// ( 8
|
||||
0x00,0x00,0x41,0x22,0x1C,0x00,// ) 9
|
||||
0x00,0x14,0x08,0x3E,0x08,0x14,// * 10
|
||||
0x00,0x08,0x08,0x3E,0x08,0x08,// + 11
|
||||
0x00,0x00,0x00,0xA0,0x60,0x00,// , 12
|
||||
0x00,0x08,0x08,0x08,0x08,0x08,// - 13
|
||||
0x00,0x00,0x60,0x60,0x00,0x00,// . 14
|
||||
0x00,0x20,0x10,0x08,0x04,0x02,// / 15
|
||||
0x00,0x3E,0x51,0x49,0x45,0x3E,// 0 16
|
||||
0x00,0x00,0x42,0x7F,0x40,0x00,// 1 17
|
||||
0x00,0x42,0x61,0x51,0x49,0x46,// 2 18
|
||||
0x00,0x21,0x41,0x45,0x4B,0x31,// 3 19
|
||||
0x00,0x18,0x14,0x12,0x7F,0x10,// 4 20
|
||||
0x00,0x27,0x45,0x45,0x45,0x39,// 5 21
|
||||
0x00,0x3C,0x4A,0x49,0x49,0x30,// 6 22
|
||||
0x00,0x01,0x71,0x09,0x05,0x03,// 7 23
|
||||
0x00,0x36,0x49,0x49,0x49,0x36,// 8 24
|
||||
0x00,0x06,0x49,0x49,0x29,0x1E,// 9 25
|
||||
0x00,0x00,0x36,0x36,0x00,0x00,// : 26
|
||||
0x00,0x00,0x56,0x36,0x00,0x00,// ; 27
|
||||
0x00,0x08,0x14,0x22,0x41,0x00,// < 28
|
||||
0x00,0x14,0x14,0x14,0x14,0x14,// = 29
|
||||
0x00,0x00,0x41,0x22,0x14,0x08,// > 30
|
||||
0x00,0x02,0x01,0x51,0x09,0x06,// ? 31
|
||||
0x00,0x3E,0x49,0x55,0x59,0x2E,// @ 32
|
||||
0x00,0x7C,0x12,0x11,0x12,0x7C,// A 33
|
||||
0x00,0x7F,0x49,0x49,0x49,0x36,// B 34
|
||||
0x00,0x3E,0x41,0x41,0x41,0x22,// C 35
|
||||
0x00,0x7F,0x41,0x41,0x22,0x1C,// D 36
|
||||
0x00,0x7F,0x49,0x49,0x49,0x41,// E 37
|
||||
0x00,0x7F,0x09,0x09,0x09,0x01,// F 38
|
||||
0x00,0x3E,0x41,0x49,0x49,0x7A,// G 39
|
||||
0x00,0x7F,0x08,0x08,0x08,0x7F,// H 40
|
||||
0x00,0x00,0x41,0x7F,0x41,0x00,// I 41
|
||||
0x00,0x20,0x40,0x41,0x3F,0x01,// J 42
|
||||
0x00,0x7F,0x08,0x14,0x22,0x41,// K 43
|
||||
0x00,0x7F,0x40,0x40,0x40,0x40,// L 44
|
||||
0x00,0x7F,0x02,0x0C,0x02,0x7F,// M 45
|
||||
0x00,0x7F,0x04,0x08,0x10,0x7F,// N 46
|
||||
0x00,0x3E,0x41,0x41,0x41,0x3E,// O 47
|
||||
0x00,0x7F,0x09,0x09,0x09,0x06,// P 48
|
||||
0x00,0x3E,0x41,0x51,0x21,0x5E,// Q 49
|
||||
0x00,0x7F,0x09,0x19,0x29,0x46,// R 50
|
||||
0x00,0x46,0x49,0x49,0x49,0x31,// S 51
|
||||
0x00,0x01,0x01,0x7F,0x01,0x01,// T 52
|
||||
0x00,0x3F,0x40,0x40,0x40,0x3F,// U 53
|
||||
0x00,0x1F,0x20,0x40,0x20,0x1F,// V 54
|
||||
0x00,0x3F,0x40,0x38,0x40,0x3F,// W 55
|
||||
0x00,0x63,0x14,0x08,0x14,0x63,// X 56
|
||||
0x00,0x07,0x08,0x70,0x08,0x07,// Y 57
|
||||
0x00,0x61,0x51,0x49,0x45,0x43,// Z 58
|
||||
0x00,0x00,0x7F,0x41,0x41,0x00,// [ 59
|
||||
0x00,0x02,0x04,0x08,0x10,0x20,// \ 60
|
||||
0x00,0x00,0x41,0x41,0x7F,0x00,// ] 61
|
||||
0x00,0x04,0x02,0x01,0x02,0x04,// ^ 62
|
||||
0x00,0x40,0x40,0x40,0x40,0x40,// _ 63
|
||||
0x00,0x00,0x01,0x02,0x04,0x00,// ` 64
|
||||
0x00,0x20,0x54,0x54,0x54,0x78,// a 65
|
||||
0x00,0x7F,0x48,0x44,0x44,0x38,// b 66
|
||||
0x00,0x38,0x44,0x44,0x44,0x20,// c 67
|
||||
0x00,0x38,0x44,0x44,0x48,0x7F,// d 68
|
||||
0x00,0x38,0x54,0x54,0x54,0x18,// e 69
|
||||
0x00,0x08,0x7E,0x09,0x01,0x02,// f 70
|
||||
0x00,0x18,0xA4,0xA4,0xA4,0x7C,// g 71
|
||||
0x00,0x7F,0x08,0x04,0x04,0x78,// h 72
|
||||
0x00,0x00,0x44,0x7D,0x40,0x00,// i 73
|
||||
0x00,0x40,0x80,0x84,0x7D,0x00,// j 74
|
||||
0x00,0x7F,0x10,0x28,0x44,0x00,// k 75
|
||||
0x00,0x00,0x41,0x7F,0x40,0x00,// l 76
|
||||
0x00,0x7C,0x04,0x18,0x04,0x78,// m 77
|
||||
0x00,0x7C,0x08,0x04,0x04,0x78,// n 78
|
||||
0x00,0x38,0x44,0x44,0x44,0x38,// o 79
|
||||
0x00,0xFC,0x24,0x24,0x24,0x18,// p 80
|
||||
0x00,0x18,0x24,0x24,0x18,0xFC,// q 81
|
||||
0x00,0x7C,0x08,0x04,0x04,0x08,// r 82
|
||||
0x00,0x48,0x54,0x54,0x54,0x20,// s 83
|
||||
0x00,0x04,0x3F,0x44,0x40,0x20,// t 84
|
||||
0x00,0x3C,0x40,0x40,0x20,0x7C,// u 85
|
||||
0x00,0x1C,0x20,0x40,0x20,0x1C,// v 86
|
||||
0x00,0x3C,0x40,0x30,0x40,0x3C,// w 87
|
||||
0x00,0x44,0x28,0x10,0x28,0x44,// x 88
|
||||
0x00,0x1C,0xA0,0xA0,0xA0,0x7C,// y 89
|
||||
0x00,0x44,0x64,0x54,0x4C,0x44,// z 90
|
||||
0x00,0x00,0x08,0x7F,0x41,0x00,// { 91
|
||||
0x00,0x00,0x00,0x7F,0x00,0x00,// | 92
|
||||
0x00,0x00,0x41,0x7F,0x08,0x00,// } 93
|
||||
0x00,0x08,0x04,0x08,0x10,0x08,// ~ 94
|
||||
};
|
||||
10
STM32F103C8T6/Drivers/BSP/LCD/lcd_data.h
Normal file
10
STM32F103C8T6/Drivers/BSP/LCD/lcd_data.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef __LCD_DATA_H_
|
||||
#define __LCD_DATA_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/*ASCII字模数据声明*/
|
||||
extern const uint8_t LCD_F8x16[][16];
|
||||
extern const uint8_t LCD_F6x8[][6];
|
||||
|
||||
#endif
|
||||
621
STM32F103C8T6/Drivers/BSP/LCD/st7735s.c
Normal file
621
STM32F103C8T6/Drivers/BSP/LCD/st7735s.c
Normal file
@@ -0,0 +1,621 @@
|
||||
/*
|
||||
* 模块名称:ST7735S LCD驱动
|
||||
* 模块功能:提供128x160 RGB TFT屏幕的初始化、画点、画线、填充、显示字符串等功能
|
||||
* 适用平台:STM32F103C8T6 + SPI2
|
||||
* 作者:王建锋
|
||||
* 创建日期:2026-07-13
|
||||
* 修改记录:
|
||||
* 2026-07-13 王建锋 创建初始版本
|
||||
*/
|
||||
|
||||
#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写一个字节
|
||||
* 入口参数:data - 要发送的数据
|
||||
* 返回值:无
|
||||
* 函数说明:模拟SPI时序发送一个字节,MSB先
|
||||
*/
|
||||
static 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;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:写入命令
|
||||
* 入口参数:cmd - 命令字节
|
||||
* 返回值:无
|
||||
* 函数说明:拉低DC,选择芯片,发送命令,释放芯片
|
||||
*/
|
||||
static 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);
|
||||
st7735s_spi_write_byte(cmd);
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:写入数据
|
||||
* 入口参数:data - 数据字节
|
||||
* 返回值:无
|
||||
* 函数说明:拉高DC,选择芯片,发送数据,释放芯片
|
||||
*/
|
||||
static 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);
|
||||
st7735s_spi_write_byte(data);
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:写入多字节数据(CS保持低)
|
||||
* 入口参数:p_data - 数据缓冲区指针
|
||||
* len - 字节数
|
||||
* 返回值:无
|
||||
* 函数说明:用于连续写入多个数据字节,省去中间CS跳变
|
||||
*/
|
||||
static void st7735s_write_data_multi(const uint8_t *p_data, 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(p_data[i]);
|
||||
}
|
||||
HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:写入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);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:LCD初始化
|
||||
* 入口参数:无
|
||||
* 返回值:无
|
||||
* 函数说明:执行ST7735S标准初始化序列
|
||||
*/
|
||||
void st7735s_init(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
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在初始化期间被错误选中 */
|
||||
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_GPIO_WritePin(LCD_RST_PORT, LCD_RST_PIN, GPIO_PIN_SET);
|
||||
HAL_Delay(120);
|
||||
|
||||
/* 软件复位 */
|
||||
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);
|
||||
|
||||
/* 帧率控制 */
|
||||
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_cmd(LCD_CMD_FRMCTR2);
|
||||
st7735s_write_data(0x01);
|
||||
st7735s_write_data(0x2C);
|
||||
st7735s_write_data(0x2D);
|
||||
|
||||
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_cmd(LCD_CMD_INVCTR);
|
||||
st7735s_write_data(0x07);
|
||||
|
||||
/* 功率控制 */
|
||||
printf("Power ctrl...\r\n");
|
||||
st7735s_write_cmd(LCD_CMD_PWCTR1);
|
||||
st7735s_write_data(0xA2);
|
||||
st7735s_write_data(0x02);
|
||||
st7735s_write_data(0x84);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_PWCTR2);
|
||||
st7735s_write_data(0xC5);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_PWCTR3);
|
||||
st7735s_write_data(0x0A);
|
||||
st7735s_write_data(0x00);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_PWCTR4);
|
||||
st7735s_write_data(0x8A);
|
||||
st7735s_write_data(0x2A);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_PWCTR5);
|
||||
st7735s_write_data(0x8A);
|
||||
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);
|
||||
|
||||
/* 像素格式: 16位RGB565 */
|
||||
printf("COLMOD...\r\n");
|
||||
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_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(0x2C);
|
||||
st7735s_write_data(0x29);
|
||||
st7735s_write_data(0x2D);
|
||||
st7735s_write_data(0x2E);
|
||||
st7735s_write_data(0x2E);
|
||||
st7735s_write_data(0x37);
|
||||
st7735s_write_data(0x3F);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data(0x00);
|
||||
st7735s_write_data(0x02);
|
||||
st7735s_write_data(0x10);
|
||||
|
||||
/* 关闭反转 */
|
||||
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");
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:设置显示窗口
|
||||
* 入口参数:x1 - 起始列
|
||||
* y1 - 起始行
|
||||
* x2 - 结束列
|
||||
* y2 - 结束行
|
||||
* 返回值:无
|
||||
* 函数说明:设置后续写入操作的窗口区域
|
||||
*/
|
||||
void st7735s_set_window(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
|
||||
{
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
st7735s_write_cmd(LCD_CMD_RAMWR);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:填充矩形区域
|
||||
* 入口参数:x1 - 起始列
|
||||
* y1 - 起始行
|
||||
* x2 - 结束列
|
||||
* y2 - 结束行
|
||||
* color - 填充颜色 (RGB565)
|
||||
* 返回值:无
|
||||
* 函数说明:用指定颜色填充矩形区域
|
||||
*/
|
||||
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];
|
||||
|
||||
if (x1 > x2) {
|
||||
uint16_t t = x1;
|
||||
x1 = x2;
|
||||
x2 = t;
|
||||
}
|
||||
if (y1 > y2) {
|
||||
uint16_t t = y1;
|
||||
y1 = y2;
|
||||
y2 = t;
|
||||
}
|
||||
|
||||
pixel_count = (uint32_t)(x2 - x1 + 1) * (y2 - y1 + 1);
|
||||
|
||||
/* 预填充颜色缓冲区 */
|
||||
for (i = 0; i < 64; i++) {
|
||||
buf[i] = color;
|
||||
}
|
||||
|
||||
st7735s_set_window(x1, y1, x2, y2);
|
||||
|
||||
/* 分块发送 */
|
||||
while (pixel_count >= 64) {
|
||||
st7735s_write_colors(buf, 64);
|
||||
pixel_count -= 64;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:画点
|
||||
* 入口参数:x - 列坐标
|
||||
* y - 行坐标
|
||||
* color - 颜色 (RGB565)
|
||||
* 返回值:无
|
||||
* 函数说明:在指定位置画一个点
|
||||
*/
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:设置屏幕方向
|
||||
* 入口参数:rotation - 方向 (0-3)
|
||||
* 返回值:无
|
||||
* 函数说明:0:竖屏 1:横屏 2:竖屏反转 3:横屏反转
|
||||
*/
|
||||
void st7735s_set_rotation(uint8_t rotation)
|
||||
{
|
||||
st7735s_write_cmd(LCD_CMD_MADCTL);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:设置背光
|
||||
* 入口参数:state - 1:开 0:关
|
||||
* 返回值:无
|
||||
* 函数说明:控制LCD背光(如有硬件背光控制)
|
||||
*/
|
||||
void st7735s_backlight(uint8_t state)
|
||||
{
|
||||
(void)state;
|
||||
/* 当前硬件未连接背光控制引脚,预留接口 */
|
||||
}
|
||||
213
STM32F103C8T6/Drivers/BSP/LCD/st7735s.h
Normal file
213
STM32F103C8T6/Drivers/BSP/LCD/st7735s.h
Normal file
@@ -0,0 +1,213 @@
|
||||
/*
|
||||
* 模块名称:ST7735S LCD驱动
|
||||
* 模块功能:提供128x160 RGB TFT屏幕的初始化、画点、画线、填充、显示字符串等功能
|
||||
* 适用平台:STM32F103C8T6 + SPI2
|
||||
* 作者:王建锋
|
||||
* 创建日期:2026-07-13
|
||||
* 修改记录:
|
||||
* 2026-07-13 王建锋 创建初始版本
|
||||
*/
|
||||
|
||||
#ifndef __ST7735S_H
|
||||
#define __ST7735S_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "main.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/* 屏幕参数 */
|
||||
#define LCD_WIDTH 128 /* 屏幕宽度 */
|
||||
#define LCD_HEIGHT 160 /* 屏幕高度 */
|
||||
|
||||
/* 颜色定义 (RGB565) */
|
||||
#define COLOR_BLACK 0x0000
|
||||
#define COLOR_WHITE 0xFFFF
|
||||
#define COLOR_RED 0xF800
|
||||
#define COLOR_GREEN 0x07E0
|
||||
#define COLOR_BLUE 0x001F
|
||||
#define COLOR_YELLOW 0xFFE0
|
||||
#define COLOR_CYAN 0x07FF
|
||||
#define COLOR_MAGENTA 0xF81F
|
||||
#define COLOR_ORANGE 0xFD20
|
||||
#define COLOR_DARK_BLUE 0x0010
|
||||
#define COLOR_DARK_GREEN 0x0200
|
||||
#define COLOR_DARK_RED 0x8000
|
||||
|
||||
/* 颜色转换宏 */
|
||||
#define RGB565(r, g, b) ((uint16_t)(((r) & 0xF8) << 8 | ((g) & 0xFC) << 3 | ((b) >> 3)))
|
||||
|
||||
/* LCD控制引脚定义 - 根据CubeMX配置修改 */
|
||||
#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 /* 软件复位 */
|
||||
#define LCD_CMD_SLPOUT 0x11 /* 退出睡眠 */
|
||||
#define LCD_CMD_DISPON 0x29 /* 显示开启 */
|
||||
#define LCD_CMD_DISPOFF 0x28 /* 显示关闭 */
|
||||
#define LCD_CMD_CASET 0x2A /* 列地址设置 */
|
||||
#define LCD_CMD_RASET 0x2B /* 行地址设置 */
|
||||
#define LCD_CMD_RAMWR 0x2C /* 写入显存 */
|
||||
#define LCD_CMD_MADCTL 0x36 /* 存储器访问控制 */
|
||||
#define LCD_CMD_COLMOD 0x3A /* 像素格式设置 */
|
||||
#define LCD_CMD_FRMCTR1 0xB1 /* 帧率控制1 */
|
||||
#define LCD_CMD_FRMCTR2 0xB2 /* 帧率控制2 */
|
||||
#define LCD_CMD_FRMCTR3 0xB3 /* 帧率控制3 */
|
||||
#define LCD_CMD_INVCTR 0xB4 /* 显示反转控制 */
|
||||
#define LCD_CMD_PWCTR1 0xC0 /* 功率控制1 */
|
||||
#define LCD_CMD_PWCTR2 0xC1 /* 功率控制2 */
|
||||
#define LCD_CMD_PWCTR3 0xC2 /* 功率控制3 */
|
||||
#define LCD_CMD_PWCTR4 0xC3 /* 功率控制4 */
|
||||
#define LCD_CMD_PWCTR5 0xC4 /* 功率控制5 */
|
||||
#define LCD_CMD_VMCTR1 0xC5 /* VCOM控制1 */
|
||||
#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初始化
|
||||
* 入口参数:无
|
||||
* 返回值:无
|
||||
* 函数说明:初始化ST7735S屏幕,包括SPI、GPIO、显示参数配置
|
||||
*/
|
||||
void st7735s_init(void);
|
||||
|
||||
/*
|
||||
* 函数功能:设置显示窗口
|
||||
* 入口参数:x1 - 起始列
|
||||
* y1 - 起始行
|
||||
* x2 - 结束列
|
||||
* y2 - 结束行
|
||||
* 返回值:无
|
||||
* 函数说明:设置后续写入操作的窗口区域
|
||||
*/
|
||||
void st7735s_set_window(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
|
||||
|
||||
/*
|
||||
* 函数功能:填充矩形区域
|
||||
* 入口参数:x1 - 起始列
|
||||
* y1 - 起始行
|
||||
* x2 - 结束列
|
||||
* 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);
|
||||
|
||||
/*
|
||||
* 函数功能:设置背光
|
||||
* 入口参数:state - 1:开 0:关
|
||||
* 返回值:无
|
||||
* 函数说明:控制LCD背光(如有硬件背光控制)
|
||||
*/
|
||||
void st7735s_backlight(uint8_t state);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __ST7735S_H */
|
||||
192
STM32F103C8T6/Drivers/BSP/MultiButton/button_driver.c
Normal file
192
STM32F103C8T6/Drivers/BSP/MultiButton/button_driver.c
Normal file
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
* 模块名称:按键驱动适配层
|
||||
* 模块功能:提供MultiButton库与STM32 HAL库的适配接口
|
||||
* 适用平台:STM32F103C8T6
|
||||
* 作者:王建锋
|
||||
* 创建日期:2026-07-13
|
||||
* 修改记录:
|
||||
* 2026-07-13 王建锋 创建初始版本
|
||||
*/
|
||||
|
||||
#include "button_driver.h"
|
||||
#include "main.h"
|
||||
#include <stdio.h>
|
||||
|
||||
/* 按键句柄数组 */
|
||||
static Button s_button_handles[BUTTON_ID_MAX];
|
||||
|
||||
/* 按键GPIO端口和引脚映射表 */
|
||||
typedef struct {
|
||||
GPIO_TypeDef* port; /* GPIO端口 */
|
||||
uint16_t pin; /* GPIO引脚 */
|
||||
} ButtonGpio_t;
|
||||
|
||||
static const ButtonGpio_t s_button_gpio[BUTTON_ID_MAX] = {
|
||||
{KEY1_GPIO_Port, KEY1_Pin}, /* KEY1: PB4 左上*/
|
||||
{KEY2_GPIO_Port, KEY2_Pin}, /* KEY2: PB5 右上*/
|
||||
{KEY3_GPIO_Port, KEY3_Pin}, /* KEY3: PB3 右下*/
|
||||
{KEY4_GPIO_Port, KEY4_Pin}, /* KEY4: PC14 左下*/
|
||||
{KEY5_GPIO_Port, KEY5_Pin}, /* KEY5: PC15 中间*/
|
||||
};
|
||||
|
||||
/*
|
||||
* 函数功能:获取指定按键的电平状态
|
||||
* 入口参数:button_id - 按键标识符
|
||||
* 返回值:GPIO电平值 (0或1)
|
||||
* 函数说明:根据按键ID读取对应GPIO引脚的电平
|
||||
*/
|
||||
uint8_t button_driver_read_level(uint8_t button_id)
|
||||
{
|
||||
if (button_id >= BUTTON_ID_MAX) {
|
||||
return 0;
|
||||
}
|
||||
return HAL_GPIO_ReadPin(s_button_gpio[button_id].port, s_button_gpio[button_id].pin);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:KEY1按键回调函数
|
||||
* 入口参数:handle - 按键句柄
|
||||
* user_data - 用户数据指针
|
||||
* 返回值:无
|
||||
* 函数说明:KEY1按下时翻转LED
|
||||
*/
|
||||
static void button_key1_callback(Button* handle, void* user_data)
|
||||
{
|
||||
(void)handle;
|
||||
(void)user_data;
|
||||
printf("KEY1\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:KEY2单击回调函数
|
||||
* 入口参数:handle - 按键句柄
|
||||
* user_data - 用户数据指针
|
||||
* 返回值:无
|
||||
* 函数说明:KEY2单击时点亮LED
|
||||
*/
|
||||
static void button_key2_single_click(Button* handle, void* user_data)
|
||||
{
|
||||
(void)handle;
|
||||
(void)user_data;
|
||||
printf("KEY2\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:KEY3单击回调函数
|
||||
* 入口参数:handle - 按键句柄
|
||||
* user_data - 用户数据指针
|
||||
* 返回值:无
|
||||
* 函数说明:KEY3单击事件处理
|
||||
*/
|
||||
static void button_key3_single_click(Button* handle, void* user_data)
|
||||
{
|
||||
(void)handle;
|
||||
(void)user_data;
|
||||
printf("KEY3: \n");
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:KEY4单击回调函数
|
||||
* 入口参数:handle - 按键句柄
|
||||
* user_data - 用户数据指针
|
||||
* 返回值:无
|
||||
* 函数说明:KEY4单击事件处理
|
||||
*/
|
||||
static void button_key4_single_click(Button* handle, void* user_data)
|
||||
{
|
||||
(void)handle;
|
||||
(void)user_data;
|
||||
printf("KEY4\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:KEY5单击回调函数
|
||||
* 入口参数:handle - 按键句柄
|
||||
* user_data - 用户数据指针
|
||||
* 返回值:无
|
||||
* 函数说明:KEY5单击事件处理
|
||||
*/
|
||||
static void button_key5_single_click(Button* handle, void* user_data)
|
||||
{
|
||||
(void)handle;
|
||||
(void)user_data;
|
||||
printf("KEY5\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:按键驱动初始化
|
||||
* 入口参数:无
|
||||
* 返回值:无
|
||||
* 函数说明:初始化所有按键对象,注册回调函数,启动按键轮询
|
||||
*/
|
||||
void button_driver_init(void)
|
||||
{
|
||||
/* 初始化KEY1 */
|
||||
button_init(&s_button_handles[BUTTON_ID_KEY1],
|
||||
button_driver_read_level,
|
||||
BUTTON_ACTIVE_LEVEL_LOW,
|
||||
BUTTON_ID_KEY1);
|
||||
button_attach(&s_button_handles[BUTTON_ID_KEY1],
|
||||
BTN_SINGLE_CLICK,
|
||||
button_key1_callback,
|
||||
NULL);
|
||||
button_start(&s_button_handles[BUTTON_ID_KEY1]);
|
||||
|
||||
/* 初始化KEY2 */
|
||||
button_init(&s_button_handles[BUTTON_ID_KEY2],
|
||||
button_driver_read_level,
|
||||
BUTTON_ACTIVE_LEVEL_LOW,
|
||||
BUTTON_ID_KEY2);
|
||||
button_attach(&s_button_handles[BUTTON_ID_KEY2],
|
||||
BTN_SINGLE_CLICK,
|
||||
button_key2_single_click,
|
||||
NULL);
|
||||
button_start(&s_button_handles[BUTTON_ID_KEY2]);
|
||||
|
||||
/* 初始化KEY3 */
|
||||
button_init(&s_button_handles[BUTTON_ID_KEY3],
|
||||
button_driver_read_level,
|
||||
BUTTON_ACTIVE_LEVEL_LOW,
|
||||
BUTTON_ID_KEY3);
|
||||
button_attach(&s_button_handles[BUTTON_ID_KEY3],
|
||||
BTN_SINGLE_CLICK,
|
||||
button_key3_single_click,
|
||||
NULL);
|
||||
button_start(&s_button_handles[BUTTON_ID_KEY3]);
|
||||
|
||||
/* 初始化KEY4 */
|
||||
button_init(&s_button_handles[BUTTON_ID_KEY4],
|
||||
button_driver_read_level,
|
||||
BUTTON_ACTIVE_LEVEL_LOW,
|
||||
BUTTON_ID_KEY4);
|
||||
button_attach(&s_button_handles[BUTTON_ID_KEY4],
|
||||
BTN_SINGLE_CLICK,
|
||||
button_key4_single_click,
|
||||
NULL);
|
||||
button_start(&s_button_handles[BUTTON_ID_KEY4]);
|
||||
|
||||
/* 初始化KEY5 */
|
||||
button_init(&s_button_handles[BUTTON_ID_KEY5],
|
||||
button_driver_read_level,
|
||||
BUTTON_ACTIVE_LEVEL_LOW,
|
||||
BUTTON_ID_KEY5);
|
||||
button_attach(&s_button_handles[BUTTON_ID_KEY5],
|
||||
BTN_SINGLE_CLICK,
|
||||
button_key5_single_click,
|
||||
NULL);
|
||||
button_start(&s_button_handles[BUTTON_ID_KEY5]);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:获取指定按键的事件
|
||||
* 入口参数:button_id - 按键标识符
|
||||
* 返回值:按键事件
|
||||
* 函数说明:返回指定按键的当前事件
|
||||
*/
|
||||
ButtonEvent button_driver_get_event(uint8_t button_id)
|
||||
{
|
||||
if (button_id >= BUTTON_ID_MAX) {
|
||||
return BTN_NONE_PRESS;
|
||||
}
|
||||
return button_get_event(&s_button_handles[button_id]);
|
||||
}
|
||||
63
STM32F103C8T6/Drivers/BSP/MultiButton/button_driver.h
Normal file
63
STM32F103C8T6/Drivers/BSP/MultiButton/button_driver.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 模块名称:按键驱动适配层
|
||||
* 模块功能:提供MultiButton库与STM32 HAL库的适配接口
|
||||
* 适用平台:STM32F103C8T6
|
||||
* 作者:王建锋
|
||||
* 创建日期:2026-07-13
|
||||
* 修改记录:
|
||||
* 2026-07-13 王建锋 创建初始版本
|
||||
*/
|
||||
|
||||
#ifndef __BUTTON_DRIVER_H
|
||||
#define __BUTTON_DRIVER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "multi_button.h"
|
||||
#include "main.h"
|
||||
|
||||
/* 按键ID定义 */
|
||||
typedef enum {
|
||||
BUTTON_ID_KEY1 = 0, /* KEY1: PB4 */
|
||||
BUTTON_ID_KEY2, /* KEY2: PB5 */
|
||||
BUTTON_ID_KEY3, /* KEY3: PB3 */
|
||||
BUTTON_ID_KEY4, /* KEY4: PC14 */
|
||||
BUTTON_ID_KEY5, /* KEY5: PC15 */
|
||||
BUTTON_ID_MAX /* 按键总数 */
|
||||
} ButtonId_t;
|
||||
|
||||
/* 按键有效电平定义 */
|
||||
#define BUTTON_ACTIVE_LEVEL_LOW 0 /* 低电平有效 */
|
||||
#define BUTTON_ACTIVE_LEVEL_HIGH 1 /* 高电平有效 */
|
||||
|
||||
/*
|
||||
* 函数功能:按键驱动初始化
|
||||
* 入口参数:无
|
||||
* 返回值:无
|
||||
* 函数说明:初始化所有按键对象,注册回调函数,启动按键轮询
|
||||
*/
|
||||
void button_driver_init(void);
|
||||
|
||||
/*
|
||||
* 函数功能:获取指定按键的电平状态
|
||||
* 入口参数:button_id - 按键标识符
|
||||
* 返回值:GPIO电平值 (0或1)
|
||||
* 函数说明:根据按键ID读取对应GPIO引脚的电平
|
||||
*/
|
||||
uint8_t button_driver_read_level(uint8_t button_id);
|
||||
|
||||
/*
|
||||
* 函数功能:获取指定按键的事件
|
||||
* 入口参数:button_id - 按键标识符
|
||||
* 返回值:按键事件
|
||||
* 函数说明:返回指定按键的当前事件
|
||||
*/
|
||||
ButtonEvent button_driver_get_event(uint8_t button_id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __BUTTON_DRIVER_H */
|
||||
334
STM32F103C8T6/Drivers/BSP/MultiButton/multi_button.c
Normal file
334
STM32F103C8T6/Drivers/BSP/MultiButton/multi_button.c
Normal file
@@ -0,0 +1,334 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Zibin Zheng <znbin@qq.com>
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
#include "multi_button.h"
|
||||
|
||||
/* 回调执行宏,包含空值检查,传递user_data */
|
||||
#define EVENT_CB(ev) do { if (handle->cb[ev]) handle->cb[ev](handle, handle->user_data); } while(0)
|
||||
|
||||
/* 按键句柄链表头 */
|
||||
static Button* head_handle = NULL;
|
||||
|
||||
/* 前向声明 */
|
||||
static void button_handler(Button* handle);
|
||||
static inline uint8_t button_read_level(Button* handle);
|
||||
|
||||
/*
|
||||
* 函数功能:初始化按键结构体句柄
|
||||
* 入口参数:handle - 按键句柄结构体指针
|
||||
* pin_level - 读取按键GPIO电平的函数指针
|
||||
* active_level - 按下时的有效电平
|
||||
* button_id - 按键标识符
|
||||
* 返回值:无
|
||||
* 函数说明:初始化按键状态机和所有参数
|
||||
*/
|
||||
void button_init(Button* handle, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t button_id)
|
||||
{
|
||||
if (!handle || !pin_level) {
|
||||
return;
|
||||
}
|
||||
|
||||
memset(handle, 0, sizeof(Button));
|
||||
handle->event = (uint8_t)BTN_NONE_PRESS;
|
||||
handle->hal_button_level = pin_level;
|
||||
handle->button_level = !active_level;
|
||||
handle->active_level = active_level;
|
||||
handle->button_id = button_id;
|
||||
handle->state = BTN_STATE_IDLE;
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:绑定按键事件回调函数
|
||||
* 入口参数:handle - 按键句柄结构体指针
|
||||
* event - 触发事件类型
|
||||
* cb - 回调函数
|
||||
* user_data - 传递给回调的用户上下文指针
|
||||
* 返回值:无
|
||||
* 函数说明:为指定事件注册回调函数
|
||||
*/
|
||||
void button_attach(Button* handle, ButtonEvent event, BtnCallback cb, void* user_data)
|
||||
{
|
||||
if (!handle || event >= BTN_EVENT_COUNT) {
|
||||
return;
|
||||
}
|
||||
handle->cb[event] = cb;
|
||||
handle->user_data = user_data;
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:解绑按键事件回调函数
|
||||
* 入口参数:handle - 按键句柄结构体指针
|
||||
* event - 触发事件类型
|
||||
* 返回值:无
|
||||
* 函数说明:移除指定事件的回调函数
|
||||
*/
|
||||
void button_detach(Button* handle, ButtonEvent event)
|
||||
{
|
||||
if (!handle || event >= BTN_EVENT_COUNT) {
|
||||
return;
|
||||
}
|
||||
handle->cb[event] = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:获取按键事件
|
||||
* 入口参数:handle - 按键句柄结构体指针
|
||||
* 返回值:按键事件
|
||||
* 函数说明:返回当前发生的按键事件
|
||||
*/
|
||||
ButtonEvent button_get_event(Button* handle)
|
||||
{
|
||||
if (!handle) {
|
||||
return BTN_NONE_PRESS;
|
||||
}
|
||||
return (ButtonEvent)(handle->event);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:获取按键重复按下次数
|
||||
* 入口参数:handle - 按键句柄结构体指针
|
||||
* 返回值:重复按下次数
|
||||
* 函数说明:返回连击次数
|
||||
*/
|
||||
uint8_t button_get_repeat_count(Button* handle)
|
||||
{
|
||||
if (!handle) {
|
||||
return 0;
|
||||
}
|
||||
return handle->repeat;
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:重置按键状态到空闲
|
||||
* 入口参数:handle - 按键句柄结构体指针
|
||||
* 返回值:无
|
||||
* 函数说明:将按键状态机重置为初始状态
|
||||
*/
|
||||
void button_reset(Button* handle)
|
||||
{
|
||||
if (!handle) {
|
||||
return;
|
||||
}
|
||||
handle->state = BTN_STATE_IDLE;
|
||||
handle->ticks = 0;
|
||||
handle->repeat = 0;
|
||||
handle->event = (uint8_t)BTN_NONE_PRESS;
|
||||
handle->debounce_cnt = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:检查按键是否按下
|
||||
* 入口参数:handle - 按键句柄结构体指针
|
||||
* 返回值:1 - 按下,0 - 未按下,-1 - 错误
|
||||
* 函数说明:检查当前按键是否处于按下状态
|
||||
*/
|
||||
int button_is_pressed(Button* handle)
|
||||
{
|
||||
if (!handle) {
|
||||
return -1;
|
||||
}
|
||||
return (handle->button_level == handle->active_level) ? 1 : 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:读取按键电平
|
||||
* 入口参数:handle - 按键句柄结构体指针
|
||||
* 返回值:按键电平
|
||||
* 函数说明:通过HAL函数读取GPIO电平
|
||||
*/
|
||||
static inline uint8_t button_read_level(Button* handle)
|
||||
{
|
||||
return handle->hal_button_level(handle->button_id);
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:按键驱动核心函数,驱动状态机
|
||||
* 入口参数:handle - 按键句柄结构体指针
|
||||
* 返回值:无
|
||||
* 函数说明:实现按键消抖和事件检测的状态机
|
||||
*/
|
||||
static void button_handler(Button* handle)
|
||||
{
|
||||
uint8_t read_gpio_level = button_read_level(handle);
|
||||
|
||||
/* 非空闲状态下增加tick计数器(饱和处理) */
|
||||
if (handle->state > BTN_STATE_IDLE) {
|
||||
if (handle->ticks < UINT16_MAX) {
|
||||
handle->ticks++;
|
||||
}
|
||||
}
|
||||
|
||||
/* 按键消抖处理 */
|
||||
if (read_gpio_level != handle->button_level) {
|
||||
if (++(handle->debounce_cnt) >= DEBOUNCE_TICKS) {
|
||||
handle->button_level = read_gpio_level;
|
||||
handle->debounce_cnt = 0;
|
||||
}
|
||||
} else {
|
||||
handle->debounce_cnt = 0;
|
||||
}
|
||||
|
||||
/* 状态机 */
|
||||
switch (handle->state) {
|
||||
case BTN_STATE_IDLE:
|
||||
if (handle->button_level == handle->active_level) {
|
||||
/* 检测到按键按下 */
|
||||
handle->event = (uint8_t)BTN_PRESS_DOWN;
|
||||
EVENT_CB(BTN_PRESS_DOWN);
|
||||
handle->ticks = 0;
|
||||
handle->repeat = 1;
|
||||
handle->state = BTN_STATE_PRESS;
|
||||
} else {
|
||||
handle->event = (uint8_t)BTN_NONE_PRESS;
|
||||
}
|
||||
break;
|
||||
|
||||
case BTN_STATE_PRESS:
|
||||
if (handle->button_level != handle->active_level) {
|
||||
/* 按键释放 */
|
||||
handle->event = (uint8_t)BTN_PRESS_UP;
|
||||
EVENT_CB(BTN_PRESS_UP);
|
||||
handle->ticks = 0;
|
||||
handle->state = BTN_STATE_RELEASE;
|
||||
} else if (handle->ticks > LONG_TICKS) {
|
||||
/* 检测到长按 */
|
||||
handle->event = (uint8_t)BTN_LONG_PRESS_START;
|
||||
EVENT_CB(BTN_LONG_PRESS_START);
|
||||
handle->state = BTN_STATE_LONG_HOLD;
|
||||
}
|
||||
break;
|
||||
|
||||
case BTN_STATE_RELEASE:
|
||||
if (handle->button_level == handle->active_level) {
|
||||
/* 按键再次按下 */
|
||||
handle->event = (uint8_t)BTN_PRESS_DOWN;
|
||||
EVENT_CB(BTN_PRESS_DOWN);
|
||||
if (handle->repeat < PRESS_REPEAT_MAX_NUM) {
|
||||
handle->repeat++;
|
||||
}
|
||||
handle->event = (uint8_t)BTN_PRESS_REPEAT;
|
||||
EVENT_CB(BTN_PRESS_REPEAT);
|
||||
handle->ticks = 0;
|
||||
handle->state = BTN_STATE_REPEAT;
|
||||
} else if (handle->ticks > SHORT_TICKS) {
|
||||
/* 超时,判断点击类型 */
|
||||
if (handle->repeat == 1) {
|
||||
handle->event = (uint8_t)BTN_SINGLE_CLICK;
|
||||
EVENT_CB(BTN_SINGLE_CLICK);
|
||||
} else if (handle->repeat == 2) {
|
||||
handle->event = (uint8_t)BTN_DOUBLE_CLICK;
|
||||
EVENT_CB(BTN_DOUBLE_CLICK);
|
||||
}
|
||||
handle->state = BTN_STATE_IDLE;
|
||||
}
|
||||
break;
|
||||
|
||||
case BTN_STATE_REPEAT:
|
||||
if (handle->button_level != handle->active_level) {
|
||||
/* 按键释放 */
|
||||
handle->event = (uint8_t)BTN_PRESS_UP;
|
||||
EVENT_CB(BTN_PRESS_UP);
|
||||
if (handle->ticks < SHORT_TICKS) {
|
||||
handle->ticks = 0;
|
||||
handle->state = BTN_STATE_RELEASE;
|
||||
} else {
|
||||
handle->state = BTN_STATE_IDLE;
|
||||
}
|
||||
} else if (handle->ticks > SHORT_TICKS) {
|
||||
/* 按住时间过长,视为普通按下 */
|
||||
handle->ticks = 0;
|
||||
handle->repeat = 0;
|
||||
handle->state = BTN_STATE_PRESS;
|
||||
}
|
||||
break;
|
||||
|
||||
case BTN_STATE_LONG_HOLD:
|
||||
if (handle->button_level == handle->active_level) {
|
||||
/* 继续保持按下 */
|
||||
handle->event = (uint8_t)BTN_LONG_PRESS_HOLD;
|
||||
EVENT_CB(BTN_LONG_PRESS_HOLD);
|
||||
} else {
|
||||
/* 从长按中释放 */
|
||||
handle->event = (uint8_t)BTN_PRESS_UP;
|
||||
EVENT_CB(BTN_PRESS_UP);
|
||||
handle->state = BTN_STATE_IDLE;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
/* 无效状态,重置为空闲 */
|
||||
handle->state = BTN_STATE_IDLE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:启动按键工作,将句柄添加到工作链表
|
||||
* 入口参数:handle - 目标句柄结构体指针
|
||||
* 返回值:0 - 成功,-1 - 已存在,-2 - 无效参数
|
||||
* 函数说明:将按键添加到轮询链表中
|
||||
*/
|
||||
int button_start(Button* handle)
|
||||
{
|
||||
if (!handle) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
Button* target = head_handle;
|
||||
while (target) {
|
||||
if (target == handle) {
|
||||
return -1;
|
||||
}
|
||||
target = target->next;
|
||||
}
|
||||
|
||||
handle->next = head_handle;
|
||||
head_handle = handle;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:停止按键工作,从工作链表移除句柄
|
||||
* 入口参数:handle - 目标句柄结构体指针
|
||||
* 返回值:无
|
||||
* 函数说明:将按键从轮询链表中移除
|
||||
*/
|
||||
void button_stop(Button* handle)
|
||||
{
|
||||
if (!handle) {
|
||||
return;
|
||||
}
|
||||
|
||||
Button** curr;
|
||||
for (curr = &head_handle; *curr; ) {
|
||||
Button* entry = *curr;
|
||||
if (entry == handle) {
|
||||
*curr = entry->next;
|
||||
entry->next = NULL;
|
||||
return;
|
||||
} else {
|
||||
curr = &entry->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:后台tick处理,定时器重复调用,间隔5ms
|
||||
* 入口参数:无
|
||||
* 返回值:无
|
||||
* 函数说明:遍历链表调用每个按键的状态机处理函数
|
||||
*/
|
||||
void button_ticks(void)
|
||||
{
|
||||
Button* target;
|
||||
Button* next;
|
||||
|
||||
target = head_handle;
|
||||
while (target) {
|
||||
next = target->next;
|
||||
button_handler(target);
|
||||
target = next;
|
||||
}
|
||||
}
|
||||
95
STM32F103C8T6/Drivers/BSP/MultiButton/multi_button.h
Normal file
95
STM32F103C8T6/Drivers/BSP/MultiButton/multi_button.h
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Zibin Zheng <znbin@qq.com>
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
#ifndef MULTI_BUTTON_H
|
||||
#define MULTI_BUTTON_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
/* 版本信息 */
|
||||
#define MULTIBUTTON_VERSION_MAJOR 1
|
||||
#define MULTIBUTTON_VERSION_MINOR 1
|
||||
#define MULTIBUTTON_VERSION_PATCH 1
|
||||
|
||||
/* 配置常量 - 可根据需求修改 */
|
||||
#define TICKS_INTERVAL 5 /* 定时器轮询间隔,单位:毫秒 */
|
||||
#define DEBOUNCE_TICKS 3 /* 消抖滤波深度,最大7 (0~7) */
|
||||
#define SHORT_TICKS (300 / TICKS_INTERVAL) /* 短按阈值 */
|
||||
#define LONG_TICKS (1000 / TICKS_INTERVAL) /* 长按阈值 */
|
||||
#define PRESS_REPEAT_MAX_NUM 15 /* 最大重复计数器值 */
|
||||
|
||||
/* 编译时检查:debounce_cnt 是3位域,最大值为7 */
|
||||
#if DEBOUNCE_TICKS > 7
|
||||
#error "DEBOUNCE_TICKS exceeds 3-bit field maximum (7)"
|
||||
#endif
|
||||
|
||||
/* 前向声明 */
|
||||
typedef struct _Button Button;
|
||||
|
||||
/* 按键回调函数类型 */
|
||||
typedef void (*BtnCallback)(Button* handle, void* user_data);
|
||||
|
||||
/* 按键事件类型 */
|
||||
typedef enum {
|
||||
BTN_PRESS_DOWN = 0, /* 按键按下 */
|
||||
BTN_PRESS_UP, /* 按键释放 */
|
||||
BTN_PRESS_REPEAT, /* 重复按下检测 */
|
||||
BTN_SINGLE_CLICK, /* 单击完成 */
|
||||
BTN_DOUBLE_CLICK, /* 双击完成 */
|
||||
BTN_LONG_PRESS_START, /* 长按开始 */
|
||||
BTN_LONG_PRESS_HOLD, /* 长按保持 */
|
||||
BTN_EVENT_COUNT, /* 事件总数 */
|
||||
BTN_NONE_PRESS /* 无事件 */
|
||||
} ButtonEvent;
|
||||
|
||||
/* 按键状态机状态 */
|
||||
typedef enum {
|
||||
BTN_STATE_IDLE = 0, /* 空闲状态 */
|
||||
BTN_STATE_PRESS, /* 按下状态 */
|
||||
BTN_STATE_RELEASE, /* 释放后等待超时 */
|
||||
BTN_STATE_REPEAT, /* 重复按下状态 */
|
||||
BTN_STATE_LONG_HOLD /* 长按保持状态 */
|
||||
} ButtonState;
|
||||
|
||||
/* 按键结构体 */
|
||||
struct _Button {
|
||||
uint16_t ticks; /* tick计数器 */
|
||||
uint8_t repeat : 4; /* 重复计数器 (0-15) */
|
||||
uint8_t event : 4; /* 当前事件 (0-15) */
|
||||
uint8_t state : 3; /* 状态机状态 (0-7) */
|
||||
uint8_t debounce_cnt : 3; /* 消抖计数器 (0-7) */
|
||||
uint8_t active_level : 1; /* 有效GPIO电平 (0或1) */
|
||||
uint8_t button_level : 1; /* 当前按键电平 */
|
||||
uint8_t button_id; /* 按键标识符 */
|
||||
uint8_t (*hal_button_level)(uint8_t button_id); /* HAL读取GPIO函数 */
|
||||
BtnCallback cb[BTN_EVENT_COUNT]; /* 回调函数数组 */
|
||||
void* user_data; /* 传递给回调的用户上下文指针 */
|
||||
Button* next; /* 链表中的下一个按键 */
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 公共API函数 */
|
||||
void button_init(Button* handle, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t button_id);
|
||||
void button_attach(Button* handle, ButtonEvent event, BtnCallback cb, void* user_data);
|
||||
void button_detach(Button* handle, ButtonEvent event);
|
||||
ButtonEvent button_get_event(Button* handle);
|
||||
int button_start(Button* handle);
|
||||
void button_stop(Button* handle);
|
||||
void button_ticks(void);
|
||||
|
||||
/* 工具函数 */
|
||||
uint8_t button_get_repeat_count(Button* handle);
|
||||
void button_reset(Button* handle);
|
||||
int button_is_pressed(Button* handle);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MULTI_BUTTON_H */
|
||||
Reference in New Issue
Block a user