首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

[分享]单片机+LCD显示菜单(C源程序+proteus仿真)

[分享]单片机+LCD显示菜单(C源程序+proteus仿真)

aytYISc4.jpg
人要保持斗志,并有疯狂的追求,生活才会精彩! QQ:24387110 手机:13871563876
主要程序:
CODE:

#define Null 0


/***********************
* 函数声明 *
***********************/
void ShowMenu(void);
void Menu_Change(unsigned char KeyNum);

/***********************
* 按键功能键宏定义 *
***********************/
#define UP '3'
#define Down '7'
#define Esc 'B'
#define Enter 'F'
#define Reset '0'


/**********************
* 目录结构体定义 *
**********************/
struct MenuItem
{
unsigned char MenuCount; //当前层节点数
unsigned char *DisplayString; //菜单标题
void (*Subs)(); //节点函数
struct MenuItem *ChildrenMenus; //子节点
struct MenuItem *ParentMenus; //父节点
};

/***********************
* 调用子函数区 *
***********************/
void NullSubs(void)
{
}
//----------------------以下为例子,请根据实际情况修改---------------------------

void TimeSet(void)
{
put_s("TimeSet");
}

void DateSet(void)
{
put_s("DateSet");
}

void AlertSet (void)
{
put_s("AlertSet");
}
//------------------------------------------------------------------------------
人要保持斗志,并有疯狂的追求,生活才会精彩! QQ:24387110 手机:13871563876
CODE:

/***********************
* 结构体区 *
***********************/
//----------------------以下为例子,请根据实际情况修改---------------------------
struct MenuItem TimeMenu[4];
struct MenuItem FlashMenu[5];
struct MenuItem VoiceMenu[5];
struct MenuItem RobotMenu[5];
struct MenuItem MainMenu[5];

struct MenuItem TimeMenu[4]=
{ //MenuCount DisplayString Subs ChildrenMenus ParentMenus
{4, "1.Time Set", TimeSet, Null, MainMenu},
{4, "2.Date Set", DateSet, Null, MainMenu},
{4, "3.AlertSet", AlertSet, Null, MainMenu},
{4, "4.Back", NullSubs, MainMenu, MainMenu},
};

struct MenuItem FlashMenu[5]=
{ //MenuCount DisplayString Subs ChildrenMenus ParentMenus
{5, "1.Flash Record", NullSubs, Null, MainMenu},
{5, "2.Play", NullSubs, Null, MainMenu},
{5, "3.Pause", NullSubs, Null, MainMenu},
{5, "4.Flash Delete", NullSubs, Null, MainMenu},
{5, "5.Back", NullSubs, MainMenu, MainMenu},
};

struct MenuItem VoiceMenu[5]=
{ //MenuCount DisplayString Subs ChildrenMenus ParentMenus
{5, "1.Voice Record" , NullSubs, Null, MainMenu},
{5, "2.Play", NullSubs, Null, MainMenu},
{5, "3.Pause", NullSubs, Null, MainMenu},
{5, "4.Voice Delete", NullSubs, Null, MainMenu},
{5, "5.Back", NullSubs, MainMenu, MainMenu},
};

struct MenuItem RobotMenu[5]=
{ //MenuCount DisplayString Subs ChildrenMenus ParentMenus
{5, "1.Turn Left", NullSubs, Null, MainMenu},
{5, "2.Turn Right", NullSubs, Null, MainMenu},
{5, "3.Go Ahead", NullSubs, Null, MainMenu},
{5, "4.Go Back", NullSubs, Null, MainMenu},
{5, "5.Back", NullSubs, MainMenu, MainMenu},
};


struct MenuItem MainMenu[5]=
{ //MenuCount DisplayString Subs ChildrenMenus ParentMenus
{5, "1.Time Set" , NullSubs, TimeMenu, Null},
{5, "2.Voice Center", NullSubs, VoiceMenu, Null},
{5, "3.Robot Control", NullSubs, RobotMenu, Null},
{5, "4.Flash Option", NullSubs, FlashMenu, Null},
{5, "5.Back", NullSubs, MainMenu, MainMenu},
};
//------------------------------------------------------------------------------
人要保持斗志,并有疯狂的追求,生活才会精彩! QQ:24387110 手机:13871563876
CODE:


/***********************
* 全局变量声明区 *
***********************/
struct MenuItem (*MenuPoint) = MainMenu; //结构体指针,指向结构体后由内部函数指针指向功能函数
unsigned char DisplayStart = 0; //显示时的第一个菜单项
unsigned char UserChoose = 0; //用户所选菜单项
unsigned char DisplayPoint = 0; //显示指针
unsigned MaxItems; //同级最大菜单数
unsigned char ShowCount=2; //同屏显示菜单数


/***********************
*显示函数区 *
***********************/
void ShowMenu(void)
{
unsigned char n;
MaxItems = MenuPoint[0].MenuCount;//定义最大同级菜单
DisplayPoint = DisplayStart;
for(n=0;DisplayPoint<MaxItems&&n<ShowCount;n++)
{if(DisplayPoint==UserChoose)
LCD_write_string(0,n,"->");
LCD_write_string(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 Esc:
if (MenuPoint[UserChoose].ParentMenus != Null)
{
MenuPoint = MenuPoint[UserChoose].ParentMenus;
UserChoose = 0;
DisplayStart = 0;
}
break;
case Down:
UserChoose ++;
if (UserChoose == MaxItems)
{
UserChoose = MaxItems-1;//下翻截至,如要回滚赋值为0
}
break;
case Enter:
if (MenuPoint[UserChoose].Subs != NullSubs)
{
(*MenuPoint[UserChoose].Subs)();
}
else if (MenuPoint[UserChoose].ChildrenMenus != Null)
{
MenuPoint = MenuPoint[UserChoose].ChildrenMenus;
UserChoose = 0;
DisplayStart = 0;
}
break;
case Reset:
MenuPoint = MainMenu;
UserChoose = 0;
DisplayStart = 0;
break;
default:break;
}
if (UserChoose%ShowCount==0) //一屏只能显示ShowCount行
DisplayStart = UserChoose;
else if(UserChoose==1||UserChoose== 3)
DisplayStart = UserChoose-1; //实现滚屏的关键
LCD_write_command(0x01); //液晶清屏,根据不同液晶函数自行修改
delay_nms(5); //液晶为慢速器件
ShowMenu();
}
}
人要保持斗志,并有疯狂的追求,生活才会精彩! QQ:24387110 手机:13871563876
[Point=5]

C语言源代码+proteus仿真文件

需要的就回复,回复之后刷新本页!即可见下载地址:

本帖隐藏的内容需要回复才可以浏览
[/Point]

[此贴子已经被作者于2009-6-3 21:37:21编辑过]

人要保持斗志,并有疯狂的追求,生活才会精彩! QQ:24387110 手机:13871563876

工程里的KEY.H有逻辑错误,需要修改

unsigned char get_key(void)
{
unsigned char i;
static unsigned char j;//按键记录

i=key_read();
if(i==0x00) //无有效按键按下
{
j=0x00; //清除按键记录
return 0x00; //程序退出
}
if(j==0) //为新按键!!!!这个地方需要改动,见下面改动
{
j=i; //保存本次结果
delay_nms(10); //延时去抖动
i=key_read();
if(i==j)
return i;
}
return 0x00;
}

修改一下

unsigned char get_key(void)
{
unsigned char i;
static unsigned char j;//按键记录

i=key_read();
if(i==0x00) //无有效按键按下
{
j=0x00; //清除按键记录
return 0x00; //程序退出
}
if(j!=i) //为新按键!!!!!改动之后!!!!
{
j=i; //保存本次结果
delay_nms(10); //延时去抖动
i=key_read();
if(i==j)
return i;
}
return 0x00;
}

这样就行了

人要保持斗志,并有疯狂的追求,生活才会精彩! QQ:24387110 手机:13871563876

可能在有的C编译器里面,一个指针没有办法设为空指针,一个函数指针也没法设置为空。

在那样的编译器中就没办法实现啦。

人要保持斗志,并有疯狂的追求,生活才会精彩! QQ:24387110 手机:13871563876
tks

我最近也在弄一个菜单程序,就是一直结构型数组怎样进参数传递的,可以讲讲吗?谢谢

结构型数组怎样进参数传递的

=========================================

需要再定义一个结构型指针来控制

人要保持斗志,并有疯狂的追求,生活才会精彩! QQ:24387110 手机:13871563876

好啊

好啊

谢谢

[em01]

Which type LCD can be used ?

I have a YM12864J module , which core chip is the KS0108.

grant.wen@liteon.com

Which type LCD can be used ?

I have a YM12864J module , which core chip is the KS0108.

=====================================================

YM12864J module include KS0108

人要保持斗志,并有疯狂的追求,生活才会精彩! QQ:24387110 手机:13871563876

谢谢

返回列表