Problem with menu + buttons + lcd

Asked

Viewed 758 times

2

Hello.

I’m having trouble making a program in C language with buttons, lcd (16x2) and a PIC18F4550.

Below, I put the description of the program and the code I have so far, a keyboard filter and some basic pic settings.

I was trying to use multiple vectors to display the two initial options and then functions for each line, but it was getting too big and confusing. And besides, when I pressed other sequences of buttons the program understood as something else and went to another screen that was not what I wanted.

So I thought of this keyboard filter, but it remains to implement it with the lcd options and everything else.

Is it really necessary to use multiple vectors? I need to use a character vector or what?

Program description:

The program starts with a simple menu with two options:

  • Option (a) and,
  • option (b)

If I press the "OK" button, the option (a) will be selected and then will open a screen with a two-line phrase.

To select the option (b) it is necessary to use the button "go down" and then "OK". Then a menu with eleven options will be opened. Each option will occupy two LCD lines (the LCD is 16x2).

So, for example, to select option two (2) you need to press the "down" button and then "OK".

Each of these eleven options, when selected, will open information of about five lines (a text of five lines, more or less).

And when, for example, the user wants to return options in the menu of selections of the eleven options just press the button "back".

And if he needs to read the options or information that he has already passed with the "down" button, just press "up".

  • (OK) - RB0;
  • (Back) - RB1;
  • (Up) - RB2;
  • (Descend) - RB3.

Code:

#include <xc.h>
#include <stdio.h>
#include "lcd.h"
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 16000000
#endif

//filtro teclado
#define MAX_FILTER_CNT     250

unsigned char Tecla1Cnt = 0;
unsigned char Tecla2Cnt = 0;
unsigned char Tecla3Cnt = 0;
unsigned char Tecla4Cnt = 0;

unsigned char Tecla1 = 0;
unsigned char Tecla2 = 0;
unsigned char Tecla3 = 0;
unsigned char Tecla4 = 0;

if (Tecla1 != RB0)
{
    Tecla1Cnt++;

    if (Tecla1Cnt > MAX_FILTER_CNT)
    {
        Tecla1Cnt  = 0;
        Tecla1  = RB0;
    } 
}
else
{
    Tecla1Cnt  = 0;
}

if (Tecla2 != RB1)
{
    Tecla2Cnt++;

    if (Tecla2Cnt > MAX_FILTER_CNT )
    {
        Tecla2Cnt  = 0;
        Tecla2  = RB1;
    } 
}
else
{
    Tecla2Cnt  = 0;
}

if (Tecla3 != RB2)
{
    Tecla3Cnt++;

    if (Tecla3Cnt > MAX_FILTER_CNT )
    {
        Tecla3Cnt  = 0;
        Tecla3  = RB2;
    } 
}
else
{
    Tecla3Cnt  = 0;
}

if (Tecla4  != RB3)
{
    Tecla4Cnt++;

    if(Tecla4Cnt > MAX_FILTER_CNT )
    {
        Tecla4Cnt  = 0;
        Tecla4  = RB3;
    } 
}
else
{
    Tecla4Cnt  = 0;
}

int main(void){

    TRISD = 0x00;
    TRISB = 0x0F;
    ADCON1 = 0XFF;
    CMCON = 0X07;
    T1CON = 0b11111001;
    PORTD = 0;
    for(aux = 0; aux < 100; aux++) 
        __delay_ms(10);
    lcd_init();

    while(1)
    {
        //manipulação dos botões e opções
    }
  • Hard to answer because your question is confused and so is the code.

1 answer

1

Good night, mate. The microcontroller PIC is wonderful to work with, but you need to know a little bit about the C language and some specific features of the compile you choose. I particularly like the compiler PCW of CCS, and I’ve noticed that you’re using the MIKRO C, right? I know little of this compiler and its syntax and features, but I made a code quickly that is very generic and that can help you a lot with menus and functions. I took the liberty of removing the button come back, because I found it kind of useless and in the place it is as in option 'a'. Of course if you want, you can implement the code with the use of it.

I haven’t had time to test with a PIC, but I think it will work. Soon I edit (if possible edit the answer, I am new here) and put the result with the PIC and LCD. Follow the code below:

#include <bibliotecas_gerais.h>

//definição dos pinos
#define subir    pino_desejado
#define descer   pino_desejado
#define ok       pino_desejado

int opcoes[][] ={{0,1},
                 {0,1,2,3,4,5,6,7,8,9,10,11}
                 };
int main(){
    /*
        configurações gerais e declarações gerais do microcontrolador...
    */
    printf("/fA. menu ");
    printf("B. menu ");
    if(opmenu1()==0){
        menu_tela_a();
    }else if(opmenu1()==1){
        opmenuB();
    }
}

int opmenu1(){
    int menu1=0
    int okk=0;
    do{
        if(menu1>1){
            menu1=1;
        }else if(menu1<0){
            menu1=0;
        }
       if(subir) {
          menu1--;
       }else if(descer){
          menu1++;
       }else if(ok){
           okk=1
       }
    }while(okk!=1);
    return menu1;
}

int opmenuB(){
    int menu2=0;
    int okk=0;
    do{
        if(menu2>11){
            menu2=11;
        }else if(menu2<0){
            menu2=0;
        }
       if(subir) {
          menu2--;
       }else if(descer){
          menu2++;
       }else if(ok){
           okk=1
       }
       menu_tela_b(okk,menu2);
    }while(okk!=1);
    return menu2;
}

void menu_tela_b(int operacao, int opc){
    do{
        switch (opcoes[menu1][opc]){
            case 0:
            case 1:
                printf("/f0. opcao ");
                printf("1. opcao ");
                break;
            case 2:
            case 3:
                printf("/f2. opcao ");
                printf("3. opcao ");
                break;
            case 4:
            case 5:
                printf("/f4. opcao ");
                printf("5. opcao ");
                break;
            case 6:
            case 7:
                printf("/f6. opcao ");
                printf("7. opcao ");
                break;
            case 8:
            case 9:
                printf("/f8. opcao ");
                printf("9. opcao ");
                break;
            case 10:
            case 11:
                printf("/f10. opcao ");
                printf("11. opcao ");
                break;
            default:
                break;
        }
    }while(operacao!=1);
}

void menu_tela_a()}{
    do{
        printf("frase....");
        printf("continuacao");
    }while(opmenu1()!=1);
}

Any questions, I’m at your disposal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.