#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "hd44780.h"
#define I2C_PORT i2c0
bool button_pressed = false;
bool counting = false;
uint32_t seconds = 0;
void button_handler() {
button_pressed = !button_pressed;
}
void setup() {
gpio_init(26); // Button pin
gpio_set_dir(26, GPIO_IN);
gpio_pull_up(26);
gpio_set_irq_enabled_with_callback(26, GPIO_IRQ_EDGE_RISE, true, &button_handler);
gpio_init(7); // LED pin
gpio_set_dir(7, GPIO_OUT);
i2c_init(I2C_PORT, 100000);
gpio_set_function(0, GPIO_FUNC_I2C); // SDA
gpio_set_function(1, GPIO_FUNC_I2C); // SCL
gpio_pull_up(0);
gpio_pull_up(1);
hd44780_init(I2C_PORT, 0x27, 16, 2);
}
void display_time(uint32_t time) {
char buffer[17];
snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d", time / 3600, (time % 3600) / 60, time % 60);
hd44780_clear();
hd44780_gotoxy(0, 0);
hd44780_puts(buffer);
}
void loop() {
if (button_pressed) {
counting = !counting;
button_pressed = false;
}
if (counting) {
seconds++;
gpio_put(7, seconds % 2);
display_time(seconds);
sleep_ms(1000);
}
}
int main() {
setup();
while (true) {
loop();
}
return 0;
}