📄 lcd.c
字号:
/*********************************************************************************************************
*
* File : LCD.c
* Hardware Environment:
* Build Environment : RealView MDK-ARM Version: 4.20
* Version : V1.0
* By :
*
* (c) Copyright 2005-2011, WaveShare
* http://www.waveshare.net
* All Rights Reserved
*
*********************************************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "LCD.h"
#include "AsciiLib.h"
/* Private define ------------------------------------------------------------*/
#define LCD_REG (*((volatile unsigned short *) 0x6F000000)) /* RS = 0 */
#define LCD_RAM (*((volatile unsigned short *) 0x6F010000)) /* RS = 1 */
/* Private variables ---------------------------------------------------------*/
static uint8_t LCD_Code;
/* Private define ------------------------------------------------------------*/
#define ILI9320 0 /* 0x9320 */
#define ILI9325 1 /* 0x9325 */
#define ILI9328 2 /* 0x9328 */
#define ILI9331 3 /* 0x9331 */
#define SSD1298 4 /* 0x8999 */
#define SSD1289 5 /* 0x8989 */
#define ST7781 6 /* 0x7783 */
#define LGDP4531 7 /* 0x4531 */
#define SPFD5408B 8 /* 0x5408 */
#define R61505U 9 /* 0x1505 0x0505 */
#define HX8347D 10 /* 0x0047 */
#define HX8347A 11 /* 0x0047 */
#define LGDP4535 12 /* 0x4535 */
#define SSD2119 13 /* 3.5 LCD 0x9919 */
/*******************************************************************************
* Function Name : LCD_CtrlLinesConfig
* Description : Configures LCD Control lines (FSMC Pins) in alternate function
Push-Pull mode.
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void LCD_CtrlLinesConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable FSMC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF |
RCC_APB2Periph_GPIOG | RCC_APB2Periph_AFIO, ENABLE);
/* Set PD.00(D2), PD.01(D3), PD.04(NOE), PD.05(NWE), PD.08(D13), PD.09(D14),
PD.10(D15), PD.14(D0), PD.15(D1) as alternate function push pull */
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_14 |
GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Set PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10),
PE.14(D11), PE.15(D12) as alternate function push pull */
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);
/* Set PE.02(A23), PE.05(A21), PE.06(A22) as alternate function push pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_5 | GPIO_Pin_6;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Set PF.00(A0 (RS)) as alternate function push pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* Set PG.05(A15), PG.12(NE4), PG.13(A24) as alternate function push pull - CE3(LCD /CS) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13;
GPIO_Init(GPIOG, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : LCD_FSMCConfig
* Description : Configures the Parallel interface (FSMC) for LCD(Parallel mode)
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void LCD_FSMCConfig(void)
{
FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
FSMC_NORSRAMTimingInitTypeDef FSMC_NORSRAMTimingInitStructure;
/* FSMC Read/Write Timing */
FSMC_NORSRAMTimingInitStructure.FSMC_AddressSetupTime = 5; /* Address Setup Time */
FSMC_NORSRAMTimingInitStructure.FSMC_AddressHoldTime = 0;
FSMC_NORSRAMTimingInitStructure.FSMC_DataSetupTime = 5; /* Data Setup Time */
FSMC_NORSRAMTimingInitStructure.FSMC_BusTurnAroundDuration = 0x00;
FSMC_NORSRAMTimingInitStructure.FSMC_CLKDivision = 0x00;
FSMC_NORSRAMTimingInitStructure.FSMC_DataLatency = 0x00;
FSMC_NORSRAMTimingInitStructure.FSMC_AccessMode = FSMC_AccessMode_A; /* FSMC Access Mode */
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM4;
FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
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_AsynchronousWait = FSMC_AsynchronousWait_Disable;
FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &FSMC_NORSRAMTimingInitStructure;
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
/* FSMC Write Timing */
FSMC_NORSRAMTimingInitStructure.FSMC_AddressSetupTime = 15;//2;//1; /* Address Setup Time */
FSMC_NORSRAMTimingInitStructure.FSMC_AddressHoldTime = 0;
FSMC_NORSRAMTimingInitStructure.FSMC_DataSetupTime = 15;//2;//1; /* Data Setup Time */
FSMC_NORSRAMTimingInitStructure.FSMC_BusTurnAroundDuration = 0x00;
FSMC_NORSRAMTimingInitStructure.FSMC_CLKDivision = 0x00;
FSMC_NORSRAMTimingInitStructure.FSMC_DataLatency = 0x00;
FSMC_NORSRAMTimingInitStructure.FSMC_AccessMode = FSMC_AccessMode_A; /* FSMC Access Mode */
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &FSMC_NORSRAMTimingInitStructure;
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
/* Enable FSMC Bank4_SRAM Bank */
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE);
}
/*******************************************************************************
* Function Name : LCD_Configuration
* Description : Configure the LCD Control pins and FSMC Parallel interface
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void LCD_Configuration(void)
{
/* Configure the LCD Control pins --------------------------------------------*/
LCD_CtrlLinesConfig();
/* Configure the FSMC Parallel interface -------------------------------------*/
LCD_FSMCConfig();
}
/*******************************************************************************
* Function Name : delay_ms
* Description : Delay Time
* Input : - cnt: Delay Time
* Output : None
* Return : None
* Return : None
* Attention : None
*******************************************************************************/
void delay_ms(uint16_t ms)
{
uint16_t i,j;
for( i = 0; i < ms; i++ )
{
for( j = 0; j < 1141; j++ );
}
}
/*******************************************************************************
* Function Name : LCD_Initializtion
* Description : SSD1963 Resets
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
uint16_t DeviceCode;
uint16_t i;
void LCD_Initializtion(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/*Back Light Control (PB.00), LCD Reset (PB.01)*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_0); /* Enable LCD back light */
GPIO_ResetBits(GPIOB, GPIO_Pin_1); /* Enable LCD_RESET */
delay_ms(100);
GPIO_SetBits(GPIOB, GPIO_Pin_1); /* Disable LCD_RESET */
LCD_Configuration();
/* Set MN(multipliers) of PLL, VCO = crystal freq * (N+1) */
/* PLL freq = VCO/M with 250MHz < VCO < 800MHz */
/* The max PLL freq is around 120MHz. To obtain 120MHz as the PLL freq */
DeviceCode = 0x0123;
DeviceCode = LCD_ReadReg(0x0000); /* Read LCD ID */
if( DeviceCode == 0x9325 || DeviceCode == 0x9328 )
{
LCD_Code = ILI9325;
LCD_WriteReg(0x00e7,0x0010);
LCD_WriteReg(0x0000,0x0001); /* start internal osc */
LCD_WriteReg(0x0001,0x0100);
LCD_WriteReg(0x0002,0x0700); /* power on sequence */
LCD_WriteReg(0x0003,(1<<12)|(1<<5)|(1<<4)|(0<<3) ); /* importance */
LCD_WriteReg(0x0004,0x0000);
LCD_WriteReg(0x0008,0x0207);
LCD_WriteReg(0x0009,0x0000);
LCD_WriteReg(0x000a,0x0000); /* display setting */
LCD_WriteReg(0x000c,0x0001); /* display setting */
LCD_WriteReg(0x000d,0x0000);
LCD_WriteReg(0x000f,0x0000);
/* Power On sequence */
LCD_WriteReg(0x0010,0x0000);
LCD_WriteReg(0x0011,0x0007);
LCD_WriteReg(0x0012,0x0000);
LCD_WriteReg(0x0013,0x0000);
delay_ms(50); /* delay 50 ms */
LCD_WriteReg(0x0010,0x1590);
LCD_WriteReg(0x0011,0x0227);
delay_ms(50); /* delay 50 ms */
LCD_WriteReg(0x0012,0x009c);
delay_ms(50); /* delay 50 ms */
LCD_WriteReg(0x0013,0x1900);
LCD_WriteReg(0x0029,0x0023);
LCD_WriteReg(0x002b,0x000e);
delay_ms(50); /* delay 50 ms */
LCD_WriteReg(0x0020,0x0000);
LCD_WriteReg(0x0021,0x0000);
delay_ms(50); /* delay 50 ms */
LCD_WriteReg(0x0030,0x0007);
LCD_WriteReg(0x0031,0x0707);
LCD_WriteReg(0x0032,0x0006);
LCD_WriteReg(0x0035,0x0704);
LCD_WriteReg(0x0036,0x1f04);
LCD_WriteReg(0x0037,0x0004);
LCD_WriteReg(0x0038,0x0000);
LCD_WriteReg(0x0039,0x0706);
LCD_WriteReg(0x003c,0x0701);
LCD_WriteReg(0x003d,0x000f);
delay_ms(50); /* delay 50 ms */
LCD_WriteReg(0x0050,0x0000);
LCD_WriteReg(0x0051,0x00ef);
LCD_WriteReg(0x0052,0x0000);
LCD_WriteReg(0x0053,0x013f);
LCD_WriteReg(0x0060,0xa700);
LCD_WriteReg(0x0061,0x0001);
LCD_WriteReg(0x006a,0x0000);
LCD_WriteReg(0x0080,0x0000);
LCD_WriteReg(0x0081,0x0000);
LCD_WriteReg(0x0082,0x0000);
LCD_WriteReg(0x0083,0x0000);
LCD_WriteReg(0x0084,0x0000);
LCD_WriteReg(0x0085,0x0000);
LCD_WriteReg(0x0090,0x0010);
LCD_WriteReg(0x0092,0x0000);
LCD_WriteReg(0x0093,0x0003);
LCD_WriteReg(0x0095,0x0110);
LCD_WriteReg(0x0097,0x0000);
LCD_WriteReg(0x0098,0x0000);
/* display on sequence */
LCD_WriteReg(0x0007,0x0133);
LCD_WriteReg(0x0020,0x0000);
LCD_WriteReg(0x0021,0x0000);
}
else if( DeviceCode == 0x9320 || DeviceCode == 0x9300 )
{
LCD_Code = ILI9320;
LCD_WriteReg(0x00,0x0000);
LCD_WriteReg(0x01,0x0100); /* Driver Output Contral */
LCD_WriteReg(0x02,0x0700); /* LCD Driver Waveform Contral */
LCD_WriteReg(0x03,0x1018); /* Entry Mode Set */
LCD_WriteReg(0x04,0x0000); /* Scalling Contral */
LCD_WriteReg(0x08,0x0202); /* Display Contral */
LCD_WriteReg(0x09,0x0000); /* Display Contral 3.(0x0000) */
LCD_WriteReg(0x0a,0x0000); /* Frame Cycle Contal.(0x0000) */
LCD_WriteReg(0x0c,(1<<0)); /* Extern Display Interface Contral */
LCD_WriteReg(0x0d,0x0000); /* Frame Maker Position */
LCD_WriteReg(0x0f,0x0000); /* Extern Display Interface Contral 2. */
delay_ms(100); /* delay 100 ms */
LCD_WriteReg(0x07,0x0101); /* Display Contral */
delay_ms(100); /* delay 100 ms */
LCD_WriteReg(0x10,(1<<12)|(0<<8)|(1<<7)|(1<<6)|(0<<4)); /* Power Control 1.(0x16b0) */
LCD_WriteReg(0x11,0x0007); /* Power Control 2 */
LCD_WriteReg(0x12,(1<<8)|(1<<4)|(0<<0)); /* Power Control 3.(0x0138) */
LCD_WriteReg(0x13,0x0b00); /* Power Control 4 */
LCD_WriteReg(0x29,0x0000); /* Power Control 7 */
LCD_WriteReg(0x2b,(1<<14)|(1<<4));
LCD_WriteReg(0x50,0); /* Set X Start */
LCD_WriteReg(0x51,239); /* Set X End */
LCD_WriteReg(0x52,0); /* Set Y Start */
LCD_WriteReg(0x53,319); /* Set Y End */
LCD_WriteReg(0x60,0x2700); /* Driver Output Control */
LCD_WriteReg(0x61,0x0001); /* Driver Output Control */
LCD_WriteReg(0x6a,0x0000); /* Vertical Srcoll Control */
LCD_WriteReg(0x80,0x0000); /* Display Position? Partial Display 1 */
LCD_WriteReg(0x81,0x0000); /* RAM Address Start? Partial Display 1 */
LCD_WriteReg(0x82,0x0000); /* RAM Address End-Partial Display 1 */
LCD_WriteReg(0x83,0x0000); /* Displsy Position? Partial Display 2 */
LCD_WriteReg(0x84,0x0000); /* RAM Address Start? Partial Display 2 */
LCD_WriteReg(0x85,0x0000); /* RAM Address End? Partial Display 2 */
LCD_WriteReg(0x90,(0<<7)|(16<<0)); /* Frame Cycle Contral.(0x0013) */
LCD_WriteReg(0x92,0x0000); /* Panel Interface Contral 2.(0x0000) */
LCD_WriteReg(0x93,0x0001); /* Panel Interface Contral 3. */
LCD_WriteReg(0x95,0x0110); /* Frame Cycle Contral.(0x0110) */
LCD_WriteReg(0x97,(0<<8));
LCD_WriteReg(0x98,0x0000); /* Frame Cycle Contral */
LCD_WriteReg(0x07,0x0173);
}
else if( DeviceCode == 0x9331 )
{
LCD_Code = ILI9331;
LCD_WriteReg(0x00E7, 0x1014);
LCD_WriteReg(0x0001, 0x0100); /* set SS and SM bit */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -