STM32的FSMC驱动TFT笔记
扫描二维码
随时随地手机看文章
1、FSMC初初了解:
FSMC 包括4个模块:
(1)AHB接口(包括FSMC配置寄存器)
(2)NOR闪存和PSRAM控制器(驱动LCD的时候LCD就好像一个PSRAM的里面只有2个16位的存储空间,一个是DATA RAM 一个是CMD RAM)
(3)NAND闪存和PC卡控制器
(4)外部设备接口
我们驱动TFT很明显用的是第二个模块:NOR闪存。
2、FSMC的外设地址选择
FSMC _Bank 用来选择外接存储器的地址,FSMC所控制的外存储器所映射的地址是0x6000 0000 ~ 0x9fff ffff
其中属于NOR FLASH 的是0x6000 0000 ~ 0x6fff ffff ,然后这部分的地址空间又被分为四份,每份64MB, 编号是BANK1 ~ BANK 4 ,每个BANK 都有一个片选引脚。
而这四个分存储块的片选,则使用NE[4:1]来选择。数据线/地址线/控制线是共享的。
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 驱动LCD
FSMC提供了所有的LCD控制器的信号:
FSMC_D[16:0]:16bit的数据总线
FSMC NEx:分配给NOR的256M,再分为4个区,每个区用来分配一个外设,这四个外设的片选分为是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的命令/数据选择端口RS一样,对于FSMC驱动TFT :RS = 0时,表示读写寄存器;RS = 1表示读写数据RAM。
4、GPIO端口配置
由于是使用FSMC来驱动TFT的,所以除了背光用的PD13和复位用的PC1要设置为通用推挽输出之外,其他引脚都必须设置为复用推挽输出。然而,很奇怪的是。当我配置好PD13时,我发现程序下载进去后是一片漆黑的,后来再三检查是背光的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寄存器数据
* Input : - index: 寄存器数据
* 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 : 读取控制器数据
* Input : None
* Output : None
* Return : 返回读取到的数据
* 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); /* 打开晶振 ,内部晶振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); /* 驱动输出控制320*240 0x2B3F */
LCD_WriteReg(0x0002,0x0600); delay_ms(50);
LCD_WriteReg(0x0010,0x0000); delay_ms(50);
LCD_WriteReg(0x0011,0x6070); delay_ms(50); /* 定义数据格式 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 */