Nedenstående sketch er lavet for Dallas Semiconductor’s DS18B20, i dag ejet af Maxim Integrated Products (Maxim).
Ja, som det kan ses af nedenstående, så skriver jeg nogle gange på Engelsk.
Teksten til højre for // er en kort beskrivelse af hvad hver linje gør.
/* * Temperature measurement with * Arduino Uno * http://arduino.cc/en/Main/arduinoBoardUno * + Maxim DS18B20 (old Dallas Semiconductor) * http://datasheets.maximintegrated.com/en/ds/DS18B20.pdf * + SparkFun 4 Digit 7-Segment Serial Display - Kelly Green - COM-11440 * http://datasheets.maximintegrated.com/en/ds/DS18B20.pdf * or a look a like... * * Program * Clearing the Display and setting the brightness level. * Reading out the temperature in a loop. * * ERROR codes * If display shows E1 meaning Device is not recognized (has not been tested) * If display shows E2 meaning CRC is not valid! * * Credentials * The library OneWire are from * http://playground.arduino.cc/Learning/OneWire * The main code regarding the DS18B20 are from * http://bildr.org/2011/07/ds18b20-arduino/ * The library SevSegFloat are from * https://github.com/ahalekelly/SevSegFloat */ #include <OneWire.h> // Include the library int DS18S20_Pin = 2; // DS18S20 Signal pin on digital 2 OneWire ds(DS18S20_Pin); #include <SevSegFloat.h> // Include the library SevSegFloat SevSeg(Serial); // Make an instance of SevSegFloat on the Serial Stream called SevSeg float printVal = 0.12345; // The value to print void setup(void) // Run once, when the sketch starts { Serial.begin(9600); // Setting communication up at 9600 bps on TX pin at Arduino Uno SevSeg.send(0x76); // Clear the display and set for Digit 1 delay(500); // Delay for clearing the Display SevSeg.send(0x7A); // Command for brightness SevSeg.send(0x01); // Brightness level delay(10); // Delay for changing brightness } void loop(void) // Run over and over again { float temperature = getTemp(); SevSeg.send(temperature); // Send data delay(1000); // Delay before next conversion / read out } float getTemp() // Returns the temperature from one DS18S20 in DEG Celsius { byte data[12]; byte addr[8]; if ( !ds.search(addr)) // If no more sensors on chain, reset search { ds.reset_search(); return -1000; } if ( OneWire::crc8( addr, 7) != addr[7]) { Serial.write(" E1 "); // CRC is not valid! delay(5000); // Time for Display read out } if ( addr[0] != 0x10 && addr[0] != 0x28) { Serial.write(" E2 "); // Device is not recognized delay(5000); // Time for Display read out } ds.reset(); ds.select(addr); ds.write(0x44,1); // Start conversion, with parasite power on at the end byte present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad for (int i = 0; i < 9; i++) // We need 9 bytes { data[i] = ds.read(); } ds.reset_search(); byte MSB = data[1]; byte LSB = data[0]; float tempRead = ((MSB << 8) | LSB); // Using two's compliment float TemperatureSum = tempRead / 16; return TemperatureSum; }
Den opmærksomme læser vil nok bemærke, at der ingen midling er af temperaturen.
SevSegFloat biblioteket scalere automatisk udlæsningen så der f.x. vises 22.12, 9.125, -.125, -1.25 og -10.5.