//#include "ADC.h"
|
#include "mainex.h"
|
#include "stm32f4xx_hal.h"
|
#include "HIDO_Timer.h"
|
#include "ADC_hal.h"
|
#include "math.h"
|
extern ADC_HandleTypeDef hadc1;
|
/*******************************************************************************
|
* Function Name : Power_GetBatteryVoltage
|
* Description : »ñÈ¡µç³Øµçѹ
|
* Input : None
|
* Output : None
|
* Return : µç³Øµçѹ
|
* Author : hido-iot.com
|
* Modified Date: : 2021Äê4ÔÂ11ÈÕ
|
*******************************************************************************/
|
|
int LowPower = 0;
|
HIDO_FLOAT fVoltage1;
|
HIDO_FLOAT Power_GetBatteryVoltage(HIDO_VOID)
|
{
|
HIDO_UINT16 u32ADC = ADC_GetValue(ADC_CH_BAT);
|
int Power_J = 0;
|
HIDO_FLOAT fVoltage = (u32ADC / 4095.0) * 3.3;
|
fVoltage = fVoltage * (HIDO_FLOAT)2;
|
|
|
fVoltage1 = (u32ADC / 4095.0) * 3.3;
|
fVoltage1 = fVoltage1 * (HIDO_FLOAT)2;
|
|
HIDO_FLOAT realVol;
|
HIDO_UINT16 VREFINT_CAL;
|
HIDO_UINT16 adValue[2];
|
|
HIDO_UINT16 u32ADC1 = ADC_GetValue(ADC_CH_BAT);
|
HIDO_UINT16 u32ADC2 = ADC_GetValue(ADC_CH_INF);
|
|
VREFINT_CAL = *(__IO uint16_t *)(0x1FFF7A2A);
|
|
|
realVol = (float)(3.3 * VREFINT_CAL * u32ADC1 *2) / (u32ADC2 * 4095);
|
if(realVol < 3.3)
|
{
|
LowPower = 1;
|
}
|
|
Power_J = (realVol - (HIDO_FLOAT)3.3) / (HIDO_FLOAT)(4.1 - 3.3)*100;
|
|
if(Power_J > 100)
|
{
|
Power_J = 100;
|
}
|
if(Power_J < 0)
|
{
|
Power_J = 0;
|
}
|
|
return fVoltage;
|
|
}
|
|
/*******************************************************************************
|
* Function Name : Power_GetBatteryPercentage
|
* Description : »ñÈ¡µç³ØÈÝÁ¿°Ù·Ö±È
|
* Input : None
|
* Output : None
|
* Return : µç³ØÈÝÁ¿°Ù·Ö±È
|
* Author : hido-iot.com
|
* Modified Date: : 2021Äê4ÔÂ11ÈÕ
|
*******************************************************************************/
|
HIDO_FLOAT Power_GetBatteryPercentage(HIDO_VOID)
|
{
|
static HIDO_FLOAT fStablePercentage = -1.0;
|
static HIDO_UINT32 u32StableTick = 0;
|
HIDO_FLOAT fVoltage = 0;
|
HIDO_FLOAT fPercentage = 0;
|
|
fVoltage = Power_GetBatteryVoltage();
|
if(fVoltage < (HIDO_FLOAT)3.3)
|
{
|
fPercentage = 0;
|
}
|
else if(fVoltage > (HIDO_FLOAT)4.0)
|
{
|
fPercentage = 100;
|
}
|
else
|
{
|
fPercentage = (fVoltage - (HIDO_FLOAT)3.3) / (HIDO_FLOAT)(4.0 - 3.3)*100;
|
}
|
|
/* Îȶ¨µç³ØÈÝÁ¿°Ù·Ö±È */
|
if(fStablePercentage < 0)
|
{
|
fStablePercentage = fPercentage;
|
u32StableTick = HIDO_TimerGetTick();
|
}
|
else
|
{
|
if(fabs(fStablePercentage - fPercentage) < 0.5)
|
{
|
u32StableTick = HIDO_TimerGetTick();
|
}
|
else
|
{
|
if((HIDO_TimerGetTick() - u32StableTick) > HIDO_TIMER_TICK_S(60))
|
{
|
fStablePercentage = fPercentage;
|
u32StableTick = HIDO_TimerGetTick();
|
}
|
}
|
}
|
|
return fStablePercentage;
|
}
|