[C51 code] Menu function [for LCD1602]

Publisher:幸福家园Latest update time:2016-07-28 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#ifndef __menu_h__

#define __menu_h__
#include "head.h"
#include "LCD1602_2.h"
#include "DS18B20.h"

#define Null 0

/***********************

* Function declaration *

***********************/

void ShowMenu(void);

void Menu_Change(volatile KeyNum);

/***********************

* Key function key macro definition *

***********************/

#define up    1

#define down  2

#define back  4

#define ok    5

/**********************

* Directory structure definition *

**********************/

struct MenuItem

{

   uchar MenuCount; //Number of nodes in the current layer

   uchar *DisplayString; //Menu title

   void (*Subs)(); //Node function

   struct MenuItem *ChildrenMenus; //child nodes

   struct MenuItem *ParentMenus; //parent node

};

/*********************** 
* Call sub-function area * 
***********************/ 
void NullSubs(void) 
{

 }

/***********************

* Structure area *

***********************/
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},

};

 

/***********************

* Global variable declaration area *

***********************/

struct MenuItem (*MenuPoint) = MainMenu; //Structure pointer, after pointing to the structure, the internal function pointer points to the function function

uchar DisplayStart = 0; //The first menu item when displayed

uchar UserChoose = 0; //Menu item selected by the user

uchar DisplayPoint = 0; //display pointer

uchar MaxItems; //Maximum number of menus at the same level

uchar ShowCount=2; //Number of menus displayed on the same screen

/***********************

*Show function area*

***********************/

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;//Upward limit, if you want to roll back, assign the value to MaxItems-1 
    }  
    break;  
    case down:  
    UserChoose ++;  
    if (UserChoose == MaxItems)  
    {  
      UserChoose = MaxItems-1;//Downward limit, if you want to roll back, assign the value to 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) //Only ShowCount rows can be displayed on one screenDisplayStart 
  = UserChoose; 
  else if(UserChoose==1||UserChoose== 3) 
    DisplayStart = UserChoose-1; //The key to scrolling    
    LCDWriteCmd(DispClear); //Clear the LCD screen, modify it according to different LCD functions 
    delay1ms(2); //LCD is a slow device 
    ShowMenu();  

}

#endif

Reference address:[C51 code] Menu function [for LCD1602]

Previous article:[C51 code] LCD12864 header file [upper and lower screen]
Next article:74hc164 driver digital tube program

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号