From 73c78133ac7214026c90b80fb130e7009667aabb Mon Sep 17 00:00:00 2001 From: bartoolina Date: Fri, 30 May 2025 19:54:29 +0200 Subject: [PATCH] fix led behevior --- CMakeLists.txt | 1 + src/defs.h | 4 ++-- src/led_pwm.c | 15 +++++++----- src/main.c | 65 ++++++++++++++++++++------------------------------ 4 files changed, 38 insertions(+), 47 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d378df..c9adc72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,4 +38,5 @@ target_sources(${PROJECT_NAME} PRIVATE src/input_handler.c src/timer_counter.c src/adc_probe_ext.c + src/led_pwm.c ) diff --git a/src/defs.h b/src/defs.h index 0fdd0d9..72153b7 100644 --- a/src/defs.h +++ b/src/defs.h @@ -43,5 +43,5 @@ #define LED_RIGHT_GREEN() (LED34_SW_PORT &= ~(1 << LED34_SW_PIN)) #define LED_RIGHT_RED() (LED34_SW_PORT |= (1 << LED34_SW_PIN)) -#define LED_GREEN_BRIGHTNESS 10 -#define LED_RED_BRIGHTNESS 80 \ No newline at end of file +#define LED_GREEN_BRIGHTNESS 2 +#define LED_RED_BRIGHTNESS 20 \ No newline at end of file diff --git a/src/led_pwm.c b/src/led_pwm.c index 571c99c..388d6ea 100644 --- a/src/led_pwm.c +++ b/src/led_pwm.c @@ -2,6 +2,9 @@ #include "defs.h" #include "led_pwm.h" +#define PWM_LEFT OCR1B +#define PWM_RIGHT OCR1A + static void led_pwm_init(void) { // Ustaw pin PB1 (OC1A) jako wyjście @@ -11,8 +14,8 @@ static void led_pwm_init(void) TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10); // Clear OC1A on compare match TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); // Prescaler 64 - OCR1A = 0; - OCR1B = 0; + PWM_RIGHT = 0; + PWM_LEFT = 0; } @@ -34,7 +37,7 @@ void led_set_color(led_color_t color, led_side_t side) { uint8_t pwm_percent = 0; - if (side && LED_LEFT) + if (side == LED_LEFT) { if (color == LED_COLOR_GREEN) { @@ -46,10 +49,10 @@ void led_set_color(led_color_t color, led_side_t side) LED_LEFT_RED(); pwm_percent = LED_RED_BRIGHTNESS; } - OCR1A = (pwm_percent * 255) / 100; // Set PWM duty cycle for left LED + PWM_LEFT = (pwm_percent * 255UL) / 100; // Set PWM duty cycle for left LED } - if (side && LED_RIGHT) + if (side == LED_RIGHT) { if (color == LED_COLOR_GREEN) { @@ -61,7 +64,7 @@ void led_set_color(led_color_t color, led_side_t side) LED_RIGHT_RED(); pwm_percent = LED_RED_BRIGHTNESS; } - OCR1B = (pwm_percent * 255) / 100; // Set PWM duty cycle for right LED + PWM_RIGHT = (pwm_percent * 255UL) / 100; // Set PWM duty cycle for right LED } } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 0c21fd4..bf5357e 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,7 @@ #include "input_handler.h" #include "timer_counter.h" #include "adc_probe_ext.h" +#include "led_pwm.h" void led_pwm(void); void probe_int_on(ButtonKey_t *key); @@ -59,16 +60,13 @@ uint16_t last_blink_time = 0; int main(void) { - LED12_SW_DDR |= (1 << LED12_SW_PIN); - LED34_SW_DDR |= (1 << LED34_SW_PIN); - PROBE_OUT_DDR |= (1<< PROBE_OUT_PIN); ESTOP_OUT_DDR |= (1<< ESTOP_OUT_PIN); PROBE_OUT_PORT |= (1<< PROBE_OUT_PIN); ESTOP_OUT_PORT |= (1<< ESTOP_OUT_PIN); - led_pwm(); + led_init(); timer0_init(); adc_init(); @@ -78,12 +76,6 @@ int main(void) { if (estop_triggered) { - // LED12_SW_PORT |= (1 << LED12_SW_PIN); - // LED34_SW_PORT &= ~(1 << LED34_SW_PIN); - - // LED12_SW_PORT &= ~(1 << LED12_SW_PIN); - // LED34_SW_PORT |= (1 << LED34_SW_PIN); - uint16_t now = ticks100us(); // Musisz mieć funkcję zwracającą czas w ms, np. z timer0 if (now - last_blink_time >= 5000) // 0.5 sekundy { @@ -91,13 +83,13 @@ int main(void) blink_state = !blink_state; if (blink_state) { - LED12_SW_PORT |= (1 << LED12_SW_PIN); - LED34_SW_PORT &= ~(1 << LED34_SW_PIN); + led_set_color(LED_COLOR_RED, LED_LEFT); + led_set_color(LED_COLOR_GREEN, LED_RIGHT); } else { - LED12_SW_PORT &= ~(1 << LED12_SW_PIN); - LED34_SW_PORT |= (1 << LED34_SW_PIN); + led_set_color(LED_COLOR_GREEN, LED_LEFT); + led_set_color(LED_COLOR_RED, LED_RIGHT); } } @@ -108,11 +100,11 @@ int main(void) ext_probe_connected = is_device_connected(); if (ext_probe_connected) { - LED34_SW_PORT |= (1 << LED34_SW_PIN); + led_set_color(LED_COLOR_RED, LED_RIGHT); } else { - LED34_SW_PORT &= ~(1 << LED34_SW_PIN); + led_set_color(LED_COLOR_GREEN, LED_RIGHT); } } for (uint8_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) @@ -122,66 +114,61 @@ int main(void) } return 0; } -void led_pwm(void) -{ - // Ustaw pin PB1 (OC1A) jako wyjście - LED_PWM_DDR |= (1 << LED_PWM1_PIN) | (1 << LED_PWM2_PIN); - // Ustaw Timer1 w trybie Fast PWM, 8-bit (WGM13:0 = 0b0101) - TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10); // Clear OC1A on compare match - TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); // Prescaler 64 - - // 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) { PROBE_OUT_PORT &= ~(1 << PROBE_OUT_PIN); - LED12_SW_PORT |= (1 << LED12_SW_PIN); + led_set_color(LED_COLOR_RED, LED_LEFT); if (!ext_probe_connected) { - LED34_SW_PORT |= (1 << LED34_SW_PIN); + led_set_color(LED_COLOR_RED, LED_RIGHT); } } void probe_int_off(ButtonKey_t *key) { - estop_triggered = 0; // Reset estop when probe back to normal + if (estop_triggered) + { + estop_triggered = 0; // Reset estop when probe back to normal + led_set_color(LED_COLOR_RED, LED_RIGHT); + } + PROBE_OUT_PORT |= (1 << PROBE_OUT_PIN); - LED12_SW_PORT &= ~(1 << LED12_SW_PIN); + led_set_color(LED_COLOR_GREEN, LED_LEFT); if (!ext_probe_connected) { - LED34_SW_PORT &= ~(1 << LED34_SW_PIN); + led_set_color(LED_COLOR_GREEN, LED_RIGHT); } } void probe_ext_on(ButtonKey_t *key) { PROBE_OUT_PORT &= ~(1 << PROBE_OUT_PIN); - LED34_SW_PORT &= ~(1 << LED34_SW_PIN); + led_set_color(LED_COLOR_GREEN, LED_RIGHT); } void probe_ext_off(ButtonKey_t *key) { - estop_triggered = 0; // Reset estop when probe back to normal + if (estop_triggered) + { + estop_triggered = 0; // Reset estop when probe back to normal + led_set_color(LED_COLOR_GREEN, LED_LEFT); + } + PROBE_OUT_PORT |= (1 << PROBE_OUT_PIN); - LED34_SW_PORT |= (1 << LED34_SW_PIN); + led_set_color(LED_COLOR_RED, LED_RIGHT); } void estop_on(ButtonKey_t *key) { estop_triggered = 1; ESTOP_OUT_PORT &= ~(1 << ESTOP_OUT_PIN); - // TCCR1A &= ~(1 << COM1A1); // Wyłącz PWM - // LED_PWM_PORT &= ~(1 << LED_PWM1_PIN); } void estop_off(ButtonKey_t *key) { ESTOP_OUT_PORT |= (1 << ESTOP_OUT_PIN); - // TCCR1A |= (1 << COM1A1); // Włącz PWM - // LED_PWM_PORT |= (1 << LED_PWM_PIN); } \ No newline at end of file