📄 temprature.c
字号:
#include <stdio.h>
#include <math.h>
double calculate_exponent();
int calculate_sign();
double calculate_mantissa();
double ieee_to_decimal();
double temprature();
double calculate_exponent(int eb)/*计算e的值*/
{
double exponent = 0;
exponent = eb - 127;
return (exponent);
}
int calculate_sign(int high_byte)/*计算符号位*/
{
if(128 & high_byte)
return (1);
else
return (0);
}
double calculate_mantissa(int high_byte, int mid_byte, int low_byte)/*计算mantissa的值*/
{
int power = 0;
int multiplier = 0;
float mantissa = 0;
long int mantissa_bits;
long int index = 8388608; /* 0b 100000000000000000000000 */
high_byte &= 127; /* strip off sign bit. */
mantissa_bits = (low_byte + (mid_byte * 256) + (high_byte * 65536) + 8388608);/*组合成标准的long int*/
for (power = 0; power >= -23; power--)/**/
{
if ((mantissa_bits & index)!=0)
multiplier = 1;
else
multiplier = 0;
mantissa = mantissa + (pow(2,power) * multiplier);
index = index/2; /* index向右移一位 */
}
return (mantissa);
}
double ieee_to_decimal(int eb, int high_byte, int mid_byte, int low_byte)
{
double decimal_result = 0;
double mantissa = 0;
double exponent = 0;
int sign = 0; /* 0 = positive, 1 = negative */
mantissa = calculate_mantissa(high_byte,mid_byte,low_byte);
exponent = calculate_exponent(eb);
sign = calculate_sign (high_byte);
decimal_result = ((pow(-1,sign))*(mantissa)*pow(2,exponent));
return (decimal_result);
}
/*double pow(x,y)是math.h里面的标准函数:是计算 x exp(y)的值 就是计算x的y次幂*/
double temprature(int temp,int ad2,int ad3,int ad1,int cal1_H,int cal1_L,int cal2_H,int cal2_L,int temp1_H,int temp1_L,int temp2_H,int temp2_L)
{
double cal1,cal2,temp1,temp2; /*要定义成全局变量*/
double N1,Vin,tempbuf;
int lowbyte,midbyte,highbyte,exp;
lowbyte=(cal1_H>>8);
midbyte=(cal1_H&0x0f);
highbyte=(cal1_L>>8);
exp=(cal1_L&0x0f);
cal1=ieee_to_decimal(exp, highbyte,midbyte, lowbyte);
lowbyte=(cal2_H>>8);
midbyte=(cal2_H&0x0f);
highbyte=(cal2_L>>8);
exp=(cal2_L&0x0f);
cal2=ieee_to_decimal(exp, highbyte,midbyte, lowbyte);
lowbyte=(temp1_H>>8);
midbyte=(temp1_H&0x0f);
highbyte=(temp1_L>>8);
exp=(temp1_L&0x0f);
temp1=ieee_to_decimal(exp, highbyte,midbyte, lowbyte);
lowbyte=(temp2_H>>8);
midbyte=(temp2_H&0x0f);
highbyte=(temp2_L>>8);
exp=(temp2_L&0x0f);
temp2=ieee_to_decimal(exp, highbyte,midbyte, lowbyte);
N1=ad3-(ad2-ad3)*cal1;
Vin=(temp-N1)/(ad1-N1)*cal2;
tempbuf=25+(Vin-temp1)/temp2;
return(tempbuf);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -