typedef struct { int16_t adc; int16_t human; } kf_t; const __flash kf_t kf_fuel[] = {{1024, 0}, {972, 0},{951, 3},{950, 4},{929, 6}, {907, 7},{886, 10},{864, 12},{843, 13},{842, 15}, {821, 16},{820, 18},{798, 19},{775, 22},{752, 25}, {729, 30},{706, 34},{684, 39},{669, 43},{662, 45}, {639, 48},{616, 52},{594, 57},{571, 63},{552, 70}, {562, 72},{548, 73},{526, 76},{502, 84},{481, 91}, {457, 97},{432, 100},{0, 100}}; const __flash kf_t kf_oel_temp[] = {{1024, 150},{976, 150},{962,140}, {942, 130}, {930, 125},{917, 120},{889, 110},{858, 100}, {826, 90},{794, 80},{762, 70},{728, 60},{695, 50}, {662, 40},{628, 30},{611, 25},{594, 20},{560, 10}, {527, 0},{0, 0}}; const __flash kf_t kf_h2o_temp[] = {{1024, 120},{917, 120},{889, 110},{858, 100}, {826, 90},{794, 80},{762, 70},{728, 60},{695, 50}, {662, 40},{628, 30},{611, 25},{594, 20},{560, 10}, {527, 0},{0, 0}}; uint8_t adc2human(int16_t adc, const __flash kf_t* kf) { uint8_t min, max, i, j; int32_t tmp; if (adc < 0) adc = 0; if (adc > 1023) adc = 1023; min = 1; max = 31; i = (min+max)/2; for (j=0; j < 5; j++) { if (kf[i].adc > adc) min = i; else max = i; i = (min+max)/2; } tmp = (adc - kf[i].adc) * (int32_t)(kf[i-1].human - kf[i].human); tmp = tmp / (kf[i-1].adc - kf[i].adc) + kf[i].human; return tmp; } int main(void){ uint8_t fuel_level; fuel_level = adc2human(0, kf_fuel); // 100 fuel_level = adc2human(450, kf_fuel); // 98 fuel_level = adc2human(960, kf_fuel); // 2 fuel_level += adc2human(1023, kf_fuel); // 0 uint8_t oel_temp; oel_temp = adc2human(0, kf_oel_temp); // 0 oel_temp = adc2human(545, kf_oel_temp); // 5 oel_temp = adc2human(969, kf_oel_temp); // 145 oel_temp += adc2human(1023, kf_oel_temp); // 150 uint8_t h2o_temp; h2o_temp = adc2human(0, kf_h2o_temp); // 0 h2o_temp = adc2human(577, kf_h2o_temp); // 15 h2o_temp = adc2human(903, kf_h2o_temp); // 115 h2o_temp += adc2human(1023, kf_h2o_temp); // 120 while (1){ } }