STM32 控制lcm液晶ILI9341驅(qū)動的液晶驅(qū)動程序
/* 選擇BANK1-BORSRAM1 連接 TFT,地址范圍為0X60000000~0X63FFFFFF
* FSMC_A16 接LCD的DC(寄存器/數(shù)據(jù)選擇)腳
* 16 bit => FSMC[24:0]對應(yīng)HADDR[25:1]
* 寄存器基地址 = 0X60000000
* RAM基地址 = 0X60020000 = 0X60000000+2^16*2 = 0X60000000 + 0X20000 = 0X60020000
* 當(dāng)選擇不同的地址線時,地址要重新計算。
*/
//#define Bank1_LCD_D((u32)0x60020000)//Disp Data ADDR
//#define Bank1_LCD_C((u32)0x60000000)//Disp Reg ADDR
/*選定LCD指定寄存器*/
//#define LCD_WR_REG(index) ((*(__IO u16 *) (Bank1_LCD_C)) = ((u16)index))
/*將LCD中的index寄存器設(shè)置為val值*/
//#define LCD_WR_CMD(index,val)do{(*(__IO u16 *) (Bank1_LCD_C)) = ((u16)index);(*(__IO u16 *) (Bank1_LCD_D)) = ((u16)(val));}while(0)
/*往LCD GRAM寫入數(shù)據(jù)*/
//#define LCD_WR_Data(val)((*(__IO u16 *) (Bank1_LCD_D)) = ((u16)(val)))
void ILI9341_LCD_Init(void);
/**********FSMC**********/
/*******************************************
* 函數(shù)名:LCD_GPIO_Config
* 描述:根據(jù)FSMC配置LCD的I/O
* 輸入: 無
* 輸出:無
* 舉例:無
* 注意:無
*********************************************/
void LCD_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能FSMC時鐘*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
/* 使能FSMC對應(yīng)相應(yīng)管腳時鐘*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* 配置LCD背光控制管腳*/
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
//GPIO_Init(GPIOD, &GPIO_InitStructure);
/* 配置LCD復(fù)位控制管腳*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* 配置FSMC相對應(yīng)的數(shù)據(jù)線,FSMC-D0~D15: PD 14 15 0 1,PE 7 8 9 10 11 12 13 14 15,PD 8 9 10*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | GPIO_Pin_9 |
GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15;
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;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* 配置FSMC相對應(yīng)的控制線
* PD4-FSMC_NOE:LCD-RD
* PD5-FSMC_NWE:LCD-WR
* PD7-FSMC_NE1:LCD-CS
* PD11-FSMC_A16 :LCD-DC
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* tft control gpio init */
//GPIO_SetBits(GPIOD, GPIO_Pin_13);// 背光打開
//GPIO_ResetBits(GPIOE, GPIO_Pin_1);//RST = 1
//GPIO_SetBits(GPIOD, GPIO_Pin_4);// RD = 1
// GPIO_SetBits(GPIOD, GPIO_Pin_5);// WR = 1
// GPIO_SetBits(GPIOD, GPIO_Pin_7);//CS = 1
GPIO_SetBits(GPIOD, GPIO_Pin_3);//RST = 1
// GPIO_SetBits(GPIOD, GPIO_Pin_6);
}
/*******************************************
* 函數(shù)名:LCD_FSMC_Config
* 描述:LCDFSMC 模式配置
* 輸入: 無
* 輸出:無
* 舉例:無
* 注意:無
*********************************************/
void LCD_FSMC_Config(void)
{
FSMC_NORSRAMInitTypeDefFSMC_NORSRAMInitStructure;
FSMC_NORSRAMTimingInitTypeDefp;
p.FSMC_AddressSetupTime = 0x02;//地址建立時間
p.FSMC_AddressHoldTime = 0x00;//地址保持時間
p.FSMC_DataSetupTime = 0x05;//數(shù)據(jù)建立時間
p.FSMC_BusTurnAroundDuration = 0x00;
p.FSMC_CLKDivision = 0x00;
p.FSMC_DataLatency = 0x00;
p.FSMC_AccessMode = FSMC_AccessMode_B;// 一般使用模式B來控制LCD
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;
FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
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_Disable;
FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
/* 使能 FSMC Bank1_SRAM Bank */
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
}
/**********************************
* 函數(shù)名:LCD_Rst
* 描述:LCD 軟件復(fù)位
* 輸入: 無
* 輸出:無
* 舉例:無
* 注意:無
*************************************/
void LCD_Rst(void)
{
GPIO_ResetBits(GPIOD, GPIO_Pin_3);
SysTickDelay(100);
GPIO_SetBits(GPIOD, GPIO_Pin_3);
SysTickDelay(100);
}
void Lcd_data_start(void)
{
LCD_WR_REG(0X2C);
}
/**********************************
* 函數(shù)名:Lcd_init_conf
* 描述:LCD寄存器初始配置
* 輸入: 無
* 輸出:無
* 舉例:無
* 注意:無
*************************************/
void Lcd_init_conf(void)
{
LCD_WR_REG(0xCF);
LCD_WR_Data(0x00);
LCD_WR_Data(0x81);
LCD_WR_Data(0x30);
LCD_WR_REG(0xED);
LCD_WR_Data(0x64);
LCD_WR_Data(0x03);
LCD_WR_Data(0x12);
LCD_WR_Data(0x81);
LCD_WR_REG(0xE8);
LCD_WR_Data(0x85);
LCD_WR_Data(0x10);
LCD_WR_Data(0x78);
LCD_WR_REG(0xCB);
LCD_WR_Data(0x39);
LCD_WR_Data(0x2C);
LCD_WR_Data(0x00);
LCD_WR_Data(0x34);
LCD_WR_Data(0x02);
LCD_WR_REG(0xF7);
LCD_WR_Data(0x20);
LCD_WR_REG(0xEA);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_REG(0xB1);
LCD_WR_Data(0x00);
LCD_WR_Data(0x1B);
LCD_WR_REG(0xB6);
LCD_WR_Data(0x0A);
LCD_WR_Data(0xA2);
LCD_WR_REG(0xC0);
LCD_WR_Data(0x35);
LCD_WR_REG(0xC1);
LCD_WR_Data(0x11);
LCD_WR_REG(0xC5);
LCD_WR_Data(0x45);
LCD_WR_Data(0x45);
LCD_WR_REG(0xC7);
LCD_WR_Data(0xA2);
LCD_WR_REG(0xF2);
LCD_WR_Data(0x00);
LCD_WR_REG(0x26);
LCD_WR_Data(0x01);
LCD_WR_REG(0xE0); //Set Gamma
LCD_WR_Data(0x0F);
LCD_WR_Data(0x26);
LCD_WR_Data(0x24);
LCD_WR_Data(0x0B);
LCD_WR_Data(0x0E);
LCD_WR_Data(0x09);
LCD_WR_Data(0x54);
LCD_WR_Data(0xA8);
LCD_WR_Data(0x46);
LCD_WR_Data(0x0C);
LCD_WR_Data(0x17);
LCD_WR_Data(0x09);
LCD_WR_Data(0x0F);
LCD_WR_Data(0x07);
LCD_WR_Data(0x00);
LCD_WR_REG(0XE1); //Set Gamma
LCD_WR_Data(0x00);
LCD_WR_Data(0x19);
LCD_WR_Data(0x1B);
LCD_WR_Data(0x04);
LCD_WR_Data(0x10);
LCD_WR_Data(0x07);
LCD_WR_Data(0x2A);
LCD_WR_Data(0x47);
LCD_WR_Data(0x39);
LCD_WR_Data(0x03);
LCD_WR_Data(0x06);
LCD_WR_Data(0x06);
LCD_WR_Data(0x30);
LCD_WR_Data(0x38);
LCD_WR_Data(0x0F);
LCD_WR_REG(0x36);
LCD_WR_Data(0x08);
LCD_WR_REG(0X2A);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0xEF);
LCD_WR_REG(0X2B);
LCD_WR_Data(0x00);
LCD_WR_Data(0x00);
LCD_WR_Data(0x01);
LCD_WR_Data(0x3F);
LCD_WR_REG(0x3a); // Memory Access Control
LCD_WR_Data(0x55);
LCD_WR_REG(0x11); //Exit Sleep
SysTickDelay(120);
LCD_WR_REG(0x29); //display on
LCD_WR_REG(0x2c);
}
/*****************************************
* 函數(shù)名:LCD_Init
* 描述:LCD 控制 I/O 初始化
*LCD FSMC 初始化
*LCD 控制器 HX8347 初始化
* 輸入: 無
* 輸出:無
* 舉例:無
* 注意:無
******************************************/
void TFT_Draw_Point(u16 x,u8 y,u16 color)
{
LCD_WR_REG(0X2A);
LCD_WR_Data(x>>8);
LCD_WR_Data(x&0xFF);
LCD_WR_REG(0X2B);
LCD_WR_Data(0x00);
LCD_WR_Data(y);
LCD_WR_REG(0x2C);//開始寫
LCD_WR_Data(color);
}
void ILI9341_LCD_Init(void)
{
unsigned long i;
LCD_GPIO_Config();
LCD_FSMC_Config();
LCD_Rst();
Lcd_init_conf();
Lcd_data_start();
for(i=0; i<(320*240); i++)
{
LCD_WR_Data(0xF800);
}
TFT_Draw_Point(319,120,0x07E0);
}
/**********FSMC**********/