How to control the push button?

Status
Not open for further replies.

bas_90

Member level 1
Joined
Mar 21, 2013
Messages
35
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,503
my project contains a menu in FIRST (On LCD)

for example:

1- Start
2- Setting

how can I make a push button when click on it go to "Start" or click in other push.. go to"setting" and display a nother menu ?!

i using MikroC and PIC 16F877A .. thnx for all
 

Re: Help me to control the push button ...

thnx Peter, but i compiled this code and don't make i want

or i don't understand it ..

i need a simple code to change the words on LCD by push button
 

Re: Help me to control the push button ...

thnx Peter, but i compiled this code and don't make i want

or i don't understand it ..

i need a simple code to change the words on LCD by push button


I use PIC18F45K22 32MHz (4xPLL), on RC0 is one button adn RC1 is second. You can adjust these pins settings to your needs. Code will work on PIC16F877/A uC also.

MikroC Pro Example :

Code:
// Lcd module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;


void main()
{
  Lcd_Init();                                 //LCD display initialization
  Lcd_Cmd(_LCD_CLEAR);                        //Clear LCD screen
  Lcd_Cmd(_LCD_CURSOR_OFF);                   //Turn cursor Off for LCD


  ANSELC.B0 = 0;                              //Configure RC0 pin as digital
  ANSELC.B1 = 0;                              //Configure RC1 pin as digital
  TRISC.B0  = 1;                              //Set RC0 pin as input
  TRISC.B1  = 1;                              //Set RC1 pin as input


  while (1) 
        {

        if (PORTC.B0 = 1) 
              {
              Lcd_Cmd(_LCD_CLEAR);           //Clear LCD screen
              Lcd_Out(1,1,"Main options");
              Lcd_Out(2,1,"First, Second");
              }

        if (PORTC.B1 = 1)
              {
              Lcd_Cmd(_LCD_CLEAR);           //Clear LCD screen
              Lcd_Out(1,1,"Settings menu");
              }

        }

}



Dont forget to include Button and LCD libraries.



Best regards,
Peter

;-)
 

Re: Help me to control the push button ...

)))))))))))))) , thnx alot .. it's working fine :razz:
 

Re: Help me to control the push button ...

can you use goto statement here....?

if( button push)
goto start
 

Re: Help me to control the push button ...

i have a question , how can use this buttons for submenu

example, now the LCD Display
1- Start
2- Setting

and we designs 2 buttons to choose what we want ..

now "2- Setting" display
1- Ch1
2- Ch2

how can use the 2 button for this submenu ... ?!

thnx
 

Re: Help me to control the push button ...

Sorry, any example ?!!

thnx

- - - Updated - - -

MikroC example, i don't understand it too

can u make a simple example ?!
 

Re: Help me to control the push button ...

Okay Sir , thnx alot

i will try ...
 

Re: Help me to control the push button ...

i failed to try it

plz write this example by Switch Statment

Code:
while (1) 
        {

        if (PORTC.B0 = 1) 
              {
              Lcd_Cmd(_LCD_CLEAR);           //Clear LCD screen
              Lcd_Out(1,1,"Main options");
              Lcd_Out(2,1,"First, Second");
              }

        if (PORTC.B1 = 1)
              {
              Lcd_Cmd(_LCD_CLEAR);           //Clear LCD screen
              Lcd_Out(1,1,"Settings menu");
              }

        }
 

Re: Help me to control the push button ...

Hi bus_90,

Here is 2min example :

RC0 is for menu Up.
RC1 is for menu Down.

There is 4 menus in this example.

I try this on PIC18F45K22 at 32MHz and all is ok.
If you have slower clock adjust value in this row :
Delay_ms(300); //Adjust this value if needed


Code:
// Lcd module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

char broj;

void main()
{
  Lcd_Init();                                 //LCD display initialization
  Lcd_Cmd(_LCD_CLEAR);                        //Clear LCD screen
  Lcd_Cmd(_LCD_CURSOR_OFF);                   //Turn cursor Off for LCD


  ANSELC.B0 = 0;                              //Configure RC0 pin as digital
  ANSELC.B1 = 0;                              //Configure RC1 pin as digital
  TRISC.B0  = 1;                              //Set RC0 pin as input
  TRISC.B1  = 1;                              //Set RC1 pin as input

  broj = 0;

  while (1)
        {

        if (PORTC.B0 = 1) broj++;
        if (PORTC.B1 = 1) broj--;

        Delay_ms(300);       //Adjust this value if needed

        switch(broj)
           {
           case 0:
                Lcd_Cmd(_LCD_CLEAR);                        //Clear LCD screen
                Lcd_Out(1,1,"Menu 1");
                break;
           case 1:
                Lcd_Cmd(_LCD_CLEAR);                        //Clear LCD screen
                Lcd_Out(1,1,"Menu 2");
                break;
           case 2:
                Lcd_Cmd(_LCD_CLEAR);                        //Clear LCD screen
                Lcd_Out(1,1,"Menu 3");
                break;
           case 3:
                Lcd_Cmd(_LCD_CLEAR);                        //Clear LCD screen
                Lcd_Out(1,1,"Menu 4");
                break;
           }

        }

}


I go to eat now.

Best regards,
Peter
 

Re: Help me to control the push button ...


Okay , Have a nice TIME

i will try it now
 

Re: Help me to control the push button ...

