detecting pushbutton

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2007
Posts: 2
Reputation: laugh is an unknown quantity at this point 
Solved Threads: 0
laugh laugh is offline Offline
Newbie Poster

detecting pushbutton

 
0
  #1
Jun 19th, 2007
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..

#include <p18f4620.h>

#pragma config OSC = HS                              
#pragma config WDT = OFF
#pragma config LVP = OFF

#define btn_on     PORTBbits.RB4
#define btn_off     PORTBbits.RB5
#define led           PORTAbits.RA0

void on(void);
void off(void);
void Delay(void);

void main(void)
{
TRISA = 0;                          //set Port A(LED) as output
PORTAbits.RA0 = 0;             //reset LED
Rpt:
    if (btn_on || btn_off)        //wait for btn press
    {   
              if (btn_on == 1)     //btn on pressed 
              {
                      Delay();     //This is the real trick, Debounce the input!!
                      if (!btn_on)      //btn on still pressed?
                          goto Rpt;     //No
                      on();
              }
               if (btn_off == 1)      //btn off pressed
              {
                      Delay();      //This is the real trick, Debounce the input!!
                      if (!btn_off)      //btn off still pressed?
                          goto Rpt;     //No
                      off();
              }
}
goto Rpt;
} //end main

    void on(void)
    {
Rpt_on:
       while(btn_on);             //wait for btn(RB4) released 
        Delay();                     //debounce
        if (btn_on)                  //btn on still released?
            goto Rpt_on;            //No
       PORTAbits.RA0 = 0x0F;        //on LED
    } //end on

    void off(void)
    {
Rpt_off:
       while(btn_off);           //wait for btn(RB5) released 
        Delay();                   //debounce
        if (btn_off)                //btn off still released?
            goto Rpt_off;         //No
       PORTAbits.RA0 = 0x00;      //off LED
    } //end off

void Delay(void)
{
int i;
for(i=0; i<2048; i++);
}
Last edited by laugh; Jun 19th, 2007 at 4:49 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: detecting pushbutton

 
0
  #2
Jun 19th, 2007
1. Don't use goto, language provides so many other better/safer/understandable ways.
2. Seems like PORTAbits.RAXX is an int/short instead of a boolean. I would suggest:
A) Trace out their values.
B) See how to check such flags. I remember there was some thread where I described how bitwise operations are done for exactly this purpose.
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: detecting pushbutton

 
0
  #3
Jun 19th, 2007
Consider writing a version which runs on your PC first, to get all of the logic sorted out.

With practice, you can do this well enough that the amount of work you need to do to get it to also do the same thing on your target is pretty minimal. If the host works, and the target doesn't, and the number of changes are localised, it really narrows down the stuff you have to look at.

Eg.
  1. #if defined(HOST)
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #else /* HOST */
  7.  
  8. #include <p18f4620.h>
  9.  
  10. #pragma config OSC = HS
  11. #pragma config WDT = OFF
  12. #pragma config LVP = OFF
  13.  
  14. #define btn_on PORTBbits.RB4
  15. #define btn_off PORTBbits.RB5
  16. #define led PORTAbits.RA0
  17.  
  18. #endif /* HOST */
  19.  
  20. void led( int state );
  21. int pressed( int button, int state );
  22.  
  23. int main ( ) {
  24. int btn1 = 0, btn2 = 0;
  25.  
  26. #if !defined(HOST)
  27. TRISA = 0; // set Port A(LED) as output
  28. led = 0; // reset LED
  29. #endif
  30.  
  31. for ( ; ; ) {
  32. btn1 = pressed( 1, btn1 );
  33. btn2 = pressed( 2, btn2 );
  34. if ( btn1 && !btn2 ) {
  35. led( 1 );
  36. } else
  37. if ( btn2 && !btn1 ) {
  38. led( 0 );
  39. }
  40. }
  41. }
  42.  
  43. void led ( int state ) {
  44. #if defined(HOST)
  45. printf("The LED state is %d\n", state );
  46. #else /* HOST */
  47. if ( state == 1 ) {
  48. led = 0x0F;
  49. } else {
  50. led = 0x00;
  51. }
  52. #endif /* HOST */
  53. }
  54.  
  55. int pressed ( int button, int state ) {
  56. #if defined(HOST)
  57. char buff[100];
  58. printf( "Button %d is currently in state %d; 1=Press, 0=Release > ",
  59. button, state );
  60. fflush(stdout);
  61. fgets( buff, sizeof buff, stdin );
  62. sscanf( buff, "%d", &state );
  63. return state;
  64. #else /* HOST */
  65. if ( button == 1 ) {
  66. return btn_on;
  67. } else {
  68. return btn_off;
  69. }
  70. #endif /* HOST */
  71. }
Compiling on the host for example,
  1. $ gcc -DHOST foo.c
  2. $ ./a.exe
  3. Button 1 is currently in state 0; 1=Press, 0=Release > 0
  4. Button 2 is currently in state 0; 1=Press, 0=Release > 0
  5. Button 1 is currently in state 0; 1=Press, 0=Release > 1
  6. Button 2 is currently in state 0; 1=Press, 0=Release > 0
  7. The LED state is 1
  8. Button 1 is currently in state 1; 1=Press, 0=Release > 1
  9. Button 2 is currently in state 0; 1=Press, 0=Release > 0
  10. The LED state is 1
  11. Button 1 is currently in state 1; 1=Press, 0=Release > 0
  12. Button 2 is currently in state 0; 1=Press, 0=Release > 0
  13. Button 1 is currently in state 0; 1=Press, 0=Release > 0
  14. Button 2 is currently in state 0; 1=Press, 0=Release > 1
  15. The LED state is 0
  16. Button 1 is currently in state 0; 1=Press, 0=Release > 0
  17. Button 2 is currently in state 1; 1=Press, 0=Release > 0
  18. Button 1 is currently in state 0; 1=Press, 0=Release >
When you compile the same code for the target, it won't have "-DHOST" so you'll get all the code which reads switches directly and which writes to the LED directly. Assuming you're happy with the logic of how it worked on the host, then it should do the same on the target.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC