Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Navigation Menu data Accessing Logic ?

Status
Not open for further replies.

thannara123

Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,576
Helped
122
Reputation
244
Reaction score
114
Trophy points
1,353
Location
India
Activity points
10,345
Button Key Navigation Menu data value Accessing Logic ?

In a part of my project [avr gcc ,atmega16-controller]
I am struggling get the updated values from the navigation key while updated values on the LCD.



images.jpg

The UP and Down Key increments or decrements the values of corresponding variable . [ variables is Hour,Minute,second]
The right and Left key changes the position of the LCD as well as the variables ie hour to Minute or Second.
And the Okay button exit the while loop and save the variables to a structure

How to do this an efficient way ?


Made the following code but not works correctly any good logic or correction .

Code:
void UP_Down_Keyvalue(struct menu *s1ptr,int i,int b)   

 {  int temp=0,ch,position=0,lower=0,upper=12;    

        int array_temp[11];        
             LCD_GoToXY(1,i);
            LCD_Printf("%d:%d:%d:%s",s1ptr->Hour,s1ptr->Minute,s1ptr->Second,"AM");

  while(OK_S!=64)
             {            
                          LCD_GoToXY(1,i);
                          LCD_DisplayNumber(10,temp,2);
                         if(UP_S)
                              {  while(UP_S);
                                    if(temp==upper)
                                     temp=-1;                                     
                                     temp++;                 
                               }
                            if(DOWN_S) // down
                               {  while(DOWN_S);
                                  if(temp==0)
                                     temp=13;    
                                     --temp;
                                }
                           exit2:
                if(RIGHT_S||LEFT_S)           
                    {        
                        array_temp[i];
                              if(RIGHT_S)                             
                              { while(RIGHT_S);
                                 if(i==11)goto exit1;            // check is it last point 10 ie 11                      
                                  i +=3;
                                  temp=0;
                              }
                              exit1:
                              if (LEFT_S)
                              { while(LEFT_S);                                 
                                  if(i==2) goto exit2;
                                  i -=3;
                                temp=0;                              
                              }
                              
                             // LCD_Printf("%d",i);
                             //DELAY_ms(10000);
                              
                    }    
                    s1ptr->Hour= array_temp[1];
                    s1ptr->Minute=array_temp[3];
                    s1ptr->Second=array_temp[5];
                
                                               
                   
                      }       
     
 }
 
Last edited:

Re: Button Key Navigation Menu data value Accessing Logic ?

You need to use a main counter and a sun counters for each sub menu.
 

Your code written in inline style is quite confusing, don't have separating tasks into specific functions, has the while statement that inserts a pause in the program run, besides containing goto instructions which are not recommended in C, breaking the structure of the program. Anyway, answering the question: A feasible option in a simple case like this can be creating a 'state machine' with the switch-case statements, where depending on the previous state, the detection of events from each button switch exectution to the appropriate state.
 
Your code written in inline style is quite confusing, don't have separating tasks into specific functions, has the while statement that inserts a pause in the program run, besides containing goto instructions which are not recommended in C, breaking the structure of the program. Anyway, answering the question: A feasible option in a simple case like this can be creating a 'state machine' with the switch-case statements, where depending on the previous state, the detection of events from each button switch exectution to the appropriate state.
The while loop in the program which break the program enne press ok button
 

This is very poor practice to use key scanning functions together with display handling which is high time consuming. There is no filtering alghorighm, flowchart itself is unclear and unreadable. I whould say, that this is a good example 'how the code should not looks like'.
 

This is very poor practice to use key scanning functions together with display handling which is high time consuming. There is no filtering alghorighm, flowchart itself is unclear and unreadable. I whould say, that this is a good example 'how the code should not looks like'.
Please am asking a good logic or sample reference or an algorithm
 

Re: Button Key Navigation Menu data value Accessing Logic ?

How to do this an efficient way ?

You have already got some tips, so I don't think anyone will implement them for you, we hope that you will do it by yourself. Just summarizing in a more direct way - if you have not read anything of what was said: Create separate functions, one for each task (display, keyboard, time, ...) and another function to control the change of state, and manage them allocating in time the execution of each one. Instead of stamping the routines straigh inline, think about dealing with flags.
 
Re: Button Key Navigation Menu data value Accessing Logic ?

You have already got some tips, so I don't think anyone will implement them for you, we hope that you will do it by yourself. Just summarizing in a more direct way - if you have not read anything of what was said: Create separate functions, one for each task (display, keyboard, time, ...) and another function to control the change of state, and manage them allocating in time the execution of each one. Instead of stamping the routines straigh inline, think about dealing with flags.

