fix led behevior

This commit is contained in:
2025-05-30 19:54:29 +02:00
parent 74a3ace504
commit 73c78133ac
4 changed files with 38 additions and 47 deletions

View File

@@ -38,4 +38,5 @@ target_sources(${PROJECT_NAME} PRIVATE
src/input_handler.c src/input_handler.c
src/timer_counter.c src/timer_counter.c
src/adc_probe_ext.c src/adc_probe_ext.c
src/led_pwm.c
) )

View File

@@ -43,5 +43,5 @@
#define LED_RIGHT_GREEN() (LED34_SW_PORT &= ~(1 << LED34_SW_PIN)) #define LED_RIGHT_GREEN() (LED34_SW_PORT &= ~(1 << LED34_SW_PIN))
#define LED_RIGHT_RED() (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_GREEN_BRIGHTNESS 2
#define LED_RED_BRIGHTNESS 80 #define LED_RED_BRIGHTNESS 20

View File

@@ -2,6 +2,9 @@
#include "defs.h" #include "defs.h"
#include "led_pwm.h" #include "led_pwm.h"
#define PWM_LEFT OCR1B
#define PWM_RIGHT OCR1A
static void led_pwm_init(void) static void led_pwm_init(void)
{ {
// Ustaw pin PB1 (OC1A) jako wyjście // 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 TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10); // Clear OC1A on compare match
TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); // Prescaler 64 TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); // Prescaler 64
OCR1A = 0; PWM_RIGHT = 0;
OCR1B = 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; uint8_t pwm_percent = 0;
if (side && LED_LEFT) if (side == LED_LEFT)
{ {
if (color == LED_COLOR_GREEN) if (color == LED_COLOR_GREEN)
{ {
@@ -46,10 +49,10 @@ void led_set_color(led_color_t color, led_side_t side)
LED_LEFT_RED(); LED_LEFT_RED();
pwm_percent = LED_RED_BRIGHTNESS; 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) if (color == LED_COLOR_GREEN)
{ {
@@ -61,7 +64,7 @@ void led_set_color(led_color_t color, led_side_t side)
LED_RIGHT_RED(); LED_RIGHT_RED();
pwm_percent = LED_RED_BRIGHTNESS; 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
} }
} }

View File

@@ -6,6 +6,7 @@
#include "input_handler.h" #include "input_handler.h"
#include "timer_counter.h" #include "timer_counter.h"
#include "adc_probe_ext.h" #include "adc_probe_ext.h"
#include "led_pwm.h"
void led_pwm(void); void led_pwm(void);
void probe_int_on(ButtonKey_t *key); void probe_int_on(ButtonKey_t *key);
@@ -59,16 +60,13 @@ uint16_t last_blink_time = 0;
int main(void) int main(void)
{ {
LED12_SW_DDR |= (1 << LED12_SW_PIN);
LED34_SW_DDR |= (1 << LED34_SW_PIN);
PROBE_OUT_DDR |= (1<< PROBE_OUT_PIN); PROBE_OUT_DDR |= (1<< PROBE_OUT_PIN);
ESTOP_OUT_DDR |= (1<< ESTOP_OUT_PIN); ESTOP_OUT_DDR |= (1<< ESTOP_OUT_PIN);
PROBE_OUT_PORT |= (1<< PROBE_OUT_PIN); PROBE_OUT_PORT |= (1<< PROBE_OUT_PIN);
ESTOP_OUT_PORT |= (1<< ESTOP_OUT_PIN); ESTOP_OUT_PORT |= (1<< ESTOP_OUT_PIN);
led_pwm(); led_init();
timer0_init(); timer0_init();
adc_init(); adc_init();
@@ -78,12 +76,6 @@ int main(void)
{ {
if (estop_triggered) 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 uint16_t now = ticks100us(); // Musisz mieć funkcję zwracającą czas w ms, np. z timer0
if (now - last_blink_time >= 5000) // 0.5 sekundy if (now - last_blink_time >= 5000) // 0.5 sekundy
{ {
@@ -91,13 +83,13 @@ int main(void)
blink_state = !blink_state; blink_state = !blink_state;
if (blink_state) if (blink_state)
{ {
LED12_SW_PORT |= (1 << LED12_SW_PIN); led_set_color(LED_COLOR_RED, LED_LEFT);
LED34_SW_PORT &= ~(1 << LED34_SW_PIN); led_set_color(LED_COLOR_GREEN, LED_RIGHT);
} }
else else
{ {
LED12_SW_PORT &= ~(1 << LED12_SW_PIN); led_set_color(LED_COLOR_GREEN, LED_LEFT);
LED34_SW_PORT |= (1 << LED34_SW_PIN); led_set_color(LED_COLOR_RED, LED_RIGHT);
} }
} }
@@ -108,11 +100,11 @@ int main(void)
ext_probe_connected = is_device_connected(); ext_probe_connected = is_device_connected();
if (ext_probe_connected) if (ext_probe_connected)
{ {
LED34_SW_PORT |= (1 << LED34_SW_PIN); led_set_color(LED_COLOR_RED, LED_RIGHT);
} }
else 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++) for (uint8_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++)
@@ -122,66 +114,61 @@ int main(void)
} }
return 0; 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) void probe_int_on(ButtonKey_t *key)
{ {
PROBE_OUT_PORT &= ~(1 << PROBE_OUT_PIN); 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) 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) 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); 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) 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) void probe_ext_on(ButtonKey_t *key)
{ {
PROBE_OUT_PORT &= ~(1 << PROBE_OUT_PIN); 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) 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); 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) void estop_on(ButtonKey_t *key)
{ {
estop_triggered = 1; estop_triggered = 1;
ESTOP_OUT_PORT &= ~(1 << ESTOP_OUT_PIN); 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) void estop_off(ButtonKey_t *key)
{ {
ESTOP_OUT_PORT |= (1 << ESTOP_OUT_PIN); ESTOP_OUT_PORT |= (1 << ESTOP_OUT_PIN);
// TCCR1A |= (1 << COM1A1); // Włącz PWM
// LED_PWM_PORT |= (1 << LED_PWM_PIN);
} }