#include <stdio.h>
#include "pico/stdlib.h"
#define seg_a 10
#define seg_b 11
#define seg_c 13
#define seg_d 14
#define seg_e 15
#define seg_f 9
#define seg_g 8
#define ch1 17 // bit 2
#define ch2 21 // bit 1
#define ch3 27 // bit 0
#define btn_config 20
#define btn_start 16
uint8_t display[7] = { seg_a, seg_b, seg_c, seg_d, seg_e, seg_f, seg_g }; // CATODO COMUM → 1 = ACENDE, 0 = APAGA
bool digito[8][7] = {
{1,1,1,1,1,1,0}, // 0
{0,1,1,0,0,0,0}, // 1
{1,1,0,1,1,0,1}, // 2
{1,1,1,1,0,0,1}, // 3
{0,1,1,0,0,1,1}, // 4
{1,0,1,1,0,1,1}, // 5
{1,0,1,1,1,1,1}, // 6
{1,1,1,0,0,0,0} // 7
};
void mostrar_num(int n) {
for (int i = 0; i < 7; i++) {
gpio_put(display[i], digito[n][i]);
}
}
int main() {
stdio_init_all();
for (int i = 0; i < 7; i++) { // Inicializa segmentos
gpio_init(display[i]);
gpio_set_dir(display[i], GPIO_OUT);
}
gpio_init(ch1); // Chaves binárias (pull-UP, pois ligação é para o GND)
gpio_set_dir(ch1, GPIO_IN);
gpio_pull_up(ch1);
gpio_init(ch2);
gpio_set_dir(ch2, GPIO_IN);
gpio_pull_up(ch2);
gpio_init(ch3);
gpio_set_dir(ch3, GPIO_IN);
gpio_pull_up(ch3);
gpio_init(btn_config); // Botões (pull-UP)
gpio_set_dir(btn_config, GPIO_IN);
gpio_pull_up(btn_config);
gpio_init(btn_start);
gpio_set_dir(btn_start, GPIO_IN);
gpio_pull_up(btn_start);
int numero_configurado = 0;
mostrar_num(0);
while (true) {
if (!gpio_get(btn_config)) { // agora apertado = 0 // Botões (pull-UP)
sleep_ms(200);
int b0 = !gpio_get(ch3); // LSB
int b1 = !gpio_get(ch2);
int b2 = !gpio_get(ch1); // MSB
numero_configurado = (b2 << 2) | (b1 << 1) | b0;
if (numero_configurado > 7) numero_configurado = 7;
mostrar_num(numero_configurado);
}
if (!gpio_get(btn_start)) { // BOTÃO START → contagem X–0
sleep_ms(200);
for (int c = numero_configurado; c >= 0; c--) {
mostrar_num(c);
sleep_ms(1000);
}
mostrar_num(numero_configurado);
}
}
}