Jump to content

Recommended Posts

Posted (edited)

Hi All,

 

I am trying to use a 4x4 matrix keypad on Orange pi GPIO ,I am connecting the keypad with 8 GPIO's 4 GPIOs for rows, 4 for coloumns ,the problem I am facing is when I try to test the keypad few keys are read only on repeated attempts or pressing. The following is my code. Is this because of Hardware debouncing?. I don't have much knowledge on debouncing. I suspect there is a delay happening in reading and writing GPIOs , because of user process scheduling can be delayed. I don't think accesing GPIO at faster rate is not possible from user application. Please suggest me the best approach.   Please suggest me where I am doing wrong. I appreciate any help, thanks in advance.

 

Spoiler



#include "gpio_lib.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>


/*

#define SUNXI_PULL_NONE (0)
#define SUNXI_PULL_UP (1)
#define SUNXI_PULL_DOWN (2)

*/
#define  R1  SUNXI_GPG(06)
#define  R2  SUNXI_GPG(07)
#define  R3  SUNXI_GPA(07)
#define  R4  SUNXI_GPA(19)
#define  C1  SUNXI_GPA(18)
#define  C3  SUNXI_GPA(02)
#define  C2  SUNXI_GPA(13)
#define  C4  SUNXI_GPA(10)

#define ROWS 4

#define COLS 4

#define LOW (0)
#define HIGH (1)

#define digitalwrite(pin,val)  sunxi_gpio_output(pin,val)
#define digitalread(pin)  sunxi_gpio_input(pin)
#define pinmode(pin,mode)    sunxi_gpio_set_cfgpin(pin,mode)
#define digitalread(pin) sunxi_gpio_input(pin)

#define INPUT SUNXI_GPIO_INPUT
#define OUTPUT SUNXI_GPIO_OUTPUT

char pressedkey = '\0';
char keys[ROWS][COLS] = {
   {'1', '2', '3', 'A'},
   {'4', '5', '6', 'B'},
   {'7', '8', '9', 'C'},
   {'*', '0', '#', 'D'}
};

int keypad_init(){
        sunxi_gpio_init();
 pinmode(R1, INPUT);
        pinmode(R2, INPUT);
        pinmode(R3, INPUT);
        pinmode(R4, INPUT);
        pinmode(C1, OUTPUT);
        pinmode(C2, OUTPUT);
        pinmode(C3, OUTPUT);
        pinmode(C4, OUTPUT);

       digitalwrite(C1,HIGH);
       digitalwrite(C2,HIGH);
       digitalwrite(C3,HIGH);
       digitalwrite(C4,HIGH);

     sunxi_gpio_pullup(R1,SUNXI_PULL_UP);
     sunxi_gpio_pullup(R2,SUNXI_PULL_UP);
     sunxi_gpio_pullup(R3,SUNXI_PULL_UP);
     sunxi_gpio_pullup(R4,SUNXI_PULL_UP);

}


int findlowRow()
{

      if (digitalread(R1) == LOW)
         return 0;
      else if (digitalread(R2) == LOW)
         return 1;
      else if (digitalread(R3) == LOW)
         return 2;
      else if (digitalread(R4) == LOW)
         return 3;
   return -1;
}


char get_key()
{
   int rowIndex,ret=1;

      digitalwrite(C1, LOW);

      rowIndex = findlowRow();
      if (rowIndex > -1)
      {
 if (!pressedkey){
            pressedkey = keys[rowIndex][0];
            printf("row is %d\n",rowIndex);
         return pressedkey;
         }
      }

      digitalwrite(C1, HIGH);


      digitalwrite(C2, LOW);

      rowIndex = findlowRow();
      if (rowIndex > -1)
      {
         if (!pressedkey){
            pressedkey = keys[rowIndex][1];printf("row is %d\n",rowIndex);
         return pressedkey;
          }
      }

      digitalwrite(C2, HIGH);
 digitalwrite(C3, LOW);

      rowIndex = findlowRow();
      if (rowIndex > -1)
      {
         if (!pressedkey)
            pressedkey = keys[rowIndex][2];
         return pressedkey;
      }

      digitalwrite(C3, HIGH);

      digitalwrite(C4, LOW);

      rowIndex = findlowRow();
      if (rowIndex > -1)
      {
         if (!pressedkey)
            pressedkey = keys[rowIndex][3];
         return pressedkey;
      }
digitalwrite(C4, HIGH);

   pressedkey = '\0';

   return pressedkey;
}

int main(void)
{

   keypad_init();

   while(1)
   {
      char x = get_key();
      if (x != '\0')
       {  printf("pressed: %c\n",x ); break;
       }

   }

   return 0;
}


 

 

Edited by TRS-80
put into code fence and spoiler tags
Posted
3 minutes ago, Sturz Marian said:

Please send library  gpio_lib.h  .

 

Please do a search yourself next time. Google is easy to use, the search box in the forum too.

 

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines