标签:style blog class c code http
上次做的NTC测温方案,功能是实现了,但是有些不足的地方,例如AD值飘得厉害,温度值也就飘,尝试调整线性函数和映射AD值,从2^10位降到8位、7位测试其稳定性。
NTC选型,除了上次用的环氧树脂型的,这次选用不锈钢或铜镀锌外壳,和小黑头插件这两种做测试。
#define Pot A2 //NTC引脚命名
int PotBufferA = 0; //AD读取数据缓存变量
int PotBufferB = 0; //AD读取数据缓存变量
int PotBufferC = 0; //AD读取数据缓存变量
void setup()
{
Serial.begin(9600); //初始化串口波特率为9600
}
void loop()
{
PotBufferA = analogRead(Pot); //读取AD值
PotBufferB = map(PotBufferA,0,1023,0,255);
PotBufferC = map(PotBufferA,0,1023,0,128);
float ta = 0.1*PotBufferA-41;
//浮点运算,根据拟合的温度与电阻曲线的斜率换算反函数
float tb = 0.4*PotBufferB-40.85;
float tc = 0.8*PotBufferC-40.65;
Serial.print("T10 = "); //串口输出“T10 = ”
Serial.print(ta); //串口输出ta的值
Serial.print("; T8 = "); //串口输出“T8 = ”
Serial.print(tb); //串口输出tb的值
Serial.print("; T7 = "); //串口输出“T7 = ”
Serial.println(tc); //串口输出tc的值
Serial.print("AD10 = "); //串口输出“AD10 = ”
Serial.print(PotBufferA); //串口输出PotBufferA的值
Serial.print("; AD8 = "); //串口输出“AD8 = ”
Serial.print(PotBufferB); //串口输出PotBufferB的值
Serial.print("; AD7 = "); //串口输出“AD7 = ”
Serial.println(PotBufferC); //串口输出PotBufferC的值
Serial.println();
delay(5000); //延时500ms
}
map(value, fromLow, fromHigh, toLow, toHigh)
value:需要映射的值
fromLow:当前范围值的下限
fromHigh:当前范围值的上限
toLow:目标范围值的下限
toHigh:目标范围值的上限
将一个数从一个范围映射到另外一个范围。也就是说,会将 fromLow 到 fromHigh 之间的值映射到 toLow 在 toHigh 之间的值。
不限制值的范围,因为范围外的值有时是刻意的和有用的。如果需要限制的范围, constrain() 函数可以用于此函数之前或之后。
map() 函数使用整型数进行运算因此不会产生分数,这时运算应该表明它需要这样做。小数的余数部分会被舍去,不会四舍五入或者平均。
Arduino 温度传感器NTC温度AD对应值映射改进,布布扣,bubuko.com
标签:style blog class c code http
原文地址:http://blog.csdn.net/bitezijie/article/details/26104425