# PIC18F4550 - Buttons and LCD

#### Bellon

Joined Sep 19, 2015
9
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.

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");
}

//(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;
CMCON=0X07;
PORTD=0;

for(aux=0; aux<200;aux++) __delay_ms(10);
lcd_init();

printf("Control system");
printf("greenhouse");

while(1)
{

}
}

#### spinnaker

Joined Oct 29, 2009
7,835
Exactly what do you expect all of those printf statements to do?

#### Bellon

Joined Sep 19, 2015
9
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.

#### djsfantasi

Joined Apr 11, 2010
7,566
A read of the code looks like your braces are not matched the way you expect (could be wrong). What are you using as your IDE? Some of them have an auto-format option which indents your code and makes it easier to see where the code segments line up.

Also, where are your functions "cultn" and "infn" are used?

And some of your names, like "insistema" are not in English and hard for me to understand.

#### Bellon

Joined Sep 19, 2015
9
I didn't use the functions yet. I was working with vectors, but it was with some problems, so I need your help, to see if it's necessary use character vector or something like that. "insistema" would be to start the system. I'll put some sensors, and measurements will start after I select for the system start .

#### Bellon

Joined Sep 19, 2015
9
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.

#### spinnaker

Joined Oct 29, 2009
7,835
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.

That isn't an answer to my question.

#### Bellon

Joined Sep 19, 2015
9
But I said.
Do you wonder what's written in the texts ? They are in five lines , nothing too important

#### spinnaker

Joined Oct 29, 2009
7,835
It is a simple question. What are you expecting the printf statements to do?

No offense but if you can't understand simple statements and questions in English then there is no point in continuing unless you can get someone to help you with your posts.

#### Bellon

Joined Sep 19, 2015
9
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.

#### spinnaker

Joined Oct 29, 2009
7,835
That is fine but it still does not answer the question.

What are you expecting the printf statement to do? If you won't answer that then there is no point in continuing.

I feel like I am corresponding to one of those automated systems.

#### Bellon

Joined Sep 19, 2015
9
Unfortunately you are not speaking clearly, so I have no more to do. I'm trying to figure out what to do with this my problem a few days ago, and I'm lost and I'm taking a long time (I'm actually out of time). I just want to know what I need to use that program. I was using vectors for the start and functions for each line options, but the program was getting confused and too big. I just wanted a tip, something like, "you can use "x" to the functions and "y" for the lines. Not to take issue with the selection of buttons, you can make “z” way." Here's the code I had before. I didn’t posted before because it is confusing and I think that would make no sense.

#### Bellon

Joined Sep 19, 2015
9
Code:
#include <xc.h>
#include <stdio.h>
#include "lcd.h"
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 16000000
#endif

char linha0_1[16]="op";
char linha0_2[16]="1";

char linha1_1[16]="op";
char linha1_2[16]="2";

int var=0;

void linha0(){
lcd_clear();
lcd_puts(linha0_1);
printf("\n");
lcd_puts(linha0_2);
}

void linha1(){
lcd_clear();
lcd_puts(linha1_1);
printf("\n");
lcd_puts(linha1_2);
}

void linha2(){
lcd_clear();
lcd_puts(linha2_1);
printf("\n");
lcd_puts(linha2_2);
}

void linha3(){
lcd_clear();
lcd_puts(linha3_1);
printf("\n");
lcd_puts(linha3_2);
}

void inicio(){
lcd_clear();
printf("Sist. de Cont.");
printf("p/ Estufas Dom.");
}

void insistema(){
lcd_clear();
printf("1: \n");
printf("2: \n");
}

void inf(){
var=1;
}

void inf0(){
lcd_clear();
printf("op1");
}
void inf1(){
lcd_clear();
printf("op2");
}
void inf2(){
lcd_clear();
printf("op3");
}
void inf3(){
lcd_clear();
printf("op4");
}
void inf4(){
lcd_clear();
printf("op5");
}

void inf4(){
lcd_clear();
printf("op6");
}

void inf4(){
lcd_clear();
printf("op7");
}

void inf4(){
lcd_clear();
printf("op8");
}

void inf4(){
lcd_clear();
printf("op9");
}

void inf4(){
lcd_clear();
printf("op10");
}

void inf4(){
lcd_clear();
printf("op11");
}

int main(void){
char temp=0, aux;

int ant0,atual0,ant1,atual1,ant2,atual2,ant3,atual3,lin=0;

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

linha1();

while(1){

if(TMR1IF){
TMR1IF=0;
TMR1L=0xB0;
TMR1H=0x3C;

/*inicio();
for(aux=0; aux<200;aux++) __delay_ms(10);
linha1();*/

atual1=RB1;
if(atual1==0&&ant1==1){
lin++;
if(lin>3)lin=3;
if(lin==0){
if(var==1)inf0();
else linha0();
}
if(lin==1){
if(var==1)inf1();
else linha1();
}
if(lin==2){
if(var==1)inf2();
else linha2();
}

}
ant1=atual1;

atual0 = RB0;
if(atual0==0&&ant0==1){
lin--;
if(lin<0)lin=0;
if(lin==0){
if(var==1)inf0();
else linha0();
}
if(lin==1){
if(var==1)inf1();
else linha1();
}
if(lin==2){
if(var==1)inf2();
else linha2();
}

}
ant0=atual0;

atual2=RB2;
if(atual2==0&&ant2==1){

switch(lin){

case 1:
if(var==0){
insistema();
for(aux=0; aux<200;aux++) __delay_ms(10);
linha2();
}
if(var==1){
lcd_clear();
printf("info ");
for(aux=0; aux<200;aux++) __delay_ms(10);

inf1();
}

break;

case 2:
if(var==0){
inf();
inf0();
}

break;

}
}
ant2=atual2;

atual3=RB3;
if(atual3==0&&ant3==1){
var=0;
linha0();

}

ant3=atual3;
}
}
}

#### spinnaker

Joined Oct 29, 2009
7,835
Unfortunately you are not speaking clearly, so I have no more to do. I'm trying to figure out what to do with this my problem a few days ago, and I'm lost and I'm taking a long time (I'm actually out of time). I just want to know what I need to use that program. I was using vectors for the start and functions for each line options, but the program was getting confused and too big. I just wanted a tip, something like, "you can use "x" to the functions and "y" for the lines. Not to take issue with the selection of buttons, you can make “z” way." Here's the code I had before. I didn’t posted before because it is confusing and I think that would make no sense.
I am "speaking" perfectly clearly. It is you that cannot understand.

What is the purpose of the printf statement sin your code? What are you expecting it to do? It's can't get any clearer than that. If you can't undertand a simple qustion such as that then no one is going to be able to help you.

#### atferrari

Joined Jan 6, 2004
4,254
A pergunta dele, é clara demais meu irmao.