📄 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_Configuration
* Description : Configure the LCD Control pins and FSMC Parallel interface
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void LCD_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOC and GPIOE clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | 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.11(RST), PD.12(WR), PD.13(RD) , PD.14(CS), PD.15(RS)*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | 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);
/* PA.00(BL_PWM)*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : LCD_WriteReg
* Description :
* 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 :
* 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;
/*
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_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOE, &GPIO_InitStructure);
*/
GPIOE->CRH = 0x44444444;
GPIOE->CRL = 0x44444444;
/*
value = GPIO_ReadInputData(GPIOE);
value = GPIO_ReadInputData(GPIOE);
*/
value = GPIOE->IDR;
value = GPIOE->IDR;
/*
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);
*/
GPIOE->CRH = 0x33333333;
GPIOE->CRL = 0x33333333;
Set_nRd;
return value;
}
/*******************************************************************************
* Function Name : LCD_WriteReg
* Description : Writes to the selected LCD register.
* Input : - LCD_Reg: address of the selected register.
* - LCD_RegValue: value to write to the selected register.
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
__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;
}
/*******************************************************************************
* Function Name : LCD_WriteReg
* Description : Reads the selected LCD Register.
* Input : None
* Output : None
* Return : LCD Register Value.
* Attention : None
*******************************************************************************/
__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
switch( LCD_Code )
{
default: /* 0x9320 0x9325 0x9328 0x9331 0x5408 0x1505 0x0505 0x7783 0x4531 0x4535 */
LCD_WriteReg(0x0020, Xpos );
LCD_WriteReg(0x0021, Ypos );
break;
case SSD1298: /* 0x8999 */
case SSD1289: /* 0x8989 */
LCD_WriteReg(0x004e, Xpos );
LCD_WriteReg(0x004f, Ypos );
break;
case HX8347A: /* 0x0047 */
case HX8347D: /* 0x0047 */
LCD_WriteReg(0x02, Xpos>>8 );
LCD_WriteReg(0x03, Xpos );
LCD_WriteReg(0x06, Ypos>>8 );
LCD_WriteReg(0x07, Ypos );
break;
case SSD2119: /* 3.5 LCD 0x9919 */
break;
}
}
/*******************************************************************************
* 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
*******************************************************************************/
void LCD_Initializtion(void)
{
uint16_t DeviceCode;
LCD_Configuration();
GPIO_ResetBits(GPIOD, GPIO_Pin_11); /* LCD_RESET */
delay_ms(100);
GPIO_SetBits(GPIOD, GPIO_Pin_11);
GPIO_SetBits(GPIOA, GPIO_Pin_0); /* Enable back light */
/* 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 = 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -