自己顶一下。我只要得到具体的电阻值就行。我用二分法做了一个表。一会儿我也贴出来,只要知道阻值。用不了几步就能得出具体的阻值来。
const u32 RTD_TAB_PT100[900] =
{
//9 8 7 6 5 4 3 2 1 0
1895, 1938, 1982, 2025, 2068, 2111, 2154, 2197, 2240, 2283, // -190 //0行
2325, 2368, 2411, 2454, 2497, 2539, 2582, 2624, 2667, 2710, // -180
2752, 2795, 2837, 2880, 2922, 2964, 3007, 3049, 3091, 3134, // -170
3176, 3218, 3260, 3302, 3344, 3386, 3428, 3470, 3512, 3554, // -160
3596, 3638, 3680, 3722, 3764, 3805, 3847, 3889, 3931, 3972, // -150
4014, 4056, 4097, 4139, 4180, 4222, 4263, 4305, 4346, 4388, // -140 //5行
4429, 4470, 4512, 4553, 4594, 4636, 4677, 4718, 4759, 4800, // -130
4842, 4883, 4924, 4965, 5006, 5047, 5088, 5129, 5170, 5211, // -120
5252, 5293, 5334, 5375, 5415, 5456, 5497, 5538, 5579, 5619, // -110
5660, 5701, 5741, 5782, 5823, 5863, 5904, 5940, 5985, 6026, // -100
6066, 6107, 6147, 6188, 6228, 6268, 6309, 6349, 6390, 6430, // -90 //10行
6470, 6511, 6551, 6591, 6631, 6672, 6712, 6752, 6792, 6833, // -80
6873, 6913, 6953, 6993, 7033, 7073, 7113, 7153, 7193, 7233, // -70
7273, 7313, 7353, 7393, 7433, 7473, 7513, 7553, 7593, 7633, // -60
7673, 7712, 7752, 7792, 7832, 7872, 7911, 7951, 7991, 8031, // -50
8070, 8110, 8150, 8189, 8229, 8269, 8308, 8348, 8387, 8427, // -40
8467, 8506, 8546, 8585, 8625, 8664, 8704, 8743, 8783, 8822, // -30
8862, 8901, 8940, 8980, 9019, 9059, 9098, 9137, 9177, 9216, // -20
9255, 9295, 9334, 9373, 9412, 9452, 9491, 9530, 9569, 9609, // -10
9648, 9687, 9726, 9765, 9804, 9844, 9883, 9922, 9961, 10000, // 0 //19行
//0 1 2 3 4 5 6 7 8 9
10000, 10039, 10078, 10117, 10156, 10195, 10234, 10273, 10312, 10351,// 0 //20行
10390, 10429, 10468, 10507, 10546, 10585, 10624, 10663, 10702, 10740,// 10
10779, 10818, 10857, 10896, 10935, 10973, 11012, 11051, 11090, 11129,// 20
11167, 11206, 11245, 11283, 11322, 11361, 11400, 11438, 11477, 11515,// 30
11554, 11593, 11631, 11670, 11708, 11747, 11786, 11824, 11863, 11901,// 40
11940, 11978, 12017, 12055, 12094, 12132, 12171, 12209, 12247, 12286,// 50 //25行
12324, 12363, 12401, 12439, 12478, 12516, 12554, 12593, 12631, 12669,// 60
12708, 12746, 12784, 12822, 12861, 12899, 12937, 12975, 13013, 13052,// 70
13090, 13128, 13166, 13204, 13242, 13280, 13318, 13357, 13395, 13433,// 80
13471, 13509, 13547, 13585, 13623, 13661, 13699, 13737, 13775, 13813,// 90
13851, 13888, 13926, 13964, 14002, 14040, 14078, 14116, 14154, 14191,// 100 //30行
14229, 14267, 14305, 14343, 14380, 14418, 14456, 14494, 14531, 14569,// 110
14607, 14644, 14682, 14720, 14757, 14795, 14833, 14870, 14908, 14946,// 120
14983, 15021, 15058, 15096, 15133, 15171, 15208, 15246, 15283, 15321,// 130
15358, 15396, 15433, 15471, 15508, 15546, 15583, 15620, 15658, 15695,// 140
15733, 15770, 15807, 15845, 15882, 15919, 15956, 15994, 16031, 16068,// 150 //35行
16105, 16143, 16180, 16217, 16254, 16291, 16329, 16366, 16403, 16440,// 160
16477, 16514, 16551, 16589, 16626, 16663, 16700, 16737, 16774, 16811,// 170
16848, 16885, 16922, 16959, 16996, 17033, 17070, 17107, 17143, 17180,// 180
17217, 17254, 17291, 17328, 17365, 17402, 17438, 17475, 17512, 17549,// 190
17586, 17622, 17659, 17696, 17733, 17769, 17806, 17843, 17879, 17916,// 200 //40行
17953, 17989, 18026, 18063, 18099, 18136, 18172, 18209, 18246, 18282,// 210
18319, 18355, 18392, 18428, 18465, 18501, 18538, 18574, 18611, 18647,// 220
18684, 18720, 18756, 18793, 18829, 18866, 18902, 18938, 18975, 19011,// 230
19047, 19084, 19120, 19156, 19192, 19229, 19265, 19301, 19337, 19374,// 240
19410, 19446, 19482, 19518, 19555, 19591, 19627, 19663, 19699, 19735,// 250 //45行
19771, 19807, 19843, 19879, 19915, 19951, 19987, 20023, 20059, 20095,// 260
20131, 20167, 20203, 20239, 20275, 20311, 20347, 20383, 20419, 20455,// 270
20490, 20526, 20562, 20598, 20634, 20670, 20705, 20741, 20777, 20813,// 280
20848, 20884, 20920, 20956, 20991, 21027, 21063, 21098, 21134, 21170,// 290
21205, 21241, 21276, 21312, 21348, 21383, 21419, 21454, 21490, 21525,// 300 //50行
21561, 21596, 21632, 21667, 21703, 21738, 21774, 21809, 21844, 21880,// 310
21915, 21951, 21986, 22021, 22057, 22092, 22127, 22163, 22198, 22233,// 320
22268, 22304, 22339, 22374, 22409, 22445, 22480, 22515, 22550, 22585,// 330
22621, 22656, 22691, 22726, 22761, 22796, 22831, 22866, 22902, 22937,// 340
22972, 23007, 23042, 23077, 23112, 23147, 23182, 23217, 23252, 23287,// 350 //55行
23321, 23356, 23391, 23426, 23461, 23496, 23531, 23566, 23600, 23635,// 360
23670, 23705, 23740, 23774, 23809, 23844, 23879, 23913, 23948, 23983,// 370
24018, 24052, 24087, 24122, 24156, 24191, 24226, 24260, 24295, 24329,// 380
24364, 24399, 24433, 24468, 24502, 24537, 24571, 24606, 24640, 24675,// 390
24709, 24744, 24778, 24813, 24847, 24881, 24916, 24950, 24585, 25019,// 400 //60行
25053, 25088, 25122, 25156, 25191, 25225, 25259, 25293, 25328, 25362,// 410
25396, 25430, 25465, 25499, 25533, 25567, 25601, 25635, 25670, 25704,// 420
25738, 25772, 25806, 25840, 25874, 25908, 25942, 25976, 26010, 26044,// 430
26078, 26112, 26146, 26180, 26214, 26248, 26282, 26316, 26350, 26384,// 440
26418, 26452, 26486, 26520, 26553, 26587, 26621, 26655, 26689, 26722,// 450 //65行
26756, 26790, 26824, 26857, 26891, 26925, 26959, 26992, 27026, 27060,// 460
27093, 27127, 27161, 27194, 27228, 27261, 27295, 27329, 27362, 27396,// 470
27429, 27463, 27496, 27530, 27563, 27597, 27630, 27664, 27697, 27731,// 480
27764, 27798, 27831, 27864, 27898, 27931, 27964, 27998, 28031, 28064,// 490
28098, 28131, 28164, 28198, 28231, 28264, 28297, 28331, 28364, 28397,// 500 //70行
28430, 28463, 28497, 28530, 28563, 28596, 28629, 28662, 28685, 28729,// 510
28762, 28795, 28828, 28861, 28894, 28927, 28960, 28993, 29026, 29059,// 520
29092, 29125, 29158, 29191, 29224, 29256, 29289, 29322, 29355, 29388,// 530
29421, 29454, 29486, 29519, 29552, 29585, 29618, 29650, 29683, 29716,// 540
29749, 29781, 29814, 29847, 29880, 29912, 29945, 29978, 30010, 30043,// 550 //75行
30075, 30108, 30141, 30173, 30206, 30238, 30271, 30303, 30336, 30369,// 560
30401, 30434, 30466, 30498, 30531, 30563, 30596, 30628, 30661, 30693,// 570
30725, 30758, 30790, 30823, 30855, 30887, 30920, 30952, 30984, 31016,// 580
31049, 31081, 31113, 31145, 31178, 31210, 31242, 31274, 31306, 31339,// 590
31371, 31403, 31435, 31467, 31499, 31531, 31564, 31596, 31628, 31660,// 600 //80行
31692, 31724, 31756, 31788, 31820, 31852, 31884, 31916, 31948, 31980,// 610
32012, 32043, 32075, 32107, 32139, 32171, 32203, 32235, 32267, 32298,// 620
32330, 32362, 32394, 32426, 32457, 32489, 32521, 32553, 32584, 32616,// 630
32648, 32679, 32711, 32743, 32774, 32806, 32838, 32869, 32901, 32932,// 640
32964, 32996, 33027, 33059, 33090, 33122, 33153, 33185, 33216, 33248 // 650 //85行
};
//获得ADC值
//ch:通道值 0~16
//返回值:转换结果
float Get_Adc(u8 ch)
{
float f_temp;//测得的温度值。
u16 fR;//测得的电阻值。
u16 i;
u16 zuidizuobiao;
u16 subscript;
u16 low_low=0;
u16 low_high=0;
u16 high_low=0;
u16 high_high=0;
u16 do_change=0;//可以解析温度标志位。当为1时,完成了取表工作,可以解析。
ADC1->SQR3&=0XFFFFFFE0;//规则序列1 通道ch
ADC1->SQR3|=ch;
ADC1->CR2|=1<<30; //启动规则转换通道
while(!(ADC1->SR&1<<1));//等待转换结束
f_temp=ADC1->DR;
fR=((f_temp*(3.3/4096))/0.01)*100;//求出电阻值。0.004是电流值,*100是将电阻值放大100倍
if (fR < RTD_TAB_PT100[0]) // 电阻值小于表格最小值,低于量程下限。
{
// return BELOW_LOW_LIMITE;
}
else if (fR > RTD_TAB_PT100[858]) // 电阻值大于表格最大值,超出量程上限。
{
// return EXCESS_UP_LIMITE;
}
else
{
zuidizuobiao=0;//开始时,数组元素最底坐标为1.
subscript=858;//开始时, 数组元素个数为859.
for (i=10;i>0;i--) // 2分法查表。到底循环多少次还需要计算一下。
{
if(do_change==0)//有问题,2个if函数调换则不行。进不了下面的if函数。无法判断当前do_change的具体值。
{
if((subscript%2==1)&&(subscript!=1))//有余数,说明有公共区间。
{//允许subscript=1
subscript=subscript/2;
low_low=zuidizuobiao;
low_high=zuidizuobiao+subscript;
high_low=low_high;
high_high=high_low+subscript;
subscript=subscript+1;
if(subscript==2)//说明只剩下最后一个数了。
{
do_change=1;//可以解析了
}
else
{
if ((RTD_TAB_PT100[low_low] <= fR)&&(fR < RTD_TAB_PT100[low_high]))
{
zuidizuobiao = low_low;
}
else if ((RTD_TAB_PT100[high_low] <= fR)&&(fR <= RTD_TAB_PT100[high_high]))
{
zuidizuobiao = high_low;
}
else
{
}
}
}
else//无公共区间
{
subscript=subscript/2;
if(subscript==1)
{
if(fR == RTD_TAB_PT100[zuidizuobiao+1])
{
zuidizuobiao=zuidizuobiao+1;
}
else
{
zuidizuobiao=zuidizuobiao;
}
do_change=1;//可以解析了
LED1=0;
}
else
{
low_low=zuidizuobiao;
low_high=zuidizuobiao+subscript-1;
high_low=zuidizuobiao+subscript;
high_high=high_low+subscript-1;
if ((RTD_TAB_PT100[low_low] <= fR)&&(fR <= RTD_TAB_PT100[low_high]))
{
zuidizuobiao = low_low;
}
else if ((RTD_TAB_PT100[high_low] <= fR)&&(fR <= RTD_TAB_PT100[high_high]))
{
zuidizuobiao = high_low;
}
else
{
}
}
}
}
if(do_change==1)//说明找到了正确的区间.subscript的值为2。当前温度为zuidizuobiao所指向的温度
{
do_change=0;
if(RTD_TAB_PT100[zuidizuobiao]==10000)
{
f_temp=0.000;
}
else if(RTD_TAB_PT100[zuidizuobiao]<10000)//负值,需要另一种处理。25/1000
{
if(zuidizuobiao<=99)
{
f_temp=(float)(((zuidizuobiao/10)*10-190)-(9-zuidizuobiao%10)-(fR-RTD_TAB_PT100[zuidizuobiao])*0.025);
//(zuidizuobiao/10)*10-190 代表整数中十位和百位部分
//9-zuidizuobiao%10 代表整数中个位部分
//(fR-RTD_TAB_PT100[zuidizuobiao])*0.025 代表小数部分。
}
else
{
f_temp=(float)((zuidizuobiao/10*10-190)-(9-((zuidizuobiao%100)%10))-(fR-RTD_TAB_PT100[zuidizuobiao])*0.025);
//(zuidizuobiao/10)*10-190 代表整数中十位和百位部分
//(9-zuidizuobiao%100)%10 代表整数中个位部分
//(fR-RTD_TAB_PT100[zuidizuobiao])*0.025 代表小数部分。
}
}
else //else if(zuidizuobiao>10000)//正值,需要另一种处理。
{
f_temp=(float)(((zuidizuobiao/10)*10-200)+(zuidizuobiao%100)%10+(fR-RTD_TAB_PT100[zuidizuobiao])*0.025);
//(zuidizuobiao/10)*10-200) 代表整数中十位和百位部分
//(zuidizuobiao%100)%10 代表整数中个位部分
//(fR-RTD_TAB_PT100[zuidizuobiao])*0.025 代表小数部分。
}
break;
}
}
}
// printf("\r\n当前温度值为:\r\n");
// printf("%f",f_temp);
return f_temp; //返回adc值
}