From 67f4677053e55c6b5201dd04ed4c12fa6a4bc707 Mon Sep 17 00:00:00 2001 From: bartool Date: Thu, 29 May 2025 20:18:09 +0200 Subject: [PATCH] convert working example to new with timer and debounce --- src/main.c | 96 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 78 insertions(+), 18 deletions(-) diff --git a/src/main.c b/src/main.c index eab3ed8..6329d06 100644 --- a/src/main.c +++ b/src/main.c @@ -3,8 +3,40 @@ #include #include #include "defs.h" +#include "input_handler.h" +#include "timer_counter.h" void led_pwm(void); +void probe_int_on(ButtonKey_t *key); +void probe_int_off(ButtonKey_t *key); +void estop_on(ButtonKey_t *key); +void estop_off(ButtonKey_t *key); + +ButtonKey_t probe_int = { + .instance = {1, &PINA, PROBE_INT_PIN}, + .state = IDLE, + .last_state = IDLE, + .active_state = GPIO_PIN_SET, + .timer_debounce_on = 10000, // 1000 ms + .timer_debounce_off = 10000, // 1 ms + .buttonReleased = probe_int_off, + .buttonPressed = probe_int_on, +}; + +ButtonKey_t estop_in = { + .instance = {2, &PINA, ESTOP_IN_PIN}, + .state = IDLE, + .last_state = IDLE, + .active_state = GPIO_PIN_RESET, + .timer_debounce_on = 10, // 1 ms + .timer_debounce_off = 10, // 1 ms + .buttonReleased = estop_off, + .buttonPressed = estop_on, +}; +ButtonKey_t *inputs[2] = { + &probe_int, + &estop_in, +}; int main(void) { @@ -14,30 +46,37 @@ int main(void) // LED12_SW_PORT |= (1 << LED12_SW_PIN); // LED34_SW_PORT |= (1 << LED34_SW_PIN); led_pwm(); + timer0_init(); + + sei(); // Enable global interrupts // uint8_t i = 0; while (1) { - if (PINA & (1 << PROBE_INT_PIN)) - { - LED12_SW_PORT |= (1 << LED12_SW_PIN); - LED34_SW_PORT |= (1 << LED34_SW_PIN); - } - else - { - LED12_SW_PORT &= ~(1 << LED12_SW_PIN); - LED34_SW_PORT &= ~(1 << LED34_SW_PIN); - } + // if (PINA & (1 << PROBE_INT_PIN)) + // { + // LED12_SW_PORT |= (1 << LED12_SW_PIN); + // LED34_SW_PORT |= (1 << LED34_SW_PIN); + // } + // else + // { + // LED12_SW_PORT &= ~(1 << LED12_SW_PIN); + // LED34_SW_PORT &= ~(1 << LED34_SW_PIN); + // } - if (!(PINA & (1 << ESTOP_IN_PIN))) + // if (!(PINA & (1 << ESTOP_IN_PIN))) + // { + // TCCR1A &= ~(1 << COM1A1); // Wyłącz PWM + // LED_PWM_PORT &= ~(1 << LED_PWM_PIN); + // } + // else + // { + // TCCR1A |= (1 << COM1A1); // Włącz PWM + // // LED_PWM_PORT |= (1 << LED_PWM_PIN); + // } + for (uint8_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) { - TCCR1A &= ~(1 << COM1A1); // Wyłącz PWM - LED_PWM_PORT &= ~(1 << LED_PWM_PIN); - } - else - { - TCCR1A |= (1 << COM1A1); // Włącz PWM - // LED_PWM_PORT |= (1 << LED_PWM_PIN); + buttonHandler(inputs[i]); } } return 0; @@ -53,4 +92,25 @@ void led_pwm(void) // Ustaw wartość w rejestrze OCR1A (wypełnienie PWM) OCR1A = 2; // 50% wypełnienia (dla 8-bit: 128/255) +} + +void probe_int_on(ButtonKey_t *key) +{ + LED12_SW_PORT |= (1 << LED12_SW_PIN); + LED34_SW_PORT |= (1 << LED34_SW_PIN); +} +void probe_int_off(ButtonKey_t *key) +{ + LED12_SW_PORT &= ~(1 << LED12_SW_PIN); + LED34_SW_PORT &= ~(1 << LED34_SW_PIN); +} +void estop_on(ButtonKey_t *key) +{ + TCCR1A &= ~(1 << COM1A1); // Wyłącz PWM + LED_PWM_PORT &= ~(1 << LED_PWM_PIN); +} +void estop_off(ButtonKey_t *key) +{ + TCCR1A |= (1 << COM1A1); // Włącz PWM + // LED_PWM_PORT |= (1 << LED_PWM_PIN); } \ No newline at end of file