Problem with menu + buttons + lcd

2

Hello.

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

Below, I've put the program description 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 I still have to implement it with the lcd options and everything else.

Do I really need to use multiple vectors? Do I need to use a character vector or what?

Description of the program:

The program starts with a simple menu with two options:

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

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

To select option (b) you must use the "down" button and then "OK". Then a menu with eleven options will open. Each option will occupy two LCD lines (LCD is 16x2).

Then, for example, to select option two (2) it is necessary to press the button "down" and then "OK".

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

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

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

  • (OK) - RB0;
  • (Back) - RB1;
  • (Up) - RB2;
  • (Down) - 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
    }
    
asked by anonymous 02.11.2015 / 19:31

1 answer

1

Good evening, mate. The PIC microcontroller is wonderful to work with, but you need to know a little about the C language and some specific features of the build you choose. I particularly like the compiler PCW of CCS , and I noticed that you're using MIKRO C , right? I know little of this compiler and its syntaxes and features, but I made a quick code that is pretty generic and that can help you a lot with menus and functions. I took the liberty of taking the button back, because I found it somewhat useless and in the place it is like option 'a'. Of course if you want, you can implement the code with the use of it.

I have not had time to test with a PIC , but I think it will work. Soon I edit (if possible edit the answer, I'm new here) and put the result with PIC and LCD . Here is the code:

#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 doubts, I am at your disposal.

    
15.04.2016 / 04:47