| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- /**
- * @file ALGO_Bat_ATY.c
- *
- * @param Project ALGO_Algorithm_ATY_LIB
- *
- * @author ATY
- *
- * @copyright
- * - Copyright 2017 - 2023 MZ-ATY
- * - This code follows:
- * - MZ-ATY Various Contents Joint Statement -
- * <a href="https://mengze.top/MZ-ATY_VCJS">
- * https://mengze.top/MZ-ATY_VCJS</a>
- * - CC 4.0 BY-NC-SA -
- * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
- * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
- * - Your use will be deemed to have accepted the terms of this statement.
- *
- * @brief Familiar functions of Bat calc
- *
- * @version
- * - 1_01_230107 > ATY
- * -# Preliminary version, first Release
- ********************************************************************************
- */
- #ifndef __ALGO_Bat_ATY_C
- #define __ALGO_Bat_ATY_C
- #include "ALGO_Bat_ATY.h"
- /******************************* For user *************************************/
- /******************************************************************************/
- /**
- * @brief
- *
- * @param vin bat vol now
- * @param lastLevel last bat level
- * @param vmax bat vol when power full
- * @param vmin bat vol when power empty or limitted low power
- * @param levels whole levels to split, start from 1(1 for empty)
- * @param vFilter max vol for adc noise, must lower than ((vmax - vmin) / levels)
- * @return uint8_t
- */
- uint8_t Bat_AD_Levels(float vin, uint8_t lastLevel, float vmax, float vmin, uint8_t levels, float vFilter)
- {
- float vUnit = (vmax - vmin) / levels;
- uint8_t resultLevel = lastLevel;
- // input limit judgement
- if(vmax <= vmin)
- return 1;
- if(vin >= vmax)
- return levels;
- else if(vin <= vmin)
- return 1;
- if(vFilter >= vUnit)
- vFilter = vUnit / 2;
- // calc
- if(vin > (vmin + (vUnit * lastLevel) + (vFilter / 2))){
- resultLevel++;
- }
- else if(vin < (vmin + (vUnit * (lastLevel - 1)) - (vFilter / 2))){
- resultLevel--;
- }
- else{}
- // output limit
- if(resultLevel > levels)
- resultLevel = levels;
- else if(resultLevel < 1)
- resultLevel = 1;
- return resultLevel;
- }
- // mbP_CVBV = 3.2;//ADC_Get(&hadc1, ADC_CHANNEL_VREFINT) * VOL_PER_AD_ATY;
- // mbP_CVPV = Bat_AD_Levels(mbP_CVBV, mbP_CVPV, 4.18f, 3.20f, 5, 0.096f);
- #endif /* __ALGO_Bat_ATY_C */
- /******************************** End Of File *********************************/
|