diff --git a/CMakeLists.txt b/CMakeLists.txt index ee1f43b..4d378df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,4 +37,5 @@ target_sources(${PROJECT_NAME} PRIVATE src/main.c src/input_handler.c src/timer_counter.c + src/adc_probe_ext.c ) diff --git a/src/adc_probe_ext.c b/src/adc_probe_ext.c new file mode 100644 index 0000000..2bb6446 --- /dev/null +++ b/src/adc_probe_ext.c @@ -0,0 +1,39 @@ +#include +#include "adc_probe_ext.h" + +void adc_init(void) +{ + // Vcc as reference, select ADC2 (MUX = 0b010) + ADMUX = (0 << REFS1) | (0 << REFS0) | // VCC as reference + (0 << ADLAR) | // Right adjust result + (0b010); // ADC2 + + // Enable ADC, prescaler = 64 (assuming 8 MHz → ADC freq = 125 kHz) + ADCSRA = (1 << ADEN) | // Enable ADC + (1 << ADPS2) | (1 << ADPS1); // Prescaler = 64 +} + +uint16_t adc_read(uint8_t channel) +{ + // Set channel (0–7), keeping the other bits in ADMUX + ADMUX = (ADMUX & 0xF8) | (channel & 0x07); + + ADCSRA |= (1 << ADSC); // Start conversion + while (ADCSRA & (1 << ADSC)) + ; // Wait for conversion to finish + + return ADC; // Return 10-bit result +} + +uint8_t is_device_connected(void) +{ + uint16_t value = adc_read(2); // ADC2 = PA2 + + // Zakładamy 5V Vcc → 2.5V = około 512 + if (value > 400 && value < 600) + { + return 1; // Podłączone (w granicach tolerancji) + } + + return 0; // Odłączone (napięcie bliskie 0 lub 5V) +} diff --git a/src/adc_probe_ext.h b/src/adc_probe_ext.h new file mode 100644 index 0000000..d1e5727 --- /dev/null +++ b/src/adc_probe_ext.h @@ -0,0 +1,5 @@ +#pragma once + +void adc_init(void); +uint16_t adc_read(uint8_t channel); +uint8_t is_device_connected(void); \ No newline at end of file diff --git a/src/main.c b/src/main.c index 3d5e8f4..8ac1620 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ #include "defs.h" #include "input_handler.h" #include "timer_counter.h" +#include "adc_probe_ext.h" void led_pwm(void); void probe_int_on(ButtonKey_t *key); @@ -38,6 +39,7 @@ ButtonKey_t *inputs[2] = { &estop_in, }; +uint8_t ext_divece_connected = 0; int main(void) { LED12_SW_DDR |= (1 << LED12_SW_PIN); @@ -45,12 +47,13 @@ int main(void) led_pwm(); timer0_init(); + adc_init(); sei(); // Enable global interrupts while (1) { - + ext_divece_connected = is_device_connected(); for (uint8_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) { buttonHandler(inputs[i]); @@ -74,12 +77,28 @@ void led_pwm(void) void probe_int_on(ButtonKey_t *key) { LED12_SW_PORT |= (1 << LED12_SW_PIN); - LED34_SW_PORT &= ~(1 << LED34_SW_PIN); + if (ext_divece_connected) + { + LED34_SW_PORT &= ~(1 << LED34_SW_PIN); + } + else + { + LED34_SW_PORT |= (1 << LED34_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); + if (ext_divece_connected) + { + LED34_SW_PORT |= (1 << LED34_SW_PIN); + } + else + { + LED34_SW_PORT &= ~(1 << LED34_SW_PIN); + } + // LED34_SW_PORT |= (1 << LED34_SW_PIN); } void estop_on(ButtonKey_t *key) {