Can't read button state (PIC)

Thread Starter

Владимир Чижиумов

Joined Aug 31, 2019
8
Hi. I ve’got a little (not) misunderstanding of my code. All I had to do was follow a button, “jump over” its ringing and see if it’s pressed. I wrote this code in button library:

Code:
typedef struct{
     uint8_t  Index;
     uint8_t  Counter;
     bool     WasPressed;
     bool     IsBeingProcceed;
     bool     IsShortPress;
     bool     IsLongPress;
} Button;


   void ReadButton(Button btn){ 
    //btn.WasPressed=false;
    //ButtonsPort = 0;
    ButtonsPort = ~PORTC;
 
      if ( (ButtonsPort & (1<<btn.Index)) && (btn.WasPressed==false) )
      {
        btn.Counter++;
        if(btn.Counter > 20 )
        {
            btn.WasPressed = true;
            btn.Counter = 45;
        }
      }
  
    if( !(ButtonsPort & (1<<btn.Index)) && (btn.WasPressed==true) )
    {
        btn.WasPressed=false;
        btn.Counter = 0;
        btn.IsBeingProceed = false;
    }    
}

bool IsButtonPressed(Button btn)
{
    if (btn.WasPressed == true && btn.IsBeingProceed != btn.WasPressed)
    {
        btn.IsBeingProceed = true;
        return true;
    }
    else
        return false;
}

And this in main:

Code:
void TMR0_DefaultInterruptHandler(void){
  
    ReadButton(Minus);
    if( IsButtonPressed(Minus) )
    {
        v2+=5;
        v1=v2-43;
    }  
}
When I press the button, it works just fine:
upload_2019-8-31_13-3-13.png

But…Here we go. When I hold it and it goes inside if (new interruption), the counter is not ‘1’, but ‘0’. Why? Code itself didn’t make it zero (Button is still being held).

- MPLab X IDE v5.25;
- XC 8 v2.10
- PIC16f1788;
- PICkit v3.
 
Top