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

 时间:2026-04-25 08:45:31

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)},     

};

  • Altium\AD16如何设定文件原点和测量
  • 【protel99】怎样使菜单汉化
  • 如何安装IAR软件?
  • 如何查看protues的版本号?
  • 实例讲解 Altium Designer 创建多子件元件
  • 热门搜索
    四川警察学院怎么样 鱼的生活习性 苏卡达陆龟怎么养 dm是什么意思 idp留学怎么样 电脑音响没声音怎么办 武汉纺织大学外经贸学院怎么样 京东白条是什么意思 如何修改电脑ip地址 电脑显示亮度怎么调