Now supports DS18B20 and DS18S20 Maxim/Dallas one-wire thermometer devices. Also supports DS18S20 in 9, 10, 11, and 12 bit resolution modes. 'Use Address' mode now checks if the correct device type is present, and informs the user which device to use. Correct temperature conversion times now used in non-parasitic mode. The device should be placed at least 6 inches (15 cm) from the mbed board in order to accurately read ambient temperature.
Fork of OneWireCRC by
Revision 1:a6643ebd84ec, committed 2015-10-15
- Comitter:
- rmcbee
- Date:
- Thu Oct 15 23:46:29 2015 +0000
- Parent:
- 0:01a6a40578c9
- Commit message:
- Initial Commit
Changed in this revision
diff -r 01a6a40578c9 -r a6643ebd84ec DS18B20.cpp --- a/DS18B20.cpp Sun Jan 03 11:57:31 2010 +0000 +++ b/DS18B20.cpp Thu Oct 15 23:46:29 2015 +0000 @@ -21,9 +21,9 @@ */ #include "DS18B20.h" -#include "DebugTrace.h" +//#include "DebugTrace.h" -DebugTrace pc_ds18B20(ON, TO_SERIAL); +//DebugTrace pc_ds18B20(ON, TO_SERIAL); DS18B20::DS18B20(bool crcOn, bool useAddr, bool parasitic, PinName pin) : OneWireThermometer(crcOn, useAddr, parasitic, pin, DS18B20_ID) @@ -81,23 +81,23 @@ { case nineBit: // 0.5 deg C increments read_temp &= 0xFFF8; // bits 2,1,0 are undefined - pc_ds18B20.traceOut("9 bit resolution ...\r\n"); + //pc_ds18B20.traceOut("9 bit resolution ...\r\n"); break; case tenBit: // 0.25 deg C increments read_temp &= 0xFFFC; // bits 1,0 are undefined - pc_ds18B20.traceOut("10 bit resolution ...\r\n"); + //pc_ds18B20.traceOut("10 bit resolution ...\r\n"); break; case elevenBit: // 0.125 deg C increments read_temp &= 0xFFFE; // bit 0 is undefined - pc_ds18B20.traceOut("11 bit resolution ...\r\n"); + //pc_ds18B20.traceOut("11 bit resolution ...\r\n"); break; case twelveBit: // 0.0625 deg C increments - pc_ds18B20.traceOut("12 bit resolution ...\r\n"); + //pc_ds18B20.traceOut("12 bit resolution ...\r\n"); break; } float realTemp = (float)read_temp/16 ; - pc_ds18B20.traceOut("TEMP_READ/REAL TEMP: %f \r\n", realTemp); + //pc_ds18B20.traceOut("TEMP_READ/REAL TEMP: %f \r\n", realTemp); return realTemp; } \ No newline at end of file
diff -r 01a6a40578c9 -r a6643ebd84ec DS18S20.cpp --- a/DS18S20.cpp Sun Jan 03 11:57:31 2010 +0000 +++ b/DS18S20.cpp Thu Oct 15 23:46:29 2015 +0000 @@ -21,9 +21,9 @@ */ #include "DS18S20.h" -#include "DebugTrace.h" +//#include "DebugTrace.h" -DebugTrace pc_ds18S20(ON, TO_SERIAL); +//DebugTrace pc_ds18S20(ON, TO_SERIAL); DS18S20::DS18S20(bool crcOn, bool useAddr, bool parasitic, PinName pin) : OneWireThermometer(crcOn, useAddr, parasitic, pin, DS18S20_ID) @@ -44,12 +44,12 @@ } float readTemp = (float)read_temp/2 ; // divide by 2 - pc_ds18S20.traceOut("TEMP_READ: %f \r\n", readTemp); // 9 bit resolution value + //pc_ds18S20.traceOut("TEMP_READ: %f \r\n", readTemp); // 9 bit resolution value // convert to real temperature float tempCount = float(data[COUNT_PER_DEG_BYTE] - data[COUNT_REMAIN_BYTE])/(float)data[COUNT_PER_DEG_BYTE]; float realTemp = (readTemp - 0.25) + tempCount; - pc_ds18S20.traceOut("Temperature: %f \r\n", realTemp); // enhanced resolution value + //pc_ds18S20.traceOut("Temperature: %f \r\n", realTemp); // enhanced resolution value return realTemp; } \ No newline at end of file
diff -r 01a6a40578c9 -r a6643ebd84ec DebugTrace.cpp --- a/DebugTrace.cpp Sun Jan 03 11:57:31 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,143 +0,0 @@ -/* -* DebugTrace. Allows dumping debug messages/values to serial or -* to file. -* -* Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk> -* -* This file is part of DebugTrace. -* -* DebugTrace is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* DebugTrace is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with DebugTrace. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include "DebugTrace.h" -#include <mbed.h> -#include <stdarg.h> -#include <string.h> - -Serial logSerial(USBTX, USBRX); -LocalFileSystem local("local"); - -const char* FILE_PATH = "/local/"; -const char* EXTN = ".bak"; - -DebugTrace::DebugTrace(eLog on, eLogTarget mode, const char* fileName, int maxSize) : - enabled(on), logMode(mode), maxFileSize(maxSize), currentFileSize(0), - logFileStatus(0) -{ - // allocate memory for file name strings - int str_size = (strlen(fileName) + strlen(FILE_PATH) + strlen(EXTN) + 1) * sizeof(char); - logFile = (char*)malloc(str_size); - logFileBackup = (char*)malloc(str_size); - - // add path to log file name - strcpy(logFile, FILE_PATH); - strcat(logFile, fileName); - - // create backup file name - strcpy(logFileBackup, logFile); - strcpy(logFileBackup, strtok(logFileBackup, ".")); - strcat(logFileBackup, EXTN); -} - -DebugTrace::~DebugTrace() -{ - // dust to dust, ashes to ashes - if (logFile != NULL) free(logFile); - if (logFileBackup != NULL) free(logFileBackup); -} - -void DebugTrace::clear() -{ - // don't care about whether these fail - remove(logFile); - remove(logFileBackup); -} - -void DebugTrace::backupLog() -{ - // delete previous backup file - if (remove(logFileBackup)) - { - // standard copy stuff - char ch; - FILE* to = fopen(logFileBackup, "wb"); - if (NULL != to) - { - FILE* from = fopen(logFile, "rb"); - if (NULL != from) - { - while(!feof(from)) - { - ch = fgetc(from); - if (ferror(from)) break; - - if(!feof(from)) fputc(ch, to); - if (ferror(to)) break; - } - } - - if (NULL != from) fclose(from); - if (NULL != to) fclose(to); - } - } - - // now delete the log file, so we are ready to start again - // even if backup creation failed - the show must go on! - logFileStatus = remove(logFile); -} - -void DebugTrace::traceOut(const char* fmt, ...) -{ - if (enabled) - { - va_list ap; // argument list pointer - va_start(ap, fmt); - - if (TO_SERIAL == logMode) - { - vfprintf(logSerial, fmt, ap); - } - else // TO_FILE - { - if (0 == logFileStatus) // otherwise we failed to remove a full log file - { - // Write data to file. Note the file size may go over limit - // as we check total size afterwards, using the size written to file. - // This is not a big issue, as this mechanism is only here - // to stop the file growing unchecked. Just remember log file sizes may - // be some what over (as apposed to some what under), so don't push it - // with the max file size. - FILE* fp = fopen(logFile, "a"); - if (NULL == fp) - { - va_end(ap); - return; - } - int size_written = vfprintf(fp, fmt, ap); - fclose(fp); - - // check if we are over the max file size - // if so backup file and start again - currentFileSize += size_written; - if (currentFileSize >= maxFileSize) - { - backupLog(); - currentFileSize = 0; - } - } - } - - va_end(ap); - } -}
diff -r 01a6a40578c9 -r a6643ebd84ec DebugTrace.h --- a/DebugTrace.h Sun Jan 03 11:57:31 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -/* -* DebugTrace. Allows dumping debug messages/values to serial or -* to file. -* -* Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk> -* -* This file is part of DebugTrace. -* -* DebugTrace is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* DebugTrace is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with DebugTrace. If not, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef SNATCH59_DEBUGTRACE_H -#define SNATCH59_DEBUGTRACE_H - -enum eLog {OFF, ON}; -enum eLogTarget {TO_SERIAL, TO_FILE}; - -class DebugTrace -{ -public: - DebugTrace(eLog on, eLogTarget mode, const char* fileName = "log.txt", const int maxSize = 1024); - ~DebugTrace(); - - void clear(); - void traceOut(const char* fmt, ...); - -private: - eLog enabled; - eLogTarget logMode; - int maxFileSize; - int currentFileSize; - char* logFile; - char* logFileBackup; - int logFileStatus; // if things go wrong, don't write any more data to file - - void backupLog(); -}; - -#endif
diff -r 01a6a40578c9 -r a6643ebd84ec OneWireThermometer.cpp --- a/OneWireThermometer.cpp Sun Jan 03 11:57:31 2010 +0000 +++ b/OneWireThermometer.cpp Thu Oct 15 23:46:29 2015 +0000 @@ -22,9 +22,9 @@ #include "OneWireThermometer.h" #include "OneWireDefs.h" -#include "DebugTrace.h" +//#include "DebugTrace.h" -DebugTrace pc(ON, TO_SERIAL); +//DebugTrace pc(ON, TO_SERIAL); // constructor specifies standard speed for the 1-Wire comms OneWireThermometer::OneWireThermometer(bool crcOn, bool useAddr, bool parasitic, PinName pin, int device_id) : @@ -42,27 +42,27 @@ // - not really needed except for device validation if using skipROM() if (useAddress) { - pc.traceOut("\r\n"); - pc.traceOut("New Scan\r\n"); + //pc.traceOut("\r\n"); + //pc.traceOut("New Scan\r\n"); oneWire.resetSearch(); if (!oneWire.search(address)) // search for 1-wire device address { - pc.traceOut("No more addresses.\r\n"); + //pc.traceOut("No more addresses.\r\n"); wait(2); return false; } - pc.traceOut("Address = "); + //pc.traceOut("Address = "); for (int i = 0; i < ADDRESS_SIZE; i++) { - pc.traceOut("%x ", (int)address[i]); + // pc.traceOut("%x ", (int)address[i]); } - pc.traceOut("\r\n"); + //pc.traceOut("\r\n"); if (OneWireCRC::crc8(address, ADDRESS_CRC_BYTE) != address[ADDRESS_CRC_BYTE]) // check address CRC is valid { - pc.traceOut("CRC is not valid!\r\n"); + //pc.traceOut("CRC is not valid!\r\n"); wait(2); return false; } @@ -70,20 +70,20 @@ if (address[0] != deviceId) { // Make sure it is a one-wire thermometer device - if (DS18B20_ID == deviceId) - pc.traceOut("You need to use a DS1820 or DS18S20 for correct results.\r\n"); - else if (DS18S20_ID == deviceId) - pc.traceOut("You need to use a DS18B20 for correct results.\r\n"); + if (DS18B20_ID == deviceId); + // pc.traceOut("You need to use a DS1820 or DS18S20 for correct results.\r\n"); + else if (DS18S20_ID == deviceId); + //pc.traceOut("You need to use a DS18B20 for correct results.\r\n"); else - pc.traceOut("Device is not a DS18B20/DS1820/DS18S20 device.\r\n"); + //pc.traceOut("Device is not a DS18B20/DS1820/DS18S20 device.\r\n"); wait(2); return false; } else { - if (DS18B20_ID == deviceId) pc.traceOut("DS18B20 present and correct.\r\n"); - if (DS18S20_ID == deviceId) pc.traceOut("DS1820/DS18S20 present and correct.\r\n"); + if (DS18B20_ID == deviceId) ;//pc.traceOut("DS18B20 present and correct.\r\n"); + if (DS18S20_ID == deviceId);// pc.traceOut("DS1820/DS18S20 present and correct.\r\n"); } } @@ -115,20 +115,20 @@ resetAndAddress(); oneWire.writeByte(READSCRATCH); // read Scratchpad - pc.traceOut("read = "); + //pc.traceOut("read = "); for (int i = 0; i < THERMOM_SCRATCHPAD_SIZE; i++) { // we need all bytes which includes CRC check byte data[i] = oneWire.readByte(); - pc.traceOut("%x ", (int)data[i]); + //pc.traceOut("%x ", (int)data[i]); } - pc.traceOut("\r\n"); + //pc.traceOut("\r\n"); // Check CRC is valid if you want to if (useCRC && !(OneWireCRC::crc8(data, THERMOM_CRC_BYTE) == data[THERMOM_CRC_BYTE])) { // CRC failed - pc.traceOut("CRC FAILED... \r\n"); + //pc.traceOut("CRC FAILED... \r\n"); dataOk = false; }
diff -r 01a6a40578c9 -r a6643ebd84ec main.cpp --- a/main.cpp Sun Jan 03 11:57:31 2010 +0000 +++ b/main.cpp Thu Oct 15 23:46:29 2015 +0000 @@ -35,7 +35,7 @@ int main() { // device( crcOn, useAddress, parasitic, mbed pin ) - THERMOMETER device(true, true, false, p25); + THERMOMETER device(true, true, false, D10); while (!device.initialize()); // keep calling until it works
diff -r 01a6a40578c9 -r a6643ebd84ec mbed.bld --- a/mbed.bld Sun Jan 03 11:57:31 2010 +0000 +++ b/mbed.bld Thu Oct 15 23:46:29 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/49a220cc26e0 +http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68 \ No newline at end of file