4x4 Matrix Keyboard
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a1z09.2.0.0.4b5a2e8dp3kXe9&id=16901479794&_u=82t4uge5d0d3
Specifications
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting.
For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here.
Just note that here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_matrixkey.c and bsp_matrixkey.h, and the folder name to Matrixkey.
Write Code
In the file bsp_matrixkey.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-09 LCKFB-lp first version
*/
#include "bsp_matrixkey.h"
#include "stdio.h"
//Rows
uint32_t gpio_row[4] = {GPIO_IN1,GPIO_IN2,GPIO_IN3,GPIO_IN4};
//Columns
uint32_t gpio_col[4] = {GPIO_IN5,GPIO_IN6,GPIO_IN7,GPIO_IN8};
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
void delay_1ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_1us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: MatrixKey_GPIO_Init
* Function Description: Initialization of matrix keys
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: Pull-up must be enabled
******************************************************************/
void MatrixKey_GPIO_Init(void)
{
gpio_config_t GG1_config = {
.pin_bit_mask = (1ULL<<GPIO_IN1)|(1ULL<<GPIO_IN2)|(1ULL<<GPIO_IN3)|(1ULL<<GPIO_IN4), //Configure pins
.mode =GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_ENABLE, //Enable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, //Disable pull-down
.intr_type = GPIO_INTR_DISABLE //Disable pin interrupt
};
gpio_config(&GG1_config);
gpio_config_t GG2_config = {
.pin_bit_mask = (1ULL<<GPIO_IN5)|(1ULL<<GPIO_IN6)|(1ULL<<GPIO_IN7)|(1ULL<<GPIO_IN8), //Configure pins
.mode =GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE, //Enable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, //Disable pull-down
.intr_type = GPIO_INTR_DISABLE //Disable pin interrupt
};
gpio_config(&GG2_config);
}
/******************************************************************
* Function Name: key_scan
* Function Description: Matrix key scan
* Function Parameters: None
* Function Return: 0=no key pressed 1~16=corresponding key pressed
* Author: LC
* Notes: None
******************************************************************/
uint8_t key_scan(void)
{
uint8_t i, j, key_val = 0;
for (i = 0; i < 4; i++)
{
// Select row i (active low)
gpio_set_level(gpio_row[i], 0);
// Detect the column status to determine whether a key is pressed
for (j = 0; j < 4; j++)
{
//If this column is pulled low
if (!gpio_get_level(gpio_col[j]))
{
// Calculate the key number
key_val = i * 4 + j + 1;
break;
}
}
// Restore the output state of row i
gpio_set_level(gpio_row[i], 1);
// If a key is pressed, exit the scan immediately
if (key_val) break;
}
// Return the key number; 0 means no key is pressed
return key_val;
}
/******************************************************************
* Function Name: Get_Key
* Function Description: Get the specific character of the key
* Function Parameters: None
* Function Return: The specific value of the key
* Author: LC
* Notes: None
******************************************************************/
uint8_t Get_Key(void)
{
uint8_t value = key_scan();
switch (value)
{
case 1: value = '1'; break;
case 2: value = '2'; break;
case 3: value = '3'; break;
case 4: value = 'A'; break;
case 5: value = '4'; break;
case 6: value = '5'; break;
case 7: value = '6'; break;
case 8: value = 'B'; break;
case 9: value = '7'; break;
case 10: value = '8'; break;
case 11: value = '9'; break;
case 12: value = 'C'; break;
case 13: value = '*'; break;
case 14: value = '0'; break;
case 15: value = '#'; break;
case 16: value = 'D'; break;
default: value = 'n'; break;
}
return value;
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
In the file bsp_matrixkey.h, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-09 LCKFB-lp first version
*/
#ifndef _BSP_MATRIXKEY_H_
#define _BSP_MATRIXKEY_H_
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_rom_sys.h"
#include "esp_timer.h"
#include "driver/uart.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gptimer.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include "driver/spi_master.h"
#include "nvs_flash.h"
#define GPIO_IN1 18
#define GPIO_IN2 17
#define GPIO_IN3 16
#define GPIO_IN4 15
#define GPIO_IN5 41
#define GPIO_IN6 42
#define GPIO_IN7 45
#define GPIO_IN8 46
uint8_t Get_Key(void);
void MatrixKey_GPIO_Init(void);
uint8_t key_scan(void);
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void delay_1us(unsigned int us);
void delay_1ms(unsigned int ms);
#endif2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Porting Verification
In the main function of your own project, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-09 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_matrixkey.h"
void app_main(void)
{
uint8_t key_value = 0;
printf("start\r\n");
MatrixKey_GPIO_Init();
while(1)
{
key_value = Get_Key();
if( key_value != 'n' )//If a key is pressed
{
printf("KEY = %c\r\n",key_value);
}
key_value = 0;
delay_1ms(500);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Power-on effect (click in sequence):
Driver code:
File Download
📌 Materials Download Center (click to jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.