多语言展示
当前在线:1605今日阅读:27今日分享:41

单片机中建立菜单的一种方法

利用结构体建立单片机菜单的方法
方法/步骤
1

新建一个结构体,需要什么按键,加入对应内容。typedef struct{    unsigned char current;    unsigned char up;    unsigned char down;    unsigned char enter;    ussigned char back;    void (*current_operation)();} key_table;

2

定义。菜单的按键连接。key_table const table[7]={   {0,1,1,1,1,(*fun0)},   {1,(*fun1)},   {2,(*fun2)},   {3,(*fun3)},    {4,(*fun4)},   {5,(*fun5)},      {6,(*fun6)},     };

3

定义变量INT8U  func_index=0;void (*current_operation_index)();

4

主菜单中的while(1){        key_temp=keyscan(key());           switch(key_temp)        {            case 10:func_index=table[func_index].enter;break;            case 11:showtime();func_index=table[func_index].back;break;               default:func_index=table[func_index].current;break;        }  current_operation_index=table[func_index].current_operation;(*current_operation_index)();}

5

注意:在CVAVR或某些编译器中:不需要星号。如下。key_table const table[7]={   {0,1,1,1,6,(fun0)},   {1,1,2,2,0,(fun1)},   {2,1,3,3,1,(fun2)},   {3,1,4,4,2,(fun3)},    {4,1,5,5,3,(fun4)},   {5,1,6,6,4,(fun5)},      {6,1,0,0,5,(fun6)},     };

推荐信息