Skip to content
View in the app

A better way to browse. Learn more.

Armbian Community Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Need help in using 4x4 matrix Keypad on Orangepi Zero GPIO

Featured Replies

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

Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.