473,408 Members | 1,743 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,408 software developers and data experts.

how to detect the push button

7
Hi..
I'm new in programming microcontroller in c. And I was given an assignment to write a simple program to turn 'ON' and "OFF' the LED by pressing push buttons. The microcontroller i'm using is PIC18F4620. And i'll need 1 LED and 2 push buttons to program this. I have already tried to write the program but it doesnt work. Here is my program:

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 press(void);
  12. void on(void);
  13. void off(void);
  14.  
  15. void main(void)
  16. {
  17.              TRISA = 0;                        //set Port A(LED) as output
  18.              PORTAbits.RA0 = 0;        //reset LED
  19.  
  20.              while(btn_on != btn_off)        //wait for btn press
  21.  
  22.     if (btn_on = 1)        //btn on pressed
  23.     on();
  24.  
  25.     if (btn_off = 1)    //btn off pressed
  26.     off();
  27. }
  28.  
  29.     void on(void)
  30.     {
  31.               while(!btn_on);        //wait for btn(RB4) released
  32.               PORTAbits.RA0 = 0x0F;        //on LED
  33.     }
  34.  
  35.     void off(void)
  36.     {
  37.               while(!btn_off);        //wait for btn(RB5) released
  38.               PORTAbits.RA0 = 0x00;        //off LED
  39.     }
  40.  
the underline statement is the one i not sure whether is correct or not. I have being working on this for a week. But still cant get the program run correctly. Hope to get some guide. Thank.
Jun 18 '07 #1
4 8183
Can you explain a bit your code with comment ?
Jun 18 '07 #2
ilikepython
844 Expert 512MB
Hi..
I'm new in programming microcontroller in c. And I was given an assignment to write a simple program to turn 'ON' and "OFF' the LED by pressing push buttons. The microcontroller i'm using is PIC18F4620. And i'll need 1 LED and 2 push buttons to program this. I have already tried to write the program but it doesnt work. Here is my program:

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 press(void);
  12. void on(void);
  13. void off(void);
  14.  
  15. void main(void)
  16. {
  17.              TRISA = 0;                        //set Port A(LED) as output
  18.              PORTAbits.RA0 = 0;        //reset LED
  19.  
  20.              while(btn_on != btn_off)        //wait for btn press
  21.  
  22.     if (btn_on = 1)        //btn on pressed
  23.     on();
  24.  
  25.     if (btn_off = 1)    //btn off pressed
  26.     off();
  27. }
  28.  
  29.     void on(void)
  30.     {
  31.               while(!btn_on);        //wait for btn(RB4) released
  32.               PORTAbits.RA0 = 0x0F;        //on LED
  33.     }
  34.  
  35.     void off(void)
  36.     {
  37.               while(!btn_off);        //wait for btn(RB5) released
  38.               PORTAbits.RA0 = 0x00;        //off LED
  39.     }
  40.  
the underline statement is the one i not sure whether is correct or not. I have being working on this for a week. But still cant get the program run correctly. Hope to get some guide. Thank.
Also, you have several errors in there. Do you mean something like this:
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 press(void);
  12. void on(void);
  13. void off(void);
  14.  
  15. void main(void)
  16. {
  17.     TRISA = 0;                        //set Port A(LED) as output
  18.     PORTAbits.RA0 = 0;        //reset LED
  19.  
  20.     while(btn_on != btn_off)                  //wait for btn press
  21.     {
  22.         if (btn_on == 1)        //btn on pressed
  23.             on();
  24.  
  25.         if (btn_off == 1)                   //btn off pressed
  26.             off();
  27.     }
  28. }
  29.  
  30.  
  31. void on(void)
  32. {
  33.     while(!btn_on)             //wait for btn(RB4) released
  34.     {
  35.         PORTAbits.RA0 = 0x0F;        //on LED
  36.     }
  37. }
  38.  
  39. void off(void)
  40. {
  41.     while(!btn_off)                                         //wait for btn(RB5) released
  42.     {
  43.         PORTAbits.RA0 = 0x00;        //off LED
  44.     }
  45. }
  46.  
Jun 18 '07 #3
shir
7
i counldn't see much change..
Jun 19 '07 #4
ilikepython
844 Expert 512MB
i counldn't see much change..
I changed the "=" (assignement) to "==" (boolean equailty) and I added braces to your while loops. Also, I indented and put the code in code tags to make it easier to read.
Jun 19 '07 #5

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

Similar topics

0
by: bala murugan via .NET 247 | last post by:
(Type your message here) -------------------------------- From: bala murugan hi, i am using DataGrid in my project , so, i would like to change the DataGrid Edit Button Properities to Push...
0
by: ar | last post by:
Hello, In IE I disable "Automatic prompting for file downloads" which causes the IE information bar to show up when I try to push a file download from an iframe. I want to keep this behaviour. ...
0
by: Ryan Liu | last post by:
Hi, Can someone tell me how to push dropdownarrows by code for a dropdown button so the menuitems can show up? The button itself does nothing, I am avoiding telling my user "Hey, you...
2
by: Chris Sharman | last post by:
I found a use for a (push) button the other day - having never wanted it before. I used button rather than input type=button, because I wanted to use an image for it. The html4 spec doesn't say...
4
by: John | last post by:
I have a form with 5 buttons, each of them executing code and showing a different report. I would like to add a 6th button 'total' that opens all 5 reports in a row (next report opens when other is...
21
by: Bob Darlington | last post by:
I have a routine which checks idle time and opens a pop up screen to advise users before shutting them down. The pop up has an option for the user to keep the application open, in which case the...
1
by: Jon Todd | last post by:
Happy holidays all! I'm developing an application and, as a relative newbie to Python, have come across a stumbling block with Tkinter. I'd like to have a button that when pressed executes a...
4
by: wqmmnm | last post by:
How can I detect when a user presses buttons on a php site
5
by: andersond | last post by:
I probably didn't ask the question right; but, I have a webpage where agents are entering information about trucks they want insured. I want to break out of a function when a button push occurs. ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.