感谢EEPW推出的“ST NUCLEO-WBA55CG无线MCU开发大作战”活动,本次来分享一下基于STM32WBA55 NUCLEO开发板的TFT屏幕驱动。
屏幕照片:
主控芯片上ST7789,采用的是4线SPI接口。CSX: (chip enable), D/CX :(data/ command flag), SCL :(serial clock) SDA: (serial data input/output).
厂商手册里面提到的读写时序如下:
根据上述时序参数,在CubeIDE内置的CubeMX里面配置使用STM32WBA55芯片的SPI1。
上述关于时钟的参数:CPOL与CPHA是有关SPI的运行模式。通过SPI时钟信号的极性和相位可配置出4种基本操作模式
Mode0~3。
查看SPI1相关的GPIO配置:
注意:PB4同时也连接到了小灯LD1。如果用作SPI功能,就不要再驱动小灯LD1,否则SPI不能正常工作。
除了时钟与数据,还有RES和dc两个控制引脚需要指定到GPIO
点击保存,自动生成代码。
void LCD_Writ_Bus(u8 dat){ HAL_SPI_Transmit(&hspi1,&dat,1,1000); }
发送一个字节的数据调用:
void LCD_WR_DATA8(u8 dat) { LCD_Writ_Bus(dat); }
发送两个Byte:
void LCD_WR_DATA(u16 dat) { LCD_Writ_Bus(dat>>8); LCD_Writ_Bus(dat); }
LCD初始化:
void LCD_Init(void) { LCD_GPIO_Init();//初始化GPIO LCD_RES_Clr();//复位 HAL_Delay(100); LCD_RES_Set(); HAL_Delay(100); LCD_BLK_Set();//打开背光 HAL_Delay(100); //************* Start Initial Sequence **********// LCD_WR_REG(0x11); //Sleep out HAL_Delay(120); //Delay 120ms //************* Start Initial Sequence **********// LCD_WR_REG(0x36); if(USE_HORIZONTAL==0)LCD_WR_DATA8(0x00); else if(USE_HORIZONTAL==1)LCD_WR_DATA8(0xC0); else if(USE_HORIZONTAL==2)LCD_WR_DATA8(0x70); else LCD_WR_DATA8(0xA0); LCD_WR_REG(0x3A); LCD_WR_DATA8(0x05); LCD_WR_REG(0xB2); LCD_WR_DATA8(0x0C); LCD_WR_DATA8(0x0C); LCD_WR_DATA8(0x00); LCD_WR_DATA8(0x33); LCD_WR_DATA8(0x33); LCD_WR_REG(0xB7); LCD_WR_DATA8(0x35); LCD_WR_REG(0xBB); LCD_WR_DATA8(0x37); LCD_WR_REG(0xC0); LCD_WR_DATA8(0x2C); LCD_WR_REG(0xC2); LCD_WR_DATA8(0x01); LCD_WR_REG(0xC3); LCD_WR_DATA8(0x12); LCD_WR_REG(0xC4); LCD_WR_DATA8(0x20); LCD_WR_REG(0xC6); LCD_WR_DATA8(0x0F); LCD_WR_REG(0xD0); LCD_WR_DATA8(0xA4); LCD_WR_DATA8(0xA1); LCD_WR_REG(0xE0); LCD_WR_DATA8(0xD0); LCD_WR_DATA8(0x04); LCD_WR_DATA8(0x0D); LCD_WR_DATA8(0x11); LCD_WR_DATA8(0x13); LCD_WR_DATA8(0x2B); LCD_WR_DATA8(0x3F); LCD_WR_DATA8(0x54); LCD_WR_DATA8(0x4C); LCD_WR_DATA8(0x18); LCD_WR_DATA8(0x0D); LCD_WR_DATA8(0x0B); LCD_WR_DATA8(0x1F); LCD_WR_DATA8(0x23); LCD_WR_REG(0xE1); LCD_WR_DATA8(0xD0); LCD_WR_DATA8(0x04); LCD_WR_DATA8(0x0C); LCD_WR_DATA8(0x11); LCD_WR_DATA8(0x13); LCD_WR_DATA8(0x2C); LCD_WR_DATA8(0x3F); LCD_WR_DATA8(0x44); LCD_WR_DATA8(0x51); LCD_WR_DATA8(0x2F); LCD_WR_DATA8(0x1F); LCD_WR_DATA8(0x1F); LCD_WR_DATA8(0x20); LCD_WR_DATA8(0x23); LCD_WR_REG(0x21); LCD_WR_REG(0x29); }
主函数中调用测试代码:
LCD_ShowChinese(0,0,"中景园电子",RED,WHITE,32,0); LCD_ShowString(0,40,"LCD_W:",RED,WHITE,16,0); LCD_ShowIntNum(48,40,LCD_W,3,RED,WHITE,16); LCD_ShowString(80,40,"LCD_H:",RED,WHITE,16,0); LCD_ShowIntNum(128,40,LCD_H,3,RED,WHITE,16); LCD_ShowString(80,40,"LCD_H:",RED,WHITE,16,0); LCD_ShowString(0,70,"Increaseing Nun:",RED,WHITE,16,0); LCD_ShowFloatNum1(128,70,t,4,RED,WHITE,16); t+=0.11; for(j=0;j<3;j++) { for(i=0;i<6;i++) { LCD_ShowPicture(40*i,120+j*40,40,40,gImage_1); } } }
实物:
此外,本次DIY活动还购置了心率传感器与温湿度传感器,后续尝试驱动这些模块。
附件:项目源码