test adc external connection

This commit is contained in:
2025-05-29 21:47:21 +02:00
parent fd658face8
commit 548cee71f4
4 changed files with 67 additions and 3 deletions

View File

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

39
src/adc_probe_ext.c Normal file
View File

@@ -0,0 +1,39 @@
#include <avr/io.h>
#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 (07), 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)
}

5
src/adc_probe_ext.h Normal file
View File

@@ -0,0 +1,5 @@
#pragma once
void adc_init(void);
uint16_t adc_read(uint8_t channel);
uint8_t is_device_connected(void);

View File

@@ -5,6 +5,7 @@
#include "defs.h" #include "defs.h"
#include "input_handler.h" #include "input_handler.h"
#include "timer_counter.h" #include "timer_counter.h"
#include "adc_probe_ext.h"
void led_pwm(void); void led_pwm(void);
void probe_int_on(ButtonKey_t *key); void probe_int_on(ButtonKey_t *key);
@@ -38,6 +39,7 @@ ButtonKey_t *inputs[2] = {
&estop_in, &estop_in,
}; };
uint8_t ext_divece_connected = 0;
int main(void) int main(void)
{ {
LED12_SW_DDR |= (1 << LED12_SW_PIN); LED12_SW_DDR |= (1 << LED12_SW_PIN);
@@ -45,12 +47,13 @@ int main(void)
led_pwm(); led_pwm();
timer0_init(); timer0_init();
adc_init();
sei(); // Enable global interrupts sei(); // Enable global interrupts
while (1) while (1)
{ {
ext_divece_connected = is_device_connected();
for (uint8_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) for (uint8_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++)
{ {
buttonHandler(inputs[i]); buttonHandler(inputs[i]);
@@ -74,12 +77,28 @@ void led_pwm(void)
void probe_int_on(ButtonKey_t *key) void probe_int_on(ButtonKey_t *key)
{ {
LED12_SW_PORT |= (1 << LED12_SW_PIN); 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) void probe_int_off(ButtonKey_t *key)
{ {
LED12_SW_PORT &= ~(1 << LED12_SW_PIN); 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) void estop_on(ButtonKey_t *key)
{ {