您的位置 首页 技术

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

#ifndef__menu_h__#define__menu_h__#includehead.h#includeLCD1602_2.h#includeDS18B20.h#defineNull0/***

#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

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/xinpin/jishu/275336.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部