4x4 matrix Keyboard Scan:
1. The Gio values of the 4 rows are set to output, and the Gio values of the root columns are set to input;
2. Set the Gio values of the four lines to 0111, 1011, 1101, and 1110 respectively. Read the Gio values of the column lines one by one to determine which button is used;
The circuit diagram is as follows:
Note:
1. The Gio used as the input in the figure must have an up-pull resistor.
2. Whether each pin in the chip is used as a gpio port requires configuring the register of the chip so that the PIN can be used as a gpio port.
The test code is as follows:
#define KEY_GIO_ROW_137#define KEY_GIO_ROW_233#define KEY_GIO_ROW_332#define KEY_GIO_ROW_435#define KEY_GIO_COL_122#define KEY_GIO_COL_223#define KEY_GIO_COL_324#define KEY_GIO_COL_436int scanKey(){int keyValue = 0;int col1Value=0,col2Value=0,col3Value=0,col4Value=0,row1Value=0,row2Value=0,row3Value=0,row4Value=0;static int press1=0,press2=0,press3=0,press4=0;static int press5=0,press6=0,press7=0,press8=0;static int press9=0,press10=0,press11=0,press12=0;static int press13=0,press14=0,press15=0,press16=0;dm365SetGPIO(KEY_GIO_ROW_1, 0);dm365SetGPIO(KEY_GIO_ROW_2, 1);dm365SetGPIO(KEY_GIO_ROW_3, 1);dm365SetGPIO(KEY_GIO_ROW_4, 1);col1Value = dm365GetGPIO(KEY_GIO_COL_1);col2Value = dm365GetGPIO(KEY_GIO_COL_2);col3Value = dm365GetGPIO(KEY_GIO_COL_3);col4Value = dm365GetGPIO(KEY_GIO_COL_4);keyValue = col1Value | (col2Value << 1) | (col3Value << 2) | (col4Value << 3);//printf("=1==keyValue = %x\n",keyValue);switch(keyValue){case 0x0E:{if(!press1){press1 = 1;printf("KEY 1\n");}}break;case 0x0D:{if(!press2){press2 = 1;printf("KEY 2\n");}}break;case 0x0B:{if(!press3){press3 = 1;printf("KEY 3\n");}}break;case 0x07:{if(!press4){press4 = 1;printf("KEY 4\n");}}break;default:{press1 = 0;press2 = 0;press3 = 0;press4 = 0;}break;}dm365SetGPIO(KEY_GIO_ROW_1, 1);dm365SetGPIO(KEY_GIO_ROW_2, 0);dm365SetGPIO(KEY_GIO_ROW_3, 1);dm365SetGPIO(KEY_GIO_ROW_4, 1);col1Value = dm365GetGPIO(KEY_GIO_COL_1);col2Value = dm365GetGPIO(KEY_GIO_COL_2);col3Value = dm365GetGPIO(KEY_GIO_COL_3);col4Value = dm365GetGPIO(KEY_GIO_COL_4);keyValue = col1Value | (col2Value << 1) | (col3Value << 2) | (col4Value << 3);//printf("=2==keyValue = %x\n",keyValue);switch(keyValue){case 0x0E:{if(!press5){press5 = 1;printf("KEY 5\n");}}break;case 0x0D:{if(!press6){press6 = 1;printf("KEY 6\n");}}break;case 0x0B:{if(!press7){press7 = 1;printf("KEY 7\n");}}break;case 0x07:{if(!press8){press8 = 1;printf("KEY 8\n");}}break;default:{press5 = 0;press6 = 0;press7 = 0;press8 = 0;}break;}dm365SetGPIO(KEY_GIO_ROW_1, 1);dm365SetGPIO(KEY_GIO_ROW_2, 1);dm365SetGPIO(KEY_GIO_ROW_3, 0);dm365SetGPIO(KEY_GIO_ROW_4, 1);col1Value = dm365GetGPIO(KEY_GIO_COL_1);col2Value = dm365GetGPIO(KEY_GIO_COL_2);col3Value = dm365GetGPIO(KEY_GIO_COL_3);col4Value = dm365GetGPIO(KEY_GIO_COL_4);keyValue = col1Value | (col2Value << 1) | (col3Value << 2) | (col4Value << 3);//printf("=3==keyValue = %x\n",keyValue);switch(keyValue){case 0x0E:{if(!press9){press9 = 1;printf("KEY 9\n");}}break;case 0x0D:{if(!press10){press10 = 1;printf("KEY 10\n");}}break;case 0x0B:{if(!press11){press11 = 1;printf("KEY 11\n");}}break;case 0x07:{if(!press12){press12 = 1;printf("KEY 12\n");}}break;default:{press9 = 0;press10 = 0;press11 = 0;press12 = 0;}break;}dm365SetGPIO(KEY_GIO_ROW_1, 1);dm365SetGPIO(KEY_GIO_ROW_2, 1);dm365SetGPIO(KEY_GIO_ROW_3, 1);dm365SetGPIO(KEY_GIO_ROW_4, 0);col1Value = dm365GetGPIO(KEY_GIO_COL_1);col2Value = dm365GetGPIO(KEY_GIO_COL_2);col3Value = dm365GetGPIO(KEY_GIO_COL_3);col4Value = dm365GetGPIO(KEY_GIO_COL_4);keyValue = col1Value | (col2Value << 1) | (col3Value << 2) | (col4Value << 3);//printf("=4==keyValue = %x\n",keyValue);switch(keyValue){case 0x0E:{if(!press13){press13 = 1;printf("KEY 13\n");}}break;case 0x0D:{if(!press14){press14 = 1;printf("KEY 14\n");}}break;case 0x0B:{if(!press15){press15 = 1;printf("KEY 15\n");}}break;case 0x07:{if(!press16){press16 = 1;printf("KEY 16\n");}}break;default:{press13 = 0;press14 = 0;press15 = 0;press16 = 0;}break;}return keyValue;}void *KeyMngThread(){int resetValue = 1;int resetCout = 0;int alarmInValue = 1;int alarmInCout = 0;while(1){resetValue = dm365GetGPIO(GIO_RESET);if(0 == resetValue){resetCout++;}else if(1 == resetValue){resetCout = 0;}if(resetCout == 30){resetCout = 0;system("rm -f /mnt/nand/sysenv.cfg");system("/bin/sync");//System("reboot");system("/tmp/shutdown -r now \n");}alarmInValue = dm365GetGPIO(GIO_ALARM_IN);if(0 == alarmInValue){dm365SetGPIO(GIO_LED,0);//control led off .}else if(1 == alarmInValue){dm365SetGPIO(GIO_LED,1);//control led on .}scanKey();usleep(100000);}}
In the code, the gpio is set to output by default in dm365setgpio,
In dm365getgpio (), the gpio is set to input by default,
The character device driver is used to implement underlying gpio operations at the application layer.
4x4 matrix keyboard Scanner