📄 lcd.c
字号:
LCD_nCS_StartByte(START_BYTE | READ_REG);
for(i = 0; i < 5; i++)
{
SPI_SendData(SPI2, 0xFF);
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_BSY) != RESET)
{
}
/* One byte of invalid dummy data read after the start byte */
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET)
{
}
SPI_ReceiveData(SPI2);
}
SPI_SendData(SPI2, 0xFF);
/* Read upper byte */
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_BSY) != RESET)
{
}
/* Read lower byte */
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET)
{
}
tmp = SPI_ReceiveData(SPI2);
SPI_SendData(SPI2, 0xFF);
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_BSY) != RESET)
{
}
/* Read lower byte */
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET)
{
}
tmp = ((tmp & 0xFF) << 8) | SPI_ReceiveData(SPI2);
LCD_CtrlLinesWrite(GPIOB, CtrlPin_NCS, Bit_SET);
/* SPI2 prescaler: 2 */
SPI2->CR1 &= 0xFFC7;
return tmp;
}
/*******************************************************************************
* Function Name : LCD_WriteRAM_Prepare
* Description : Prepare to write to the LCD RAM.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void LCD_WriteRAM_Prepare(void)
{
LCD_WriteRegIndex(R34); /* Select GRAM Reg */
/* Reset LCD control line(/CS) and Send Start-Byte */
LCD_nCS_StartByte(START_BYTE | WRITE_REG);
}
/*******************************************************************************
* Function Name : LCD_WriteRAMWord
* Description : Writes 1 word to the LCD RAM.
* Input : - RGB_Code: the pixel color in RGB mode (5-6-5).
* Output : None
* Return : None
*******************************************************************************/
void LCD_WriteRAMWord(u16 RGB_Code)
{
LCD_WriteRAM_Prepare();
LCD_WriteRAM(RGB_Code);
LCD_CtrlLinesWrite(GPIOB, CtrlPin_NCS, Bit_SET);
}
/*******************************************************************************
* Function Name : LCD_WriteRegHX8312
* Description : Writes to the selected LCD HX8312 register.
* Input : - LCD_Reg: address of the selected register.
* - LCD_RegValue: value to write to the selected register.
* Output : None
* Return : None
*******************************************************************************/
static void LCD_WriteRegHX8312(u8 LCD_Reg, u8 LCD_RegValue)
{
u16 tmp = 0;
LCD_CtrlLinesWrite(GPIOD, CtrlPin_NWR, Bit_RESET);
LCD_CtrlLinesWrite(GPIOD, CtrlPin_RS, Bit_RESET);
LCD_CtrlLinesWrite(GPIOB, CtrlPin_NCS, Bit_RESET);
tmp = LCD_Reg << 8;
tmp |= LCD_RegValue;
SPI_SendData(SPI2, tmp);
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_BSY) != RESET)
{
}
LCD_CtrlLinesWrite(GPIOB, CtrlPin_NCS, Bit_SET);
}
/*******************************************************************************
* 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
*******************************************************************************/
void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue)
{
if(LCDType == LCD_ILI9320)
{
LCD_WriteRegILI9320(LCD_Reg, LCD_RegValue);
}
else if(LCDType == LCD_HX8312)
{
LCD_WriteRegHX8312(LCD_Reg, ((u8)LCD_RegValue));
}
}
/*******************************************************************************
* Function Name : LCD_WriteRAM
* Description : Writes to the LCD RAM.
* Input : - RGB_Code: the pixel color in RGB mode (5-6-5).
* Output : None
* Return : None
*******************************************************************************/
void LCD_WriteRAM(u16 RGB_Code)
{
if(LCDType == LCD_ILI9320)
{
SPI_SendData(SPI2, RGB_Code >> 8);
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_BSY) != RESET)
{
}
SPI_SendData(SPI2, RGB_Code & 0xFF);
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_BSY) != RESET)
{
}
}
if(LCDType == LCD_HX8312)
{
LCD_CtrlLinesWrite(GPIOD, CtrlPin_NWR, Bit_RESET);
LCD_CtrlLinesWrite(GPIOD, CtrlPin_RS, Bit_SET);
LCD_CtrlLinesWrite(GPIOB, CtrlPin_NCS, Bit_RESET);
SPI_SendData(SPI2, RGB_Code);
while(SPI_GetFlagStatus(SPI2, SPI_FLAG_BSY) != RESET)
{
}
LCD_CtrlLinesWrite(GPIOB, CtrlPin_NCS, Bit_SET);
}
}
/*******************************************************************************
* Function Name : LCD_PowerOn
* Description : Power on the LCD.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void LCD_PowerOn(void)
{
if(LCDType == LCD_ILI9320)
{
/* Power On sequence ---------------------------------------------------------*/
LCD_WriteReg(R16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
LCD_WriteReg(R17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
LCD_WriteReg(R18, 0x0000); /* VREG1OUT voltage */
LCD_WriteReg(R19, 0x0000); /* VDV[4:0] for VCOM amplitude */
Delay(20); /* Dis-charge capacitor power voltage (200ms) */
LCD_WriteReg(R16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
LCD_WriteReg(R17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
Delay(5); /* Delay 50 ms */
LCD_WriteReg(R18, 0x0139); /* VREG1OUT voltage */
Delay(5); /* delay 50 ms */
LCD_WriteReg(R19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
LCD_WriteReg(R41, 0x0013); /* VCM[4:0] for VCOMH */
Delay(5); /* delay 50 ms */
LCD_WriteReg(R7, 0x0173); /* 262K color and display ON */
}
else if(LCDType == LCD_HX8312)
{
/* Power On Set */
LCD_WriteReg(R28, 0x73);
LCD_WriteReg(R36, 0x74);
LCD_WriteReg(R30, 0x01);
LCD_WriteReg(R24, 0xC1);
Delay(1); /* Delay 10 ms */
LCD_WriteReg(R24, 0xE1);
LCD_WriteReg(R24, 0xF1);
Delay(6); /* Delay 60 ms */
LCD_WriteReg(R24, 0xF5);
Delay(6); /* Delay 60 ms */
LCD_WriteReg(R27, 0x09);
Delay(1); /* Delay 10 ms */
LCD_WriteReg(R31, 0x11);
LCD_WriteReg(R32, 0x0E);
LCD_WriteReg(R30, 0x81);
Delay(1); /* Delay 10 ms */
}
}
/*******************************************************************************
* Function Name : LCD_DisplayOn
* Description : Enables the Display.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void LCD_DisplayOn(void)
{
if(LCDType == LCD_ILI9320)
{
/* Display On */
LCD_WriteReg(R7, 0x0173); /* 262K color and display ON */
}
else if(LCDType == LCD_HX8312)
{
LCD_WriteReg(R1, 0x50);
LCD_WriteReg(R5, 0x04);
/* Display On */
LCD_WriteReg(R0, 0x80);
LCD_WriteReg(R59, 0x01);
Delay(4); /* Delay 40 ms */
LCD_WriteReg(R0, 0x20);
}
}
/*******************************************************************************
* Function Name : LCD_DisplayOff
* Description : Disables the Display.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void LCD_DisplayOff(void)
{
if(LCDType == LCD_ILI9320)
{
/* Display Off */
LCD_WriteReg(R7, 0x0);
}
else if(LCDType == LCD_HX8312)
{
/* Display Off */
LCD_WriteReg(R0, 0xA0);
Delay(4); /* Delay 40 ms */
LCD_WriteReg(R59, 0x00);
}
}
/*******************************************************************************
* Function Name : LCD_CtrlLinesConfig
* Description : Configures LCD control lines in Output Push-Pull mode.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void LCD_CtrlLinesConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure NCS (PB.02) in Output Push-Pull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure NWR(RNW), RS (PD.15, PD.07) in Output Push-Pull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_15;
GPIO_Init(GPIOD, &GPIO_InitStructure);
LCD_CtrlLinesWrite(GPIOD, CtrlPin_NWR, Bit_SET);
LCD_CtrlLinesWrite(GPIOD, CtrlPin_RS, Bit_SET);
}
/*******************************************************************************
* Function Name : LCD_CtrlLinesWrite
* Description : Sets or reset LCD control lines.
* Input : - GPIOx: where x can be B or D to select the GPIO peripheral.
* - CtrlPins: the Control line. This parameter can be:
* - CtrlPin_NCS: Chip Select pin (PB.02)
* - CtrlPin_NWR: Read/Write Selection pin (PD.15)
* - CtrlPin_RS: Register/RAM Selection pin (PD.07)
* - BitVal: specifies the value to be written to the selected bit.
* This parameter can be:
* - Bit_RESET: to clear the port pin
* - Bit_SET: to set the port pin
* Output : None
* Return : None
*******************************************************************************/
void LCD_CtrlLinesWrite(GPIO_TypeDef* GPIOx, u16 CtrlPins, BitAction BitVal)
{
/* Set or Reset the control line */
GPIO_WriteBit(GPIOx, CtrlPins, BitVal);
}
/*******************************************************************************
* Function Name : LCD_SPIConfig
* Description : Configures the SPI2 interface.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void LCD_SPIConfig(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOB clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Enable SPI2 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/* Configure SPI2 pins: NSS, SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = 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_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI_DeInit(SPI2);
/* SPI2 Config */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
if(LCDType == LCD_ILI9320)
{
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
}
else if(LCDType == LCD_HX8312)
{
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
}
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI2, &SPI_InitStructure);
/* SPI2 enable */
SPI_Cmd(SPI2, ENABLE);
}
/*******************************************************************************
* Function Name : LCD_DisplayString
* Description : Displays a maximum of 200 char on the LCD.
* Input : - Line: the starting Line where to display the character shape.
* This parameter can be one of the following values:
* - Linex: where x can be 0..9
* - *ptr: pointer to string to display on LCD.
* Output : None
* Return : None
*******************************************************************************/
void LCD_DisplayString(u8 Line, u8 *ptr)
{
u32 i = 0, column = 0, index = 0, spaceindex = 0;
u16 refcolumn = 319;
u32 length = 0;
/* Get the string length */
length = StrLength(ptr);
if(length > 200)
{
/* Set the Cursor position */
LCD_SetCursor(Line, 0x013F);
/* Clear the Selected Line */
LCD_ClearLine(Line);
LCD_DisplayStringLine(Line, " String too long ");
}
else
{
/* Set the Cursor position */
LCD_SetCursor(Line, 0x013F);
/* Clear the Selected Line */
LCD_ClearLine(Line);
while(length--)
{
if(index == 20)
{
if(*ptr == 0x20)
{
ptr++;
}
else
{
for(i = 0; i < spaceindex; i++)
{
LCD_DisplayChar(Line, column, ' ');
column -= 16;
}
ptr -= (spaceindex - 1);
length += (spaceindex - 1);
}
Line += 24;
/* Clear the Selected Line */
LCD_ClearLine(Line);
refcolumn = 319;
index = 0;
}
/* Display one character on LCD */
LCD_DisplayChar(Line, refcolumn, *ptr);
/* Increment character number in one line */
index++;
/* Decrement the column position by 16 */
refcolumn -= 16;
/* Point on the next character */
ptr++;
/* Increment the number of character after the last space */
spaceindex++;
if(*ptr == 0x20)
{
spaceindex = 0;
column = refcolumn - 16;
}
}
}
}
/*******************************************************************************
* Function Name : StrLength
* Description : Returns length of string.
* Input : - Str: Character Pointer.
* Output : None
* Return : String length.
*******************************************************************************/
static u32 StrLength(u8 *Str)
{
u32 Index = 0;
/* Increment the Index unless the end of string */
for(Index = 0; *Str != '\0'; Str++, Index++)
{
}
return Index;
}
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -