fix(bat): map 6.8V to 10% and 8.4V to 100%

Stretch the 1.6V working range across the gauge so the low-battery shutdown no longer appears at 35%.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yuquanjun 2026-09-28 17:15:41 +08:00
parent e0b91d17b4
commit 4983a424e5
1 changed files with 19 additions and 7 deletions

View File

@ -65,7 +65,7 @@ uint8_t percentage = 0;
//static uint32_t charge_step_tick = 0; //static uint32_t charge_step_tick = 0;
#define VBAT_MIN_RAW 2520 #define VBAT_MIN_RAW 2520
#define VBAT_MAX_RAW 3528 #define VBAT_MAX_RAW 3528
/* 包电压标定:MIN≈6.0V(0%) … MAX≈8.4V(100%);关机阈值 6.8V(每节 3.4V) */ /* 包电压标定:ADC 6.0V→2520、8.4V→3528。显示电量只把 6.8V~8.4V 映到 10%~100%。关机阈值仍是 6.8V */
#define VBAT_PACK_V_MIN_MV 6000 #define VBAT_PACK_V_MIN_MV 6000
#define VBAT_PACK_V_MAX_MV 8400 #define VBAT_PACK_V_MAX_MV 8400
#define VBAT_SHUTDOWN_MV 6800 #define VBAT_SHUTDOWN_MV 6800
@ -411,6 +411,23 @@ static void app_adc_battery_do_sample(void)
bat_index = (bat_index + 1) % BAT_SAMPLE_COUNT; bat_index = (bat_index + 1) % BAT_SAMPLE_COUNT;
} }
/* 6.8V→10%,8.4V→100%。6.8~8.4V 的 1.6V 线性对应 90 个百分点。低于等于 6.8V 仍显示 10%。 */
static uint8_t bat_voltage_percent(uint16_t filtered)
{
uint32_t span;
uint32_t pct;
if (filtered <= (uint16_t)VBAT_SHUTDOWN_RAW)
return 10u;
if (filtered >= VBAT_MAX_RAW)
return 100u;
span = (uint32_t)VBAT_MAX_RAW - (uint32_t)VBAT_SHUTDOWN_RAW;
pct = 10u + (90u * ((uint32_t)filtered - (uint32_t)VBAT_SHUTDOWN_RAW)) / span;
if (pct > 100u)
pct = 100u;
return (uint8_t)pct;
}
/* 显示电量:四舍五入到 5%,之后每次最多升/降 5,避免一下掉一截。关机仍看原始电压。 */ /* 显示电量:四舍五入到 5%,之后每次最多升/降 5,避免一下掉一截。关机仍看原始电压。 */
static uint8_t bat_display_percent(uint8_t raw_pct) static uint8_t bat_display_percent(uint8_t raw_pct)
{ {
@ -439,12 +456,7 @@ static void app_adc_battery_report(void)
uint8_t pct_changed; uint8_t pct_changed;
uint8_t allow_changed; uint8_t allow_changed;
if (filtered < VBAT_MIN_RAW) pct = bat_voltage_percent(filtered);
pct = 0;
else if (filtered > VBAT_MAX_RAW)
pct = 100;
else
pct = (uint8_t)(100 * (filtered - VBAT_MIN_RAW) / (VBAT_MAX_RAW - VBAT_MIN_RAW));
allow = (filtered > (uint16_t)VBAT_SHUTDOWN_RAW) ? 1u : 0u; allow = (filtered > (uint16_t)VBAT_SHUTDOWN_RAW) ? 1u : 0u;
s_vbat_raw = filtered; s_vbat_raw = filtered;