Hot!Push button states

Author
naeem1234
Super Member
  • Total Posts : 358
  • Reward points : 0
  • Joined: 2015/02/19 06:39:28
  • Location: 0
  • Status: offline
2018/03/05 18:42:47 (permalink)
0

Push button states

Hello everyone
 
My chip is PIC18F46K22 and using XC8 compiler.
 
I am checking the state of a push button after every 100 msec using following code.
void pushbutton(void)
{

    if (PORTEbits.RE0==0U && prev_RE0==1U)
    { // Falling edge
        prev_RE0=0U;
        FP_Ignition_Input=1; // IGNON
    }
    else
    if (PORTEbits.RE0==0U && prev_RE0==0U)
    { // LOW
    }
    else
    if (PORTEbits.RE0==1U && prev_RE0==0U)
    { // Rising edge
        prev_RE0=1U;
        FP_Ignition_Input=0; // IGNOFF
    }
    else
    if (PORTEbits.RE0==1U && prev_RE0==1U)
    { // HIGH
    }
    
}

 
I am trying to implement a state machine for it so that i can detect that the current state of push button has arrived after cycling through the previous state and its not just the same logic level.
 
Please suggest is it correct to do like above?
Your help is highly appreciated.
 
Edit: correction in code for RB1
post edited by naeem1234 - 2018/03/05 19:04:19
#1

