2015-06-04 229 views
0

當測量負溫度時,在我的LED輸出上沒有顯示正確的值。我看到類似4983的東西。我需要我的代碼的建議。Arduino溫度傳感器負溫度

我使用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

歡迎堆棧溢出!您可能想查看[如何提出問題](http://stackoverflow.com/help/how-to-ask)。正確地格式化您的問題將有助於您找到所需的答案。 –

回答

0

首先,改變:

Data [i] = ds.read(); 

data[i] = ds.read(); 

然後看看你會得到什麼樣的結果。

我不確定你的計算結果。我使用下面的代碼,以九個十六進制值轉換成溫度的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