/****************************************Copyright (c)************************************************** * File Name: Lcd.c * Function Describe: Driver of LCD * Explain: * Writer: ShiLiangWen * Date: 2016-1-8 ********************************************************************************************************/ #define THIS_FILE_ID 15 //------------------------------------------------------- #include "includes.h" unsigned char showApn=1; unsigned char LcdSleepFlag; unsigned char LastLcdSleepFlag; extern const unsigned char g_apucLetter16[]; extern const unsigned char g_apucCLetter16[]; extern const unsigned char g_apucLetter24[]; extern const unsigned char g_apucCLetter24[]; extern const unsigned char g_apucFonts16[]; extern const unsigned char g_apucCFonts16[]; extern const unsigned char g_apucFonts24[]; extern const unsigned char g_apucCFonts24[]; void LcdShowStr(u16 x, u16 y,const char *string,u8 mode); void LcdDrawVLine(u16 y1, u16 y2, u16 x, u8 width); void LcdDrawHLine(u16 x1, u16 x2, u16 y, u8 width); void LcdClearRect(u16 xl, u16 yl, u16 xr, u16 yr); void LcdFillRect(u16 xl, u16 yl, u16 xr, u16 yr); static u16 susLcdBackColor; //背景颜色 static u16 susLcdForeColor; //画笔颜色 /*************************************************************************** *LcdSetColor 设置画笔颜色和背景颜色 ****************************************************************************/ void LcdSetColor(u16 BrushColor,u16 BackColor) { susLcdForeColor=BrushColor; //画笔颜色 susLcdBackColor=BackColor; //背景颜色 } /******************************************************************************* * Function Name : Lcd_Configuration * Description : Configures LCD Control lines * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ static void LcdPortInit(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; #if 0 /* Enable GPIOD and GPIOE clocks */ RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; LCD_CS_HIGH; LCD_BL_HIGH; GPIO_InitStructure.GPIO_Pin = (LCD_CS_PIN|LCD_SCK_PIN|LCD_DCX_PIN|LCD_SDA_PIN); GPIO_Init(GPIOB, &GPIO_InitStructure); LCD_CS_HIGH; LCD_SCK_HIGH; LCD_SDA_HIGH; LCD_DCX_HIGH; LCD_RESET_HIGH; LCD_BL_HIGH; GPIO_InitStructure.GPIO_Pin = (LCD_RESET_PIN|LCD_BL_PIN); GPIO_Init(GPIOC, &GPIO_InitStructure); LCD_RESET_HIGH; LCD_BL_HIGH; #else RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); GPIO_InitStructure.GPIO_Pin = LCD_CS_PIN|LCD_DCX_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = (LCD_SCK_PIN|LCD_SDA_PIN);; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = (LCD_RESET_PIN|LCD_BL_PIN); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); LCD_RESET_HIGH; LCD_BL_HIGH; /*!< SPI configuration */ SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //决定SPI 速度 //2 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); // /*!< Enable the sFLASH_SPI */ SPI_Cmd(SPI2, ENABLE); #endif } u8 LCD_ReadWriteByte(u8 byte) { // !< Send byte through the SPI1 peripheral SPI_I2S_SendData(SPI2, byte); // < Loop while DR register in not emplty while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); } void LCD_Write_SR(u8 sr) { LCD_ReadWriteByte(sr); //写入一个字节 } void LcdSendCommand(unsigned char Cmd) { unsigned char i,j; LCD_CS_LOW; LCD_DCX_LOW; // for(i=0;i<8;i++){ // j=0x80>>i; // LCD_SCK_LOW; // if(Cmd&j)LCD_SDA_HIGH; // else LCD_SDA_LOW; // LCD_SCK_HIGH; // } LCD_Write_SR(Cmd); LCD_CS_HIGH; } void LcdSendData(unsigned char Data) { unsigned char i,j; LCD_CS_LOW; LCD_DCX_HIGH; // for(i=0;i<8;i++){ // j=0x80>>i; // LCD_SCK_LOW; // if(Data & j)LCD_SDA_HIGH; // else LCD_SDA_LOW; // LCD_SCK_HIGH; // } LCD_Write_SR(Data); LCD_CS_HIGH; } void LcdInit(void) { LcdPortInit(); LCD_BL_LOW;//用户体验 //reset LCD_RESET_HIGH; DelayMs(1); LCD_RESET_LOW; DelayMs(1); LCD_RESET_HIGH; DelayMs(120); //-----------------------------------ST7735S Set REG---------------------------------------------// LcdSendCommand(0x11); //Sleep out DelayMs(120); //Delay 120ms //------------------------------------ST7735S Frame Rate-----------------------------------------// LcdSendCommand(0xB1); LcdSendData(0x05); LcdSendData(0x3C); LcdSendData(0x3C); LcdSendCommand(0xB2); LcdSendData(0x05); LcdSendData(0x3C); LcdSendData(0x3C); LcdSendCommand(0xB3); LcdSendData(0x05); LcdSendData(0x3C); LcdSendData(0x3C); LcdSendData(0x05); LcdSendData(0x3C); LcdSendData(0x3C); //------------------------------------End ST7735S Frame Rate-----------------------------------------// LcdSendCommand(0xB4); //Dot inversion LcdSendData(0x03); //------------------------------------ST7735S Power Sequence-----------------------------------------// LcdSendCommand(0xC0); LcdSendData(0x28); LcdSendData(0x08); LcdSendData(0x04); LcdSendCommand(0xC1); LcdSendData(0XC0); LcdSendCommand(0xC2); LcdSendData(0x0D); LcdSendData(0x00); LcdSendCommand(0xC3); LcdSendData(0x8D); LcdSendData(0x2A); LcdSendCommand(0xC4); LcdSendData(0x8D); LcdSendData(0xEE); //---------------------------------End ST7735S Power Sequence-------------------------------------// LcdSendCommand(0xC5); //VCOM LcdSendData(0x1A); LcdSendCommand(0x36); //MX, MY, RGB mode //LcdSendData(0x60);//-----------这个值非常重要,决定了内部显存和显示区域的关系,以及RGB颜色顺序 LcdSendData(0xA0); //------------------------------------ST7735S Gamma Sequence-----------------------------------------// LcdSendCommand(0xE0); LcdSendData(0x04); LcdSendData(0x22); LcdSendData(0x07); LcdSendData(0x0A); LcdSendData(0x2E); LcdSendData(0x30); LcdSendData(0x25); LcdSendData(0x2A); LcdSendData(0x28); LcdSendData(0x26); LcdSendData(0x2E); LcdSendData(0x3A); LcdSendData(0x00); LcdSendData(0x01); LcdSendData(0x03); LcdSendData(0x13); LcdSendCommand(0xE1); LcdSendData(0x04); LcdSendData(0x16); LcdSendData(0x06); LcdSendData(0x0D); LcdSendData(0x2D); LcdSendData(0x26); LcdSendData(0x23); LcdSendData(0x27); LcdSendData(0x27); LcdSendData(0x25); LcdSendData(0x2D); LcdSendData(0x3B); LcdSendData(0x00); LcdSendData(0x01); LcdSendData(0x04); LcdSendData(0x13); //------------------------------------End ST7735S Gamma Sequence-----------------------------------------// LcdSendCommand(0x3A); //65k mode LcdSendData(0x05); LcdSendCommand(0x29); //Display on //设置前景色和背景色 LcdSetColor(0x0000,0xffff); //ffff 白色 //LcdSetColor(0xffff,0x0000); //清屏 //LcdClrAll(); // LCD_RESET_LOW; //防止其刷屏 } /**************************************************************************** * 名 称:void Set_ramaddr(u16 x,u16 y) * 功 能:指定坐标点 * 入口参数:x轴y轴 * 出口参数:无 * 说 明:2012.4.27 hxm v1.0 * 调用方法:无 ****************************************************************************/ void LcdSetRamAddr(u16 x,u16 y) { LcdSendCommand(0x2a); LcdSendData(x>>8); LcdSendData(x&0xff); LcdSendCommand(0x2b); LcdSendData(y>>8); LcdSendData(y&0xff); } /********************************************************************************************** 设置一个写地址区域 ************************************************************************************************/ void LcdBlockWrite(u16 x1,u16 y1,u16 x2,u16 y2) { u16 Xstart,Xend,Ystart,Yend;//u16 #if(LCD_TYPE==0) //旧屏 Xstart=x1+1; Xend=x2+1; Ystart=y1+2; Yend=y2+2; #else //新屏 Xstart=x1; Xend=x2; Ystart=y1; Yend=y2; #endif LcdSendCommand(0x2a); LcdSendData(Xstart>>8); LcdSendData(Xstart&0xff); LcdSendData(Xend>>8); LcdSendData(Xend&0xff); LcdSendCommand(0x2b); LcdSendData(Ystart>>8); LcdSendData(Ystart&0xff); LcdSendData(Yend>>8); LcdSendData(Yend&0xff); LcdSendCommand(0x2c); } /*************************************************************************** LcdClrAll ****************************************************************************/ void LcdClrAll(void) { static int i,j; unsigned char colorH,colorL; colorH=susLcdBackColor>>8; colorL=susLcdBackColor&0xff; LcdBlockWrite(0,0,159,127); for(i=0;ixr||yl>yr)return; len=(xr-xl+1)*(yr-yl+1); colorH = susLcdBackColor >> 8; colorL = susLcdBackColor & 0x00ff; LcdBlockWrite(xl,yl,xr,yr); for(i=0;ixr||yl>yr)return; len=(xr-xl+1)*(yr-yl+1); colorH = susLcdForeColor >> 8; colorL = susLcdForeColor & 0x00ff; LcdBlockWrite(xl,yl,xr,yr); for(i=0;i>8); LcdSendData(x&0x00ff); LcdSendCommand(0x2b); LcdSendData(y>>8); LcdSendData(y&0x00ff); LcdSendCommand(0X2C); LcdSendData(susLcdForeColor>>8); LcdSendData(susLcdForeColor&0x00ff); } /**************************************************************************** LcdDrawHLine 画水平线 ****************************************************************************/ void LcdDrawHLine(u16 x1, u16 x2, u16 y, u8 width) { if(width==0)return; if(x2x2||y1>y2)return; LcdDrawHLine(x1,x2,y1,width); LcdDrawHLine(x1,x2,y2,width); LcdDrawVLine(y1,y2,x1,width); LcdDrawVLine(y1,y2,x2,width); } /*************************************************************************** *在LCD上显示一个字符 ****************************************************************************/ void LcdShowChar(u16 x,u16 y,u8 hiByte,u8 loByte,u8 mode) { unsigned short i,j,k,m; unsigned short width,height; unsigned char ByteWidth,data; unsigned char colorH,colorL; unsigned char bgcolorH,bgcolorL; const unsigned char *l_pucFont,*l_pucLetter; unsigned char *p; unsigned char font;//字体 unsigned char cover;//覆盖方式 font=mode&0xf0; cover=mode&0x0f; k = 0; if(!hiByte) //english { if(font==0x00) //8 * 16 { width=8; height = 16; ByteWidth = 1; l_pucFont = g_apucFonts16; l_pucLetter = g_apucLetter16; }else{ //16 * 24 width=16; height = 24; ByteWidth = 2; l_pucFont = g_apucFonts24; l_pucLetter = g_apucLetter24; } // while(*l_pucLetter) { if(loByte == *l_pucLetter)break; l_pucLetter ++; k ++; } } else //chinese { if(font == 0x00)//16 * 16 { width=16; height = 16; ByteWidth = 2; l_pucFont = g_apucCFonts16; l_pucLetter = g_apucCLetter16; }else{//24 * 24 width=24; height = 24; ByteWidth = 3; l_pucFont = g_apucCFonts24; l_pucLetter = g_apucCLetter24; } // while(*l_pucLetter) { if(hiByte == *l_pucLetter && loByte == *(l_pucLetter + 1))break; l_pucLetter += 2; k ++; } } //查找到字库的开始位置 l_pucFont += k * ByteWidth * height; //刷屏显示 colorH=susLcdForeColor>>8; colorL=susLcdForeColor&0xff; bgcolorH=susLcdBackColor>>8; bgcolorL=susLcdBackColor&0xff; LcdBlockWrite(x,y,x+width-1,y+height-1); for(j=0;j>k)){ LcdSendData(colorH); LcdSendData(colorL); }else{ //if(cover){ LcdSendData(bgcolorH); LcdSendData(bgcolorH); //} } } } } } /****************************************************************************** LcdShowStr 指定位置显示字符串 左上角坐标:x,y 显示内容:string 模式:mode 高4位代表字体,低4位为模式 ******************************************************************************/ void LcdShowStr(u16 x, u16 y,const char *string,u8 mode) { unsigned char width1,width2; unsigned char *p; unsigned short i; unsigned char font;//字体 font=mode&0xf0; if(font == 0x00) //16bit font { width1 = 2; width2 = 1; } else //24bit font { width1 = 3; width2 = 1; } p = (unsigned char *)string; i = 0; while(*p) { if (*p > 0x9f) //chinese letter { LcdShowChar(x+i,y,*p, *(p+1),mode); i += width1 * 8; p += 2; } else //english letter { LcdShowChar(x + i,y,0,*p,mode); if(font==0){ i += width2 * 8; }else{ i += width2 * 16; } p ++; } } } //void KeyLedControl(char status) //{ // if(status) // KEY_LED(0); // else // { // if(g_ulKeyValue != KEY_PPT && // g_ulKeyValue != KEY_PPT_MIC) // KEY_LED(1); // } //} void LCDTimeOut(int Ct) { LastLcdSleepFlag=LcdSleepFlag; if(Ct){ LCD_BL_LOW; //LcdSendCommand(0x28); LcdSleepFlag=1;// }else{ //LcdSendCommand(0x29); LCD_BL_HIGH; LcdSleepFlag=0; } } /********************************************************************************************************* END FILE *********************************************************************************************************/