Hints on structure of a menu system?

Discussion in 'Programmer's Corner' started by lowprofile, Aug 3, 2012.

  1. lowprofile

    Thread Starter Member

    Oct 31, 2011
    Hello all,

    I'm taking a course on microcontroller interfacing, and we are programming PIC16F877A's with CCS C.

    I want to make a program slightly more advanced than the lab topics, but I have been pondering how to structure it for days, and I would like some tips or advice.

    I have an LCD, and 3 buttons: up, down, and select. There is a temperature sensor, which I have interfaced successfully. The LCD has a backlight. I want to have a menu system where by pressing the select button, it changes menus, and pressing up/down alters the values on the current menu. Through the menu one could enable/disable the backlight, and change the displayed temperature to C or F, and a few other things.

    I am thinking that each press of the "select" button increments a "selected_menu" variable, and each time the main loop runs, it checks (through a SWITCH statement) which menu it's in, and displays the appropriate information. Also in this loop, it would check the state of various flags, like BACKLIGHT_ON and UNIT and take the appropriate course of action.

    I know my question sounds sort of vague, it is because I am a beginner programmer. But what sort of structure should I use for this? Am I on the right track by using the menu to set variables, and then checking these variables in a loop?

    I have heard people mention "finite state machines" for menus, but I am not sure if this would work for me, as I think there would have to be many "modes" (eg a mode where backlight=off AND unit=C, basically you'd need a mode for each possible combination of these settings).

    So, to recap, what advice can you offer? I will do my own investigation if you can give me some clues.

    Thanks for your time.
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    So you want to do a menu on your LCD? That's what I do, it works nice, tough I like the bigger displays so I think 4 lines of 20 characters is pretty small.

    I will make a menu that looks something line this:
    Code ( (Unknown Language)):
    3.  > TASK ONE
    4.    TASK TWO
    5.    TASK THREE
    I also use 5 buttons to control the display in a pattern I noticed on my Cable TV remote: Up/Down/Left/Right buttons with a central Enter button. You don't need all the buttons but it is a nice intuitive useful pattern.

    For the above menu the Up & Down buttons select which task, indicated by the ">" arrow. Hitting the Enter key performs the task.

    You do need a state variable to know where the ">" is (I call it the carrot). How you perform the task depends on how you want your menu organized, as you can do a sub call (which will return you back to the same menu at the same place) or as jump if the next item does not have to "pop" back up the menu chain.

    Also note when using a call type structure you need to redraw the display when you return.

    Here is a menu where having all 5 buttons is nice
    Code ( (Unknown Language)):
    4.    NUMBER 123456
    5.                ^
    Left & Right pick which digit can be changed, and up & down change it. Enter selects it.
  3. MrChips


    Oct 2, 2009
    I prefer minimal solutions. If the LCD is only 1-line x 16 and you have only three buttons "<" ">" and "SELECT", let's see what you can do.

    I don't like "state machines". I would prefer one routine to handle all variables.

    Pressing SELECT will cycle through an index number, referring to which variable is being modified, index = 0 can be the main screen.

    Pressing "<" or ">" will decrement or increment the value of the variable. Each variable as a MIN and MAX value. For a logical variable, MIN and MAX are 0 and 1 respectively.

    So you need an array of the number of variables x three values, VALUE, MIN, MAX.
    If all your variables are logical you can omit the MIN & MAX values.