Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

mikro c button function code is not working

Status
Not open for further replies.
Hi;
i am using four buttons with my PIC16F877A microcontroller. i am using button function if( Button(&PORTD,0,100,0)) in order to detect Logic 0 by a keypress but it is not detecting it. here is my code:

Code:
// INPUTS
#define enter PORTD.F0
#define reset PORTD.F1
#define up PORTD.F2
#define down PORTD.F3

//CONSTANTS
#define ms 100   // for button debounce

int scroll_up( int , int );
int scroll_down( int , int );

void main()
{
    unsigned int i=0;  //i=loops
 // INPUTS:
  TRISD.F0=1;    // ENTER
  TRISD.F1=1;   // RESET
  TRISD.F2=1;    // UP
  TRISD.F3=1;    // DOWN
  TRISA = 0xff;     // port A as input

 while(1)
  {



                    if(Button(&PORTD,0,ms,0))  //when Enter is pressed
                     {
                          int itrue=1;
                          x=0;
                          Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                          Lcd_Cmd(_LCD_CURSOR_OFF);
                          Lcd_Out(1,1,CopyConst2Ram(msg,ep));   //enter passward
                          Delay_ms(t);
                             while(Button(&PORTD,0,ms,0)) // press enter
                             {    x++;
                               Lcd_Out(2,x,CopyConst2Ram(msg,asteric));  //*

                                 if(x==4)
                                 {
                                   Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                   Lcd_Cmd(_LCD_CURSOR_OFF);
                                   Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                   Lcd_Out(1,1,CopyConst2Ram(msg,pw));   //please wait
                                   Delay_ms(1000);

                                   Lcd_Cmd(_LCD_CLEAR);
                                   Lcd_Out(1,1,CopyConst2Ram(msg,gwt));  // Generator warm up time

                                   inc=0;  //for selection of variable
                                   itrue=1;   // Entring and Exiting main menu
                                   opt=1;    // option b/w selection number or variable

                                             while(itrue)
                                             {
                                                  if(Button(&PORTD,0,ms,0))  // pressing enter to enter time
                                                  {
                                                   opt=0;
                                                   Lcd_Out(2,1,CopyConst2Ram(msg,sp));
                                                  }


                                                  if(Button(&PORTD,1,ms,0)) //press reset to exit
                                                  itrue=0;

                                                  if(Button(&PORTD,2,ms,0))  //up
                                                  inc=scroll_up(opt,inc);

                                                  if(Button(&PORTD,3,ms,0))   // down
                                                  inc=scroll_down(opt,inc);
                                             }

                                              x++;
                                 }   //if
                             }    //while
                          Delay_ms(1000);
                     }

  }    // while

}      // main

please tell me where i am going wrong in Button function. please don't care of lcd or scroll functions code missing; but i think i am missing some thing in button function. i am just having the problem in button function. my hardware is functioning well, means no problem in there in soldering or wiring connections.
waiting for your kind help, and any help will be much appreciated.

Thanking you in anticipation

Regards
Qaisar Azeemi
 
(Button(&PORTD,0,ms,0))
The button function above will monitor the PortD.0 with 100ms key debouncing and for the transition for 1-->0.
It is perfectly fine, if what you mean is like above.
 
(Button(&PORTD,0,ms,0))
The button function above will monitor the PortD.0 with 100ms key debouncing and for the transition for 1-->0.
It is perfectly fine, if what you mean is like above.

acctually when i press the push button my hardware does not detect it........... my hardware is ok... so the problem is in code. should the button function be defined before main function?
 
Do you have a pullup resistor? Is the PORTD pin set to be an INPUT?

Using a voltmeter, does the voltage on the PIC input pin go from 5v to 0v when the button is pressed?
 
Do you have a pullup resistor? Is the PORTD pin set to be an INPUT?

Using a voltmeter, does the voltage on the PIC input pin go from 5v to 0v when the button is pressed?

Answer for all above Questions is ........ YES ......... :)
 
Last edited:
Code:
if((!But1) && (!But2))

what about this? :rolleyes:

Hi ese2709... Its not the reading of the port that Qaisar is having problems with... Its the function supplied by MikroC , There seems to be an internal issue.

Qaisar!! try to mess about with the debounce delay.... Try different values.... the constant "ms" may be causing the problem...
 
should the button function be defined before main function?
Not required because it is in the library of MikroC.
But yes Ian says may be try to play ms value of debouncing.May be that is causing the problem.
 
Last edited:
how to declear port D of pic16f877a as input??? kindly guide me about this.... i have decleard it as :

Code:
#define enter PORTD.F0
AND 

TRISD.F0=1;

IS this a true declearation???
 
Thank you very much DJ, Ian Rogers and all. the problem was in my software. i have reinstalled the software and the problem is solved:) .. now it is compiling the true hex file. i am again very thankful to you for your kind help :)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top