READING A PUSHBUTTON & display on LCD

Discussion in 'Embedded Systems and Microcontrollers' started by fantabulous68, Sep 9, 2009.

  1. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    51
    4
    I recently got a pikit 2, its the 1st time that im using push button switches.
    im using 4 pushbuttons.
    All i want for now is:
    When i power the circuit the 1st thing that happens is The LCD displays "infrared liquid level detector" and it stays that way always, so the lcd code and initialising is working.

    when i press ENTER/Settings button....i want "enter/settings" to display on the LCD. And that is not happening when i press the button. Please help me solve this problem....i attached a picture of how i connect the switch to the pic.


    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>
    3. #include "pic.h"
    4. #include "delay.h"
    5. #include "math.h"
    6. #include <stdio.h>
    7. #include <stdlib.h>
    8.  
    9.  
    10. char input_sw;
    11. #define HOME_SW RC2             //HOME switch  
    12. #define INCREASE_SW RC3         //INCREASE switch
    13. #define DECREASE_SW RC4         //DECREASE switch
    14. #define ENTERSETTINGS_SW RA4        //ENTERSETTINGS switch
    15.  
    16. void init(void)
    17. {  
    18.  
    19.     OSCCON|=0x60; //set fosc to 4Mhz
    20.     TRISB=0x00;
    21.     TRISC=0xFC;
    22.         TRISA = 0b10000010;     // RA7 high imp, RA3 is serial out,
    23.     ANSEL=0x02;            //set RA1 as analog input for GP2 sensor
    24.     ANSELH=0x00;
    25.     lcd_init(); //call LCD initialisation
    26. }
    27.  
    28.  void read_input_sw(void)
    29. {
    30.  char input_sw2;
    31.  input_sw = ((HOME_SW*8)+(INCREASE_SW*4)+(DECREASE_SW*2)+(ENTERSETTINGS_SW*1)); //create a binary combination of the input switches
    32.  if (input_sw != 0) //check if any of the switches are pressed
    33.     {
    34.      DelayMs(20); //switch debouncing
    35.      input_sw2 = ((HOME_SW*8)+(INCREASE_SW*4)+(DECREASE_SW*2)+(ENTERSETTINGS_SW*1));
    36.      while (input_sw2 != 0) //if a switch is pressed wait for release
    37.             {
    38.              input_sw2 = ((HOME_SW*8)+(INCREASE_SW*4)+(DECREASE_SW*2)+(ENTERSETTINGS_SW*1));
    39.              DelayMs(20); //switch debouncing
    40.             }
    41.      }
    42. }
    43.  
    44.    
    45. [COLOR="Red"]void home_screen(void)[/COLOR]
    46. {
    47.     lcd_clear();
    48.     lcd_goto_L1();
    49.     [COLOR="Red"]lcd_puts("INFRARED LIQUID"); //home screen message (line 1)[/COLOR]
    50.     lcd_goto_L2();
    51. [COLOR="Red"]   lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)[/COLOR]
    52.  
    53.     read_input_sw();
    54.     while(input_sw == 0)
    55.  
    56.         read_input_sw();
    57.         switch(input_sw)
    58.     {
    59.         case 8: break; 
    60.         //  ENTER/SETTINGS  button pressed 
    61.         case 1: {   lcd_clear();
    62.                     lcd_goto_L1();
    63.         [COLOR="Red"]            lcd_puts(" ENTER/SETTINGS ");[/COLOR]
    64.                     DelayS(1);
    65.                     lcd_clear();
    66.                    
    67.                     break;
    68.                 }
    69.                   default : break; //should any abnormalties occur
    70.     }
    71.     return;
    72. }
    73.  
    74.  
    75. void main(void)
    76. {
    77.     init(); // initialise I/O ports, LCD
    78.     while (1)
    79.     {
    80.     home_screen();
    81.    
    82.     }
    83.      
    84. }
    85.    
     
    Last edited: Sep 9, 2009
  2. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    You appear to be writing the 'Home Screen' display to the LCD on EVERY pass through the loop, rather than just when it should be on that specific screen?

    Try using a variable to set a screen number, then display the appropriate screen text to match the variable. Clear it to Zero for a home or startup screen during the init stage.

    The button presses then change the screen number or do other functions, depending on the present screen number (ie. softkeys, the functions match the displayed button titles).
     
  3. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    51
    4
    thanks Mr Jenkins. Yeah you right, it keeps looping
     
Loading...