整个程序如下:
int main(void)
{
RCC_cfg();
NVIC_Configuration();
GPIO_cfg();
FSMC_cfg();
LCD_cfg();
while(1)
{
LCD_Clear(Blue);
}
}
void FSMC_cfg(void)
{
//FSMC_NOR_Init();
//FSMC_NAND_Init();
//FSMC_SRAM_Init();
SystemGpioInit();
FSMC_LCD_Init_Color();
}
void LCD_cfg(void)
{
//LCD初始化
LCD_init();
}
void RCC_cfg(void)
{
//定义错误状态变量
ErrorStatus HSEStartUpStatus;
//将RCC寄存器重新设置为默认值
RCC_DeInit();
//打开外部高速时钟晶振
RCC_HSEConfig(RCC_HSE_ON);
//等待外部高速时钟晶振工作
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
//设置AHB时钟(HCLK)为系统时钟
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//设置高速AHB时钟(APB2)为HCLK时钟
RCC_PCLK2Config(RCC_HCLK_Div1);
//设置低速AHB时钟(APB1)为HCLK的2分频
RCC_PCLK1Config(RCC_HCLK_Div2);
//设置FLASH代码延时
FLASH_SetLatency(FLASH_Latency_2);
//使能预取指缓存
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//设置PLL时钟,为HSE的9倍频 8MHz * 9 = 72MHz
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
//使能PLL
RCC_PLLCmd(ENABLE);
//等待PLL准备就绪
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
//设置PLL为系统时钟源
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//判断PLL是否是系统时钟
while(RCC_GetSYSCLKSource() != 0x08);
}
}
void NVIC_Configuration(void)
{
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
}
//GPIO配置
void GPIO_cfg(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//打开GPIO时钟,复用功能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);
//打开FSMC时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/*-- GPIO Configuration ------------------------------------------------------*/
/* Data lines configuration */
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);
/* Address lines configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 |
GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 |
GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* NE2 NE3 NE4 configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_12;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //上面配置成GPIO_Mode_AF_PP,用万用表量了没有输出,一直是高电平3.3V,拉不低
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_ResetBits(GPIOG, GPIO_Pin_9);
GPIO_ResetBits(GPIOG, GPIO_Pin_10);
//GPIO_ResetBits(GPIOG, GPIO_Pin_11);
GPIO_ResetBits(GPIOG, GPIO_Pin_12);//LCD_CS(1);
GPIO_SetBits(GPIOG, GPIO_Pin_9);
GPIO_SetBits(GPIOG, GPIO_Pin_10);
//GPIO_SetBits(GPIOG, GPIO_Pin_11);
GPIO_SetBits(GPIOG, GPIO_Pin_12);
/* NCE2 configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* NBL0, NBL1 configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* NOE and NWE configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_Init(GPIOD, &GPIO_InitStructure);
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
//GPIO_Init(GPIOF, &GPIO_InitStructure);
/* INT2 NAND pin configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOG, &GPIO_InitStructure);
}
void FSMC_LCD_Init_Color(void)
{
FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
FSMC_NORSRAMTimingInitTypeDef pr;
//总时间14nS"*(16)=224>lcd周期200
pr.FSMC_AddressSetupTime = 0;//不能小于5
pr.FSMC_AddressHoldTime = 0;
pr.FSMC_DataSetupTime = 3;//不能小于9
pr.FSMC_BusTurnAroundDuration = 0;
pr.FSMC_CLKDivision = 0;
pr.FSMC_DataLatency = 0;
pr.FSMC_AccessMode = FSMC_AccessMode_C;
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM4;
FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Enable;//必须使能该处
FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;
FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth =FSMC_MemoryDataWidth_16b; //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 = ≺
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = ≺
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
/* Enable FSMC Bank1_NOR Bank */
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE);
}
void SystemGpioInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//KEY_LED,USER_LED,LCD_RESET,USB_CON,USB_LED,POWER_MODE
GPIO_InitStructure.GPIO_Pin = KEY_LED_Gpin7|USER_LED_Gpin8|LCD_RESET_Gpin11|USB_CON_Gpin13|USB_LED_Gpin14|POWER_MODE_Gpin15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure);
//关闭各类灯,断开usb连接,置电源工作模式为固定频率模式
GPIO_SetBits(GPIOG, KEY_LED_Gpin7|USER_LED_Gpin8|LCD_RESET_Gpin11|USB_CON_Gpin13|USB_LED_Gpin14|POWER_MODE_Gpin15);
//关闭液晶背光
GPIO_InitStructure.GPIO_Pin = LCD_LED_Dpin4;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,LCD_LED_Dpin4);
GPIO_ResetBits(GPIOA,LCD_LED_Dpin4);
//Nor Flash 复位信号
// GPIO_InitStructure.GPIO_Pin = NOR_RESET_Epin6;
// GPIO_Init(GPIOE, &GPIO_InitStructure);
// GPIO_SetBits(GPIOE,NOR_RESET_Epin6);
}
void LCD_init(void)
{
GPIO_ResetBits(GPIOG, GPIO_Pin_12);//LCD_CS(0);//
GPIO_ResetBits(GPIOF, GPIO_Pin_0);//LCD_RS(0);//
GPIO_ResetBits(GPIOD, GPIO_Pin_5);
GPIO_ResetBits(GPIOD, GPIO_Pin_4);
GPIO_SetBits(GPIOG, GPIO_Pin_12);//LCD_CS(1);
GPIO_SetBits(GPIOF, GPIO_Pin_0);//LCD_RS(1);//
GPIO_SetBits(GPIOD, GPIO_Pin_5);//LCD_WR(1);
GPIO_SetBits(GPIOD, GPIO_Pin_4);//LCD_RD(1);
GPIO_SetBits(GPIOG, GPIO_Pin_11);//LCD_RST(1) ;
_delay_(2);//Delay(2);
GPIO_ResetBits(GPIOG, GPIO_Pin_11);//LCD_RST(0);
_delay_(2);
GPIO_SetBits(GPIOG, GPIO_Pin_11);//LCD_RST(1);
_delay_(2);
{
W_COM1(0XE3,0X3008); //以下所有行是厂家给的初始化代码,但不知道是什么原理,也是我的问题所在,都弄了大概3个星期了,没有头绪
W_COM1(0XE7,0X0012);
W_COM1(0XEF,0X1231);
W_COM1(0x01, 0x0000); // set SS and SM bit 0000
W_COM1(0x02, 0x0700); // set 1 line inversion 0700
W_COM1(0x03, 0x1080); // set GRAM write direction and BGR=1.
W_COM1(0x04, 0x0000); // Resize register
W_COM1(0x08, 0x0207); // set the back porch and front porch
W_COM1(0x09, 0x0000); // set non-display area refresh cycle ISC[3:0]
W_COM1(0x0A, 0x0000); // FMARK function
W_COM1(0x0C, 0x0000); // RGB interface setting
W_COM1(0x0D, 0x0000); // Frame marker Position
W_COM1(0x0F, 0x0000); // RGB interface polarity
//--------------Power On sequence ---------//
W_COM1(0x10, 0x0000); // SAP, BT[3:0], AP, DSTB, SLP, STB
W_COM1(0x11, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0]
W_COM1(0x12, 0x0000); // VREG1OUT voltage
W_COM1(0x13, 0x0000); // VDV[4:0] for VCOM amplitude
_delay_(2);//Delay(2); // Dis-charge capacitor power voltage
W_COM1(0x10, 0x1290); // SAP, BT[3:0], AP, DSTB, SLP, STB
W_COM1(0x11, 0x0227); // DC1[2:0], DC0[2:0], VC[2:0]
_delay_(1);//Delay(1); // Delay 50ms
W_COM1(0x12, 0x001A); // Internal reference voltage= Vci;
Delay(1); // Delay 50ms
W_COM1(0x13, 0x1800); // Set VDV[4:0] for VCOM amplitude
W_COM1(0x29, 0x0028); // Set VCM[5:0] for VCOMH
W_COM1(0x2B, 0x000C); // Set Frame Rate
_delay_(1);//Delay(1); // Delay 50ms
// ----------- Adjust the Gamma Curve ----------//
W_COM1(0x30, 0x0000);
W_COM1(0x31, 0x0305);
W_COM1(0x32, 0x0003);
W_COM1(0x35, 0x0304);
W_COM1(0x36, 0x000F);
W_COM1(0x37, 0x0407);
W_COM1(0x38, 0x0204);
W_COM1(0x39, 0x0707);
W_COM1(0x3C, 0x0403);
W_COM1(0x3D, 0x1604);
//------------------ Set GRAM area ---------------//
W_COM1(0x50, 0x0000); // Horizontal GRAM Start Address
W_COM1(0x51, 0x00EF); // Horizontal GRAM End Address
W_COM1(0x52, 0x0000); // Vertical GRAM Start Address
W_COM1(0x53, 0x013F); // Vertical GRAM Start Address
W_COM1(0x20, 0x0000); // GRAM horizontal Address
W_COM1(0x21, 0x0000); // GRAM Vertical Address
W_COM1(0x60, 0x2700); // Gate Scan Line
W_COM1(0x61, 0x0001); // NDL,VLE, REV
W_COM1(0x6A, 0x0000); // set scrolling line
//-------------- Partial Display Control ---------//
W_COM1(0x80, 0x0000);
W_COM1(0x81, 0x0000);
W_COM1(0x82, 0x0000);
W_COM1(0x83, 0x0000);
W_COM1(0x84, 0x0000);
W_COM1(0x85, 0x0000);
//-------------- Panel Control -------------------//
W_COM1(0x90, 0x0010);
W_COM1(0x92, 0x0600);
W_COM1(0x07, 0x0133); // 262K color and display ON
_delay_(1);//Delay(1); // Delay 50ms
}
}
void W_COM1(u8 cmd,u16 sdata) //根据厂家给的原代码改了一下,不知道这是什么意思,稀里糊涂
{
u16 temp;
temp=sdata;
GPIO_ResetBits(GPIOF, GPIO_Pin_0);//LCD_RS(0);//下指令
GPIO_ResetBits(GPIOG, GPIO_Pin_12);//LCD_CS(0);
*(__IO uint16_t *) (Bank1_LCD_C)= cmd;//DATAOUT(cmd);
GPIO_ResetBits(GPIOD, GPIO_Pin_5);//LCD_WR(0);
GPIO_SetBits(GPIOD, GPIO_Pin_5);//LCD_WR(1);
GPIO_SetBits(GPIOF, GPIO_Pin_0);//LCD_RS(1);//下參數
*(__IO uint16_t *) (Bank1_LCD_D)= sdata; //#define Bank1_LCD_D ((uint32_t)0x6c000002) //#define Bank1_LCD_C ((uint32_t)0x6c000000)
//DATAOUT(sdata);
//DATAOUT(temp);
//DATAOUT(temp);
GPIO_ResetBits(GPIOD, GPIO_Pin_5);//LCD_WR(0);
GPIO_ResetBits(GPIOD, GPIO_Pin_5);//LCD_WR(0);
GPIO_ResetBits(GPIOD, GPIO_Pin_5);//LCD_WR(0);
GPIO_ResetBits(GPIOD, GPIO_Pin_5);//LCD_WR(0);
GPIO_ResetBits(GPIOD, GPIO_Pin_5);//LCD_WR(0);
GPIO_ResetBits(GPIOD, GPIO_Pin_5);//LCD_WR(0);
GPIO_SetBits(GPIOD, GPIO_Pin_5);//LCD_WR(1);
GPIO_SetBits(GPIOD, GPIO_Pin_5);//LCD_WR(1);
GPIO_SetBits(GPIOD, GPIO_Pin_5);//LCD_WR(1);
GPIO_SetBits(GPIOD, GPIO_Pin_5);//LCD_WR(1);
GPIO_SetBits(GPIOF, GPIO_Pin_0);//LCD_CS(1);
}
还有那个什么RS=0,=1的问题
我将LCD 映射到Bank1的NE4上
#define Bank1_LCD_D ((uint32_t)0x6c000002)
#define Bank1_LCD_C ((uint32_t)0x6c000000)
接下来就是W_COM1();这个函数的具体实现了
这个是真心不懂,到现在都不知道LCD有没有初始化好,更不要说显示图片,字符之类的了
求高手指点,万分感激,请吃饭、K歌……,QQ70499619,电话15502121063 |