金牌会员
- 积分
- 1596
- 金钱
- 1596
- 注册时间
- 2012-12-30
- 在线时间
- 240 小时
|
发表于 2014-5-29 07:37:57
|
显示全部楼层
//写寄存器函数
//regval:寄存器值
void LCD_WR_REG(u16 regval)
{
LCD->LCD_REG=regval;//写入要写的寄存器序号
}
//写LCD数据
//data:要写入的值
void LCD_WR_DATA(u16 data)
{
LCD->LCD_RAM=data;
}
//读LCD数据
//返回值:读到的值
u16 LCD_RD_DATA(void)
{
return LCD->LCD_RAM;
}
//写寄存器
//LCD_Reg:寄存器地址
//LCD_RegValue:要写入的数据
void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue)
{
LCD->LCD_REG = LCD_Reg; //写入要写的寄存器序号
LCD->LCD_RAM = LCD_RegValue;//写入数据
}
//读寄存器
//LCD_Reg:寄存器地址
//返回值:读到的数据
u16 LCD_ReadReg(u8 LCD_Reg)
{
LCD_WR_REG(LCD_Reg); //写入要读的寄存器序号
delay_us(5);
return LCD_RD_DATA(); //返回读到的值
}
//开始写GRAM
void LCD_WriteRAM_Prepare(void)
{
LCD->LCD_REG=lcddev.wramcmd;
}
//LCD写GRAM
//RGB_Code:颜色值
void LCD_WriteRAM(u16 RGB_Code)
{
LCD->LCD_RAM = RGB_Code;//写十六位GRAM
}
//从ILI93xx读出的数据为GBR格式,而我们写入的时候为RGB格式。
//通过该函数转换
//c:GBR格式的颜色值
//返回值:RGB格式的颜色值
u16 LCD_BGR2RGB(u16 c)
{
u16 r,g,b,rgb;
b=(c>>0)&0x1f;
g=(c>>5)&0x3f;
r=(c>>11)&0x1f;
rgb=(b<<11)+(g<<5)+(r<<0);
return(rgb);
}
//LCD开启显示
void LCD_DisplayOn(void)
{
if(lcddev.id==0X6804)LCD_WR_REG(0X29); //开启显示
GPIO_SetBits(LCD_LED_GPIO, LCD_LED_Pin);
}
//LCD关闭显示
void LCD_DisplayOff(void)
{
if(lcddev.id==0X6804)
LCD_WR_REG(0X28); //关闭显示
GPIO_ResetBits(LCD_LED_GPIO, LCD_LED_Pin);
}
//设置光标位置
//Xpos:横坐标
//Ypos:纵坐标
__inline void LCD_SetCursor(u16 Xpos, u16 Ypos)
{
if(lcddev.id==0X6804){
LCD_WR_REG(0x2a);
LCD_WR_DATA((Xpos >> 8) & 0x00ff);
LCD_WR_DATA(Xpos & 0X00FF);
LCD_WR_REG(0x2b);
LCD_WR_DATA((Ypos >> 8) & 0x00ff);
LCD_WR_DATA(Ypos & 0X00FF);
LCD_WR_REG(0x3A); // 16/18 bits
LCD_WR_REG(0x55);
LCD_WR_REG(0x2c);
}
}
//画点
//x,y:坐标
//POINT_COLOR:此点的颜色
void LCD_DrawPoint(u16 x,u16 y,u16 color)
{
LCD_SetCursor(x,y); //设置光标位置 反转横屏
LCD_WriteRAM_Prepare(); //开始写入GRAM
LCD_WR_DATA(color);
}
void LCD_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
FSMC_NORSRAMTimingInitTypeDef readWriteTiming;
FSMC_NORSRAMTimingInitTypeDef writeTiming;
/* Enable GPIOs clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOG, ENABLE);
/* Enable FSMC clock */
RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
/*
PD14 <-> FSMC_D0 | D15 <-> FSMC_D1 | D0 <-> FSMC_D2
PD1 <-> FSMC_D3 | PE7 <-> FSMC_D4 | E8 <-> FSMC_D5
PE9 <-> FSMC_D6 | E10 <-> FSMC_D7 | E11 <-> FSMC_D8
PE12 <-> FSMC_D9 | E13 <-> FSMC_D10 | E14 <-> FSMC_D11
PE15 <-> FSMC_D12 | D8 <-> FSMC_D13 | D9 <-> FSMC_D14
PD10 <-> FSMC_D15 | D4 <-> FSMC_NOE | PD5 <-> FSMC_NWE
PD11 <-> FSMC_A16 | PG12 <-> FSMC_NE4 |
*/
/* GPIOD configuration */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, 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_PinSource11, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FSMC);
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_11 |
GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* GPIOE configuration */
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_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);
/* GPIOG configuration */
GPIO_PinAFConfig(GPIOG, GPIO_PinSource12 , GPIO_AF_FSMC);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOG, &GPIO_InitStructure);
//LCD_BL
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOG, &GPIO_InitStructure);
readWriteTiming.FSMC_AddressSetupTime = 0x01; //地址建立时间(ADDSET)为2个HCLK 1/36M=27ns
readWriteTiming.FSMC_AddressHoldTime = 0x00; //地址保持时间(ADDHLD)模式A未用到
readWriteTiming.FSMC_DataSetupTime = 0x0f; // 数据保存时间为16个HCLK,因为液晶驱动IC的读数据的时候,速度不能太快,尤其对1289这个IC。
readWriteTiming.FSMC_BusTurnAroundDuration = 0x00;
readWriteTiming.FSMC_CLKDivision = 0x00;
readWriteTiming.FSMC_DataLatency = 0x00;
readWriteTiming.FSMC_AccessMode = FSMC_AccessMode_A; //模式A
writeTiming.FSMC_AddressSetupTime = 0x00; //地址建立时间(ADDSET)为1个HCLK
writeTiming.FSMC_AddressHoldTime = 0x00; //地址保持时间(A
writeTiming.FSMC_DataSetupTime = 0x03; ////数据保存时间为4个HCLK
writeTiming.FSMC_BusTurnAroundDuration = 0x00;
writeTiming.FSMC_CLKDivision = 0x00;
writeTiming.FSMC_DataLatency = 0x00;
writeTiming.FSMC_AccessMode = FSMC_AccessMode_A; //模式A
/*-- FSMC Configuration ------------------------------------------------------*/
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 = &readWriteTiming; //读写时序
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &writeTiming; //写时序
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); //初始化FSMC配置
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE); // 使能BANK1
delay_ms(50); // delay 50 ms
LCD_WriteReg(0x0000,0x0001);
delay_ms(50); // delay 50 ms
lcddev.id = LCD_ReadReg(0x0000);
LCD_WR_REG(0XBF);
LCD_RD_DATA(); //dummy read
LCD_RD_DATA(); //读回0X01
LCD_RD_DATA(); //读回0XD0
lcddev.id=LCD_RD_DATA();//这里读回0X68
lcddev.id<<=8;
lcddev.id |= LCD_RD_DATA();//这里读回0X04
printf(" LCD ID:%x\r\n",lcddev.id); //打印LCD ID
if(lcddev.id==0x6804){ //6804初始化
delay_ms(50); // delay 50 ms
LCD_WR_REG(0xFF);
LCD_WR_REG(0xFF);
delay_ms(5);
LCD_WR_REG(0xFF);
LCD_WR_REG(0xFF);
LCD_WR_REG(0xFF);
LCD_WR_REG(0xFF);
delay_ms(20);
LCD_WR_REG(0X11);
delay_ms(20);
LCD_WR_REG(0XD0);//VCI1 VCL VGH VGL DDVDH VREG1OUT power amplitude setting
LCD_WR_DATA(0X07);
LCD_WR_DATA(0X41);
LCD_WR_DATA(0X1C);
LCD_WR_REG(0XD1);//VCOMH VCOM_AC amplitude setting
LCD_WR_DATA(0X00);
LCD_WR_DATA(0X1a);
LCD_WR_DATA(0X09);
LCD_WR_REG(0XD2);//Operational Amplifier Circuit Constant Current Adjust , charge pump frequency setting
LCD_WR_DATA(0X01);
LCD_WR_DATA(0X22);
LCD_WR_REG(0XC0);//REV SM GS
LCD_WR_DATA(0X10);
LCD_WR_DATA(0X3B);
LCD_WR_DATA(0X00);
LCD_WR_DATA(0X02);
LCD_WR_DATA(0X11);
LCD_WR_REG(0XC5);// Frame rate setting = 72HZ when setting 0x03
LCD_WR_DATA(0X03);
LCD_WR_REG(0XC8);//Gamma setting
LCD_WR_DATA(0X00);
LCD_WR_DATA(0X25);
LCD_WR_DATA(0X21);
LCD_WR_DATA(0X05);
LCD_WR_DATA(0X00);
LCD_WR_DATA(0X0a);
LCD_WR_DATA(0X65);
LCD_WR_DATA(0X25);
LCD_WR_DATA(0X77);
LCD_WR_DATA(0X50);
LCD_WR_DATA(0X0f);
LCD_WR_DATA(0X00);
LCD_WR_REG(0XF8);
LCD_WR_DATA(0X01);
LCD_WR_REG(0XFE);
LCD_WR_DATA(0X00);
LCD_WR_DATA(0X02);
LCD_WR_REG(0X20);//Exit invert mode
LCD_WR_REG(0X3A);
LCD_WR_DATA(0X05);//16位模式
LCD_WR_REG(0x36);
LCD_WR_DATA(0x28);
LCD_WR_REG(0x2A);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x01);
LCD_WR_DATA(0xDF);//X:480
LCD_WR_REG(0x2B);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x01);
LCD_WR_DATA(0x3F);//Y:320
delay_ms(120);
LCD_WR_REG(0X29);
LCD_WR_REG(0X2C);
}else if(0x9486 == lcddev.id){
delay_ms(50); // delay 50 ms
LCD_WR_REG(0xFF);
LCD_WR_REG(0xFF);
delay_ms(5);
LCD_WR_REG(0xFF);
LCD_WR_REG(0xFF);
LCD_WR_REG(0xFF);
LCD_WR_REG(0xFF);
delay_ms(10);
LCD_WR_REG(0xF2);
LCD_WR_REG(0x18);
LCD_WR_DATA(0xA3);
LCD_WR_DATA(0x12);
LCD_WR_DATA(0x02);
LCD_WR_DATA(0xb2);
LCD_WR_DATA(0x12);
LCD_WR_DATA(0xFF);
LCD_WR_DATA(0x10);
LCD_WR_DATA(0x00);
LCD_WR_REG(0xF1);
LCD_WR_DATA(0x36);
LCD_WR_DATA(0x04);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x3c);
LCD_WR_DATA(0x0f);
LCD_WR_DATA(0x8f);
LCD_WR_REG(0xF8);
LCD_WR_DATA(0x21);
LCD_WR_DATA(0x04);
LCD_WR_REG(0xF9);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x08);
LCD_WR_REG(0x36);
LCD_WR_DATA(0x08);
LCD_WR_REG(0x20);
LCD_WR_DATA(0x00);
LCD_WR_REG(0x3A);
LCD_WR_DATA(0x55);
LCD_WR_REG(0xB4);
LCD_WR_DATA(0x01);
LCD_WR_REG(0xC0);
LCD_WR_DATA(0x0f);
LCD_WR_DATA(0x0f);
LCD_WR_REG(0xC1);
LCD_WR_DATA(0x43);
LCD_WR_REG(0xC2);
LCD_WR_DATA(0x22);
LCD_WR_REG(0xC5);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x44);
LCD_WR_REG(0xB1);
LCD_WR_DATA(0xc0);
LCD_WR_DATA(0x11);
LCD_WR_REG(0xb7);
LCD_WR_DATA(0xc6);
LCD_WR_REG(0xE0);
LCD_WR_DATA(0x0f);
LCD_WR_DATA(0x19);
LCD_WR_DATA(0x17);
LCD_WR_DATA(0x06);
LCD_WR_DATA(0x0b);
LCD_WR_DATA(0x05);
LCD_WR_DATA(0x49);
LCD_WR_DATA(0x67);
LCD_WR_DATA(0x3d);
LCD_WR_DATA(0x07);
LCD_WR_DATA(0x12);
LCD_WR_DATA(0x05);
LCD_WR_DATA(0x17);
LCD_WR_DATA(0x0a);
LCD_WR_DATA(0x00);
LCD_WR_REG(0xE1);
LCD_WR_DATA(0x0f);
LCD_WR_DATA(0x38);
LCD_WR_DATA(0x3a);
LCD_WR_DATA(0x08);
LCD_WR_DATA(0x09);
LCD_WR_DATA(0x02);
LCD_WR_DATA(0x4d);
LCD_WR_DATA(0x52);
LCD_WR_DATA(0x38);
LCD_WR_DATA(0x06);
LCD_WR_DATA(0x0d);
LCD_WR_DATA(0x01);
LCD_WR_DATA(0x24);
LCD_WR_DATA(0x20);
LCD_WR_DATA(0x00);
LCD_WR_REG(0xB6);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x42);
LCD_WR_DATA(0x3B);
LCD_WR_REG(0x11);
LCD_WR_DATA(0x00);
LCD_WR_REG(0x36);
LCD_WR_DATA(0x28);
LCD_WR_REG(0x2A);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x01);
LCD_WR_DATA(0xDF);//X:480
LCD_WR_REG(0x2B);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x01);
LCD_WR_DATA(0x3F);//Y:320
delay_ms(100);
LCD_WR_REG(0x29);
LCD_WR_DATA(0x00);
}
lcddev.wramcmd=0X2C;
lcddev.setxcmd=0X2A;
lcddev.setycmd=0X2B;
lcddev.width=480;
lcddev.height=320;
lcdpen.bcolor = BLACK;
GPIO_SetBits(LCD_LED_GPIO, LCD_LED_Pin);
LCD_Clear(BLACK);
}
//清屏函数
//color:要清屏的填充色
void LCD_Clear(u16 color)
{
u32 index=0;
u32 totalpoint=0;
totalpoint = lcddev.width*lcddev.height; //得到总点数
LCD_SetCursor(0x00,0x0000); //设置光标位置
LCD_WriteRAM_Prepare(); //开始写入GRAM
for(index=0;index<totalpoint;index++)
LCD_WR_DATA(color);
}
我在初始化已经把屏设为横屏了 可供你参考我用的407ZE FSMC接口> |
|