蓝桥杯嵌入式CT117E硬件开发平台LCD显示设计 | 驱动制作流程
官方提供了LCD的驱动,故而当我们使用的LCD的时候,只需要调用驱动函数就好了,极大的方便我们开发使用。调用方法官方也给了很详细的使用教程,如下代码:
STM3210B_LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
LCD_DrawLine(120,0,320,Horizontal);
LCD_DrawLine(0,160,240,Vertical);
Delay_Ms(1000);
LCD_Clear(Blue);
LCD_DrawRect(70,210,100,100);
Delay_Ms(1000);
LCD_Clear(Blue);
LCD_DrawCircle(120,160,50);
Delay_Ms(1000);
LCD_Clear(Blue);
LCD_DisplayStringLine(Line4 ,(unsigned char *)" Hello,world. ");
Delay_Ms(1000);
LCD_SetBackColor(White);
LCD_DisplayStringLine(Line0,(unsigned char *)" ");
LCD_SetBackColor(Black);
LCD_DisplayStringLine(Line1,(unsigned char *)" ");
LCD_SetBackColor(Grey);
LCD_DisplayStringLine(Line2,(unsigned char *)" ");
LCD_SetBackColor(Blue);
LCD_DisplayStringLine(Line3,(unsigned char *)" ");
LCD_SetBackColor(Blue2);
LCD_DisplayStringLine(Line4,(unsigned char *)" ");
LCD_SetBackColor(Red);
LCD_DisplayStringLine(Line5,(unsigned char *)" ");
LCD_SetBackColor(Magenta);
LCD_DisplayStringLine(Line6,(unsigned char *)" ");
LCD_SetBackColor(Green);
LCD_DisplayStringLine(Line7,(unsigned char *)" ");
LCD_SetBackColor(Cyan);
LCD_DisplayStringLine(Line8,(unsigned char *)" ");
LCD_SetBackColor(Yellow);
LCD_DisplayStringLine(Line9,(unsigned char *)" ");
自制LCD驱动函数,在原有LCD显示基础上,设计一个LCD显示框架。具体实现方法如下:
- 设计LCD的显示结构体,C语言结构体的优势在于可以在其内定义多个不同的成员变量,并且添加成员变量不影响现有的结构,所以非常时候构建工程。在设计的时候我制作了LCD的三个类型:LCD显示菜单(枚举类型),LCD默认界面参数(结构体),LCD设置界面参数(结构体)。大量使用结构体,能够使工程有很明确的思想以及框架,看起来不会太乱(强烈建议复习下C语言的结构体使用方法)。通过下面代码可以很明确知道三种结构的目的:当设计不同任务时,只需要响应稍微小改就可以。
typedef enum{
LCDMENU_DEFAULT = 0,
LCDMENU_Setting
}LCDMENU_TypeDef;
extern LCDMENU_TypeDef LCD_MENU;
typedef struct{
float Volt;
u8 Status_Num;
}LCDDISVALUE_DEF_TypeDef;
extern LCDDISVALUE_DEF_TypeDef LCDDISVALUE_DEF_Structure;
typedef struct{
float LCDSET_MaxVolt;
float LCDSET_MinVolt;
u8 LCDSET_Upper;
u8 LCDSET_Lower;
u8 LCDHighlight_Line;
}LCDDISVALUE_SET_TypeDef;
extern LCDDISVALUE_SET_TypeDef LCDDISVALUE_SET_Structure;
- 在设计LCD驱动时需要注意的是:由于不同界面切换要显示的内容可能不一样,如果不清屏,可能导致上一届的内容残留在当前界面,但是不可能每进入一次LCD驱动函数就清屏一次,这样又会导致LCD屏幕显示闪烁问题,故在设计LCD显示驱动时,我加入了LCD界面判断操作,这样可以保证只有当界面切换时才做清屏操作。在设计LCD设置界面,还做了高亮行显示功能满足有些蓝桥杯任务的要求。LCD驱动的实现方法如下。
const char NowStatus_Table[3][7]={
" Upper",
" Lower",
"Normal",
};
char LCD_DisBuff[21]={0};
LCDMENU_TypeDef LCD_MENU = LCDMENU_DEFAULT;
LCDDISVALUE_DEF_TypeDef LCDDISVALUE_DEF_Structure;
LCDDISVALUE_SET_TypeDef LCDDISVALUE_SET_Structure;
#define LCD_BACKCOLOR White
#define LCD_TEXTCOLOR Black
#define LCD_HIGHLIGHTCOLOR Green
void LCD_Proc()
{
static LCDMENU_TypeDef LCD_MenuState_Last = LCDMENU_DEFAULT;
if(LCD_MENU != LCD_MenuState_Last)
{
LCD_MenuState_Last = LCD_MENU;
LCD_Clear(LCD_BACKCOLOR);
LCD_SetBackColor(LCD_BACKCOLOR);
LCD_SetTextColor(LCD_TEXTCOLOR);
}
if(LCD_MENU == LCDMENU_DEFAULT)
{
LCD_SetBackColor(LCD_BACKCOLOR);
LCD_SetTextColor(LCD_TEXTCOLOR);
memset(LCD_DisBuff,'0',sizeof(LCD_DisBuff));
sprintf(LCD_DisBuff," Main ");
LCD_DisplayStringLine(Line1 ,(unsigned char *)LCD_DisBuff);
memset(LCD_DisBuff,'0',sizeof(LCD_DisBuff));
sprintf(LCD_DisBuff," Volt: %.02fV ",LCDDISVALUE_DEF_Structure.Volt);
LCD_DisplayStringLine(Line4 ,(unsigned char *)LCD_DisBuff);
memset(LCD_DisBuff,'0',sizeof(LCD_DisBuff));
sprintf(LCD_DisBuff," Status:%s",NowStatus_Table[LCDDISVALUE_DEF_Structure.Status_Num]);
LCD_DisplayStringLine(Line6 ,(unsigned char *)LCD_DisBuff);
}
else if(LCD_MENU == LCDMENU_Setting)
{
LCD_SetBackColor(LCD_BACKCOLOR);
LCD_SetTextColor(LCD_TEXTCOLOR);
memset(LCD_DisBuff,'0',sizeof(LCD_DisBuff));
sprintf(LCD_DisBuff," Setting ");
LCD_DisplayStringLine(Line1 ,(unsigned char *)LCD_DisBuff);
if(LCDDISVALUE_SET_Structure.LCDHighlight_Line == 0)
LCD_SetTextColor(LCD_HIGHLIGHTCOLOR);
else
LCD_SetTextColor(LCD_TEXTCOLOR);
memset(LCD_DisBuff,'0',sizeof(LCD_DisBuff));
sprintf(LCD_DisBuff," Max Volt: %.1fV ",LCDDISVALUE_SET_Structure.LCDSET_MaxVolt);
LCD_DisplayStringLine(Line3 ,(unsigned char *)LCD_DisBuff);
if(LCDDISVALUE_SET_Structure.LCDHighlight_Line == 1)
LCD_SetTextColor(LCD_HIGHLIGHTCOLOR);
else
LCD_SetTextColor(LCD_TEXTCOLOR);
memset(LCD_DisBuff,'0',sizeof(LCD_DisBuff));
sprintf(LCD_DisBuff," Min Volt: %.1fV ",LCDDISVALUE_SET_Structure.LCDSET_MinVolt);
LCD_DisplayStringLine(Line5 ,(unsigned char *)LCD_DisBuff);
if(LCDDISVALUE_SET_Structure.LCDHighlight_Line == 2)
LCD_SetTextColor(LCD_HIGHLIGHTCOLOR);
else
LCD_SetTextColor(LCD_TEXTCOLOR);
memset(LCD_DisBuff,'0',sizeof(LCD_DisBuff));
sprintf(LCD_DisBuff," Upper: LD%d ",LCDDISVALUE_SET_Structure.LCDSET_Upper);
LCD_DisplayStringLine(Line7 ,(unsigned char *)LCD_DisBuff);
if(LCDDISVALUE_SET_Structure.LCDHighlight_Line == 3)
LCD_SetTextColor(LCD_HIGHLIGHTCOLOR);
else
LCD_SetTextColor(LCD_TEXTCOLOR);
memset(LCD_DisBuff,'0',sizeof(LCD_DisBuff));
sprintf(LCD_DisBuff," Lower: LD%d ",LCDDISVALUE_SET_Structure.LCDSET_Lower);
LCD_DisplayStringLine(Line9 ,(unsigned char *)LCD_DisBuff);
}
else{}
}