altering lcd menus with pushbutton

Discussion in 'General Electronics Chat' started by sryzdn, Aug 11, 2014.

  1. sryzdn

    Thread Starter New Member

    Jun 1, 2014
    23
    0
    Hi,

    I have an ATmega32 interfaced with an lcd on port B. I want to switch between the lcd menus upon pushing a push button.

    First, it should show XXXX and upon pressing the button it should switch to YYYY.
    Actually menu X and Y each represent a lot of other things. But first I want to learn how to do this switching part.

    The C program I have written for that is as below:

    Code ( (Unknown Language)):
    1. while (1){
    2.       lcd_gotoxy(0,0);
    3.       lcd_putsf("XXXX");
    4.      
    5.       if (PINA.0==0){
    6.       delay_ms(250);
    7.       lcd_clear();
    8.       lcd_gotoxy(0,0);
    9.       lcd_putsf("YYYY");}}
    at first XXXX is shown with no problem, but of course as I push the button, the second menu only remains for 250ms. How can I switch between the menus using a push button?
     
  2. pwdixon

    Member

    Oct 11, 2012
    488
    56
    Use a variable to store the current display state set by the button press then test that in the loop before displaying the screen data.
     
    sryzdn likes this.
  3. sryzdn

    Thread Starter New Member

    Jun 1, 2014
    23
    0
    Hi,

    I have encountered another problem with this circuit. I'd be very grateful to receive your help.

    1. Having two push buttons interfaced to portC, I am getting a lot of ripples on the lcd screen. Why?

    2. The C program I have written for it is as below, but I am getting an undesired L=001 before I set "L" by pushing the button.

    3. Also, ""while (PINC.0/1==0);"" loops do not work.

    Code ( (Unknown Language)):
    1. while (1)
    2.       {
    3.       if(set==0){
    4.       lcd_gotoxy(0,0);
    5.       lcd_putsf("1) TEMP");
    6.  
    7.       lcd_gotoxy(0,1);
    8.       lcd_putsf("2) SET"); }
    9.      
    10.       if (PINC.1==0){
    11.       set=2;}
    12.      
    13.       if (set==2){
    14.       lcd_clear();
    15.       if (PINC.0==0){
    16.       delay_ms(250);
    17.       while (PINC.0==0);
    18.       i++;}
    19.       sprintf(d, "H=%3.3d", i);
    20.       lcd_gotoxy(0,1);
    21.       lcd_puts(d);
    22.      
    23.       if (PINC.1==0){
    24.       delay_ms(250);
    25.       while (PINC.1==0);
    26.       j++;}
    27.       sprintf(e, "L=%3.3d", j);
    28.       lcd_gotoxy(8,1);
    29.       lcd_puts(e);}}
     
  4. pwdixon

    Member

    Oct 11, 2012
    488
    56
    Now you have attached an image of a simulation all bets are off. There are many times when people post a problem and it turns out to be a problem with the simulation itself. Are you trying this on actual hardware at all?
     
  5. pwdixon

    Member

    Oct 11, 2012
    488
    56
    Of course it could be something to do with no pullups on the switches, but it does depend on how you simulation handles the original processor setup and whether there are internal pullups on the processor in any case.
     
  6. sryzdn

    Thread Starter New Member

    Jun 1, 2014
    23
    0
    I am working on an actual setup simultaneously, but I thought it is better to send the picture of its simulation. The problem with the ripples was solved. It was due to the "lcd_clear();"

    But the rest still has remained. PortC pull up is ok.
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
     
    sryzdn likes this.
  8. sryzdn

    Thread Starter New Member

    Jun 1, 2014
    23
    0
    I'm sorry for neglecting the good coding in my previous posts. I have completed the code and the project as follows and I have problems which I would be grateful if you kindly help me find a solution:

    As you can see this is supposed to be a temperature monitor.
    The code has the following problems:

    1. In the third menu, "L" would start from 1 as it is shown in the schematic. (the same in the hardware). It should start from zero.

    2. The back button does not direct one step at a time and it always takes me to the "first menu"!!!!.

    3. I don't know why "while (PINC.1/0==0);" loops do not work in the third menu.

    I will be really thankful if you kindly take a look at my code and images and let me know where I'm going wrong.

    Code ( (Unknown Language)):
    1.  
    2. while (1)
    3.       {
    4. [B][COLOR=blue]//#######################################//[/COLOR][/B]
    5.           [B] //The first menu:[/B]
    6.       while (set==0)
    7.      {
    8.            lcd_gotoxy(0,0);
    9.            lcd_putsf("1) TEMP");
    10.            lcd_gotoxy(0,1);
    11.            lcd_putsf("2) SET");
    12.      
    13.    [B]//By pressing the first button, we should go to the second menu:[/B]
    14.            if (PINC.0==0)
    15.           {
    16.                 lcd_clear();
    17.                 set=1;
    18.                 back=1;
    19.           }
    20.            
    21.   [B] //By the second button we should go to the third menu:[/B]
    22.            if (PINC.1==0)
    23.           {  
    24.                 lcd_clear();
    25.                 set=2;
    26.                 back=2;
    27.           }
    28.      }
    29. [B][COLOR=blue]//#######################################//[/COLOR][/B]      
    30.       [B]//The second menu:[/B]
    31. if (set==1 && back==1)
    32. {
    33.    while (set==1)
    34.   {
    35.           lcd_clear();
    36.           lcd_gotoxy(0,0);
    37.      
    38. [B]     //Reading the temperature[/B] [B]from LM35[/B]:
    39.           v=read_adc(0);
    40.           v=(v*500.0)/1023.0;
    41.           sprintf(d, "TEMP=%2.2fC",v);
    42.           lcd_gotoxy(0,0);
    43.           lcd_puts(d);      
    44.  
    45.           lcd_gotoxy(0,1);
    46.           sprintf(d, "H=%3.3d", i);
    47.           lcd_gotoxy(0,1);
    48.           lcd_puts(d);
    49.      
    50.           lcd_gotoxy(8,1);
    51.           sprintf(e, "L=%3.3d", j);
    52.           lcd_gotoxy(8,1);
    53.           lcd_puts(e);
    54.  
    55. [B]//the third button should return us to the first menu:[/B]      
    56.          if (PINC.2==0){
    57.          lcd_clear();
    58.          back=back-1;
    59.          set=set-1;}
    60.  
    61. [B]//the second button should take us to the third menu:[/B]      
    62.          if (PINC.1==0)
    63.        {
    64.         lcd_clear();
    65.         set=2;
    66.         back=2;
    67.        }
    68.    }  
    69. }
    70. [B][COLOR=blue]//#######################################//[/COLOR][/B]
    71.   [B]\\The third menu:[/B]
    72.  while (set==2 || back==2)
    73. {
    74.       if (PINC.0==0)
    75.      {
    76.       delay_ms(250);
    77.       while (PINC.0==0);
    78.       i++;
    79.       }
    80.       sprintf(d, "H=%3.3d", i);
    81.       lcd_gotoxy(0,1);
    82.       lcd_puts(d);
    83.      
    84.       if (PINC.1==0)
    85.      {
    86.       delay_ms(250);
    87.       while (PINC.1==0);
    88.       j++;
    89.      }
    90.       sprintf(e, "L=%3.3d", j);
    91.       lcd_gotoxy(8,1);
    92.       lcd_puts(e);
    93.  
    94. [B]//Comparing the temp with the limits of H and L to set the LEDs:[/B]
    95.       if (v>=i)
    96.      {
    97.       PIND.0=1;
    98.      }
    99.      
    100.       if (v<=j)
    101.      {
    102.       PIND.1=1;
    103.      }
    104.      
    105.       if (v>j && v<j)
    106.      {
    107.       PIND.0=0;
    108.       PIND.1=0;
    109.      }
    110. [B][COLOR=blue]//#######################################//[/COLOR][/B]      
    111.       if (PINC.2==0)
    112.      {
    113.       back=1;
    114.       set=1;
    115.       lcd_clear();}
    116.       }
    117.  
    118.       }
    119. }
     
Loading...