Implement Battery monitoring #6

Open
localhorst wants to merge 2 commits from feature/battery-level-indicator into main

88
main.c
View File

@ -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)
@ -76,13 +77,13 @@ int main(void)
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
battery_level_indicator(); // TODO: Implement
bool bLedEnabledOld = bLedEnabled; bool bLedEnabledOld = bLedEnabled;
eModeCurrent = ANIMATION_BLINK; // Set the mode to start with eModeCurrent = ANIMATION_BLINK; // Set the mode to start with
while (true) while (true)
{ {
battery_level_indicator();
bBtnPressed = handleSwitch(); // Check switch state bBtnPressed = handleSwitch(); // Check switch state
// Light LEDs while button is pressed // Light LEDs while button is pressed
@ -162,6 +163,7 @@ static inline void switchMode(void)
*/ */
static void configureLowPower(void) static void configureLowPower(void)
{ {
return;
// Set unused pins as outputs LOW to prevent floating inputs // Set unused pins as outputs LOW to prevent floating inputs
// Floating inputs can cause extra current consumption // Floating inputs can cause extra current consumption
PORTA.DIRSET = PIN4_bm | PIN5_bm; // Set PA4, PA5 as outputs if unused PORTA.DIRSET = PIN4_bm | PIN5_bm; // Set PA4, PA5 as outputs if unused
@ -248,13 +250,93 @@ static inline void leds_on(void)
setPWM_PA2(255U); setPWM_PA2(255U);
} }
/**
* @brief Read battery voltage using internal 1.1V reference
* @return Estimated battery voltage in millivolts
*/
uint16_t readBatteryVoltage(void)
{
// Enable ADC with proper configuration
ADC0.CTRLC = ADC_REFSEL_VDDREF_gc | // VCC as reference
ADC_PRESC_DIV4_gc; // Prescaler DIV4
// 10-bit resolution (default)
ADC0.CTRLA = ADC_RESSEL_10BIT_gc;
// Select internal voltage reference as input to measure
ADC0.MUXPOS = ADC_MUXPOS_INTREF_gc;
// Enable ADC
ADC0.CTRLA |= ADC_ENABLE_bm;
// 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
// 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;
// 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;
}
/** /**
* @brief Battery monitoring * @brief Battery monitoring
*/ */
static void battery_level_indicator(void) static void battery_level_indicator(void)
{ {
// TODO: Implement uint16_t voltage = readBatteryVoltage();
VPORTA.OUT |= (PA6_SET_MASK | PA7_SET_MASK); // green + red OFF
// 1S LiPo voltage ranges:
// Good: >=3700mV
// 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)
{
// Green ON, Red OFF - Good battery
VPORTA.OUT &= ~PA6_SET_MASK; // Green ON (active low)
}
else if (voltage >= 3500)
{
// Both ON (yellow/orange) - Medium battery
VPORTA.OUT &= ~(PA6_SET_MASK | PA7_SET_MASK);
}
else
{
// Green OFF, Red ON - Low battery
VPORTA.OUT &= ~PA7_SET_MASK; // Red ON (active low)
}
} }
/** /**