NO

Author Topic: how to detect push button  (Read 10315 times)

shirley

  • Guest
how to detect push button
« on: June 18, 2007, 04:01:15 AM »
i need to write a program using c langauge for microcontroller PIC18F4620. the requirement are, turning ON & OFF a LED using 2 push button. here is my program:

#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
      }


i have been workin on this for a week, but still cant work. i need some guide. thank you
« Last Edit: June 18, 2007, 05:39:28 AM by shirley »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: how to detect push button
« Reply #1 on: June 18, 2007, 11:00:11 AM »
You know, I suppose that this is not a general 'C' programming site, and also that PellesC doesn't helps with PIC controllers.
Anyway and just for this time I wanna help you.
Code: [Select]
#include <p18f4620.h>

#pragma config OSC = HS  //Are you using crystal or ceramic resonator for oscillator?
#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 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 - NOTE: comparation operator is double equal!!!!!!!
      {
          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;
}

      void on(void)
      {
Rpt_on:
         while(btn_on); //wait for btn(RB4) released - NOTE:you have to stay here while buttorn is pressed!
          Delay();        //debounce
          if (btn_on)     //btn on still released?
              goto Rpt_on;  //No
         PORTAbits.RA0 = 0x0F;      //on LED
      }
   
      void off(void)
      {
Rpt_off:
         while(btn_off);         //wait for btn(RB5) released - See before!
          Delay();        //debounce
          if (btn_off)     //btn off still released?
              goto Rpt_off;  //No
         PORTAbits.RA0 = 0x00;      //off LED
      }

void Delay(void)
{
   int i;

   for(i=0; i<2048; i++);
}
« Last Edit: June 18, 2007, 11:17:45 AM by frankie »
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

shirley

  • Guest
Re: how to detect push button
« Reply #2 on: June 18, 2007, 11:42:57 AM »
i suppose is crystal resonator.

:D and thank a lot for the help. sorry i wasn't that this site doesnt deal with PIC microcontroller. but any way thank again for the help..

shirley

  • Guest
Re: how to detect push button
« Reply #3 on: June 19, 2007, 03:35:10 AM »
thank for the help.
i have tested your code, but i press the button, the LED still did not light up. ???
i still dont understand.. where is problem..??

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: how to detect push button
« Reply #4 on: June 19, 2007, 09:24:39 AM »
Are you sure that the hardware is working?
There many things to check, with a microcontroller you have to test the software but also the hardware.
Anyway on software side maybe the delay constant 2048 is too large and you have to reduce it (try beginning with 100).
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

shirley

  • Guest
Re: how to detect push button
« Reply #5 on: June 19, 2007, 10:14:58 AM »
the hardware is working..
i try to run the program, but it keep looping >>>if (btn_on || btn_off) then go back to the same statement. even when i press the button it keep looping the same statement.. ??? ???

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: how to detect push button
« Reply #6 on: June 19, 2007, 05:12:40 PM »
As far as I can understand your input never get true.
If you are running an in circuit emulator try to check the state of input, or with a multimeter measuring between the input pin and the ground you should get 0V and Vcc respectively with pushbutton released and pressed.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide