2.4寸TFT240370PQ显示屏,驱动IC为ILI9325。该屏屏幕电源为2.8~3.3V,背光电源最高3.2V(在3.3V电源下串20Ω电阻或5V下串200Ω电阻),兼容8/16位数据接口(可选)。数据口电平理论上不能超3.3V,由于TFT内部有电压钳位,样品测试时可使用5V单片机。
写下几点注意事项:
1、不要急着写屏,首先读ID,确认驱动IC是否ILI9325。ILI9320、ILI9325、ILI9328等驱动IC的初始化参数会稍有不同。读ID还可以确认数据接口选择的正确性。如果是16位数据接口,ID读到0x9325;如果是8位数据接口,ID读两次可得0x93,0x25。
2、虽然TFT屏的初始化参数比较多,但参数要求并不很严格,不会因为一点点的差错而点不亮(当然,不写display on的命令肯定不会亮),所以,在点不亮时不要首先怀疑Init()的参数设置,应该先检查连线的正确性,I/O口的设置。
3、要注意给RST的复位有足够的时间,写命令和写数据的子函数里RS的选择。
4、卖屏的商家通常提供的是STC的例程,若STM32驱不起来,可先用STC把屏点亮,以确认该屏正常,再仔细寻找问题所在。
以下是例程,该例程使用I/O口驱动,虽然选择16位数据接口,但刷屏速度有点慢,建议使用LCD总线。背光使用PWM调节对比度(该段程序省略):
#defineP_TFT_RS GPIOB
#define TFT_RS GPIO_Pin_0
#defineP_TFT_WR GPIOB
#define TFT_WR GPIO_Pin_1
#defineP_TFT_RD GPIOB
#define TFT_RD GPIO_Pin_2
#defineP_TFT_CS GPIOB
#define TFT_CS GPIO_Pin_10
#defineP_TFT_RSTGPIOB
#define TFT_RST GPIO_Pin_11
#defineP_TFT_BK GPIOB
#define TFT_BKGPIO_Pin_7 // TIM4_CH2 PWM
#define Set_CS GPIO_SetBits(P_TFT_CS,TFT_CS);
#define Clr_CS GPIO_ResetBits(P_TFT_CS,TFT_CS);
#define Set_RSGPIO_SetBits(P_TFT_RS,TFT_RS);
#define Clr_RS GPIO_ResetBits(P_TFT_RS,TFT_RS);
#define Set_WR GPIO_SetBits(P_TFT_WR,TFT_WR);
#define Clr_WR GPIO_ResetBits(P_TFT_WR,TFT_WR);
#define Set_RD GPIO_SetBits(P_TFT_RD,TFT_RD);
#define Clr_RD GPIO_ResetBits(P_TFT_RD,TFT_RD);
#define Set_RST GPIO_SetBits(P_TFT_RST,TFT_RST);
#define Clr_RST GPIO_ResetBits(P_TFT_RST,TFT_RST);
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;// for PWM
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void DataToIO(u16 val)
{
u16 dat;
dat = GPIO_ReadOutputData(GPIOC);// DBL
dat = (dat&0xFF00) | (val&0xFF);
GPIO_Write(GPIOC, dat);
dat = GPIO_ReadOutputData(GPIOA);// DBH
dat = (dat&0xFF00) | ((val>>8)&0xFF);
GPIO_Write(GPIOA, dat);
}
void TFT_Write_com(u16 dat) //发送命令
{
Clr_RS;
Set_RD;
DataToIO(dat);
Clr_WR;
Set_WR;
}
void TFT_Write_dat(u16 dat)// 发送数据
{
Set_RS;
Set_RD;
DataToIO(dat);
Clr_WR;
Set_WR;
}
u16 TFT_ReadReg(u16 reg)
{
u16 datL, datH;
GPIO_InitTypeDef GPIO_InitStructure;
Clr_CS;
TFT_Write_com(reg);
GPIO_InitStructure.GPIO_Pin = 0xFF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ;// 上拉输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIOA->ODR = 0xFFFF;
GPIOC->ODR = 0xFFFF;
Set_RS;
Set_WR;
Clr_RD;
datH = GPIOA->IDR;
datL = GPIOC->IDR;
Set_RD;
Set_CS;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = 0xFF;
GPIO_Init(GPIOA, &GPIO_InitStructure);
DataToIO(0xFFFF);
datH = ((datH<8)&0xFF00) | (datL&0x00FF);
return(datH);
}
void TFT_Init(void)
{
u16 DeviceCode;
Set_RST;
Delayms(5);
Clr_RST;
Delayms(5);
Set_RST;
Set_CS;
Set_RD;
Set_WR;
Delayms(5);
DeviceCode = TFT_ReadReg(0x0000); // 读ID,以确认驱动IC为ILI9325
send_data(DeviceCode>>8);
send_data(DeviceCode&0xFF);
Delayms(50);
Clr_CS;// 打开片选使能
//
TFT_Write_com(0x00E5); TFT_Write_dat(0x78F0);// set SRAM internal timing 虽然手册上没有介绍这个时钟的设置,但实际上这个并不很重要,即使不加上去,也可以把屏驱起来,只是图像是否会清晰就有待验证了
//TFT_Write_com(0x00E3); TFT_Write_dat(0x3008);// set SRAM internal timing
//TFT_Write_com(0x00E7); TFT_Write_dat(0x0012);// set SRAM internal timing
//TFT_Write_com(0x00EF); TFT_Write_dat(0x1231);// set SRAM internal timing
//TFT_Write_com(0x0000); TFT_Write_dat(0x0001);// start OcillationILI9325不需要这一句
TFT_Write_com(0x0001); TFT_Write_dat(0x0100);// set SS and SM bit (S720 -> S1)
TFT_Write_com(0x0002); TFT_Write_dat(0x0700);// set 1 line inversion
TFT_Write_com(0x0003); TFT_Write_dat(0x1030);// set GRAM write direction and BGR=1.、、65K
TFT_Write_com(0x0004); TFT_Write_dat(0x0000);// Resize register
TFT_Write_com(0x0008); TFT_Write_dat(0x0207);// set the back porch and front porch
TFT_Write_com(0x0009); TFT_Write_dat(0x0000);// set non-display area refresh cycle ISC[3:0]
TFT_Write_com(0x000A); TFT_Write_dat(0x0000);// FMARK function
TFT_Write_com(0x000C); TFT_Write_dat(0x0000);// RGB interface setting
TFT_Write_com(0x000D); TFT_Write_dat(0x0000);// Frame marker Position
TFT_Write_com(0x000F); TFT_Write_dat(0x0000);// RGB interface polarity
//
TFT_Write_com(0x0010); TFT_Write_dat(0x0000);// SAP, BT[3:0], AP, DSTB, SLP, STB
TFT_Write_com(0x0011); TFT_Write_dat(0x0007);// DC1[2:0], DC0[2:0], VC[2:0]
TFT_Write_com(0x0012); TFT_Write_dat(0x0000);// VREG1OUT voltage
TFT_Write_com(0x0013); TFT_Write_dat(0x0000);// VDV[4:0] for VCOM amplitude
TFT_Write_com(0x0007); TFT_Write_dat(0x0001);
Delayms(50); // Dis-charge capacitor power voltage
TFT_Write_com(0x0010); TFT_Write_dat(0x1090);// SAP, BT[3:0], AP, DSTB, SLP, STB
TFT_Write_com(0x0011); TFT_Write_dat(0x0227);// DC1[2:0], DC0[2:0], VC[2:0]
Delayms(50);
TFT_Write_com(0x0012); TFT_Write_dat(0x001F);//001C// Internal reference voltage= Vci;
Delayms(50);
TFT_Write_com(0x0013); TFT_Write_dat(0x1500);//Set VDV[4:0] for VCOM amplitude
TFT_Write_com(0x0029); TFT_Write_dat(0x0027);//Set VCM[5:0] for VCOMH 电压
TFT_Write_com(0x002B); TFT_Write_dat(0x000D);// Set Frame Rate 000C 帧频
Delayms(50);
TFT_Write_com(0x0020); TFT_Write_dat(0x0000);// GRAM horizontal Address
TFT_Write_com(0x0021); TFT_Write_dat(0x0000);// GRAM Vertical Address
// ----------- Adjust the Gamma Curve ----------//
TFT_Write_com(0x0030); TFT_Write_dat(0x0000);
TFT_Write_com(0x0031); TFT_Write_dat(0x0707);
TFT_Write_com(0x0032); TFT_Write_dat(0x0307);
TFT_Write_com(0x0035); TFT_Write_dat(0x0200);
TFT_Write_com(0x0036); TFT_Write_dat(0x0008);
TFT_Write_com(0x0037); TFT_Write_dat(0x0004);
TFT_Write_com(0x0038); TFT_Write_dat(0x0000);
TFT_Write_com(0x0039); TFT_Write_dat(0x0707);
TFT_Write_com(0x003C); TFT_Write_dat(0x0002);
TFT_Write_com(0x003D); TFT_Write_dat(0x1D04);
//------------------ Set GRAM area ---------------//
TFT_Write_com(0x0050); TFT_Write_dat(0x0000);// Horizontal GRAM Start Address
TFT_Write_com(0x0051); TFT_Write_dat(0x00EF);// Horizontal GRAM End Address
TFT_Write_com(0x0052); TFT_Write_dat(0x0000);// Vertical GRAM Start Address
TFT_Write_com(0x0053); TFT_Write_dat(0x013F);// Vertical GRAM Start Address
TFT_Write_com(0x0060); TFT_Write_dat(0xA700);// Gate Scan Line
TFT_Write_com(0x0061); TFT_Write_dat(0x0001);// NDL,VLE, REV
TFT_Write_com(0x006A); TFT_Write_dat(0x0000);// set scrolling line
//-------------- Partial Display Control ---------//
TFT_Write_com(0x0080); TFT_Write_dat(0x0000);
TFT_Write_com(0x0081); TFT_Write_dat(0x0000);
TFT_Write_com(0x0082); TFT_Write_dat(0x0000);
TFT_Write_com(0x0083); TFT_Write_dat(0x0000);
TFT_Write_com(0x0084); TFT_Write_dat(0x0000);
TFT_Write_com(0x0085); TFT_Write_dat(0x0000);
//-------------- Panel Control -------------------//
TFT_Write_com(0x0090); TFT_Write_dat(0x0010);
TFT_Write_com(0x0092); TFT_Write_dat(0x0600);
TFT_Write_com(0x0007); TFT_Write_dat(0x0133);// 262K color and display ON 开显示
Set_CS;//关闭片选使能
}