0
zhyinch
2021-12-10 7a67526ee4c5c0664e2b8b9a56e86a82eb9b238a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "ADC.h"
#include "main.h"
uint16_t Get_ADC_Value()
{
//HAL_ADC_Start(&hadc);
// HAL_ADC_PollForConversion(&hadc, 10); 
//    if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc), HAL_ADC_STATE_REG_EOC))
//        {
//            return HAL_ADC_GetValue(&hadc);
//        }          
}
float bat_volt;
uint8_t Get_Battary(void)
{static float last_value = 100;
static uint8_t first=1;
    //HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin, GPIO_PIN_RESET);
    bat_volt = (float)Get_ADC_Value()/621;  //(value/4096*3.3*2-3.5)/0.7
    //HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin, GPIO_PIN_SET);
    if(bat_volt>=3.5)
    {
        if(first)
        {
        first = 0;
                bat_volt = (float)Get_ADC_Value()/621;
            last_value =(bat_volt-3.5)*167;//(bat_volt-3.5)/0.6*100
        }
        last_value = 0.9*last_value + (bat_volt-3.5)*16.7;
    if(last_value>100)
        last_value = 100;
    if(last_value<0)
        last_value = 0;
}else{
    last_value=0;
}
    return last_value;    
}