/* 程序说明: CT117E嵌入式竞赛板LCD驱动程序 软件环境: Keil uVision 4.10 硬件环境: CT117E嵌入式竞赛板 日 期: 2011-8-9 */ #include "lcd.h" #include "fonts.h" //#include "systick.h" static vu16 TextColor = 0x0000, BackColor = 0xFFFF; vu16 dummy; /******************************************************************************* * Function Name : Delay_LCD * Description : Inserts a delay time. * Input : nCount: specifies the delay time length. * Output : None * Return : None *******************************************************************************/ void Delay_LCD(u16 n) { u16 i,j; for (i = 0;i= Height) { LCD_WriteReg(R80, (Xpos - Height + 1)); } else { LCD_WriteReg(R80, 0); } LCD_WriteReg(R81, Xpos); if(Ypos >= Width) { LCD_WriteReg(R82, (Ypos - Width + 1)); } else { LCD_WriteReg(R82, 0); } /* Vertical GRAM End Address */ LCD_WriteReg(R83, Ypos); LCD_SetCursor(Xpos, Ypos); } /******************************************************************************* * Function Name : LCD_WindowModeDisable * Description : Disables LCD Window mode. * Input : None * Output : None * Return : None *******************************************************************************/ void LCD_WindowModeDisable(void) { LCD_SetDisplayWindow(239, 0x13F, 240, 320); LCD_WriteReg(R3, 0x1018); } /******************************************************************************* * Function Name : LCD_DrawLine * Description : Displays a line. * Input : - Xpos: specifies the X position. * - Ypos: specifies the Y position. * - Length: line length. * - Direction: line direction. * This parameter can be one of the following values: Vertical * or Horizontal. * Output : None * Return : None *******************************************************************************/ void LCD_DrawLine(u8 Xpos, u16 Ypos, u16 Length, u8 Direction) { u32 i = 0; LCD_SetCursor(Xpos, Ypos); if(Direction == Horizontal) { LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ for(i = 0; i < Length; i++) { LCD_WriteRAM(TextColor); } } else { for(i = 0; i < Length; i++) { LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor); Xpos++; LCD_SetCursor(Xpos, Ypos); } } } /******************************************************************************* * Function Name : LCD_DrawRect * Description : Displays a rectangle. * Input : - Xpos: specifies the X position. * - Ypos: specifies the Y position. * - Height: display rectangle height. * - Width: display rectangle width. * Output : None * Return : None *******************************************************************************/ void LCD_DrawRect(u8 Xpos, u16 Ypos, u8 Height, u16 Width) { LCD_DrawLine(Xpos, Ypos, Width, Horizontal); LCD_DrawLine((Xpos + Height), Ypos, Width, Horizontal); LCD_DrawLine(Xpos, Ypos, Height, Vertical); LCD_DrawLine(Xpos, (Ypos - Width + 1), Height, Vertical); } /******************************************************************************* * Function Name : LCD_DrawCircle * Description : Displays a circle. * Input : - Xpos: specifies the X position. * - Ypos: specifies the Y position. * - Height: display rectangle height. * - Width: display rectangle width. * Output : None * Return : None *******************************************************************************/ void LCD_DrawCircle(u8 Xpos, u16 Ypos, u16 Radius) { s32 D; u32 CurX; u32 CurY; D = 3 - (Radius << 1); CurX = 0; CurY = Radius; while (CurX <= CurY) { LCD_SetCursor(Xpos + CurX, Ypos + CurY); LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor); LCD_SetCursor(Xpos + CurX, Ypos - CurY); LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor); LCD_SetCursor(Xpos - CurX, Ypos + CurY); LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor); LCD_SetCursor(Xpos - CurX, Ypos - CurY); LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor); LCD_SetCursor(Xpos + CurY, Ypos + CurX); LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor); LCD_SetCursor(Xpos + CurY, Ypos - CurX); LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor); LCD_SetCursor(Xpos - CurY, Ypos + CurX); LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor); LCD_SetCursor(Xpos - CurY, Ypos - CurX); LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ LCD_WriteRAM(TextColor); if (D < 0) { D += (CurX << 2) + 6; } else { D += ((CurX - CurY) << 2) + 10; CurY--; } CurX++; } } /******************************************************************************* * Function Name : LCD_DrawMonoPict * Description : Displays a monocolor picture. * Input : - Pict: pointer to the picture array. * Output : None * Return : None *******************************************************************************/ void LCD_DrawMonoPict(uc32 *Pict) { u32 index = 0, i = 0; LCD_SetCursor(0, 319); LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ for(index = 0; index < 2400; index++) { for(i = 0; i < 32; i++) { if((Pict[index] & (1 << i)) == 0x00) { LCD_WriteRAM(BackColor); } else { LCD_WriteRAM(TextColor); } } } } /******************************************************************************* * Function Name : LCD_WriteBMP * Description : Displays a bitmap picture loaded in the internal Flash. * Input : - BmpAddress: Bmp picture address in the internal Flash. * Output : None * Return : None *******************************************************************************/ void LCD_WriteBMP(u32 BmpAddress) { u32 index = 0, size = 0; size = *(vu16 *) (BmpAddress + 2); size |= (*(vu16 *) (BmpAddress + 4)) << 16; index = *(vu16 *) (BmpAddress + 10); index |= (*(vu16 *) (BmpAddress + 12)) << 16; size = (size - index)/2; BmpAddress += index; LCD_WriteReg(R3, 0x1008); LCD_WriteRAM_Prepare(); for(index = 0; index < size; index++) { LCD_WriteRAM(*(vu16 *)BmpAddress); BmpAddress += 2; } LCD_WriteReg(R3, 0x1018); } /******************************************************************************* * 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) { GPIOB->BRR |= GPIO_PIN_9; GPIOB->BRR |= GPIO_PIN_8; GPIOB->BSRR |= GPIO_PIN_5; GPIOC->ODR = LCD_Reg; GPIOB->BRR |= GPIO_PIN_5; __nop(); __nop(); __nop(); GPIOB->BSRR |= GPIO_PIN_5; GPIOB->BSRR |= GPIO_PIN_8; GPIOC->ODR = LCD_RegValue; GPIOB->BRR |= GPIO_PIN_5; __nop(); __nop(); __nop(); GPIOB->BSRR |= GPIO_PIN_5; GPIOB->BSRR |= GPIO_PIN_8; } /******************************************************************************* * Function Name : LCD_ReadReg * Description : Reads the selected LCD Register. * Input : None * Output : None * Return : LCD Register Value. *******************************************************************************/ u16 LCD_ReadReg(u8 LCD_Reg) { u16 temp; GPIOB->BRR |= GPIO_PIN_9; GPIOB->BRR |= GPIO_PIN_8; GPIOB->BSRR |= GPIO_PIN_5; GPIOC->ODR = LCD_Reg; GPIOB->BRR |= GPIO_PIN_5; __nop(); __nop(); __nop(); GPIOB->BSRR |= GPIO_PIN_5; GPIOB->BSRR |= GPIO_PIN_8; LCD_BusIn(); GPIOA->BRR |= GPIO_PIN_8; __nop(); __nop(); __nop(); temp = GPIOC->IDR; GPIOA->BSRR |= GPIO_PIN_8; LCD_BusOut(); GPIOB->BSRR |= GPIO_PIN_9; return temp; } /******************************************************************************* * Function Name : LCD_WriteRAM_Prepare * Description : Prepare to write to the LCD RAM. * Input : None * Output : None * Return : None *******************************************************************************/ void LCD_WriteRAM_Prepare(void) { GPIOB->BRR |= GPIO_PIN_9; GPIOB->BRR |= GPIO_PIN_8; GPIOB->BSRR |= GPIO_PIN_5; GPIOC->ODR = R34; GPIOB->BRR |= GPIO_PIN_5; __nop(); __nop(); __nop(); GPIOB->BSRR |= GPIO_PIN_5; GPIOB->BSRR |= GPIO_PIN_8; __nop(); __nop(); __nop(); GPIOB->BSRR |= GPIO_PIN_9; } /******************************************************************************* * 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) { GPIOB->BRR |= GPIO_PIN_9; GPIOB->BSRR |= GPIO_PIN_8; GPIOB->BSRR |= GPIO_PIN_5; GPIOC->ODR = RGB_Code; GPIOB->BRR |= GPIO_PIN_5; __nop(); __nop(); __nop(); GPIOB->BSRR |= GPIO_PIN_5; GPIOB->BSRR |= GPIO_PIN_8; __nop(); __nop(); __nop(); GPIOB->BSRR |= GPIO_PIN_9; } /******************************************************************************* * Function Name : LCD_ReadRAM * Description : Reads the LCD RAM. * Input : None * Output : None * Return : LCD RAM Value. *******************************************************************************/ u16 LCD_ReadRAM(void) { u16 temp; GPIOB->BRR |= GPIO_PIN_9; GPIOB->BRR |= GPIO_PIN_8; GPIOB->BSRR |= GPIO_PIN_5; GPIOC->ODR = R34; GPIOB->BRR |= GPIO_PIN_5; __nop(); __nop(); __nop(); GPIOB->BSRR |= GPIO_PIN_5; GPIOB->BSRR |= GPIO_PIN_8; LCD_BusIn(); GPIOA->BRR |= GPIO_PIN_8; __nop(); __nop(); __nop(); temp = GPIOC->IDR; GPIOA->BSRR |= GPIO_PIN_8; LCD_BusOut(); GPIOB->BSRR |= GPIO_PIN_9; return temp; } /******************************************************************************* * Function Name : LCD_PowerOn * Description : Power on the LCD. * Input : None * Output : None * Return : None *******************************************************************************/ void LCD_PowerOn(void) { LCD_WriteReg(R16, 0x0000); LCD_WriteReg(R17, 0x0000); LCD_WriteReg(R18, 0x0000); LCD_WriteReg(R19, 0x0000); Delay_LCD(20); LCD_WriteReg(R16, 0x17B0); LCD_WriteReg(R17, 0x0137); Delay_LCD(5); LCD_WriteReg(R18, 0x0139); Delay_LCD(5); LCD_WriteReg(R19, 0x1d00); LCD_WriteReg(R41, 0x0013); Delay_LCD(5); LCD_WriteReg(R7, 0x0173); } /******************************************************************************* * Function Name : LCD_DisplayOn * Description : Enables the Display. * Input : None * Output : None * Return : None *******************************************************************************/ void LCD_DisplayOn(void) { LCD_WriteReg(R7, 0x0173); } /******************************************************************************* * Function Name : LCD_DisplayOff * Description : Disables the Display. * Input : None * Output : None * Return : None *******************************************************************************/ void LCD_DisplayOff(void) { LCD_WriteReg(R7, 0x0); } /******************************************************************************* * Function Name : LCD_CtrlLinesConfig * Description : Configures LCD Control lines. Push-Pull mode. * Input : None * Output : None * Return : None *******************************************************************************/ void LCD_CtrlLinesConfig(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_8 | GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_8 ; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); LCD_BusOut(); GPIOA->BSRR |= 0x0100; GPIOB->BSRR |= 0x0220; } /******************************************************************************* * Function Name : LCD_BusIn * Description : Configures the Parallel interface for LCD(PortC) * Input : None * Output : None * Return : None *******************************************************************************/ void LCD_BusIn(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = GPIO_PIN_All; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); } /******************************************************************************* * Function Name : LCD_BusOut * Description : Configures the Parallel interface for LCD(PortC) * Input : None * Output : None * Return : None *******************************************************************************/ void LCD_BusOut(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = GPIO_PIN_All; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); } /******************************************************************************* * Function Name : LCD_DrawPicture * Description : Displays a 16 color picture. * Input : - picture: pointer to the picture array. * Output : None * Return : None *******************************************************************************/ void LCD_DrawPicture(const u8* picture) { int index; LCD_SetCursor(0x00, 0x0000); LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */ for(index = 0; index < 76800; index++) { LCD_WriteRAM(picture[2*index+1]<<8|picture[2*index]); } }