hal_saadc.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "hal_saadc.h"
  2. /********************** 变量区 *************************/
  3. const uint16_t PerSentlist[2][16] = {
  4. {100,90,80,70,60,50,40,30,20,10,5,4,3,2,1,0},
  5. {4200,4060,3980,3870,3820,3790,3770,3740,3680,3450,3338,3226,3114,3000}
  6. };
  7. //*****************************************************************//
  8. /********************** 函数声明区 *************************/
  9. uint16_t SaadcVal_BatPercent_Map(int16_t saadc_val)
  10. {
  11. saadc_val = saadc_val * 5 / 3; //分压比例
  12. saadc_val = saadc_val * 3600 / 1024;
  13. if(saadc_val>PerSentlist[1][0])return 100;
  14. if(saadc_val<=PerSentlist[1][11])return 0;
  15. for(int i=0;i<16;i++)
  16. {
  17. if((saadc_val > PerSentlist[1][i+1]) && (saadc_val <= PerSentlist[1][i]))
  18. {
  19. #if Debug_SaadcINT
  20. //SEGGER_RTT_printf(0,"adcvalue:%d,%d,%d,%d,%d,%d,%d\n",PerSentlist[0][i],PerSentlist[0][i+1],adcvalue,PerSentlist[1][i+1],PerSentlist[1][i],PerSentlist[1][i+1],PerSentlist[0][i+1]);
  21. #endif
  22. return (PerSentlist[0][i]-PerSentlist[0][i+1])*(saadc_val-PerSentlist[1][i+1])/(PerSentlist[1][i]-PerSentlist[1][i+1]) + PerSentlist[0][i+1];
  23. }
  24. }
  25. return 100;
  26. }
  27. uint16_t GetBatPercent(void)
  28. {
  29. int16_t saadc_val = 0;
  30. SAADC_Init();
  31. saadc_val = GetOneSaadcSample();
  32. SaadcUnInit();
  33. #if DEBUG_SaadcINT
  34. SEGGER_RTT_printf(0,"OneSaadcSample:%d\r\n",saadc_val);
  35. SEGGER_RTT_printf(0,"currentBatPercent:%d\r\n",SaadcVal_BatPercent_Map(saadc_val));
  36. #endif
  37. return SaadcVal_BatPercent_Map(saadc_val);
  38. }
  39. void Saadc_Process(void)
  40. {
  41. }