C51编写菜单的例子
看看有没有借鉴意义。
/* Module :Kbdmenu.c Date:------ Usage:P10~P12 use to DownKey,UpKey,CRKey */
#include #include
#define SIZE_OF_KEYBD_MENU 20 //菜单长度
uchar KeyFuncIndex=0; //uchar KeyFuncIndexNew=0;
void (*KeyFuncPtr)(); //按键功能指针
typedef struct { uchar KeyStateIndex; //当前状态索引号 uchar KeyDnState; //按下"向下"键时转向的状态索引号 uchar KeyUpState; //按下"向上"键时转向的状态索引号 uchar KeyCrState; //按下"回车"键时转向的状态索引号 void (*CurrentOperate)(); //当前状态应该执行的功能操作 } KbdTabStruct;
KbdTabStruct code KeyTab[SIZE_OF_KEYBD_MENU]= { { 0, 0, 0, 1,(*DummyJob)},//顶层
{ 1, 2, 0, 3,(*DspUserInfo)},//第二层 { 2, 1, 1, 9,(*DspServiceInfo)}, //第二层
{ 3, 0, 0, 1,(*DspVoltInfo)},//第三层>>DspUserInfo的展开 { 4, 0, 0, 1,(*DspCurrInfo)},//第三层>>DspUserInfo的展开 { 5, 0, 0, 1,(*DspFreqInfo)},//第三层>>DspUserInfo的展开 { 6, 0, 0, 1,(*DspCableInfo)},//第三层>>DspUserInfo的展开 ........... { 9, 0, 0, 1,(*DspSetVoltLevel)}//第三层>>DspServiceInfo的展 开 .......... };
void GetKeyInput(void) { uchar KeyValue; KeyValue=P1&0x07; //去掉高5bit delay(50000); switch(KeyValue) { case 1: //回车键,找出新的菜单状态编号 { KeyFuncIndex=KeyTab[KeyFuncIndex].KeyCrState; break; } case 2: //向上键,找出新的菜单状态编号 { KeyFuncIndex=KeyTab[KeyFuncIndex].KeyUpState; break; } case 4: //向下键,找出新的菜单状态编号 { KeyFuncIndex=KeyTab[KeyFuncIndex].KeyDnState; break; } default: //按键错误的处理 ...... break; } KeyFuncPtr=KeyTab[KeyFuncIndex].CurrentOperate; (*KeyFuncPtr)();//执行当前按键的操作 } //其中KeyTab的设计颇费尽心机
|