1、FSMC初初了解:
FSMC 包括4個模塊:
(1)AHB接口(包括FSMC配置寄存器)
(2)NOR閃存和PSRAM控制器(驅(qū)動LCD的時候LCD就好像一個PSRAM的里面只有2個16位的存儲空間,一個是DATA RAM 一個是CMD RAM)
(3)NAND閃存和PC卡控制器
(4)外部設備接口
我們驅(qū)動TFT很明顯用的是第二個模塊:NOR閃存。
2、FSMC的外設地址選擇
FSMC _Bank 用來選擇外接存儲器的地址,F(xiàn)SMC所控制的外存儲器所映射的地址是0x6000 0000 ~ 0x9fff ffff
其中屬于NOR FLASH 的是0x6000 0000 ~ 0x6fff ffff ,然后這部分的地址空間又被分為四份,每份64MB, 編號是BANK1 ~ BANK 4 ,每個BANK 都有一個片選引腳。
而這四個分存儲塊的片選,則使用NE[4:1]來選擇。數(shù)據(jù)線/地址線/控制線是共享的。
NE1 ->Bank1 NE2->Bank2 NE3->Bank3 NE4->Bank4
第一塊:6000 0000h--63ff ffffh
第二塊:6400 0000h--67ff ffffh
第三塊:6800 0000h--6bff ffffh
第四塊:6c00 0000h--6fff ffffh
3、FSMC 驅(qū)動LCD
FSMC提供了所有的LCD控制器的信號:
FSMC_D[16:0]:16bit的數(shù)據(jù)總線
FSMC NEx:分配給NOR的256M,再分為4個區(qū),每個區(qū)用來分配一個外設,這四個外設的片選分為是NE1-NE4,對應的引腳為:PD7—NE1,PG9—NE2,PG10-NE3,PG12—NE4
FSMC NOE:輸出使能,連接LCD的RD腳。
FSMC NWE:寫使能,連接LCD的RW腳。
FSMC Ax:用在LCD顯示RAM和寄存器之間進行選擇的地址線,即該線用于選擇LCD的RS腳,該線可用地址線的任意一根線,范圍:FSMC_A[25:0]。
正如我們操作12864的命令/數(shù)據(jù)選擇端口RS一樣,對于FSMC驅(qū)動TFT :RS = 0時,表示讀寫寄存器;RS = 1表示讀寫數(shù)據(jù)RAM。
4、GPIO端口配置
由于是使用FSMC來驅(qū)動TFT的,所以除了背光用的PD13和復位用的PC1要設置為通用推挽輸出之外,其他引腳都必須設置為復用推挽輸出。然而,很奇怪的是。當我配置好PD13時,我發(fā)現(xiàn)程序下載進去后是一片漆黑的,后來再三檢查是背光的PD13的問題。必須把PD13置高才可以亮,否則默認低電平,就是一片黑了。
5、 具體程序
/*******************************************************************************
* Function Name : Lcd_Configuration
* Description : Configures LCD Control lines
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void LCD_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOD and GPIOE clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, ENABLE);
/* PE.00(D0), PE.01(D1), PE.02(D2), PE.03(D3), PE.04(D4), PE.05(D5), PE.06(D6), PE.07(D7), PE.08(D8)
PE.09(D9), PE.10(D10), PE.11(D11), PE.12(D12), PE.13(D13), PE.14(D14), PE.15(D15) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* PD.13(RS), PD.14(WR), PD.15(RD) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* PD.12(CS)*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : LCD_WriteReg
* Description : LCD控制器寄存器地址
* Input : - index: 寄存器地址
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
__inline void LCD_WriteIndex(uint16_t index)
{
Clr_Rs;
Set_nRd;
GPIOE->ODR = index; /* GPIO_Write(GPIOE,index); */
Clr_nWr;
Set_nWr;
}
/*******************************************************************************
* Function Name : LCD_WriteReg
* Description : LCD寄存器數(shù)據(jù)
* Input : - index: 寄存器數(shù)據(jù)
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
__inline void LCD_WriteData(uint16_t data)
{
Set_Rs;
GPIOE->ODR = data; /* GPIO_Write(GPIOE,data); */
Clr_nWr;
Set_nWr;
}
/*******************************************************************************
* Function Name : LCD_ReadData
* Description : 讀取控制器數(shù)據(jù)
* Input : None
* Output : None
* Return : 返回讀取到的數(shù)據(jù)
* Attention : None
*******************************************************************************/
__inline uint16_t LCD_ReadData(void)
{
uint16_t value;
Set_Rs;
Set_nWr;
Clr_nRd;
GPIOE->CRH = 0x44444444;
GPIOE->CRL = 0x44444444;
value = GPIOE->IDR;
value = GPIOE->IDR;
GPIOE->CRH = 0x33333333;
GPIOE->CRL = 0x33333333;
Set_nRd;
return value;
}
__inline void LCD_WriteReg(uint16_t LCD_Reg,uint16_t LCD_RegValue)
{
/* Write 16-bit Index, then Write Reg */
Clr_Cs;
LCD_WriteIndex(LCD_Reg);
/* Write 16-bit Reg */
LCD_WriteData(LCD_RegValue);
Set_Cs;
}
__inline uint16_t LCD_ReadReg(uint16_t LCD_Reg)
{
uint16_t LCD_RAM;
/* Write 16-bit Index (then Read Reg) */
Clr_Cs;
LCD_WriteIndex(LCD_Reg);
/* Read 16-bit Reg */
LCD_RAM = LCD_ReadData();
Set_Cs;
return LCD_RAM;
}
static void LCD_SetCursor( uint16_t Xpos, uint16_t Ypos )
{
#if ( DISP_ORIENTATION == 90 ) || ( DISP_ORIENTATION == 270 )
uint16_t temp = Xpos;
Xpos = Ypos;
Ypos = ( MAX_X - 1 ) - temp;
#elif ( DISP_ORIENTATION == 0 ) || ( DISP_ORIENTATION == 180 )
#endif
LCD_WriteReg(0x004e, Xpos );
LCD_WriteReg(0x004f, Ypos );
}
static void delay_ms(uint16_t ms)
{
uint16_t i,j;
for( i = 0; i < ms; i++ )
{
for( j = 0; j < 1141; j++ );
}
}
void LCD_Initializtion(void)
{
LCD_Configuration();
// DeviceCode = LCD_ReadReg(0x0000);
/* 讀取屏ID */
LCD_WriteReg(0x0000,0x0001); delay_ms(50); /* 打開晶振 ,內(nèi)部晶振500k*/
LCD_WriteReg(0x0003,0xA8A4); delay_ms(50);
LCD_WriteReg(0x000C,0x0000); delay_ms(50);
LCD_WriteReg(0x000D,0x080C); delay_ms(50);
LCD_WriteReg(0x000E,0x2B00); delay_ms(50);
LCD_WriteReg(0x001E,0x00B0); delay_ms(50);
LCD_WriteReg(0x0001,0x2B3F); delay_ms(50); /* 驅(qū)動輸出控制320*240 0x2B3F */
LCD_WriteReg(0x0002,0x0600); delay_ms(50);
LCD_WriteReg(0x0010,0x0000); delay_ms(50);
LCD_WriteReg(0x0011,0x6070); delay_ms(50); /* 定義數(shù)據(jù)格式 16位色 橫屏 0x6070 */
LCD_WriteReg(0x0005,0x0000); delay_ms(50);
LCD_WriteReg(0x0006,0x0000); delay_ms(50);
LCD_WriteReg(0x0016,0xEF1C); delay_ms(50);
LCD_WriteReg(0x0017,0x0003); delay_ms(50);
LCD_WriteReg(0x0007,0x0133); delay_ms(50);
LCD_WriteReg(0x000B,0x0000); delay_ms(50);
LCD_WriteReg(0x000F,0x0000); delay_ms(50); /* 掃描開始地址 */
LCD_WriteReg(0x0041,0x0000); delay_ms(50);
LCD_WriteReg(0x0042,0x0000); delay_ms(50);
LCD_WriteReg(0x0048,0x0000); delay_ms(50);
LCD_WriteReg(0x0049,0x013F); delay_ms(50);
LCD_WriteReg(0x004A,0x0000); delay_ms(50);
LCD_WriteReg(0x004B,0x0000); delay_ms(50);
LCD_WriteReg(0x0044,0xEF00); delay_ms(50);
LCD_WriteReg(0x0045,0x0000); delay_ms(50);
LCD_WriteReg(0x0046,0x013F); delay_ms(50);
LCD_WriteReg(0x0030,0x0707); delay_ms(50);
LCD_WriteReg(0x0031,0x0204); delay_ms(50);
LCD_WriteReg(0x0032,0x0204); delay_ms(50);
LCD_WriteReg(0x0033,0x0502); delay_ms(50);
LCD_WriteReg(0x0034,0x0507); delay_ms(50);
LCD_WriteReg(0x0035,0x0204); delay_ms(50);
LCD_WriteReg(0x0036,0x0204); delay_ms(50);
LCD_WriteReg(0x0037,0x0502); delay_ms(50);
LCD_WriteReg(0x003A,0x0302); delay_ms(50);
LCD_WriteReg(0x003B,0x0302); delay_ms(50);
LCD_WriteReg(0x0023,0x0000); delay_ms(50);
LCD_WriteReg(0x0024,0x0000); delay_ms(50);
LCD_WriteReg(0x0025,0x8000); delay_ms(50);
LCD_WriteReg(0x004f,0); /* 行首址0 */