By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,905 Members | 2,083 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

detecting pushbutton

P: 7
hi.. i need help with this program. this is a program to turn 'ON' and 'OFF' 1 LED using 2 push buttons.
the underline statement suppose to wait for button press. but it is alway false even i have press the button. is there any one who can tell me what's wrong with the statement.. thank alot..

Expand|Select|Wrap|Line Numbers
  1. #include <p18f4620.h>
  2.  
  3. #pragma config OSC = HS                              
  4. #pragma config WDT = OFF
  5. #pragma config LVP = OFF
  6.  
  7. #define btn_on     PORTBbits.RB4
  8. #define btn_off     PORTBbits.RB5
  9. #define led           PORTAbits.RA0
  10.  
  11. void on(void);
  12. void off(void);
  13. void Delay(void);
  14.  
  15. void main(void)
  16. {
  17. TRISA = 0;                             //set Port A(LED) as output
  18. PORTAbits.RA0 = 0;               //reset LED
  19. Rpt:
  20.     if (btn_on || btn_off)               //wait for btn press
  21.     {   
  22.               if (btn_on == 1)         //btn on pressed 
  23.               {
  24.                       Delay();            //This is the real trick, Debounce the input!!
  25.                       if (!btn_on)        //btn on still pressed?
  26.                           goto Rpt;      //No
  27.                       on();
  28.               }
  29.                if (btn_off == 1)        //btn off pressed
  30.               {
  31.                       Delay();           //This is the real trick, Debounce the input!!
  32.                       if (!btn_off)       //btn off still pressed?
  33.                           goto Rpt;    //No
  34.                       off();
  35.               }
  36. }
  37. goto Rpt;
  38. } //end main
  39.  
  40.     void on(void)
  41.     {
  42. Rpt_on:
  43.        while(btn_on);            //wait for btn(RB4) released 
  44.         Delay();                    //debounce
  45.         if (btn_on)                 //btn on still released?
  46.             goto Rpt_on;        //No
  47.        PORTAbits.RA0 = 0x0F;       //on LED
  48.     } //end on
  49.  
  50.     void off(void)
  51.     {
  52. Rpt_off:
  53.        while(btn_off);            //wait for btn(RB5) released 
  54.         Delay();                   //debounce
  55.         if (btn_off)                //btn off still released?
  56.             goto Rpt_off;        //No
  57.        PORTAbits.RA0 = 0x00;       //off LED
  58.     } //end off
  59.  
  60. void Delay(void)
  61. {
  62. int i;
  63. for(i=0; i<2048; i++);
  64. }
  65.  
Jun 19 '07 #1
Share this Question
Share on Google+
9 Replies


arunmib
100+
P: 104
Did you check the status of btn_on and btn_off before the if loop?
Jun 19 '07 #2

P: 7
Did you check the status of btn_on and btn_off before the if loop?
what do i need to check for..?
Jun 19 '07 #3

arunmib
100+
P: 104
what do i need to check for..?
Your point was the if loop fails whatever be the states of btn_on and btn_off right..So just thought if both of them are '0's then the loop will fail...

Or did i get something completely wrong, if so enlighten me...
Jun 19 '07 #4

P: 7
Your point was the if loop fails whatever be the states of btn_on and btn_off right..So just thought if both of them are '0's then the loop will fail...

Or did i get something completely wrong, if so enlighten me...

if both '0's become false then it will loop agian and again untill 1 button is pressed. this part is right, but the problem is when i press 1 button it still become false. it did not execute the next statement.
Jun 19 '07 #5

P: 6
hi,

give some debounce delay before you check the push button.



debounce delay : its just some 100ms delay


just write one 100ms delay function


call this function before you check the key status






try this ,you will get
Jun 19 '07 #6

arunmib
100+
P: 104
Ok I will put the question this way, how certain are you that when you press BUTTON 1 it's corresponding bit in the register is set. Like did you try using the bit value in the register directly in the code or did you probe the signal level using an oscilloscope etc..etc...Because, if my understanding is correct we are conversing based on the assumption that there is no problem in the signal levels or no hardware problems....

Again if I am missing something, then enlighten me....
Jun 19 '07 #7

P: 7
Ok I will put the question this way, how certain are you that when you press BUTTON 1 it's corresponding bit in the register is set.
that i not sure. because when i press nothing happen.

Like did you try using the bit value in the register directly in the code or did you probe the signal level using an oscilloscope etc..etc...
i dont really understand. what should i check for..??
Jun 20 '07 #8

P: 7
do i need to set PORT B as input..?
Jun 20 '07 #9

arunmib
100+
P: 104
that i not sure. because when i press nothing happen.


i dont really understand. what should i check for..??
In simple terms, when you press a button there will be some voltage level variation (for eg : either from 0V to 5V or from 5V to 0V) and this variation will be the one acting as input to your PortB register. If you do not find this variation, then the corresponding 'bit' in the PortB register will be not set (either to '1' or to '0'). This means whatever you do with your code, there will be not output.

The reason I am asking you to do this is, since your code seem fine the problem could be on the hardware side. That is you might not be getting any input in port B. check with your electronics engineer...
Jun 22 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.