From 15fd31a6ce18b7c04d64e864df30a8afe73b11ed6a9409742f4539930b00be43 Mon Sep 17 00:00:00 2001 From: localhorst Date: Sun, 9 Nov 2025 18:56:56 +0100 Subject: [PATCH] some tries to get adc bat refernce --- main.c | 58 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/main.c b/main.c index b9cb86b..f8f454e 100644 --- a/main.c +++ b/main.c @@ -26,6 +26,7 @@ #define BUTTON_CONFIRMATION_BLINK_DURATION_MS 50U #define GLOW_BRIGHTNESS_MIN 10U #define GLOW_BRIGHTNESS_MAX 100U +#define INTERNAL_VREF_MV 1024UL /** Convert milliseconds to system ticks */ #define MS_TO_TICKS(ms) ((ms) / MAIN_LOOP_SLEEP) @@ -75,7 +76,7 @@ int main(void) VPORTA.DIR &= ~BUTTON_PIN_MASK; // Input PORTA.PIN0CTRL = PORT_PULLUPEN_bm; // Pull-up enabled - leds_off(); // Ensure all LEDs off at startup + leds_off(); // Ensure all LEDs off at startup bool bLedEnabledOld = bLedEnabled; eModeCurrent = ANIMATION_BLINK; // Set the mode to start with @@ -255,31 +256,46 @@ static inline void leds_on(void) */ uint16_t readBatteryVoltage(void) { - // Enable ADC - ADC0.CTRLA = ADC_ENABLE_bm; + // Enable ADC with proper configuration + ADC0.CTRLC = ADC_REFSEL_VDDREF_gc | // VCC as reference + ADC_PRESC_DIV4_gc; // Prescaler DIV4 - // Select internal voltage reference as input + // 10-bit resolution (default) + ADC0.CTRLA = ADC_RESSEL_10BIT_gc; + + // Select internal voltage reference as input to measure ADC0.MUXPOS = ADC_MUXPOS_INTREF_gc; - // Use VCC as reference (default) - ADC0.CTRLC = ADC_PRESC_DIV4_gc; // Prescaler for 5MHz/4 = 1.25MHz ADC clock + // Enable ADC + ADC0.CTRLA |= ADC_ENABLE_bm; - // Start conversion + // Wait for ADC to stabilize + _delay_us(100); + + // Dummy conversion for stability ADC0.COMMAND = ADC_STCONV_bm; + while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) + ; + ADC0.INTFLAGS = ADC_RESRDY_bm; // Clear flag - // Wait for conversion complete + // Actual conversion + ADC0.COMMAND = ADC_STCONV_bm; while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) ; uint16_t adcResult = ADC0.RES; // Disable ADC to save power - ADC0.CTRLA = !ADC_ENABLE_bm; + ADC0.CTRLA |= !ADC_ENABLE_bm; - // Calculate VCC voltage - // V_battery = 1.1V × 1023 / ADC_result - // Result in millivolts: 1100 × 1023 / ADC_result - uint32_t voltage_mv = (1100UL * 1023UL) / adcResult; + // Check for valid reading + if (adcResult == 0 || adcResult >= 1023) + { + return 0; // Invalid reading + } + + // Calculate VCC: V_VCC = VREF × 1023 / ADC_result + uint32_t voltage_mv = (INTERNAL_VREF_MV * 1023 / adcResult); return (uint16_t)voltage_mv; } @@ -293,9 +309,18 @@ static void battery_level_indicator(void) // 1S LiPo voltage ranges: // Good: >=3700mV - // Low: >=3500mV + // Low: >=3500mV --> - // VPORTA.OUT &= ~(PA6_SET_MASK | PA7_SET_MASK); // Turn off both LEDs first + // VPORTA.OUT &= ~(PA6_SET_MASK | PA7_SET_MASK); // Turn off both LEDs first + VPORTA.OUT |= (PA6_SET_MASK | PA7_SET_MASK); // Red Debug ON + + if (voltage <= 65534) + { + // Green OFF, Red ON - Low battery + //VPORTA.OUT &= ~PA7_SET_MASK; // Red Debug OFF + } + + return; if (voltage >= 3700) { @@ -312,9 +337,8 @@ static void battery_level_indicator(void) // Green OFF, Red ON - Low battery VPORTA.OUT &= ~PA7_SET_MASK; // Red ON (active low) } - - VPORTA.OUT &= ~(PA6_SET_MASK | PA7_SET_MASK); } + /** * @brief Handle momentary switch input on PA0 *