Arduino温度传感器负温度

时间:2015-06-04 14:34:53

标签: arduino sensor temperature

测量负温度时,我的LED输出没有显示正确的值。我看到像4983这样的东西。我的代码需要建议。

我使用arduino uno。 Senzor的类型:DS18B20

代码:

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display (OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp [] =
{B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000};

#if (SSD1306_LCDHEIGHT! = 64)
#error ("Error, Adafruit_SSD1306.h!");
#endif
#include <OneWire.h>
OneWire ds (2);

void setup () {
display.begin (SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay ();

}

void loop () {


byte i;
byte present = 0;
byte data[12];
byte addr[8];

if (! ds.search (addr)) {
Serial.print ("\ n");
ds.reset_search ();
return;
}

ds.reset ();
ds.select (addr);
ds.write (0x44,1); 
delay (1000); 
ds.reset ();
ds.select (addr);
ds.write (0xBE); 

for (i = 0; i <9; i ++) {
Data [i] = ds.read ();
}

Serial.print ("CRC ="); 
Serial.print (OneWire :: CRC8 (data, 8), HEX);
Serial.println ();
float Tc_100;
Tc_100 = (data [1] * 256. + data [0]) / 16 .;

Serial.print (Tc_100);


display.clearDisplay ();
display.setCursor (5,5);
display.setTextSize (1);
display.setTextColor (WHITE);
display.println ("TEMPERATURE"); 
display.setCursor (65,5);
display.setTextSize (1);
display.setTextColor (WHITE);
display.println ("MOTORA"); 
display.setCursor (109,5);
display.setTextSize (1);
display.setTextColor (WHITE);
display.println ("O"); 
display.setCursor (115,5);
display.setTextSize (1);
display.setTextColor (WHITE);
display.println ("C"); 
display.setCursor (30,28);
display.setTextSize (4);
display.setTextColor (WHITE);
display.print (Tc_100, 1); 
display.display ();
delay(1000);
}

1 个答案:

答案 0 :(得分:0)

首先,改变:

Data [i] = ds.read ();

data[i] = ds.read();

然后看看你得到了什么样的结果。

之后我不确定你的计算方法。我使用以下代码将9个十六进制值转换为DS18B20的温度:

  int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
  LowByte = data[0];
  HighByte = data[1];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) { // negative
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;


  if (SignBit) { // If its negative
     Serial.print("-");
  }
  Serial.print(Whole);
  Serial.print(".");
  if (Fract < 10) {
     Serial.print("0");
  }
  Serial.print(Fract);

  Serial.print("\n");

请参阅OneWire Temperature