微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > [C51代码]菜单函数[用于LCD1602]

[C51代码]菜单函数[用于LCD1602]

时间:11-10 来源:互联网 点击:
#ifndef __menu_h__

#define __menu_h__
#include "head.h"
#include "LCD1602_2.h"
#include "DS18B20.h"

#define Null 0

/***********************

* 函数声明 *

***********************/

void ShowMenu(void);

void Menu_Change(uchar KeyNum);

/***********************

* 按键功能键宏定义 *

***********************/

#define up 1

#define down 2

#define back 4

#define ok 5

/**********************

* 目录结构体定义 *

**********************/

struct MenuItem

{

uchar MenuCount; //当前层节点数

uchar *DisplayString; //菜单标题

void (*Subs)(); //节点函数

struct MenuItem *ChildrenMenus; //子节点

struct MenuItem *ParentMenus; //父节点

};

/***********************
* 调用子函数区 *
***********************/
void NullSubs(void)
{

}

/***********************

* 结构体区 *

***********************/
struct MenuItem NameMenu[2];

struct MenuItem NumberMenu[2];

struct MenuItem MainMenu[3];

struct MenuItem NameMenu[2]=

{ //MenuCount DisplayString Subs ChildrenMenus ParentMenus

{2, "WeiMen",NullSubs, Null, MainMenu},

{2, "Back",NullSubs, MainMenu, MainMenu},

};

struct MenuItem NumberMenu[2]=
{ //MenuCount DisplayString Subs ChildrenMenus ParentMenus

{2, "0500320128", NullSubs, Null, MainMenu},

{2, "Back",NullSubs,MainMenu, MainMenu},

};
struct MenuItem MainMenu[3]=

{ //MenuCount DisplayString Subs ChildrenMenus ParentMenus

{3, "1.Show Name ", NullSubs, NameMenu, Null},

{3, "2.Show Number",NullSubs, NumberMenu, Null},

{3, "3.Back", MainMenu, MainMenu},

};

/***********************

* 全局变量声明区 *

***********************/

struct MenuItem (*MenuPoint) = MainMenu; //结构体指针,指向结构体后由内部函数指针指向功能函数

uchar DisplayStart = 0; //显示时的第一个菜单项

uchar UserChoose = 0; //用户所选菜单项

uchar DisplayPoint = 0; //显示指针

uchar MaxItems; //同级最大菜单数

uchar ShowCount=2; //同屏显示菜单数

/***********************

*显示函数区*

***********************/

void ShowMenu(void)
{
unsigned char n;
MaxItems = MenuPoint[0].MenuCount;//定义最大同级菜单
DisplayPoint = DisplayStart;
for(n=0;DisplayPoint{if(DisplayPoint==UserChoose)
LCDDispListChar(0,n,"->");
LCDDispListChar(2,n,MenuPoint[DisplayPoint++].DisplayString);

}

}

void Menu_Change(unsigned char KeyNum)
{
if(KeyNum)
{
switch(KeyNum)
{
case up:
UserChoose --;
if (UserChoose ==255)
{
UserChoose = 0;//上翻截至,如果要回滚赋值MaxItems-1
}
break;
case down:
UserChoose ++;
if (UserChoose == MaxItems)
{
UserChoose = MaxItems-1;//下翻截至,如要回滚赋值为0
}
break;
case back:
if (MenuPoint[UserChoose].ParentMenus != Null)
{
MenuPoint = MenuPoint[UserChoose].ParentMenus;
UserChoose = 0;
DisplayStart = 0;
}
break;
case ok:
if (MenuPoint[UserChoose].Subs != NullSubs)
{
(*MenuPoint[UserChoose].Subs)();
}
else if (MenuPoint[UserChoose].ChildrenMenus != Null)
{
MenuPoint = MenuPoint[UserChoose].ChildrenMenus;
UserChoose = 0;
DisplayStart = 0;
}
break;

default:break;
}
if (UserChoose%ShowCount==0) //一屏只能显示ShowCount行
DisplayStart = UserChoose;
else if(UserChoose==1||UserChoose== 3)
DisplayStart = UserChoose-1; //实现滚屏的关键
LCDWriteCmd(DispClear); //液晶清屏,根据不同液晶函数自行修改
delay1ms(2); //液晶为慢速器件
ShowMenu();
}
}

#endif

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top