适用于小空间mcu的移位查表(ntc查表输出温度)
需要数组从小到大排列,电路是ntc上拉所以数组和ad值都用4095相减。
点击查看代码
void Temp()
{
u8 temp,count;
u16 temp_ad_v;
temp_ad_v = 4095 - Board_GetAD_12bit(ADC_CH4);
temp = 0x00;
count = 0x40; //0x20,MAX:63; 0x40,MAX:127; 0x80,MAX:191(移位超过位数异常)
/*
0x40|0x20=0x60=96
0x40|0x20|0x10=0x70=112
0x40|0x20|0x10|0x08|0x04|0x02|0x01=0x7F=127
*/
while(count)
{
if((count|temp) > 100)
{
count >>= 1;
continue;
}
if(temp_ad_v >= ad_temp_12bit[count|temp]) temp |= count;
if(temp > 100) temp = 100;
count >>= 1;
}
TempResult = temp;
}
验证代码:
点击查看代码
#include <stdio.h>
unsigned int temp,count;
unsigned int Tab[200];
int main()
{
unsigned int i,value;
for(i=0;i<200;i++)
{
Tab[i] = i;
printf("%d\r\n",Tab[i]);
}
value = 0;
for(i=0;i<200;i++)
{
temp = 0x00;
count = 0x40;
while(count)
{
//printf("count|temp=%d\r\n",count|temp);
// if((count|temp) > 100)
// {
// count >>= 1;
// continue;
// }
if(value >= Tab[(count|temp)]) temp |= count;
// if(temp > 100) temp = 100;
count >>= 1;
}
printf("value=%d,temp=%d\r\n",value,temp);
value++;
}
return 1;
}