/****************************************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" //------------------------------------------------------- #define LCD_CS_PIN GPIO_Pin_12 #define LCD_CS_PORT GPIOB #define LCD_CS_HIGH LCD_CS_PORT->BSRR = LCD_CS_PIN #define LCD_CS_LOW LCD_CS_PORT->BRR = LCD_CS_PIN #define LCD_SCK_PIN GPIO_Pin_13 #define LCD_SCK_PORT GPIOB #define LCD_SCK_HIGH LCD_SCK_PORT->BSRR = LCD_SCK_PIN #define LCD_SCK_LOW LCD_SCK_PORT->BRR = LCD_SCK_PIN #define LCD_DCX_PIN GPIO_Pin_14 #define LCD_DCX_PORT GPIOB #define LCD_DCX_HIGH LCD_DCX_PORT->BSRR = LCD_DCX_PIN #define LCD_DCX_LOW LCD_DCX_PORT->BRR = LCD_DCX_PIN #define LCD_SDA_PIN GPIO_Pin_15 #define LCD_SDA_PORT GPIOB #define LCD_SDA_HIGH LCD_SDA_PORT->BSRR = LCD_SDA_PIN #define LCD_SDA_LOW LCD_SDA_PORT->BRR = LCD_SDA_PIN #define LCD_RESET_PIN GPIO_Pin_6 #define LCD_RESET_PORT GPIOC #define LCD_RESET_HIGH LCD_RESET_PORT->BSRR = LCD_RESET_PIN #define LCD_RESET_LOW LCD_RESET_PORT->BRR = LCD_RESET_PIN #define LCD_BL_PIN GPIO_Pin_7 #define LCD_BL_PORT GPIOC #define LCD_BL_HIGH LCD_BL_PORT->BSRR = LCD_BL_PIN #define LCD_BL_LOW LCD_BL_PORT->BRR = LCD_BL_PIN 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[]; u16 g_usBackColor; //背景颜色 u16 g_usColor; //画笔颜色 /******************************************************************************* * Function Name : Lcd_Configuration * Description : Configures LCD Control lines * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ static void LcdPortInit(void) { GPIO_InitTypeDef GPIO_InitStructure; /* 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; } 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_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_CS_HIGH; } void LcdInit(void) { LcdPortInit(); //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颜色顺序 //------------------------------------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(COLOR_BLUE,COLOR_WHITE); //清屏 LcdClrAll(); } /*************************************************************************** *LcdSetColor 设置画笔颜色和背景颜色 ****************************************************************************/ void LcdSetColor(u16 BrushColor,u16 BackColor) { g_usColor=BrushColor; //画笔颜色 g_usBackColor=BackColor; //背景颜色 } /**************************************************************************** * 名 称: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; Xstart=x1+1; Xend=x2+1; Ystart=y1+2; Yend=y2+2; 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) { int i,j; unsigned char colorH,colorL; colorH=g_usBackColor>>8; colorL=g_usBackColor&0xff; LcdBlockWrite(0,0,159,127); for(i=0;ixr||yl>yr)return; len=(xr-xl+1)*(yr-yl+1); colorH = g_usBackColor >> 8; colorL = g_usBackColor & 0x00ff; LcdBlockWrite(xl,yl,xr,yr);//LcdBlockWrite(xl,yl,yr,yr); for(i=0;ixr||yl>yr)return; len=(xr-xl+1)*(yr-yl+1); colorH = g_usColor >> 8; colorL = g_usColor & 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(g_usColor>>8); LcdSendData(g_usColor&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=g_usColor>>8; colorL=g_usColor&0xff; bgcolorH=g_usBackColor>>8; bgcolorL=g_usBackColor&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 LcdTest(u16 color) { int x,y; LcdSetColor(COLOR_BLUE,COLOR_WHITE); LcdClrAll(); //LcdShowStr(0,0,"深圳圳",0x10); LcdDrawRect(10,10,80,40,2); LcdFillRect(10,20,60,60); LcdDrawHLine(5,120,5,1); LcdDrawVLine(5,120,130,2); /* LcdBlockWrite(159,0,159,0); color=0xf800; LcdSendData(color>>8); LcdSendData(color&0x00ff); LcdBlockWrite(0,0,7,7); color=0xf800; for(x=0;x<64;x++){ LcdSendData(color>>8); LcdSendData(color&0x00ff); } LcdBlockWrite(0,120,7,127); color=0x07e0; for(x=0;x<64;x++){ LcdSendData(color>>8); LcdSendData(color&0x00ff); } LcdBlockWrite(152,120,159,127); color=0x001f; for(x=0;x<64;x++){ LcdSendData(color>>8); LcdSendData(color&0x00ff); } */ } /********************************************************************************************************* END FILE *********************************************************************************************************/