some tries to get adc bat refernce

This commit is contained in:
2025-11-09 18:56:56 +01:00
parent 5f1141399f
commit 15fd31a6ce

54
main.c
View File

@ -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)
@ -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); // 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
*