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

how to detect the push button

P: 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
Share this Question
Share on Google+
4 Replies


P: 55
Can you explain a bit your code with comment ?
Jun 18 '07 #2

ilikepython
Expert 100+
P: 844
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

P: 7
i counldn't see much change..
Jun 19 '07 #4

ilikepython
Expert 100+
P: 844
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

Post your reply

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