Arduino的328芯片内部的温度计及读出方法

常见的Arduino的avr芯片内都有一个温度传感器,用于监测异常高温便于编程提供保护。含有温度计的arduino芯片列表如下(yes为有):

ATmega8 : No
ATmega8L : No
ATmega8A : No
ATmega168 : No
ATmega168A : Yes
ATmega168P : Yes
ATmega328 : Yes
ATmega328P : Yes
ATmega1280 (Arduino Mega) : No
ATmega2560 (Arduino Mega 2560) : No
ATmega32U4 (Arduino Leonardo) : Yes

所以常用的UNO有,2560就没有。

可以使用下面的程序来读出内部温度计的读数,只有刚开机时的温度才是室温,随后随着芯片工作产热,温度会有上升。程序里提供的温度单位是摄氏度,由串口监视窗口显出,准确性依赖于GetTemp函数里t的线性转换公式,你可以改变104.31这个数来校准读数。

// Internal Temperature Sensor
// Example sketch for ATmega328 types.
//
// April 2012, Arduino 1.0

void setup()
{
Serial.begin(9600);

Serial.println(F("Internal Temperature Sensor"));
}

void loop()
{
// Show the temperature in degrees Celcius.
Serial.println(GetTemp(),1);
delay(1000);
}

double GetTemp(void)
{
unsigned int wADC;
double t;

// The internal temperature has to be used
// with the internal reference of 1.1V.
// Channel 8 can not be selected with
// the analogRead function yet.

// Set the internal reference and mux.
ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));
ADCSRA |= _BV(ADEN); // enable the ADC

delay(20); // wait for voltages to become stable.

ADCSRA |= _BV(ADSC); // Start the ADC

// Detect end-of-conversion
while (bit_is_set(ADCSRA,ADSC));

// Reading register "ADCW" takes care of how to read ADCL and ADCH.
wADC = ADCW;

// The offset of 324.31 could be wrong. It is just an indication.
t = (wADC - 104.31 ) / 1.22;

// The returned temperature is in degrees Celcius.
return (t);
}

参考网站: http://playground.arduino.cc//Main/InternalTemperatureSensor
http://tieba.baidu.com/p/2114029581

坐沙发

发表评论