Loading

STM32F103+HAL+TFT_SPI通讯_2.0英寸_ST7789V2驱动

TFT_SPI2.0.c 驱动文件如下:
  1 /******************2.0寸  7PIN SPI TFT FOR STM32F103*************
  2 *****STM32F103驱动**************************
  3 ***** PB5----3  SCK   ,  PB6----4  SDA   ,    PB7---5  RST   ,
  4       PB8    ---6   DC   ,    PB9 ----7   CS
  5 *********ST7789V2
  6 
  7 从左往右,从上往下    240*320  18bit 6 6 6 RGB 模式  高位模式
  8 ********************************************************/
  9 #include "TFT_SPI2.0.h"
 10 #include "main.h"
 11 #include "stm32f1xx_hal.h"
 12 #include "spi.h"
 13 
 14 #define OLED_COLUMN_NUMBER 240
 15 #define OLED_LINE_NUMBER 320
 16 #define OLED_COLUMN_OFFSET 0
 17 
 18 #define RED 0XFF0000
 19 #define GREEN 0X00FF00
 20 #define BLUE 0X0000FF
 21 
 22 /**********GPIO引脚分配*********/
 23 // 硬件SPI1引脚由CubeMX配置
 24 // PA5: SCK, PA7: MOSI
 25 
 26 // 控制引脚定义
 27 #define TFT_RST_PIN GPIO_PIN_7
 28 #define TFT_RST_PORT GPIOB
 29 #define TFT_DC_PIN GPIO_PIN_8
 30 #define TFT_DC_PORT GPIOB
 31 #define TFT_CS_PIN GPIO_PIN_9
 32 #define TFT_CS_PORT GPIOB
 33 
 34 // 引脚操作宏
 35 #define SPI_RST_0 HAL_GPIO_WritePin(TFT_RST_PORT, TFT_RST_PIN, GPIO_PIN_RESET)
 36 #define SPI_RST_1 HAL_GPIO_WritePin(TFT_RST_PORT, TFT_RST_PIN, GPIO_PIN_SET)
 37 #define SPI_DC_0 HAL_GPIO_WritePin(TFT_DC_PORT, TFT_DC_PIN, GPIO_PIN_RESET)
 38 #define SPI_DC_1 HAL_GPIO_WritePin(TFT_DC_PORT, TFT_DC_PIN, GPIO_PIN_SET)
 39 #define SPI_CS_0 HAL_GPIO_WritePin(TFT_CS_PORT, TFT_CS_PIN, GPIO_PIN_RESET)
 40 #define SPI_CS_1 HAL_GPIO_WritePin(TFT_CS_PORT, TFT_CS_PIN, GPIO_PIN_SET)
 41 
 42 const unsigned char *point;
 43 
 44 // 声明CubeMX生成的SPI1句柄
 45 extern SPI_HandleTypeDef hspi1;
 46 
 47 // 声明外部函数(由CubeMX生成)
 48 extern void SystemClock_Config(void);
 49 extern void MX_GPIO_Init(void);
 50 extern void MX_SPI1_Init(void);
 51 
 52 
 53 // 函数前向声明
 54 void delay_us(unsigned int _us_time);
 55 void delay_ms(unsigned int _ms_time);
 56 void SPI_SendByte(unsigned char byte);
 57 void TFT_SEND_CMD(unsigned char o_command);
 58 void TFT_SEND_DATA(unsigned char o_data);
 59 void OLED_clear(void);
 60 void OLED_full(unsigned long color);
 61 void OLED_init(void);
 62 void Picture_display(const unsigned char *ptr_pic);
 63 void display_char16_16(unsigned int x, unsigned int y, unsigned long color, const unsigned char *point);
 64 void Picture_ReverseDisplay(const unsigned char *ptr_pic);
 65 
 66 // HAL库已处理系统时钟初始化,不再需要此函数
 67 
 68 void IO_init(void)
 69 {
 70     // 控制引脚由CubeMX配置,此函数保持兼容性
 71     // 确保CS、DC、RST引脚初始状态正确
 72     SPI_CS_1;
 73     SPI_DC_1;
 74     SPI_RST_1;
 75 }
 76 void delay_us(unsigned int _us_time)
 77 {
 78     unsigned char x = 0;
 79     for (; _us_time > 0; _us_time--)
 80     {
 81         x++;
 82         x++;
 83         x++;
 84         x++;
 85         x++;
 86         x++;
 87         x++;
 88         x++;
 89         x++;
 90         x++;
 91         x++;
 92         x++;
 93         x++;
 94         x++;
 95         x++;
 96         x++;
 97         x++;
 98         x++;
 99         x++;
100         x++;
101         x++;
102         x++;
103         x++;
104         x++;
105     }
106 }
107 void delay_ms(unsigned int _ms_time)
108 {
109     unsigned int i, j;
110     for (i = 0; i < _ms_time; i++)
111     {
112         for (j = 0; j < 900; j++)
113         {
114             ;
115         }
116     }
117 }
118 /**************************SPI模块发送函数************************************************
119 使用硬件SPI1发送字节
120  *************************************************************************/
121 void SPI_SendByte(unsigned char byte)
122 {
123     HAL_SPI_Transmit(&hspi1, &byte, 1, HAL_MAX_DELAY);
124 }
125 
126 void TFT_SEND_CMD(unsigned char o_command)
127 {
128     SPI_DC_0;
129     SPI_CS_0;
130     SPI_SendByte(o_command);
131     SPI_CS_1;
132 
133     // SPI_DC_1;
134 }
135 void TFT_SEND_DATA(unsigned char o_data)
136 {
137     SPI_DC_1;
138     SPI_CS_0;
139     SPI_SendByte(o_data);
140     SPI_CS_1;
141 }
142 void OLED_clear(void)
143 {
144     unsigned int ROW, column;
145     TFT_SEND_CMD(0x2a);     // Column address set
146     TFT_SEND_DATA(0x00); // start column
147     TFT_SEND_DATA(0x00);
148     TFT_SEND_DATA(0x00); // end column
149     TFT_SEND_DATA(0xEF);
150 
151     TFT_SEND_CMD(0x2b);     // Row address set
152     TFT_SEND_DATA(0x00); // start row
153     TFT_SEND_DATA(0x00);
154     TFT_SEND_DATA(0x01); // end row
155     TFT_SEND_DATA(0x3F);
156     TFT_SEND_CMD(0x2C);                             // Memory write
157     for (ROW = 0; ROW < OLED_LINE_NUMBER; ROW++) // ROW loop
158     {
159 
160         for (column = 0; column < OLED_COLUMN_NUMBER; column++) // column loop
161         {
162             TFT_SEND_DATA(0x00);
163             TFT_SEND_DATA(0x00);
164             TFT_SEND_DATA(0x00);
165         }
166     }
167 }
168 void OLED_full(unsigned long color)
169 {
170     unsigned int ROW, column;
171     TFT_SEND_CMD(0x2a);     // Column address set
172     TFT_SEND_DATA(0x00); // start column
173     TFT_SEND_DATA(0x00);
174     TFT_SEND_DATA(0x00); // end column
175     TFT_SEND_DATA(0xEF);
176 
177     TFT_SEND_CMD(0x2b);     // Row address set
178     TFT_SEND_DATA(0x00); // start row
179     TFT_SEND_DATA(0x00);
180     TFT_SEND_DATA(0x01); // end row
181     TFT_SEND_DATA(0x3F);
182     TFT_SEND_CMD(0x2C);                             // Memory write
183     for (ROW = 0; ROW < OLED_LINE_NUMBER; ROW++) // ROW loop
184     {
185 
186         for (column = 0; column < OLED_COLUMN_NUMBER; column++) // column loop
187         {
188             TFT_SEND_DATA(color >> 16);
189             TFT_SEND_DATA(color >> 8);
190             TFT_SEND_DATA(color);
191         }
192     }
193 }
194 void OLED_init(void) ////ST7789V2
195 {
196     SPI_RST_0;
197     delay_ms(1000);
198     SPI_RST_1;
199     delay_ms(1000);
200     TFT_SEND_CMD(0x11); // Sleep Out
201     delay_ms(120);        // DELAY120ms
202     //--------------------------------ST7789S Frame rate setting----------------------------------//
203     TFT_SEND_CMD(0x2a);     // Column address set    
204     TFT_SEND_DATA(0x00); // start column
205     TFT_SEND_DATA(0x00);
206     TFT_SEND_DATA(0x00); // end column
207     TFT_SEND_DATA(0xef);
208 
209     TFT_SEND_CMD(0x2b);     // Row address set
210     TFT_SEND_DATA(0x00); // start row
211     TFT_SEND_DATA(0x28);
212     TFT_SEND_DATA(0x01); // end row
213     TFT_SEND_DATA(0x17);
214 
215     TFT_SEND_CMD(0xb2); // Porch control
216     TFT_SEND_DATA(0x0c);
217     TFT_SEND_DATA(0x0c);
218     TFT_SEND_DATA(0x00);
219     TFT_SEND_DATA(0x33);
220     TFT_SEND_DATA(0x33);
221 
222     TFT_SEND_CMD(0x20); // Display Inversion Off
223 
224     TFT_SEND_CMD(0xb7);     // Gate control
225     TFT_SEND_DATA(0x56); // 35
226     //---------------------------------ST7789S Power setting--------------------------------------//
227     TFT_SEND_CMD(0xbb);     // VCOMS Setting
228     TFT_SEND_DATA(0x18); // 1f
229 
230     TFT_SEND_CMD(0xc0); // LCM Control
231     TFT_SEND_DATA(0x2c);
232 
233     TFT_SEND_CMD(0xc2); // VDV and VRH Command Enable
234     TFT_SEND_DATA(0x01);
235 
236     TFT_SEND_CMD(0xc3);     // VRH Set
237     TFT_SEND_DATA(0x1f); // 12
238 
239     TFT_SEND_CMD(0xc4); // VDV Setting
240     TFT_SEND_DATA(0x20);
241 
242     TFT_SEND_CMD(0xc6); // FR Control 2
243     TFT_SEND_DATA(0x0f);
244     // TFT_SEND_CMD(0xca);
245     // TFT_SEND_DATA(0x0f);
246     // TFT_SEND_CMD(0xc8);
247     // TFT_SEND_DATA(0x08);
248     // TFT_SEND_CMD(0x55);
249     // TFT_SEND_DATA(0x90);
250     TFT_SEND_CMD(0xd0);     // Power Control 1
251     TFT_SEND_DATA(0xa6); // a4
252     TFT_SEND_DATA(0xa1);
253     //--------------------------------ST7789S gamma setting---------------------------------------//
254 
255     TFT_SEND_CMD(0xe0);
256     TFT_SEND_DATA(0xd0);
257     TFT_SEND_DATA(0x0d);
258     TFT_SEND_DATA(0x14);
259     TFT_SEND_DATA(0x0b);
260     TFT_SEND_DATA(0x0b);
261     TFT_SEND_DATA(0x07);
262     TFT_SEND_DATA(0x3a);
263     TFT_SEND_DATA(0x44);
264     TFT_SEND_DATA(0x50);
265     TFT_SEND_DATA(0x08);
266     TFT_SEND_DATA(0x13);
267     TFT_SEND_DATA(0x13);
268     TFT_SEND_DATA(0x2d);
269     TFT_SEND_DATA(0x32);
270 
271     TFT_SEND_CMD(0xe1); // Negative Voltage Gamma Contro
272     TFT_SEND_DATA(0xd0);
273     TFT_SEND_DATA(0x0d);
274     TFT_SEND_DATA(0x14);
275     TFT_SEND_DATA(0x0b);
276     TFT_SEND_DATA(0x0b);
277     TFT_SEND_DATA(0x07);
278     TFT_SEND_DATA(0x3a);
279     TFT_SEND_DATA(0x44);
280     TFT_SEND_DATA(0x50);
281     TFT_SEND_DATA(0x08);
282     TFT_SEND_DATA(0x13);
283     TFT_SEND_DATA(0x13);
284     TFT_SEND_DATA(0x2d);
285     TFT_SEND_DATA(0x32);
286 
287     TFT_SEND_CMD(0x36); // Memory data access control
288     TFT_SEND_DATA(0x00);
289 
290     TFT_SEND_CMD(0x3A); // Interface pixel format
291     // TFT_SEND_DATA(0x55);            //65K
292     TFT_SEND_DATA(0x66); // 262K  RGB 6 6 6
293 
294     TFT_SEND_CMD(0xe7); // SPI2 enable    启用2数据通道模式
295     TFT_SEND_DATA(0x00);
296 
297     TFT_SEND_CMD(0x21); // Display inversion on
298     TFT_SEND_CMD(0x29); // Display on
299     TFT_SEND_CMD(0x2C); // Memory write
300 }
301 
302 void Picture_display(const unsigned char *ptr_pic,const unsigned int row,const unsigned int column)
303 {
304     unsigned int ROW, COLUNMN;
305     TFT_SEND_CMD(0x2a);     // Column address set
306     TFT_SEND_DATA(0x00); // start column
307     TFT_SEND_DATA(0x00);
308     TFT_SEND_DATA(0x00); // end column
309     TFT_SEND_DATA(0x63);
310 
311     TFT_SEND_CMD(0x2b);     // Row address set
312     TFT_SEND_DATA(0x00); // start row
313     TFT_SEND_DATA(0x00);
314     TFT_SEND_DATA(0x00); // end row
315     TFT_SEND_DATA(0x63);
316     TFT_SEND_CMD(0x2C);                // Memory write
317     for (ROW = 0; ROW < row; ROW++) // ROW loop
318     {
319 
320         for (COLUNMN = 0; COLUNMN < column; COLUNMN++) // column loop
321         {
322             TFT_SEND_DATA(*ptr_pic++);
323             TFT_SEND_DATA(*ptr_pic++);
324             TFT_SEND_DATA(*ptr_pic++);
325         }
326     }
327 }
328 void display_char16_16(unsigned int x, unsigned int y, unsigned long color, const unsigned char *point)
329 {
330     unsigned int column;
331     unsigned char tm = 0, temp;
332     TFT_SEND_CMD(0x2a);       // Column address set
333     TFT_SEND_DATA(x >> 8); // start column
334     TFT_SEND_DATA(x);
335     x = x + 15;
336     TFT_SEND_DATA(x >> 8); // end column
337     TFT_SEND_DATA(x);
338 
339     TFT_SEND_CMD(0x2b);       // Row address set
340     TFT_SEND_DATA(y >> 8); // start row
341     TFT_SEND_DATA(y);
342     y = y + 15;
343     TFT_SEND_DATA(y >> 8); // end row
344     TFT_SEND_DATA(y);
345     TFT_SEND_CMD(0x2C); // Memory write
346 
347     for (column = 0; column < 32; column++) // column loop
348     {
349         temp = *point;
350         for (tm = 0; tm < 8; tm++)
351         {
352             if (temp & 0x01)
353             {
354                 TFT_SEND_DATA(color >> 16);
355                 TFT_SEND_DATA(color >> 8);
356                 TFT_SEND_DATA(color);
357             }
358             else
359             {
360                 TFT_SEND_DATA(0);
361                 TFT_SEND_DATA(0);
362                 TFT_SEND_DATA(0);
363             }
364             temp >>= 1;
365         }
366         point++;
367     }
368 }
369 void Picture_ReverseDisplay(const unsigned char *ptr_pic)
370 {
371     unsigned int ROW, column, data;
372     TFT_SEND_CMD(0x2a);     // Column address set
373     TFT_SEND_DATA(0x00); // start column
374     TFT_SEND_DATA(0x00);
375     TFT_SEND_DATA(0x00); // end column
376     TFT_SEND_DATA(0xef);
377 
378     TFT_SEND_CMD(0x2b);     // Row address set
379     TFT_SEND_DATA(0x00); // start row
380     TFT_SEND_DATA(0x28);
381     TFT_SEND_DATA(0x01); // end row
382     TFT_SEND_DATA(0x17);
383     TFT_SEND_CMD(0x2C);                             // Memory write
384     for (ROW = 0; ROW < OLED_LINE_NUMBER; ROW++) // ROW loop
385     {
386 
387         for (column = 0; column < OLED_COLUMN_NUMBER; column++) // column loop
388         {
389             data = *ptr_pic++;
390             data = ~data;
391             TFT_SEND_DATA(data);
392         }
393     }
394 }
TFT_SPI2.0.h 驱动文件如下:
 1 #ifndef __TFT_SPI_H
 2 #define __TFT_SPI_H
 3 
 4 #include "stm32f1xx_hal.h"
 5 
 6 // 屏幕参数定义
 7 #define OLED_COLUMN_NUMBER 240
 8 #define OLED_LINE_NUMBER 320
 9 #define OLED_COLUMN_OFFSET 0
10 
11 // 颜色定义
12 #define RED 0XFF0000
13 #define GREEN 0X00FF00
14 #define BLUE 0X0000FF
15 
16 // 控制引脚定义
17 #define TFT_RST_PIN GPIO_PIN_7
18 #define TFT_RST_PORT GPIOB
19 #define TFT_DC_PIN GPIO_PIN_8
20 #define TFT_DC_PORT GPIOB
21 #define TFT_CS_PIN GPIO_PIN_9
22 #define TFT_CS_PORT GPIOB
23 
24 // 引脚操作宏
25 #define SPI_RST_0 HAL_GPIO_WritePin(TFT_RST_PORT, TFT_RST_PIN, GPIO_PIN_RESET)
26 #define SPI_RST_1 HAL_GPIO_WritePin(TFT_RST_PORT, TFT_RST_PIN, GPIO_PIN_SET)
27 #define SPI_DC_0 HAL_GPIO_WritePin(TFT_DC_PORT, TFT_DC_PIN, GPIO_PIN_RESET)
28 #define SPI_DC_1 HAL_GPIO_WritePin(TFT_DC_PORT, TFT_DC_PIN, GPIO_PIN_SET)
29 #define SPI_CS_0 HAL_GPIO_WritePin(TFT_CS_PORT, TFT_CS_PIN, GPIO_PIN_RESET)
30 #define SPI_CS_1 HAL_GPIO_WritePin(TFT_CS_PORT, TFT_CS_PIN, GPIO_PIN_SET)
31 
32 // 外部变量声明
33 extern const unsigned char *point;
34 
35 // 外部函数声明(由CubeMX生成)
36 extern void SystemClock_Config(void);
37 extern void MX_GPIO_Init(void);
38 extern void MX_SPI1_Init(void);
39 
40 
41 // 函数声明
42 void delay_us(unsigned int _us_time);
43 void delay_ms(unsigned int _ms_time);
44 void SPI_SendByte(unsigned char byte);
45 void TFT_SEND_CMD(unsigned char o_command);
46 void TFT_SEND_DATA(unsigned char o_data);
47 void OLED_clear(void);
48 void OLED_full(unsigned long color);
49 void OLED_init(void);
50 void Picture_display(const unsigned char *ptr_pic,const unsigned int row,const unsigned int column);
51 void display_char16_16(unsigned int x, unsigned int y, unsigned long color, const unsigned char *point);
52 void Picture_ReverseDisplay(const unsigned char *ptr_pic);
53 void IO_init(void);
54 
55 #endif /* __TFT_SPI_H */

 

posted @ 2025-12-16 20:31  CiceroCole  阅读(2)  评论(0)    收藏  举报