14 Replies Related Threads

    qɥb
    Monolothic Member
    • Total Posts : 2621
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: online
    Re: Push button states 2018/03/05 18:45:41 (permalink)
    +1 (1)
    Is there any hardware filtering on the pushbutton signal?
    Without that, pushbuttons tend to "bounce" generating hundreds of rapid "on and off" transitions, which can play havoc with code like that.
    The typical solution is to "debounce" in software, so you only detect a change when it has been stable for several tens of milliseconds.
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #2
    du00000001
    Just Some Member
    • Total Posts : 1875
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Push button states 2018/03/05 18:53:16 (permalink)
    +1 (1)
    While your code might be ok (I didn't check in detail), I personally would recommend to use more curly brackets.
    OK - the one reference to .RB1 might be a typo.
     
    And I cannot see any advantage over simply copying the inverse of RE0 to the FP_ variable.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #3
    naeem1234
    Super Member
    • Total Posts : 358
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: Push button states 2018/03/05 19:07:35 (permalink)
    0
    qɥb
    Is there any hardware filtering on the pushbutton signal?
    Without that, pushbuttons tend to "bounce" generating hundreds of rapid "on and off" transitions, which can play havoc with code like that.
    The typical solution is to "debounce" in software, so you only detect a change when it has been stable for several tens of milliseconds.
     




    As I am pooling the push button after every 100 msec (once in the super loop) so can it can give some de-bounce effect? There is no de-bouncing in the hardware also.
    #4
    naeem1234
    Super Member
    • Total Posts : 358
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: Push button states 2018/03/05 19:09:58 (permalink)
    0
    du00000001
    While your code might be ok (I didn't check in detail), I personally would recommend to use more curly brackets.
    OK - the one reference to .RB1 might be a typo.

     
    Yes that was a typo, now corrected.
     
    du00000001 
    And I cannot see any advantage over simply copying the inverse of RE0 to the FP_ variable.



     
    Can you explain what you mean by inverse of RE0?
    #5
    du00000001
    Just Some Member
    • Total Posts : 1875
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Push button states 2018/03/05 19:53:46 (permalink)
    0
    Your code is basically
    if RE0 == 0 then FP_ = 1
    else FP_ = 0
     
    so - very coarse -  FP_ = !RE0

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #6
    naeem1234
    Super Member
    • Total Posts : 358
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: Push button states 2018/03/13 09:02:04 (permalink)
    0
    du00000001
    Your code is basically
    if RE0 == 0 then FP_ = 1
    else FP_ = 0
     
    so - very coarse -  FP_ = !RE0




    I have now changed my code as following:
    void pushbutton(void)
    {

        if (PORTEbits.RE0==0U && prev_RE0==1U)
        { // Falling edge
            prev_RE0=0U;
        }
        else
        if (PORTEbits.RE0==0U && prev_RE0==0U)
        { // LOW
            FP_Ignition_Input=1; // IGNON
        }
        else
        if (PORTEbits.RE0==1U && prev_RE0==0U)
        { // Rising edge
            prev_RE0=1U;
        }
        else
        if (PORTEbits.RE0==1U && prev_RE0==1U)
        { // HIGH
            FP_Ignition_Input=0; // IGNOFF
        }
        
    }

     
    Is it better than before now? Is it proper state machine implementation to read a push button and to implicitly get rid of the mechanical bouncing problem?
    How can I do better than this to read a push button? On hardware there is a pull up resistor 10 K on the GPIO Pin and the PB is connected between the GPIO Pin and GND.
    #7
    DavidBLit
    Super Member
    • Total Posts : 1548
    • Reward points : 0
    • Joined: 2012/02/18 13:08:48
    • Location: The Land of Confusion
    • Status: offline
    Re: Push button states 2018/03/13 11:40:53 (permalink)
    0
    Study this.

    Yeah, "//Code and stuff".
    #8
    bblessing
    Super Member
    • Total Posts : 675
    • Reward points : 0
    • Joined: 2008/12/04 06:44:21
    • Location: Cincinnati, OH
    • Status: offline
    Re: Push button states 2018/03/13 13:16:54 (permalink)
    0
    if I understand this correctly, pushbutton is called once every 100 ms. It seems that you're first looking for a change and then if things match on the second pass then you change your FP_Ignition_Input variable.

    I would think the following would simplify things:

    <code>
    void pushbutton (void)
    {
    if (PORTEbits.RE0 != prev_RE0)
    prev_RE0 = PORTEbits.RE0;
    else
    FP_Ignition_Input = !PORTEbits.RE0;
    }
    </code>

    Edit: use the tertiary operator and the logic becomes a single line.
    post edited by bblessing - 2018/03/13 13:22:07
    #9
    jack@kksound
    code tags!
    • Total Posts : 2575
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: Push button states 2018/03/13 13:36:00 (permalink)
    +2 (2)
    Edit: use the tertiary operator and the logic becomes a single line.

    tertiary? I think that s/b ternary, right? That's how I remember it being spelled.
    #10
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2586
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Push button states 2018/03/13 15:19:13 (permalink)
    0
    "Unary operators" is the terminology for operators like: + - ! ~ ++ --
    Also address and pointer & *
     
    Try putting a 100nF cap between gnd and the switch output.
    Assuming this chip has Schmitt Trigger inputs then you will not get any bounce.
     
    If this chip has Change Notify you can get the switch status of down and up, even double clicks are possible.  Let the hardware do it for you.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #11
    jack@kksound
    code tags!
    • Total Posts : 2575
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: Push button states 2018/03/13 15:38:10 (permalink)
    0
    "Unary operators" is the terminology for operators like: + - ! ~ ++ --
    Also address and pointer & *

    Yes but I wrote ternary, not unary. Ternary is for the ?: operator. Ternary not tertiary.
    #12
    bblessing
    Super Member
    • Total Posts : 675
    • Reward points : 0
    • Joined: 2008/12/04 06:44:21
    • Location: Cincinnati, OH
    • Status: offline
    Re: Push button states 2018/03/13 15:53:45 (permalink)
    0
    jack@kksound
    "Unary operators" is the terminology for operators like: + - ! ~ ++ --
    Also address and pointer & *
    Yes but I wrote ternary, not unary. Ternary is for the ?: operator. Ternary not tertiary.

    I meant ternary (? with :). Sometimes the brain farts...
    #13
    jack@kksound
    code tags!
    • Total Posts : 2575
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: Push button states 2018/03/13 16:01:10 (permalink)
    0
     Sometimes the brain farts...

    Yep I know that! Tertiary seems reasonable, meaning "third in order or level.", but allas it is not (tertiary does NOT mean "three" - as in "3 operands"). Which is not to say that the suggestion was not good or correct (it is). Smile: Smile
    #14
    DavidBLit
    Super Member
    • Total Posts : 1548
    • Reward points : 0
    • Joined: 2012/02/18 13:08:48
    • Location: The Land of Confusion
    • Status: offline
    Re: Push button states 2018/03/14 06:25:49 (permalink)
    +1 (1)
    bblessing
    jack@kksound
    "Unary operators" is the terminology for operators like: + - ! ~ ++ --
    Also address and pointer & *
    Yes but I wrote ternary, not unary. Ternary is for the ?: operator. Ternary not tertiary.

    I meant ternary (? with :). Sometimes the brain farts...

    ...or Gorts...  wink: wink

    Yeah, "//Code and stuff".
    #15
    Jump to:
    © 2018 APG vNext Commercial Version 4.5