convert working example to new with timer and debounce

This commit is contained in:
2025-05-29 20:18:09 +02:00
parent 8a4bac8ee6
commit 67f4677053

View File

@@ -3,8 +3,40 @@
#include <util/delay.h> #include <util/delay.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "defs.h" #include "defs.h"
#include "input_handler.h"
#include "timer_counter.h"
void led_pwm(void); 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) int main(void)
{ {
@@ -14,30 +46,37 @@ int main(void)
// LED12_SW_PORT |= (1 << LED12_SW_PIN); // LED12_SW_PORT |= (1 << LED12_SW_PIN);
// LED34_SW_PORT |= (1 << LED34_SW_PIN); // LED34_SW_PORT |= (1 << LED34_SW_PIN);
led_pwm(); led_pwm();
timer0_init();
sei(); // Enable global interrupts
// uint8_t i = 0; // uint8_t i = 0;
while (1) while (1)
{ {
if (PINA & (1 << PROBE_INT_PIN)) // if (PINA & (1 << PROBE_INT_PIN))
{ // {
LED12_SW_PORT |= (1 << LED12_SW_PIN); // LED12_SW_PORT |= (1 << LED12_SW_PIN);
LED34_SW_PORT |= (1 << LED34_SW_PIN); // LED34_SW_PORT |= (1 << LED34_SW_PIN);
} // }
else // else
{ // {
LED12_SW_PORT &= ~(1 << LED12_SW_PIN); // LED12_SW_PORT &= ~(1 << LED12_SW_PIN);
LED34_SW_PORT &= ~(1 << LED34_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 buttonHandler(inputs[i]);
LED_PWM_PORT &= ~(1 << LED_PWM_PIN);
}
else
{
TCCR1A |= (1 << COM1A1); // Włącz PWM
// LED_PWM_PORT |= (1 << LED_PWM_PIN);
} }
} }
return 0; return 0;
@@ -53,4 +92,25 @@ void led_pwm(void)
// Ustaw wartość w rejestrze OCR1A (wypełnienie PWM) // Ustaw wartość w rejestrze OCR1A (wypełnienie PWM)
OCR1A = 2; // 50% wypełnienia (dla 8-bit: 128/255) 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);
} }