#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "rom/gpio.h"
// define os nomes dos endereços dos registradores
#define GPIO_ENABLE_REG 0x3FF44020
#define GPIO_OUT_REG 0x3FF44004
#define GPIO_IN1_REG 0x3FF44040
//define os pinos das chaves
#define chave1 34 //bit 2
#define chave2 35 //bit 3
#define chave3 32 //bit 0
#define chave4 33 //bit 1
// --- Definição dos LEDs (saídas) ---
#define LED1 19
#define LED2 21
#define LED3 22
#define LED4 23
//cria um vetor com os valores em hexadecimal de 0 a F
uint32_t display[16] = { 0x60035, 0x70004,0x70035,0x20031,0x40004, 0x30025,
0x70025,0x50015,0x50031,0x30035,0x70034,0x30031,0x50025,0x40024, 0x30015,0x30030 };
//cria um vetor com as chaves
uint8_t chave[4] = { chave1, chave2, chave3, chave4 };
void app_main() {
//configura as chaves como entrada
for (uint8_t index = 0; index < 4; index++ ){
gpio_pad_select_gpio( chave[ index ] );
gpio_set_direction ( chave[ index ], GPIO_MODE_INPUT );
gpio_set_pull_mode ( chave[ index ], GPIO_PULLUP_ONLY);
}
//habilita os pinos
REG_WRITE(GPIO_ENABLE_REG, 0b111011110000000000110101);
uint32_t valor_data = REG_READ(GPIO_IN1_REG) & 0xF;
while (true) {
//le o valor do registrador para as chaves
valor_data = REG_READ(GPIO_IN1_REG);
//coloca como saída o valor do vetor dsplay
REG_WRITE(GPIO_OUT_REG, display[valor_data]);
vTaskDelay(50 / portTICK_PERIOD_MS);
// --- Atualiza os LEDs conforme os bits das chaves ---
if (valor_data & (1 << 2)) {
REG_WRITE(GPIO_OUT_W1TS_REG, (1 << LED1)); // Liga LED1
} else {
REG_WRITE(GPIO_OUT_W1TC_REG, (1 << LED1)); // Desliga LED1
}
if (valor_data & (1 << 3)) {
REG_WRITE(GPIO_OUT_W1TS_REG, (1 << LED2));
} else {
REG_WRITE(GPIO_OUT_W1TC_REG, (1 << LED2));
}
if (valor_data & (1 << 0)) {
REG_WRITE(GPIO_OUT_W1TS_REG, (1 << LED3));
} else {
REG_WRITE(GPIO_OUT_W1TC_REG, (1 << LED3));
}
if (valor_data & (1 << 1)) {
REG_WRITE(GPIO_OUT_W1TS_REG, (1 << LED4));
} else {
REG_WRITE(GPIO_OUT_W1TC_REG, (1 << LED4));
}
}
}Bit0
Bit1
Bit2
Bit3