昨天使用探索者STM32F407将ILI9486 TFTLCD显示屏驱动已经正常调通,下面将附上所有的程序代码
//tftlcd.c 文件程序如下所示
/****************************************************************************************
* 函 数 名: TFTLCD_GPIO_Init
* 函数功能: tft lcd 显示屏初始化函数
* 输 入:
无
* 输 出: 无
* 修改记录:
* 作 者:
* 日 期:
* 功能测试:功能测试正常
*******************************************************************************************/
void TFTLCD_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE|
RCC_AHB1Periph_GPIOF|RCC_AHB1Periph_GPIOG, ENABLE);//使能PB,PD,PE,PF,PG时钟
GPIO_PinAFConfig(GPIOD,GPIO_PinSource14,GPIO_AF_FSMC); //FSMC_D0-FSMC_D15
GPIO_PinAFConfig(GPIOD,GPIO_PinSource15,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource0,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource1,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource7,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource8,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource10,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource12,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource13,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource14,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource15,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource8,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource9,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource10,GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_AF_FSMC); //FSMC_NOE
GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_FSMC); //FSMC_NWE
GPIO_PinAFConfig(GPIOF,GPIO_PinSource12,GPIO_AF_FSMC); //FSMC_A6
GPIO_PinAFConfig(GPIOG,GPIO_PinSource12,GPIO_AF_FSMC); //FSMC_NE4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4
| GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_9
| GPIO_Pin_10 | GPIO_Pin_14| GPIO_Pin_15;//PD0,1,4,5,8,9,10,14,15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9
| GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12
| GPIO_Pin_13 | GPIO_Pin_14| GPIO_Pin_15;//PE7~15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//PF12,FSMC_A6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//PG12,FSMC_NE4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //开启背光
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/****************************************************************************************
* 函 数 名: TFTLCD_FSMC_Init
* 函数功能: tft fsmc 参数初始化函数
* 输 入:
无
* 输 出: 无
* 修改记录:
* 作 者:
* 日 期:
* 功能测试:功能测试正常
*******************************************************************************************/
void TFTLCD_FSMC_Init(void)
{
FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
FSMC_NORSRAMTimingInitTypeDef FSMC_ReadNORSRAMTiming;
FSMC_NORSRAMTimingInitTypeDef FSMC_WriteNORSRAMTiming;
RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC,ENABLE);//使能FSMC时钟
FSMC_ReadNORSRAMTiming.FSMC_AddressSetupTime = 0X01; //地址建立时间(ADDSET)为2个HCLK 1/168M=6ns*2=12ns
FSMC_ReadNORSRAMTiming.FSMC_AddressHoldTime = 0x00; //地址保持时间(ADDHLD)模式A未用到
FSMC_ReadNORSRAMTiming.FSMC_DataSetupTime = 0x0f; //数据保存时间为16个HCLK 因为液晶驱动IC的读数据的时候,速度不能太快
FSMC_ReadNORSRAMTiming.FSMC_BusTurnAroundDuration = 0x00;
FSMC_ReadNORSRAMTiming.FSMC_CLKDivision = 0x00;
FSMC_ReadNORSRAMTiming.FSMC_DataLatency = 0x00;
FSMC_ReadNORSRAMTiming.FSMC_AccessMode = FSMC_AccessMode_A; //模式A
FSMC_WriteNORSRAMTiming.FSMC_AddressSetupTime =0x10; //地址建立时间(ADDSET)为1个HCLK
FSMC_WriteNORSRAMTiming.FSMC_AddressHoldTime = 0x10; //地址保持时间(A
FSMC_WriteNORSRAMTiming.FSMC_DataSetupTime = 0x18; //数据保存时间为6ns*9个HCLK=54ns
FSMC_WriteNORSRAMTiming.FSMC_BusTurnAroundDuration = 0x00;
FSMC_WriteNORSRAMTiming.FSMC_CLKDivision = 0x00;
FSMC_WriteNORSRAMTiming.FSMC_DataLatency = 0x00;
FSMC_WriteNORSRAMTiming.FSMC_AccessMode = FSMC_AccessMode_A; //模式A
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM4;// 这里我们使用NE4 ,也就对应BTCR[6],[7]。
FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; // 不复用数据地址
FSMC_NORSRAMInitStructure.FSMC_MemoryType =FSMC_MemoryType_SRAM;// FSMC_MemoryType_SRAM; //SRAM
FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;//存储器数据宽度为16bit
FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode =FSMC_BurstAccessMode_Disable;// FSMC_BurstAccessMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait=FSMC_AsynchronousWait_Disable;
FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; // 存储器写使能
FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable; // 读写使用不同的时序
FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &FSMC_ReadNORSRAMTiming; //读写时序
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &FSMC_WriteNORSRAMTiming; //写时序
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); //初始化FSMC配置
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE); // 使能BANK1
}
/**************************************************************************************
* 函 数 名: openBackLight
* 函数功能: 开启LCD的背光
* 输 入:
无
* 输 出: 无
* 修改记录:
* 作 者:
* 日 期:
* 功能测试:功能测试正常
*******************************************************************************************/
void openLCDBackLight(void)
{
GPIO_SetBits(GPIOB, GPIO_Pin_15);
}
/**************************************************************************************
* 函 数 名: LCD_WriteCmd
* 函数功能: lcd 往寄存器中写命令
* 输 入:
无
* 输 出: 无
* 修改记录:
* 作 者:
* 日 期:
* 功能测试:功能测试正常
*******************************************************************************************/
void LCD_WriteCmd(u16 cmd)
{
TFTLCD->LCD_CMD=cmd;
}
/****************************************************************************************
* 函 数 名: LCD_WriteData
* 函数功能: lcd 往寄存器中数据
* 输 入:
无
* 输 出: 无
* 修改记录:
* 作 者:
* 日 期:
* 功能测试:功能测试正常
*******************************************************************************************/
void LCD_WriteData(u16 data)
{
TFTLCD->LCD_DATA=data;
}
/****************************************************************************************
* 函 数 名: LCD_WriteCmdData
* 函数功能: lcd 往寄存器中写命令和数据
* 输 入:
无
* 输 出: 无
* 修改记录:
* 作 者:
* 日 期:
* 功能测试:功能测试正常
*******************************************************************************************/
void LCD_WriteCmdData(u16 cmd,u16 data)
{
LCD_WriteCmd(cmd);
LCD_WriteData(data);
}
void LCD_WriteData_Color(u16 color)
{
TFTLCD->LCD_DATA=color;
}
u16 LCD_ReadData(void)
{
return TFTLCD->LCD_DATA;
}
//设置LCD显示方向
//dir:0,竖屏;1,横屏
void LCD_Display_Dir(u8 dir)
{
if(dir==0) //默认竖屏方向
{
LCD_WriteCmd(0x36); //设置彩屏显示方向的寄存器
LCD_WriteData(0x00);
tftlcd_data.height=480;
tftlcd_data.width=320;
tftlcd_data.dir= 0 ;
}
else
{
LCD_WriteCmd(0x36); //设置彩屏显示方向的寄存器
LCD_WriteData(0x60);
tftlcd_data.height=320;
tftlcd_data.width=480;
tftlcd_data.dir= 1;
}
}
void TFTLCD_Init(void)
{
//u16 i;
TFTLCD_GPIO_Init();
TFTLCD_FSMC_Init();
delay_ms(50);
LCD_WriteCmd(0Xd3);
tftlcd_data.id=TFTLCD->LCD_DATA; //dummy read
tftlcd_data.id=TFTLCD->LCD_DATA; //00
tftlcd_data.id=TFTLCD->LCD_DATA; //94
tftlcd_data.id<<=8;
tftlcd_data.id|=TFTLCD->LCD_DATA; //86
printf(" LCD ID:%x\r\n",tftlcd_data.id); //打印LCD ID
printf(" read lcd id succsee \r\n");
LCD_WriteCmd(0xB0);
LCD_WriteData(0x00);
LCD_WriteCmd(0x11);
delay_ms(20);
LCD_WriteCmd(0xB3);
LCD_WriteData(0x02);
LCD_WriteData(0x00);
LCD_WriteData(0x00);
LCD_WriteData(0x00);
LCD_WriteCmd(0xC0);
LCD_WriteData(0x10);//13
LCD_WriteData(0x3B);//480
LCD_WriteData(0x00);
LCD_WriteData(0x02);
LCD_WriteData(0x00);
LCD_WriteData(0x01);
LCD_WriteData(0x00);//NW
LCD_WriteData(0x43);
LCD_WriteCmd(0xC1);
LCD_WriteData(0x10);//LCD_WriteData(0x08);
LCD_WriteData(0x10);//LCD_WriteData(0x16);//CLOCK
LCD_WriteData(0x08);
LCD_WriteData(0x08);
LCD_WriteCmd(0xC4);
LCD_WriteData(0x11);
LCD_WriteData(0x07);
LCD_WriteData(0x03);
LCD_WriteData(0x03);
LCD_WriteCmd(0xC6);
LCD_WriteData(0x00);
LCD_WriteCmd(0xC8);//GAMMA
LCD_WriteData(0x03);
LCD_WriteData(0x03);
LCD_WriteData(0x13);
LCD_WriteData(0x5C);
LCD_WriteData(0x03);
LCD_WriteData(0x07);
LCD_WriteData(0x14);
LCD_WriteData(0x08);
LCD_WriteData(0x00);
LCD_WriteData(0x21);
LCD_WriteData(0x08);
LCD_WriteData(0x14);
LCD_WriteData(0x07);
LCD_WriteData(0x53);
LCD_WriteData(0x0C);
LCD_WriteData(0x13);
LCD_WriteData(0x03);
LCD_WriteData(0x03);
LCD_WriteData(0x21);
LCD_WriteData(0x00);
LCD_WriteCmd(0x35);
LCD_WriteData(0x00);
LCD_WriteCmd(0x36);
LCD_WriteData(0x00);
LCD_WriteCmd(0x3A);
LCD_WriteData(0x55);
LCD_WriteCmd(0x44);
LCD_WriteData(0x00);
LCD_WriteData(0x01);
LCD_WriteCmd(0xB6);
LCD_WriteData(0x00);
LCD_WriteData(0x22);//0 GS SS SM ISC[3:0];其中GS SS控制显示方向,同时修改R36
LCD_WriteData(0x3B);
LCD_WriteCmd(0xD0);
LCD_WriteData(0x07);
LCD_WriteData(0x07);//VCI1
LCD_WriteData(0x1D);//VRH
LCD_WriteCmd(0xD1);
LCD_WriteData(0x00);
LCD_WriteData(0x03);//VCM
LCD_WriteData(0x00);//VDV
LCD_WriteCmd(0xD2);
LCD_WriteData(0x03);
LCD_WriteData(0x14);
LCD_WriteData(0x04);
LCD_WriteCmd(0x29);
delay_ms(20);
LCD_WriteCmd(0xB4);
LCD_WriteData(0x00);
delay_ms(20);
LCD_WriteCmd(0x2C);
LCD_Display_Dir(TFTLCD_DIR); //0:竖屏 1:横屏 默认竖屏
openLCDBackLight(); //开启背光
LCD_Clear(GREEN);
}
//设置窗口,并自动设置画点坐标到窗口左上角(sx,sy).
//sx,sy:窗口起始坐标(左上角)
//width,height:窗口宽度和高度,必须大于0!!
//窗体大小:width*height.
void LCD_Set_Window(u16 sx,u16 sy,u16 width,u16 height)
{
LCD_WriteCmd(0x2A);
LCD_WriteData(sx/256);
LCD_WriteData(sx%256);
LCD_WriteData(width/256);
LCD_WriteData(width%256);
LCD_WriteCmd(0x2B);
LCD_WriteData(sy/256);
LCD_WriteData(sy%256);
LCD_WriteData(height/256);
LCD_WriteData(height%256);
LCD_WriteCmd(0x2C);
}
//清屏函数
//color:要清屏的填充色
void LCD_Clear(u16 color)
{
uint16_t i, j ;
LCD_Set_Window(0, 0, tftlcd_data.width-1, tftlcd_data.height-1); //作用区域
for(i=0; i
下面是tftlcd.h文件源代码
#ifndef _tftlcd_H
#define _tftlcd_H
#include "system.h"
#define TFTLCD_ILI9486
#define TFTLCD_DIR 0 //0:竖屏 1:横屏 默认竖屏
//TFTLCD地址结构体
typedef struct
{
u16 LCD_CMD;
u16 LCD_DATA;
}TFTLCD_TypeDef;
//使用NOR/SRAM的 Bank1.sector4,地址位HADDR[27,26]=11 A6作为数据命令区分线
//注意设置时STM32内部会右移一位对齐! 111 1110=0X7E
#define TFTLCD_BASE ((u32)(0x6C000000 | 0x0000007E))
#define TFTLCD ((TFTLCD_TypeDef *) TFTLCD_BASE)
//TFTLCD重要参数集
typedef struct
{
u16 width; //LCD 宽度
u16 height; //LCD 高度
u16 id; //LCD ID
u8 dir; //LCD 方向
}_tftlcd_data;
//LCD参数
extern _tftlcd_data tftlcd_data; //管理LCD重要参数
//LCD的前端颜色和背景色
extern u16 FRONT_COLOR;//前端颜色 默认红色
extern u16 BACK_COLOR; //背景颜色.默认为白色
//画笔颜色
#define WHITE 0xFFFF
#define BLACK 0x0000
#define RED 0x001F
#define BRED 0XF81F
#define GRED 0XFFE0
#define GBLUE 0X07FF
#define BLUE 0xF800
#define MAGENTA 0xF81F
#define GREEN 0x07E0
#define CYAN 0x7FFF
#define YELLOW 0xFFE0
#define BROWN 0XBC40 //棕色
#define BRRED 0XFC07 //棕红色
#define GRAY 0X8430 //灰色
void LCD_WriteCmd(u16 cmd);
void LCD_WriteData(u16 data);
void LCD_WriteCmdData(u16 cmd,u16 data);
void LCD_WriteData_Color(u16 color);
void TFTLCD_Init(void); //初始化
void LCD_Set_Window(u16 sx,u16 sy,u16 width,u16 height);
void LCD_Clear(u16 Color); //清屏
void LCD_Fill(u16 xState,u16 yState,u16 xEnd,u16 yEnd,u16 color);
void LCD_DrawPoint(u16 x,u16 y);//画点
void LCD_DrawFRONT_COLOR(u16 x,u16 y,u16 color);
u16 LCD_ReadPoint(u16 x,u16 y);
void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2);
void LCD_DrawLine_Color(u16 x1, u16 y1, u16 x2, u16 y2,u16 color);
void LCD_DrowSign(uint16_t x, uint16_t y, uint16_t color);
void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2);
void LCD_Draw_Circle(u16 x0,u16 y0,u8 r);
void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode);
void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len,u8 size);
void LCD_ShowxNum(u16 x,u16 y,u32 num,u8 len,u8 size,u8 mode);
void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p);
void LCD_ShowFontHZ(u16 x, u16 y, u8 *cn);
void LCD_ShowPicture(u16 x, u16 y, u16 wide, u16 high,u8 *pic);
#endif
将初始化的在主程序中调用就可以正常驱动该屏了。