本帖最后由 qwettyuio 于 2022-1-19 17:33 编辑
输入的CurrentAD不在查询的数组中时,返回距离最近的值下标,查询140时,没有该数,侧返回139下标,求指导一下,该怎么解决
C语言源程序如下:
uchar code tabe1[11] = {0xF0,0x00,0xD0,0x90,0x20,0xB0,0xF0,0x10,0xF0,0xB0};
uchar code tabe2[11] = {0xA0,0xA0,0x60,0xE0,0xE0,0xC0,0xC0,0xA0,0xE0,0xE0};
uchar a0,a1,a2;
uint num;
uchar code NTC_AD[141] = {
25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,45,46,47,48,50,51,52,54,55, //60℃ --- 87℃
56,58,59,30,62,63,65,66,68,69,71,72,74,75,77,79,80,82,83,85,87,88,90,92,93,95,97,98, //88℃ --- 115℃
100,102,103,105,107,108,110,112,112,115,116,118,120,121,123,125,126,128,130,131,133, //116℃ --- 136℃
134,136,137,139,141,142,144,145,147,148,150,151,152,154,155,157,158,159,161,162,163, //137℃ --- 157℃
165,166,167,169,170,171,172,173,174,175,176,177,178,179,181,182,183,184,185,186,187, //158℃ --- 178℃
188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209 //179℃ --- 200℃
};
/*******************************************************************************
* Function Name : Sheshi_Init()
* Description : 显示对应的温度以及熟度(℃)
*******************************************************************************/
void Sheshi_Init(uchar a0,uchar a1,uchar a2)
{
Write_1621(23,tabe1[a0],4);
Write_1621(24,tabe2[a0],4);
Write_1621(25,tabe1[a1],4);
Write_1621(26,tabe2[a1],4);
Write_1621(27,tabe1[a2],4);
Write_1621(28,tabe2[a2],4);
}
float Bin_Search(uchar CurrentAD)
{
int start = 0;
int end = 141;
int mid = 0;
while(start <= end)
{
mid =(start+end)/2;
if(CurrentAD==NTC_AD[mid]) break;
else if(CurrentAD>NTC_AD[mid])
{
start = mid +1;
}
else if(CurrentAD < NTC_AD[mid])
{
end = mid-1;
}
}
return mid;
}
void main( )
{
uchar CurrentAD=0;
Init_1621(); //初始化HT1621
HT1621_all_off(); //清空LCD显示
while(1)
{
CurrentAD=31;
num=Bin_Search(CurrentAD)+60;
a0=num/100;
a1=num/10%10;
a2=num%10;
Sheshi_Init(a0,a1,a2);
}
}
|