473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

detecting pushbutton

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
9 2131
arunmib
104 100+
Did you check the status of btn_on and btn_off before the if loop?
Jun 19 '07 #2
shir
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
104 100+
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
shir
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
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
104 100+
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
shir
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
shir
7
do i need to set PORT B as input..?
Jun 20 '07 #9
arunmib
104 100+
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

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

Similar topics

2
by: Tom Van den Brandt | last post by:
Hello, Is there anyone using pyQt who can tell me if I can set/change the internal id of a pushbutton myself ? I can't find anything in the docs... Tnx, -- Tom Van den Brandt I try...
5
by: Maziar Aflatoun | last post by:
Hi everyone, Does anyone know how I can add a CSS (Ex. button1 ) to the following Edit button is ASP .NET? <asp:EditCommandColumn ButtonType="PushButton" EditText="Edit" CancelText="Edit"...
1
by: Mark | last post by:
I have two buttons in a DataGrid. If I leave the ButtonType = "LinkButton", the EditCommand and DeleteCommand events fire fine. If I change th ButtonType = "PushButton", the events do not fire. ...
2
by: Nick Gilbert | last post by:
We have a Datagrid which contains a template column with a button in it, as well as an actual button column. If either of the buttons are clicked, the ItemCommand event does not fire. If...
1
by: angus | last post by:
Dear All, I am using VB.net It is found that the <asp:EditCommandColumn/> wouldn't fired an event for OnEditCommand If pushbutton has been used: that is:
6
by: Tim Meagher | last post by:
Can anyone help me figure out how to apply a stylesheet to a pushbutton defined in the asp:BoundColumn or asp:EditCommandColumn elements of a datagrid?
2
by: John Mason | last post by:
Hi, I have a Datagrid control with an edit command column, which has a PushButton ButtonType. I am trying to apply formatting to the plain grey button, to make it look more appealing on...
1
by: serge calderara | last post by:
Dear all, If I used the following code on a datagrid object <Columns> <asp:ButtonColumn Text="Events" ButtonType=LinkButton CommandName="ShowEvent"> </asp:ButtonColumn> </Columns>
0
by: DotQuery | last post by:
Hello ASP.NET team : I have a user control that be added into page at runtime , in a postback event handler . I save the information to view state , and load the user control again in...
1
by: Blasting Cap | last post by:
I am having trouble changing the font for a PushButton control in a datagrid button column. I have seen several posts refer to styles and simple changes to the HTML for font changes but most of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.