some tries to get adc bat refernce
This commit is contained in:
58
main.c
58
main.c
@ -26,6 +26,7 @@
|
|||||||
#define BUTTON_CONFIRMATION_BLINK_DURATION_MS 50U
|
#define BUTTON_CONFIRMATION_BLINK_DURATION_MS 50U
|
||||||
#define GLOW_BRIGHTNESS_MIN 10U
|
#define GLOW_BRIGHTNESS_MIN 10U
|
||||||
#define GLOW_BRIGHTNESS_MAX 100U
|
#define GLOW_BRIGHTNESS_MAX 100U
|
||||||
|
#define INTERNAL_VREF_MV 1024UL
|
||||||
|
|
||||||
/** Convert milliseconds to system ticks */
|
/** Convert milliseconds to system ticks */
|
||||||
#define MS_TO_TICKS(ms) ((ms) / MAIN_LOOP_SLEEP)
|
#define MS_TO_TICKS(ms) ((ms) / MAIN_LOOP_SLEEP)
|
||||||
@ -75,7 +76,7 @@ int main(void)
|
|||||||
VPORTA.DIR &= ~BUTTON_PIN_MASK; // Input
|
VPORTA.DIR &= ~BUTTON_PIN_MASK; // Input
|
||||||
PORTA.PIN0CTRL = PORT_PULLUPEN_bm; // Pull-up enabled
|
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;
|
bool bLedEnabledOld = bLedEnabled;
|
||||||
eModeCurrent = ANIMATION_BLINK; // Set the mode to start with
|
eModeCurrent = ANIMATION_BLINK; // Set the mode to start with
|
||||||
@ -255,31 +256,46 @@ static inline void leds_on(void)
|
|||||||
*/
|
*/
|
||||||
uint16_t readBatteryVoltage(void)
|
uint16_t readBatteryVoltage(void)
|
||||||
{
|
{
|
||||||
// Enable ADC
|
// Enable ADC with proper configuration
|
||||||
ADC0.CTRLA = ADC_ENABLE_bm;
|
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;
|
ADC0.MUXPOS = ADC_MUXPOS_INTREF_gc;
|
||||||
|
|
||||||
// Use VCC as reference (default)
|
// Enable ADC
|
||||||
ADC0.CTRLC = ADC_PRESC_DIV4_gc; // Prescaler for 5MHz/4 = 1.25MHz ADC clock
|
ADC0.CTRLA |= ADC_ENABLE_bm;
|
||||||
|
|
||||||
// Start conversion
|
// Wait for ADC to stabilize
|
||||||
|
_delay_us(100);
|
||||||
|
|
||||||
|
// Dummy conversion for stability
|
||||||
ADC0.COMMAND = ADC_STCONV_bm;
|
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))
|
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm))
|
||||||
;
|
;
|
||||||
|
|
||||||
uint16_t adcResult = ADC0.RES;
|
uint16_t adcResult = ADC0.RES;
|
||||||
|
|
||||||
// Disable ADC to save power
|
// Disable ADC to save power
|
||||||
ADC0.CTRLA = !ADC_ENABLE_bm;
|
ADC0.CTRLA |= !ADC_ENABLE_bm;
|
||||||
|
|
||||||
// Calculate VCC voltage
|
// Check for valid reading
|
||||||
// V_battery = 1.1V × 1023 / ADC_result
|
if (adcResult == 0 || adcResult >= 1023)
|
||||||
// Result in millivolts: 1100 × 1023 / ADC_result
|
{
|
||||||
uint32_t voltage_mv = (1100UL * 1023UL) / adcResult;
|
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;
|
return (uint16_t)voltage_mv;
|
||||||
}
|
}
|
||||||
@ -293,9 +309,18 @@ static void battery_level_indicator(void)
|
|||||||
|
|
||||||
// 1S LiPo voltage ranges:
|
// 1S LiPo voltage ranges:
|
||||||
// Good: >=3700mV
|
// 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)
|
if (voltage >= 3700)
|
||||||
{
|
{
|
||||||
@ -312,9 +337,8 @@ static void battery_level_indicator(void)
|
|||||||
// Green OFF, Red ON - Low battery
|
// Green OFF, Red ON - Low battery
|
||||||
VPORTA.OUT &= ~PA7_SET_MASK; // Red ON (active low)
|
VPORTA.OUT &= ~PA7_SET_MASK; // Red ON (active low)
|
||||||
}
|
}
|
||||||
|
|
||||||
VPORTA.OUT &= ~(PA6_SET_MASK | PA7_SET_MASK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Handle momentary switch input on PA0
|
* @brief Handle momentary switch input on PA0
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user