Sir I didn't understand what you mention inline style.
I rewritten the function as mentioned earlier ,yet i didn't tested it.
is it look ok? is it time consuming ,optimized ,
is it a better logic .

Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[CODE]
 
int navigation_key(int key,int key_max_value,int key_lower_value )
{ 
    int step = 3;// two digti and ":"
    int key_value;
    while(!key)
    {
    
    int last_postion  = 11  // for LCD Last Postion for display
    int first_position = 2   // for LCD first position for display
    
    swithc(key)
    {
    case 3:   // for UP key
    if(position == key_max_value)
        key_value -= key_lower_value; // resetting key_value  
     key_value++;
     return key_value; // return to display
     
    
    case 4:  // for down key
    if(key_value == lower_value)
        key_value = key_max_value;
    key_value--;
    return key_value;
    
    case 2:  // Right key
    if(position == last_postion)
        position -=step;          // if lcd position maximum backward one position
    position++;
    return position;
    
    
    case 1: // left Key
    if(position == first_position)
        position +=step;           // if lcd position first forward to next 
    position--;
    return position;
    
    }[/CODE]

 

Re: Button Key Navigation Menu data value Accessing Logic ?

sorry, position variable is not returning it changed as a pointer .
 
Last edited:

This wont work:

Code:
 while(!key)
...
    {   
    swithc(key)
    case 4: ...
    case 3: ...
    case 2: ...
    case 1: ...

You are toggling the key variable, whereas should manage it within the switch...case structure. Moreover, terminating each case with return statement instead of using break, although in theory should work, it is not usual, so you will be likely playng with the compiler behaviour. Just to remind: The state variable I meant is not the key pressed, but rather the Menu current position.
 
besides containing goto instructions which are not recommended in C, breaking the structure of the program.

Why not recommended goto structure , I have seen peoples used somewhere.

- - - Updated - - -

The following program works well for me
Code:
/* Function Key Value For UP_Down Key */    int UP_Down_Keyvalue(int upper,unsigned int lower,char position)
    {   int ch;
        int key_value = 0;
        do{ LCD_GoToXY(1,position);
            LCD_DisplayNumber(10,key_value,2);
            ch = Key_pressed();
            if(ch==3)
             { if(key_value==upper)
                { key_value = lower; goto exit1;}
             key_value++;
             }                  
            if(ch==4)            
              { if(key_value==lower)
                 { key_value = upper;goto exit1;}
               key_value--;
              }    
              exit1:    continue;          
          }while(ch!=5);    
        
        
            return key_value;
        }

function call

Code:
temp =UP_Down_Keyvalue(59,0,2);// uper value ,lower value ,lcd display position

what is the draw back i want to make the button library for up and down key.
 

Hi,

Consider to use a debounce technique..

Currently I only see level sensitive key_press processing, but you need to process key_down events (edges) only.
But maybe I overlooked this.

Klaus
 
Hi,

Consider to use a debounce technique..

Currently I only see level sensitive key_press processing, but you need to process key_down events (edges) only.
But maybe I overlooked this.

Klaus

I will replace it with switch bounce after checking with hardware (by capacitor) solution.
The pressed function is following
Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
int Key_pressed(void)
{
while(1){
if (LEFT_S) ms_delay()50; { while(LEFT_S);return 1; }
if (RIGHT_S)ms_delay()50;{ while(RIGHT_S);return 2; }
if (UP_S) ms_delay()50; { while(UP_S); return 3; }
if (DOWN_S) ms_delay()50;{ while(DOWN_S);return 4 ; }
if (OK_S)ms_delay()50; { while(OK_S);return 5 ; }
else return 0;
}
}
 

You can use the code posted in post #14. It has debounce implemented.
 

You can use the code posted in post #14. It has debounce implemented.

It is an unusual way to debounce keyboard, totally unsuitable.
This code will block execution while awaiting button released.

- - - Updated - - -

Why not recommended goto structure , I have seen peoples used somewhere

It is just a matter of good practices on structured programming, since in this way your routines will no longer have well defined entry and exit points turning it hard to debug; which does not prevent the use of such a resource, but I doubt that people actually use it when strictly necessary.
 

Clear WDT and timeout feature has to be implemented while waiting at the while() loop.
 
It is just a matter of good practices on structured programming, since in this way your routines will no longer have well defined entry and exit points turning it hard to debug; which does not prevent the use of such a resource, but I doubt that people actually use it when strictly necessary.
But I didn't have enough logic .I am child in this field but ,
I haven't live without electronics , it's my passion.
 

Clear WDT and timeout feature has to be implemented while waiting at the while() loop.

Do not reinforce a bad recommendation, the above routine is awful, it blocks any other task of the program.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top