Connect with us

PIC18F4550 - Buttons and LCD

Discussion in 'Microcontrollers, Programming and IoT' started by Bellon, Nov 1, 2015.

  1. Bellon

    Bellon

    10
    0
    Sep 20, 2015
    Hello!
    I need to schedule four buttons with the following functions "back" , " OK" , " up " and " down " . I have this code here but found it difficult to compile in mplab and also program along with the messages that I want to appear on my LCD (16x2) . I was trying to use vectors to display the phrases in the LCD , but it's getting so big that I'm losing myself easily. So I decided to delete that part of the code and only left the "skeleton" of it.
    I appreciate if you could help me on this, the question of the buttons and how to put a menu to appear on the LCD with this code . I'm kind stuck and what I found on the Internet did not help much .

    I'm using a pic18f4550.

    All variables that start with "cult" are the options. When they are selected via the "ok" button , will show the information, " inf ".
    I put everything in office , but probably the part of the options do not need.

    Back -> RB1
    OK -> RB0
    UP-> RB2
    Down-> RB3

    Code:
    #include <xc.h>
    #include <stdio.h>
    #include "lcd.h"
    #ifndef _XTAL_FREQ
    #define _XTAL_FREQ 16000000
    #endif
    
    //filter keyboard
    #define MAX_FILTER_CNT     250
    
    unsigned char Key1Cnt = 0;
    unsigned char Key2Cnt = 0;
    unsigned char Key3Cnt = 0;
    unsigned char Key4Cnt = 0;
    
    unsigned char Key1 = 0;
    unsigned char Key2 = 0;
    unsigned char Key3 = 0;
    unsigned char Key4 = 0;
    
    if (Key1  != RB0)
    {
    Key1Cnt++;
    
    if(Key1Cnt > MAX_FILTER_CNT )
    {
        Key1Cnt  = 0;
        Key1  = RB0;
    }
    }
    
    else
    {
        Key1Cnt  = 0;
    }
    
    if (Key2  != RB1)
    {
    Key2Cnt++;
    
    if(Key2Cnt > MAX_FILTER_CNT )
    {
        Key2Cnt  = 0;
        Key2  = RB1;
    }
    }
    
    else
    {
        Key2Cnt  = 0;
    }
    
    if (Key3  != RB2)
    {
    Key3Cnt++;
    
    if(Key3Cnt > MAX_FILTER_CNT )
    {
        Key3Cnt  = 0;
        Key3  = RB2;
    }
    }
    
    else
    {
        Key3Cnt  = 0;
    }
    
    if (Key4  != RB3)
    {
    Key4Cnt++;
    
    if(Key4Cnt > MAX_FILTER_CNT )
    {
        Key4Cnt  = 0;
        Key4  = RB3;
    }
    }
    
    else
    {
        Key4Cnt  = 0;
    }
    
    void inicio(){
        lcd_clear();
        printf("Control system");
        printf("greenhouse");
    }
    
    void insistema(){  
        lcd_clear();
        printf("1 \n");    
        printf("2 \n"); 
                                   
    }
    
    void informacoes(){         //shows a list of options
                               
        var=1;
    }
    
    //options
    void cult0(){
    lcd_clear();
        printf("op1");
    }
    void cult1(){
    lcd_clear();
        printf("op2");
    }
    void cult2(){
    lcd_clear();
        printf("op3 ");
    }
    void cult3(){
    lcd_clear();
        printf("op4");
    }
    void cult4(){
    lcd_clear();
        printf("op5");
    }
    void cult5(){
    lcd_clear();
        printf("op6");
    }
    void cult6(){
    lcd_clear();
        printf("op7");
    }
    void cult7(){
    lcd_clear();
        printf("op8");
    }
    
    void cult8(){
    lcd_clear();
        printf("op9");
    }
    
    void cult9(){
    lcd_clear();
        printf("op10");
    }
    
    void cult10(){
    lcd_clear();
        printf("op11");
    }
    
    void cult11(){
    lcd_clear();
        printf("op12");
    }
    
    //information about each option 
    //(It needs more than four lines for each information option)
    void inf0(){
    lcd_clear();
        printf("inf1 ....");
    }
    void inf1(){
    lcd_clear();
        printf("inf2 .....");
    }
    void inf2(){
    lcd_clear();
        printf("inf3 .....");
    }
    void inf3(){
    lcd_clear();
        printf("inf4 ......");
    }
    void inf4(){
    lcd_clear();
        printf("inf5  ......");
    }
    void inf5(){
    lcd_clear();
        printf("inf6 ......");
    }
    void inf6(){
    lcd_clear();
        printf("inf7 ......");
    }
    void inf7(){
    lcd_clear();
        printf("inf8 .....");
    }
    
    void inf8(){
    lcd_clear();
        printf("inf9 .....");
    }
    
    void inf9(){
    lcd_clear();
        printf("inf10 ......");
    }
    
    void inf10(){
    lcd_clear();
        printf("inf11 .....");
    }
    
    void inf11(){
    lcd_clear();
        printf("inf12 ......");
    }
    
    
    
    int main(void){
       
        TRISD=0x00;
        TRISB=0x0F;
        ADCON1=0XFF;
        CMCON=0X07;
        PORTD=0;
       
        for(aux=0; aux<200;aux++) __delay_ms(10);
        lcd_init();
       
        printf("Control system"); 
        printf("greenhouse");
       
        while(1)
        {
           
        }
    }
     
  2. Bellon

    Bellon

    10
    0
    Sep 20, 2015
    More about the program:
    User will have four options buttons: back, ok, up and down. Through these buttons, the user will be able to read the information on the LCD and select them. My idea was not to leave the message automatically appearing, but that the user had control when it was necessary to go up or down the screen to read the information.
     
  3. BobK

    BobK

    7,599
    1,641
    Jan 5, 2010
    I cannot make sense of either your description or your code.

    Please explain what you are trying to do in clear language.

    For example:

    I have 4 buttons, labeled UP, DOWN, OK and BACK.

    The LCD displays <whatever >

    When I push the UP button <what happens>
    When I push the DOWN button <what happens>
    When I push the OK button <what happens>
    When I push the BACK button <what happens>

    If two buttons are pressed at once <what happens>

    Then you have to break up the program into smaller tasks.

    First you must write a loop that will check the four buttons. This loop must handle debouncing, since a single press of a button will toggle the value multiple times within a short period (about 10ms).

    Then you must add in the code to perform the actions indicated by the buttons.

    Bob
     
    Jack41 likes this.
  4. Jack41

    Jack41

    5
    0
    Nov 2, 2015
    Sorry, I will try to explain better.
    The program begins with a simple menu with two options: Option (a) and option (b).
    If you press the "OK" button, the option(a) is selected, and then will open a screen with a two-line sentence.
    To select the option (b), you must use the "down" button and then "ok". Then opens a menu with eleven options. Each option will occupy two lines of LCD (16x2).
    So, for example, to select option two(2), you must press the "down" button and then "OK" . Each of these eleven options, when selected, will give information about five rows (five text lines, more or less).
    And when, for example, you want to return to the eleven options menu, just press the "back" button.
    And if he needs to read the options or information that he already passed with the "down" button, just hit "up" button.
     
  5. BobK

    BobK

    7,599
    1,641
    Jan 5, 2010
    Okay, that makes a little more sense.

    I would suggest that you start by writing a function that gets the next button press. I.e., when I call the function it will wait until some button is pressed and return a code for that button (1-4) for example.

    Then make your main function loop, calling this function and putting a message on the screen telling which button was pressed and how many times it has been pressed, like this:

    DOWN button pressed N times.

    This will allow you to make sure this function is working properly.

    Next, I would make a function called "main_menu" to keep track of which option is selected and display that on the screen, handling the UP and DOWN buttons and looks for the OK button. What happens on the BACK button (I would suggest go back to the top of the option list)

    When you see an OK button, then call a function to display the text for that option. This can be a single function that takes an array of pointers to strings for the lines of selected option. Inside this function, you can look for UP and DOWN to scroll through the text and BACK to return to the main menu.

    The main function then looks like initializations plus a call to the main_menu function.

    I hope this gets you headed in the right direction.

    Bob
     
    Jack41 likes this.
  6. Jack41

    Jack41

    5
    0
    Nov 2, 2015
    That's exactly what I needed. Thank you! I will follow his instructions
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-