i working Ok but don't do what i want

because it working sequential command .. and i want to make for example 4 buttons

1 button go to Home (Main Menu)
1 button go to back (Cancel)
1 button go to "Choose 1"
1 button go to "Choose 2"

thnx about yr TIME ..
 

Re: Help me to control the push button ...



Its not problem, but we need additional two buttons.

I will write one example for that.
 

Re: Help me to control the push button ...

Its not problem, but we need additional two buttons.

I will write one example for that.

Ok no problem , wait u

really thnx alot ^_^
 

Re: Help me to control the push button ...

?!!!!!!!!!!
 

Re: Help me to control the push button ...

Today all day I was in a crowd, I've entered the home before 30 min. :|

See this example:

RC0 - change selection
RC1 - choose selected option
RC2 - back to main menu

Note: RC0,RC1,RC2 should be in pull-down.



Code:
// Lcd module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

int select, select_op, selection, submenu;

void main()
{
  Lcd_Init();                                 //LCD display initialization
  Lcd_Cmd(_LCD_CLEAR);                        //Clear LCD screen
  Lcd_Cmd(_LCD_CURSOR_OFF);                   //Turn cursor Off for LCD


  ANSELC = 0;                                 //Configure PortC pins as digital
  TRISC.B0  = 1;                              //Set RC0 pin as input - Selection
  TRISC.B1  = 1;                              //Set RC1 pin as input - Choose selection
  TRISC.B2  = 1;                              //Set RC2 pin as input - Back to Main menu


  select = 1;
  selection = 1;
  submenu = 0;
  select_op = 1;

  //Display main menu
  Lcd_Cmd(_LCD_CLEAR);      //Clear LCD screen
  Lcd_Out(1, 2,"Menu1");
  Lcd_Out(2, 2,"Menu2");
  Lcd_Out(1,10,"Menu3");
  Lcd_Out(2,10,"Menu4");


  while (1)
        {

        if ((PORTC.B0 = 1) && (submenu == 0))              //When press RC0 '>' selecting display options
             {
                switch(selection)
                    {
                    case 1:
                         Lcd_Chr(1,1,'>');
                         selection = 2;
                         select = 1;
                         Lcd_Chr(2,9,' ');
                    break;
                    case 2:
                         Lcd_Chr(2,1,'>');
                         selection = 3;
                         select = 2;
                         Lcd_Chr(1,1,' ');
                    break;
                    case 3:
                         Lcd_Chr(1,9,'>');
                         selection = 4;
                         select = 3;
                         Lcd_Chr(2,1,' ');
                    break;
                    case 4:
                         Lcd_Chr(2,9,'>');
                         selection = 1;
                         select = 4;
                         Lcd_Chr(1,9,' ');
                    break;
                    }
             }


        if (PORTC.B1 = 1)              //When press RC1 - choose selected option/menu
             {
             switch(select)
                {
                case 1:
                   Lcd_Cmd(_LCD_CLEAR);                        //Clear LCD screen
                   Lcd_Out(1, 1,"Menu 1");
                   Lcd_Out(1,10,"Option1");
                   Lcd_Out(2,10,"Option2");
                   submenu = 1;
                break;
                case 2:
                   Lcd_Cmd(_LCD_CLEAR);                        //Clear LCD screen
                   Lcd_Out(1, 1,"Menu 2");
                   Lcd_Out(1,10,"Option1");
                   Lcd_Out(2,10,"Option2");
                   submenu = 1;
                break;
                case 3:
                   Lcd_Cmd(_LCD_CLEAR);                        //Clear LCD screen
                   Lcd_Out(1, 1,"Menu 3");
                   Lcd_Out(1,10,"Option1");
                   Lcd_Out(2,10,"Option2");
                   submenu = 1;
                break;
                case 4:
                   Lcd_Cmd(_LCD_CLEAR);                        //Clear LCD screen
                   Lcd_Out(1, 1,"Menu 4");
                   Lcd_Out(1,10,"Option1");
                   Lcd_Out(2,10,"Option2");
                   submenu = 1;
                break;
                }
             }


        if ((PORTC.B0 = 1) && (submenu == 1))
            {
             switch(select_op)
                 {
                 case 1:
                    Lcd_Chr(1,9,'>');
                    select_op = 2;
                    Lcd_Chr(2,9,' ');
                 break;
                 case 2:
                    Lcd_Chr(2,9,'>');
                    select_op = 1;
                    Lcd_Chr(1,9,' ');
                 break;
                 }
            }






        //RETURN TO MAIN MENU IF RC2 is PRESSED
        if (PORTC.B2 = 1)
             {
             Lcd_Cmd(_LCD_CLEAR);      //Clear LCD screen
             Lcd_Out(1, 2,"Menu1");
             Lcd_Out(2, 2,"Menu2");
             Lcd_Out(1,10,"Menu3");
             Lcd_Out(2,10,"Menu4");
             submenu = 0;
             }



        Delay_ms(300);

        }

}


LCD Screenshots :

When you power device LCD show main menu :



When you press RC0 selection mark is showen '>' :



After proper selection we call selection with RC1 and second menu opens :



Again we use RC0 for selecting options :



.
.
.

When RC2 is pressed we back to main menu :








I write this in hurry, but can be optimized more.



Best regards,
Peter

:wink:

- - - Updated - - -

Tested and works 100% :smile:
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…