#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
void setup_gpio();
char scan_keypad();
#define ROWS 4
#define COLS 4
int scanRowPins[ROWS] = {0, 45, 48, 47};
int colPins[COLS] = {38, 37, 36, 35};
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
void app_main() {
setup_gpio();
while (1) {
char key = scan_keypad();
if (key != '\0') {
printf("You pressed: %c\n", key);
}
vTaskDelay(pdMS_TO_TICKS(50));
}
}
void setup_gpio() {
for (int i = 0; i < ROWS; i++) {
gpio_set_direction(scanRowPins[i], GPIO_MODE_OUTPUT);
gpio_set_level(scanRowPins[i], 1);
}
for (int i = 0; i < COLS; i++) {
gpio_set_direction(colPins[i], GPIO_MODE_INPUT);
gpio_set_pull_mode(colPins[i], GPIO_PULLUP_ONLY); // all buttons are set as pullup configuration using the internal pullup resistor in the microcontroller
}
}
char scan_keypad() {
uint8_t scanVal = '\0';
for (int i = 0; i < ROWS; i++) {
scanVal = ~(0 << i);
for (int j = 0; j < ROWS; j++) {
gpio_set_level(scanRowPins[j], (scanVal >>j) & 1); // here, 'pins' can be either row pins or column pins. Select the correct one
}
vTaskDelay(pdMS_TO_TICKS(5));
for (int k = 0; k < COLS; k++) {
if (gpio_get_level(colPins[k]) == 0) { // pullup configuration
while (gpio_get_level(colPins[k]) == 0) {
vTaskDelay(pdMS_TO_TICKS(50));
}
return keys[i][k];
}
}
}
return '\0';
}Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1