Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   how to detect the push button (http://www.go4expert.com/forums/detect-push-button-t4733/)

shir 18Jun2007 08:54

how to detect the push button
 
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:


Code:

#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 press(void);
void on(void);
void off(void);
 
void main(void)
{
            TRISA = 0;                                  //set Port A(LED) as output
            PORTAbits.RA0 = 0;                    //reset LED
   
            while(btn_on != btn_off)        //wait for btn press
   
            if (btn_on = 1)                          //btn on pressed
            on();
 
            if (btn_off = 1)    //btn off pressed
            off();
}
 
    void on(void)
    {
              while(btn_on);                      //wait for btn(RB4) released
              PORTAbits.RA0 = 0x0F;                  //on LED
    }
   
    void off(void)
    {
            while(btn_off);                  //wait for btn(RB5) released
              PORTAbits.RA0 = 0x00;              //off LED
    }

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.

DaWei 18Jun2007 16:27

Re: how to detect the push button
 
Don't compare btn_on to btn-off, they're not the same. Read the state of the button and save it. Then read the state of the button and compare it to the saved state to detect a change. On a change, perform your action and save the new state.

shir 19Jun2007 07:09

Re: how to detect the push button
 
how do i read the state of the button and save it..??

DaWei 19Jun2007 18:36

Re: how to detect the push button
 
You're the one with the specification, so I just have to guess. It looks like (from your code) the switch is a single pole, double throw with the poles wired between pins 4 and 5 of port B and common tied to either ground or V++. Actually, I'd build it so that it was a SPST just wired between the two pins.

Presume that pin 4 is ground when the switch is pressed and V++ when it isn't. A normal switch is going to bounce for a while when it makes contact. I'd check pin 4 to see if it was ground. If it was, I'd check again something like 20 ms later. If it was still ground I'd set a variable to say that it was pressed, and turn on the LED. I would then check it repeatedly to see when it went to V++ (switches don't usually bounce on the break, only on the make). When it did, I'd set the LED off.

If your system does more than monitor the switch, then you'll want a non-blocking operation. This means making the check when you receive an appropriate timer interrupt or a hardware interrupt tied to the switch pin.

Again, this is just generic. You're the one with the parts and their specs.


All times are GMT +5.5. The time now is 03:54.