#include "hal_saadc.h" /********************** 变量区 *************************/ const uint16_t PerSentlist[2][16] = { {100,90,80,70,60,50,40,30,20,10,5,4,3,2,1,0}, {4200,4060,3980,3870,3820,3790,3770,3740,3680,3450,3338,3226,3114,3000} }; //*****************************************************************// /********************** 函数声明区 *************************/ uint16_t SaadcVal_BatPercent_Map(int16_t saadc_val) { saadc_val = saadc_val * 5 / 3; //分压比例 saadc_val = saadc_val * 3600 / 1024; if(saadc_val>PerSentlist[1][0])return 100; if(saadc_val<=PerSentlist[1][11])return 0; for(int i=0;i<16;i++) { if((saadc_val > PerSentlist[1][i+1]) && (saadc_val <= PerSentlist[1][i])) { #if Debug_SaadcINT //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]); #endif 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]; } } return 100; } uint16_t GetBatPercent(void) { int16_t saadc_val = 0; SAADC_Init(); saadc_val = GetOneSaadcSample(); SaadcUnInit(); #if DEBUG_SaadcINT SEGGER_RTT_printf(0,"OneSaadcSample:%d\r\n",saadc_val); SEGGER_RTT_printf(0,"currentBatPercent:%d\r\n",SaadcVal_BatPercent_Map(saadc_val)); #endif return SaadcVal_BatPercent_Map(saadc_val); } void Saadc_Process(void) { }