单片机 AD转换 怎么变成小数

发布网友 发布时间:2022-04-23 13:36

我来回答

4个回答

热心网友 时间:2023-10-16 00:26

unsigned short AD_result; //AD转换结果

//碰到ATEMGA8不能浮点运算,我用查表,精确度不高

const unsigned short code[100] = { 785,775,765,755,745,735,725,714,704,693,/*0 -> +9*/
682,671,660,9,637,626,614,603,592,580,/*+10 -> +19*/
569,557,546,534,523,512,500,4,478,467,/*+20 -> +29*/
456,445,434,424,413,403,393,383,373,363,/*+30 -> +39*/
354,344,335,326,317,308,300,292,283,276,/*+40 -> +49*/
268,260,253,246,238,232,225,218,212,208,/*+50 -> +59*/
200,194,188,183,177,172,167,162,157,153,/*+60 -> +69*/
148,144,139,135,131,127,124,120,116,113,/*+70 -> +79*/
110,106,103,100, 97, 94, 92, , 86, 84,/*+80 -> +*/
81, 79, 77, 75, 72, 70, 68, 66, , 63 /*+90 -> +99*/
};
void get_AD_result(void)
{
char i = 0;

if (AD_READY == 1)
{
AD_READY = 0;

if (AD_result > 785)
{
temperature = 0;
return;
}
else if (AD_result < 63)
{
temperature = 99;
return;
}

for (i=0; i<100; i++)
{
if ((AD_result<=code[i]) && (AD_result>=code[i+1]))
{
if (AD_result > ((code[i]+code[i+1])>>1))
{
temperature = i;
}
else
{
temperature = i+1;
}
}
}
}
}

热心网友 时间:2023-10-16 00:26

不用浮点数来计算。
用整型数,完全可以计算无限的小数位数。
参考:
http://hi.baidu.com/%D7%F6%B6%F8%C2%DB%B5%C0/blog/item/37e0749500c51c15d21b7090.html

热心网友 时间:2023-10-16 00:26

unsigned short AD_result; //AD转换结果

//碰到ATEMGA8不能浮点运算,我用查表,精确度不高

const unsigned short code[100] = { 785,775,765,755,745,735,725,714,704,693,/*0 -> +9*/
682,671,660,9,637,626,614,603,592,580,/*+10 -> +19*/
569,557,546,534,523,512,500,4,478,467,/*+20 -> +29*/
456,445,434,424,413,403,393,383,373,363,/*+30 -> +39*/
354,344,335,326,317,308,300,292,283,276,/*+40 -> +49*/
268,260,253,246,238,232,225,218,212,208,/*+50 -> +59*/
200,194,188,183,177,172,167,162,157,153,/*+60 -> +69*/
148,144,139,135,131,127,124,120,116,113,/*+70 -> +79*/
110,106,103,100, 97, 94, 92, , 86, 84,/*+80 -> +*/
81, 79, 77, 75, 72, 70, 68, 66, , 63 /*+90 -> +99*/
};
void get_AD_result(void)
{
char i = 0;

if (AD_READY == 1)
{
AD_READY = 0;

if (AD_result > 785)
{
temperature = 0;
return;
}
else if (AD_result < 63)
{
temperature = 99;
return;
}

for (i=0; i<100; i++)
{
if ((AD_result<=code[i]) && (AD_result>=code[i+1]))
{
if (AD_result > ((code[i]+code[i+1])>>1))
{
temperature = i;
}
else
{
temperature = i+1;
}
}
}
}
}

热心网友 时间:2023-10-16 00:26

不用浮点数来计算。
用整型数,完全可以计算无限的小数位数。
参考:
http://hi.baidu.com/%D7%F6%B6%F8%C2%DB%B5%C0/blog/item/37e0749500c51c15d21b7090.html

热心网友 时间:2023-10-16 00:27

用C语言,汇编比较麻烦

热心网友 时间:2023-10-16 00:27

用C语言,汇编比较麻烦

热心网友 时间:2023-10-16 00:27

查表法

热心网友 时间:2023-10-16 00:27

查表法

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com