ALGO_Bat_ATY.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @file ALGO_Bat_ATY.c
  3. *
  4. * @param Project ALGO_Algorithm_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2025 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of Bat calc
  20. *
  21. * @version
  22. * - 1_01_230107 > ATY
  23. * -# Preliminary version, first Release
  24. ********************************************************************************
  25. */
  26. #ifndef __ALGO_Bat_ATY_C
  27. #define __ALGO_Bat_ATY_C
  28. #include "ALGO_Bat_ATY.h"
  29. /******************************* For user *************************************/
  30. /******************************************************************************/
  31. /**
  32. * @brief
  33. *
  34. * @param vin bat vol now
  35. * @param lastLevel last bat level
  36. * @param vmax bat vol when power full
  37. * @param vmin bat vol when power empty or limitted low power
  38. * @param levels whole levels to split, start from 1(1 for empty)
  39. * @param vFilter max vol for adc noise, must lower than ((vmax - vmin) / levels)
  40. * @return uint8_t
  41. */
  42. uint8_t Bat_AD_Levels(float vin, uint8_t lastLevel, float vmax, float vmin, uint8_t levels, float vFilter)
  43. {
  44. float vUnit = (vmax - vmin) / levels;
  45. uint8_t resultLevel = lastLevel;
  46. // input limit judgement
  47. if(vmax <= vmin)
  48. return 1;
  49. if(vin >= vmax)
  50. return levels;
  51. else if(vin <= vmin)
  52. return 1;
  53. if(vFilter >= vUnit)
  54. vFilter = vUnit / 2;
  55. // calc
  56. if(vin > (vmin + (vUnit * lastLevel) + (vFilter / 2))){
  57. resultLevel++;
  58. }
  59. else if(vin < (vmin + (vUnit * (lastLevel - 1)) - (vFilter / 2))){
  60. resultLevel--;
  61. }
  62. else{}
  63. // output limit
  64. if(resultLevel > levels)
  65. resultLevel = levels;
  66. else if(resultLevel < 1)
  67. resultLevel = 1;
  68. return resultLevel;
  69. }
  70. // mbP_CVBV = 3.2;//ADC_Get(&hadc1, ADC_CHANNEL_VREFINT) * VOL_PER_AD_ATY;
  71. // mbP_CVPV = Bat_AD_Levels(mbP_CVBV, mbP_CVPV, 4.18, 3.20, 5, 0.096);
  72. #endif /* __ALGO_Bat_ATY_C */
  73. /******************************** End Of File *********************************/