SY883x_For_Client_JLAB_JS07/UsrSrc/bat/bat.c

239 lines
3.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
******************************************************************************
*
* @file bat.c
* @brief Voltage-based RC model gauge algorithm
*
*
* @version 1.0
* @date 2023/02/20 17:35:40
* @author Alex Xu
*
* Copyright (c) 2013-2099,Tkplusemi Technology Co.,Ltd.
* All Rights Reserved
*
* History:
* Revision Date Author Desc
* 1.0.0 2023/02/20 Alex build this file
******************************************************************************
*/
#include "bat.h"
#include "adc.h"
#include "hall.h"
#include "userapp.h"
#include "system.h"
#include "sleep.h"
#include "led.h"
#include "vox_module.h"
#include "charger_module.h"
uint8_t bat_level;
uint8_t bat_level_pdata;
bit F_batlevel_low; //电池低压
bit Vox_Chg_TX_Flag; //电池电压≤3.6VVox发电量码标志位发完清零。
#if BAT_VALUE
#define C_offset_bat_level_MAX 190
#define C_offset_bat_level_MIN 10
bit F_batlevel_protect; //低电保护
bit F_batlevel_lowprotect; //低电保护,发码不升压
uint8_t offset_bat_level;
/*充电电池电压会浮高,具体电压需要测试。*/
#define C_batLevel_SetMax 11
const uint16_t Boost_batlevel_Threshold[C_batLevel_SetMax] =
{
#if 0
3200, //0
3450, //10%
3500, //20%
3550, //30%
3600, //40%
3650, //50%
3700, //60%
3800, //70%
3900, //80%
4000, //90%
4200 //100%
#else
3050, //0
3200, //10%
3450, //20%
3500, //30%
3550, //40%
3600, //50%
3700, //60%
3800, //70%
3900, //80%
4000, //90%
4200 //100%
#endif
};
const uint8_t batlev_data[C_batLevel_SetMax] =
{
#if 0
0x0A, //0 0b001010
0x13, //10% 0b010011
0x18, //20% 0b011000
0x19, //30% 0b011001
0x1A, //40% 0b011010
0x1B, //50% 0b011011
0x20, //60% 0b100000
0x22, //70% 0b100010
0x28, //80% 0b101000
0x2C, //90% 0b101100
0x32 //100% 0b110010
#else
0x03, //0 0b000011
0x0A, //10% 0b001010
0x13, //20% 0b010011
0x18, //30% 0b011000
0x19, //40% 0b011001
0x1A, //50% 0b011010
0x20, //60% 0b100000
0x22, //70% 0b100010
0x28, //80% 0b101000
0x2C, //90% 0b101100
0x32 //100% 0b110010
#endif
};
//uint8_t bat_level_bk = 0;
void check_bat_level(void)
{
uint8_t i = 0;
//static uint8_t bat_level;
static uint8_t bat_level_bk;
//get bat level
#if ADC_ENABLE
for(i=0; i < C_batLevel_SetMax; i++)
{
if( Vbat_Adc < Boost_batlevel_Threshold[i] )
{
break;
}
}
#endif
#if 1
/*系统重启后,快速采集电量*/
if( RST_FLAG )
{
RST_FLAG = 0;
bat_level = i;
}
#endif
if( i > bat_level )
{
offset_bat_level++;
}
else
{
offset_bat_level--;
}
//debounce
if( (offset_bat_level > C_offset_bat_level_MAX) || (offset_bat_level < C_offset_bat_level_MIN) )
{
//update
bat_level = i;
if( bat_level_bk != bat_level )
{
bat_level_pdata = batlev_data[i];
bat_level_bk = bat_level;
}
if( Charger_Done )
{
bat_level_pdata = batlev_data[C_batLevel_SetMax - 1];
}
offset_bat_level = ( C_offset_bat_level_MAX + C_offset_bat_level_MIN ) / 2;
}
if( bat_level <= C_bat_level_lowprotect )
{
F_batlevel_lowprotect = 1;
}
if( bat_level <= C_bat_level_protect )
{
F_batlevel_protect = 1;
}
if( bat_level <= C_bat_level_lowpower )
{
F_batlevel_low = 1;
}
}
#endif