STM32 控制lcm液晶ILI9341驱动的液晶驱动程序

/* 选择BANK1-BORSRAM1 连接 TFT,地址范围为0X60000000~0X63FFFFFF
* FSMC_A16 接LCD的DC(寄存器/数据选择)脚
* 16 bit => FSMC[24:0]对应HADDR[25:1]
* 寄存器基地址 = 0X60000000
* RAM基地址 = 0X60020000 = 0X60000000+2^16*2 = 0X60000000 + 0X20000 = 0X60020000
* 当选择不同的地址线时,地址要重新计算。
*/
//#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寄存器设置为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写入数据*/
//#define LCD_WR_Data(val)  ((*(__IO u16 *) (Bank1_LCD_D)) = ((u16)(val)))   

void ILI9341_LCD_Init(void);
/**********FSMC**********/
/*******************************************
* 函数名:LCD_GPIO_Config
* 描述  :根据FSMC配置LCD的I/O
* 输入  : 无 
* 输出  :无
* 举例  :无
* 注意  :无
*********************************************/  

void LCD_GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    /* 使能FSMC时钟*/
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
    
    /* 使能FSMC对应相应管脚时钟*/
    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复位控制管脚*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ;      
    GPIO_Init(GPIOD, &GPIO_InitStructure);             
    
    /* 配置FSMC相对应的数据线,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相对应的控制线
     * 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); 
}

/*******************************************
* 函数名:LCD_FSMC_Config
* 描述  :LCD  FSMC 模式配置
* 输入  : 无 
* 输出  :无
* 举例  :无
* 注意  :无
*********************************************/ 
void LCD_FSMC_Config(void)
{
    FSMC_NORSRAMInitTypeDef  FSMC_NORSRAMInitStructure;
    FSMC_NORSRAMTimingInitTypeDef  p; 
    
    
    p.FSMC_AddressSetupTime = 0x02;     //地址建立时间
    p.FSMC_AddressHoldTime = 0x00;     //地址保持时间
    p.FSMC_DataSetupTime = 0x05;         //数据建立时间
    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);  
}

/**********************************
* 函数名:LCD_Rst
* 描述  :LCD 软件复位
* 输入  : 无 
* 输出  :无
* 举例  :无
* 注意  :无
*************************************/
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);
}
/**********************************
* 函数名: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); 

}

/*****************************************
* 函数名: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**********/
展开阅读全文

没有更多推荐了,返回首页