SY883x_For_Client_JLAB_JS07/UsrSrc/bat/bat.c

178 lines
2.9 KiB
C

/*
******************************************************************************
*
* @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"
uint8_t bat_level = 0;
uint8_t bat_level_pdata = 0;
bit F_batlevel_low = 0; //电池低压
#if BAT_VALUE
#define C_offset_bat_level_MAX 190
#define C_offset_bat_level_MIN 10
bit F_batlevel_protect = 0; //低电保护
uint8_t offset_bat_level = ( C_offset_bat_level_MAX + C_offset_bat_level_MIN ) / 2;
/*充电电池电压会浮高,具体电压需要测试。*/
#define C_batLevel_SetMax 11
const uint16_t Boost_batlevel_Threshold[C_batLevel_SetMax] =
{
3200, //0
3500, //10%
3600, //20%
3700, //30%
3800, //40%
3900, //50%
3950, //60%
4000, //70%
4100, //80%
4150, //90%
4200 //100%
};
const uint8_t batlev_data[C_batLevel_SetMax] =
{
0x0A, //0 0b001010
0x18, //10% 0b011000
0x1A, //20% 0b011010
0x20, //30% 0b100000
0x22, //40% 0b100010
0x28, //50% 0b101000
0x29, //60% 0b101001
0x2A, //70% 0b101010
0x30, //80% 0b110000
0x31, //90% 0b110001
0x32 //100% 0b110010
};
//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;
}
offset_bat_level = ( C_offset_bat_level_MAX + C_offset_bat_level_MIN ) / 2;
}
if( bat_level <= C_bat_level_protect )
{
F_batlevel_protect = 1;
}
#if 0
else
if( bat_level > ( C_bat_level_protect + 1 ) )
{
F_batlevel_protect = 0;
}
#endif
if( bat_level <= C_bat_level_lowpower )
{
F_batlevel_low = 1;
}
else if( bat_level > (C_bat_level_lowpower + 2) )
{
F_batlevel_low = 0;
}
}
#endif