CodeWarrior 编译器警告 : "Possible loss of data"

标签 c compiler-warnings microcontroller codewarrior analog-digital-converter

请考虑以下代码:

unsigned int  beta;

unsigned int theta;

unsigned int m = 4;

unsigned int c = 986;

unsigned int Rpre = 49900;



unsigned int getUAnalog(unsigned char channel) // to get the PT1000 Signal

{

unsigned int result;

unsigned int f_temp;

//select channel and initiate conversion

ADCSC1 |= (channel & 0b11111);



//wait until conversion is complete

while(!ADCSC1_COCO);



f_temp = ADCRH;

f_temp <<= 8;

f_temp += ADCRL;



beta = (((f_temp) / (4096*28))*1000); // warning: possible loss of data.

theta = ((((beta)*(Rpre))/(1-beta))*1000);

result = (theta-c)/(m);

return result;
}

我在 CodeWarrior 5.9.0 版上使用 MC9S08DZ60 ( http://www.freescale.com/files/microcontrollers/doc/data_sheet/MC9S08DZ60.pdf ) 和 PT1000 温度传感器。此函数用于计算温度并返回“结果”。但是“beta”和“theta”值仍然为 0。没有变化。

我还收到 C2705 警告:可能丢失数据。 “结果”的值(value)是不对的。请帮忙,因为我不知道出了什么问题!

提前致谢!

最佳答案

您的 4096*28 不适合 16 位无符号整数,将被截断,给您错误的结果,这就是您收到警告的原因。

但最重要的是......

这个

beta = (((f_temp) / (4096*28))*1000); // warning: possible loss of data.

theta = ((((beta)*(Rpre))/(1-beta))*1000);

result = (theta-c)/(m);

相当于

beta = (((f_temp) / (4096*28))*1000);

theta = ((((beta)*(49900))/(1-beta))*1000);

result = (theta-986)/(4);

反过来相当于:

result = ((((((((f_temp) / (4096*28))*1000))*(49900))/(1-(((f_temp) / (4096*28))*1000)))*1000)-986)/(4)

如果你plot it ,你会看到有一个 discontinuity at f_temp = 14336/125 ≈ 115the range of result is from -986/4 (≈-247) at f_temp=0 to ≈-4*109 at f_temp=115 or -∞ at f_temp=14336/125 .

这表明要么你做错了什么(你有错误的公式或常量),要么你没有给我们足够的信息(f_temp 的有效范围是很高兴有)。由于 result 的范围,使用整数算法进行这些计算会出现问题。

关于CodeWarrior 编译器警告 : "Possible loss of data",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15950514/

相关文章:

c++ - 是否可以成功包装退出方法?

ios - Xcode 7 : Warnings when API version is higher than target version?

compiler-warnings - 对 SWI Prolog 编译器警告感到困惑

assembly - 延迟循环 PIC 汇编代码在面包板上不起作用

c - 在Windows中使用C在两个线程之间共享缓冲区的方法有哪些?

缓存模拟器::命中与未命中问题

c - 在C中随机化一个字符串

c++ - 如何摆脱 boost::flyweight 在 VS2008 中产生的 C4800 警告

c - 作为二元运算的数学函数

c - 为什么选择 Bitshift 运算符?