affichage fonctionne, valeurs reçues (255)

Dependencies:   mbed

Fork of Main_V3_Old by EI2I_4_projet_1_2017-2018

Committer:
Aureb29
Date:
Mon Jan 15 11:22:40 2018 +0000
Revision:
9:e79b746421b1
Parent:
5:ae57f8977663
Main V3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ChrisnieM 4:784c4b53a3d4 1 /*
ChrisnieM 4:784c4b53a3d4 2 * OneWireThermometer. Base class for Maxim One-Wire Thermometers.
ChrisnieM 4:784c4b53a3d4 3 * Uses the OneWireCRC library.
ChrisnieM 4:784c4b53a3d4 4 *
ChrisnieM 4:784c4b53a3d4 5 * Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk>
ChrisnieM 4:784c4b53a3d4 6 *
ChrisnieM 4:784c4b53a3d4 7 * This file is part of OneWireThermometer.
ChrisnieM 4:784c4b53a3d4 8 *
ChrisnieM 4:784c4b53a3d4 9 * OneWireThermometer is free software: you can redistribute it and/or modify
ChrisnieM 4:784c4b53a3d4 10 * it under the terms of the GNU General Public License as published by
ChrisnieM 4:784c4b53a3d4 11 * the Free Software Foundation, either version 3 of the License, or
ChrisnieM 4:784c4b53a3d4 12 * (at your option) any later version.
ChrisnieM 4:784c4b53a3d4 13 *
ChrisnieM 4:784c4b53a3d4 14 * OneWireThermometer is distributed in the hope that it will be useful,
ChrisnieM 4:784c4b53a3d4 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ChrisnieM 4:784c4b53a3d4 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ChrisnieM 4:784c4b53a3d4 17 * GNU General Public License for more details.
ChrisnieM 4:784c4b53a3d4 18 *
ChrisnieM 4:784c4b53a3d4 19 * You should have received a copy of the GNU General Public License
ChrisnieM 4:784c4b53a3d4 20 * along with OneWireThermometer. If not, see <http://www.gnu.org/licenses/>.
ChrisnieM 4:784c4b53a3d4 21 */
ChrisnieM 4:784c4b53a3d4 22
ChrisnieM 4:784c4b53a3d4 23 #include "OneWireThermometer.h"
ChrisnieM 4:784c4b53a3d4 24 #include "OneWireDefs.h"
ChrisnieM 4:784c4b53a3d4 25 #include "DebugTrace.h"
ChrisnieM 4:784c4b53a3d4 26
ChrisnieM 4:784c4b53a3d4 27 DebugTrace pc(ON, TO_SERIAL);
ChrisnieM 4:784c4b53a3d4 28
ChrisnieM 4:784c4b53a3d4 29 // constructor specifies standard speed for the 1-Wire comms
ChrisnieM 4:784c4b53a3d4 30 OneWireThermometer::OneWireThermometer(bool crcOn, bool useAddr, bool parasitic, PinName pin, int device_id) :
ChrisnieM 4:784c4b53a3d4 31 useCRC(crcOn), useAddress(useAddr), useParasiticPower(parasitic),
ChrisnieM 4:784c4b53a3d4 32 oneWire(pin, STANDARD), deviceId(device_id), resolution(twelveBit)
ChrisnieM 4:784c4b53a3d4 33 {
ChrisnieM 4:784c4b53a3d4 34 // NOTE: the power-up resolution of a DS18B20 is 12 bits. The DS18S20's resolution is always
ChrisnieM 4:784c4b53a3d4 35 // 9 bits + enhancement, but we treat the DS18S20 as fixed to 12 bits for calculating the
ChrisnieM 4:784c4b53a3d4 36 // conversion time Tconv.
ChrisnieM 4:784c4b53a3d4 37 }
ChrisnieM 4:784c4b53a3d4 38
ChrisnieM 4:784c4b53a3d4 39 bool OneWireThermometer::initialize()
ChrisnieM 4:784c4b53a3d4 40 {
ChrisnieM 4:784c4b53a3d4 41 // get the device address for use in selectROM() when reading the temperature
ChrisnieM 4:784c4b53a3d4 42 // - not really needed except for device validation if using skipROM()
ChrisnieM 4:784c4b53a3d4 43 if (useAddress)
ChrisnieM 4:784c4b53a3d4 44 {
ChrisnieM 4:784c4b53a3d4 45 pc.traceOut("\r\n");
ChrisnieM 4:784c4b53a3d4 46 pc.traceOut("New Scan\r\n");
ChrisnieM 4:784c4b53a3d4 47
ChrisnieM 4:784c4b53a3d4 48 oneWire.resetSearch();
ChrisnieM 4:784c4b53a3d4 49 if (!oneWire.search(address)) // search for 1-wire device address
ChrisnieM 4:784c4b53a3d4 50 {
ChrisnieM 4:784c4b53a3d4 51 pc.traceOut("No more addresses.\r\n");
ChrisnieM 4:784c4b53a3d4 52 wait(2);
ChrisnieM 4:784c4b53a3d4 53 return false;
ChrisnieM 4:784c4b53a3d4 54 }
ChrisnieM 4:784c4b53a3d4 55
ChrisnieM 4:784c4b53a3d4 56 pc.traceOut("Address = ");
ChrisnieM 4:784c4b53a3d4 57 for (int i = 0; i < ADDRESS_SIZE; i++)
ChrisnieM 4:784c4b53a3d4 58 {
ramialjed 5:ae57f8977663 59 pc.traceOut("%x ", (int)address[i]);
ChrisnieM 4:784c4b53a3d4 60 }
ChrisnieM 4:784c4b53a3d4 61 pc.traceOut("\r\n");
ChrisnieM 4:784c4b53a3d4 62
ChrisnieM 4:784c4b53a3d4 63 if (OneWireCRC::crc8(address, ADDRESS_CRC_BYTE) != address[ADDRESS_CRC_BYTE]) // check address CRC is valid
ChrisnieM 4:784c4b53a3d4 64 {
ChrisnieM 4:784c4b53a3d4 65 pc.traceOut("CRC is not valid!\r\n");
ChrisnieM 4:784c4b53a3d4 66 wait(2);
ChrisnieM 4:784c4b53a3d4 67 return false;
ChrisnieM 4:784c4b53a3d4 68 }
ChrisnieM 4:784c4b53a3d4 69
ChrisnieM 4:784c4b53a3d4 70 if (address[0] != deviceId)
ChrisnieM 4:784c4b53a3d4 71 {
ChrisnieM 4:784c4b53a3d4 72 // Make sure it is a one-wire thermometer device
ChrisnieM 4:784c4b53a3d4 73 if (DS18B20_ID == deviceId)
ChrisnieM 4:784c4b53a3d4 74 pc.traceOut("You need to use a DS1820 or DS18S20 for correct results.\r\n");
ChrisnieM 4:784c4b53a3d4 75 else if (DS18S20_ID == deviceId)
ChrisnieM 4:784c4b53a3d4 76 pc.traceOut("You need to use a DS18B20 for correct results.\r\n");
ChrisnieM 4:784c4b53a3d4 77 else
ChrisnieM 4:784c4b53a3d4 78 pc.traceOut("Device is not a DS18B20/DS1820/DS18S20 device.\r\n");
ChrisnieM 4:784c4b53a3d4 79
ChrisnieM 4:784c4b53a3d4 80 wait(2);
ChrisnieM 4:784c4b53a3d4 81 return false;
ChrisnieM 4:784c4b53a3d4 82 }
ChrisnieM 4:784c4b53a3d4 83 else
ChrisnieM 4:784c4b53a3d4 84 {
ChrisnieM 4:784c4b53a3d4 85 if (DS18B20_ID == deviceId) pc.traceOut("DS18B20 present and correct.\r\n");
ChrisnieM 4:784c4b53a3d4 86 if (DS18S20_ID == deviceId) pc.traceOut("DS1820/DS18S20 present and correct.\r\n");
ChrisnieM 4:784c4b53a3d4 87 }
ChrisnieM 4:784c4b53a3d4 88 }
ChrisnieM 4:784c4b53a3d4 89
ChrisnieM 4:784c4b53a3d4 90 return true;
ChrisnieM 4:784c4b53a3d4 91 }
ChrisnieM 4:784c4b53a3d4 92
ChrisnieM 4:784c4b53a3d4 93 // NOTE ON USING SKIP ROM: ok to use before a Convert command to get all
ChrisnieM 4:784c4b53a3d4 94 // devices on the bus to do simultaneous temperature conversions. BUT can
ChrisnieM 4:784c4b53a3d4 95 // only use before a Read Scratchpad command if there is only one device on the
ChrisnieM 4:784c4b53a3d4 96 // bus. For purpose of this library it is assumed there is only one device
ChrisnieM 4:784c4b53a3d4 97 // on the bus.
ChrisnieM 4:784c4b53a3d4 98 void OneWireThermometer::resetAndAddress()
ChrisnieM 4:784c4b53a3d4 99 {
ChrisnieM 4:784c4b53a3d4 100 oneWire.reset(); // reset device
ChrisnieM 4:784c4b53a3d4 101 if (useAddress)
ChrisnieM 4:784c4b53a3d4 102 {
ChrisnieM 4:784c4b53a3d4 103 oneWire.matchROM(address); // select which device to talk to
ChrisnieM 4:784c4b53a3d4 104 }
ChrisnieM 4:784c4b53a3d4 105 else
ChrisnieM 4:784c4b53a3d4 106 {
ChrisnieM 4:784c4b53a3d4 107 oneWire.skipROM(); // broadcast
ChrisnieM 4:784c4b53a3d4 108 }
ChrisnieM 4:784c4b53a3d4 109 }
ChrisnieM 4:784c4b53a3d4 110
ChrisnieM 4:784c4b53a3d4 111 bool OneWireThermometer::readAndValidateData(BYTE* data)
ChrisnieM 4:784c4b53a3d4 112 {
ChrisnieM 4:784c4b53a3d4 113 bool dataOk = true;
ChrisnieM 4:784c4b53a3d4 114
ChrisnieM 4:784c4b53a3d4 115 resetAndAddress();
ChrisnieM 4:784c4b53a3d4 116 oneWire.writeByte(READSCRATCH); // read Scratchpad
ChrisnieM 4:784c4b53a3d4 117
ramialjed 5:ae57f8977663 118 // pc.traceOut("read = ");
ChrisnieM 4:784c4b53a3d4 119 for (int i = 0; i < THERMOM_SCRATCHPAD_SIZE; i++)
ChrisnieM 4:784c4b53a3d4 120 {
ChrisnieM 4:784c4b53a3d4 121 // we need all bytes which includes CRC check byte
ChrisnieM 4:784c4b53a3d4 122 data[i] = oneWire.readByte();
ramialjed 5:ae57f8977663 123 // pc.traceOut("%x ", (int)data[i]);
ChrisnieM 4:784c4b53a3d4 124 }
ramialjed 5:ae57f8977663 125 //pc.traceOut("\r\n");
ChrisnieM 4:784c4b53a3d4 126
ChrisnieM 4:784c4b53a3d4 127 // Check CRC is valid if you want to
ChrisnieM 4:784c4b53a3d4 128 if (useCRC && !(OneWireCRC::crc8(data, THERMOM_CRC_BYTE) == data[THERMOM_CRC_BYTE]))
ChrisnieM 4:784c4b53a3d4 129 {
ChrisnieM 4:784c4b53a3d4 130 // CRC failed
ChrisnieM 4:784c4b53a3d4 131 pc.traceOut("CRC FAILED... \r\n");
ChrisnieM 4:784c4b53a3d4 132 dataOk = false;
ChrisnieM 4:784c4b53a3d4 133 }
ChrisnieM 4:784c4b53a3d4 134
ChrisnieM 4:784c4b53a3d4 135 return dataOk;
ChrisnieM 4:784c4b53a3d4 136 }
ChrisnieM 4:784c4b53a3d4 137
ChrisnieM 4:784c4b53a3d4 138 float OneWireThermometer::readTemperature()
ChrisnieM 4:784c4b53a3d4 139 {
ChrisnieM 4:784c4b53a3d4 140 BYTE data[THERMOM_SCRATCHPAD_SIZE];
ChrisnieM 4:784c4b53a3d4 141 float realTemp = -999;
ChrisnieM 4:784c4b53a3d4 142
ChrisnieM 4:784c4b53a3d4 143 resetAndAddress();
ChrisnieM 4:784c4b53a3d4 144 oneWire.writeByte(CONVERT); // issue Convert command
ChrisnieM 4:784c4b53a3d4 145
ChrisnieM 4:784c4b53a3d4 146 if (useParasiticPower)
ChrisnieM 4:784c4b53a3d4 147 {
ChrisnieM 4:784c4b53a3d4 148 // wait while converting - Tconv (according to resolution of reading)
ChrisnieM 4:784c4b53a3d4 149 wait_ms(CONVERSION_TIME[resolution]);
ChrisnieM 4:784c4b53a3d4 150 }
ChrisnieM 4:784c4b53a3d4 151 else
ChrisnieM 4:784c4b53a3d4 152 {
ChrisnieM 4:784c4b53a3d4 153 // TODO
ChrisnieM 4:784c4b53a3d4 154 // after the Convert command, the device should respond by transmitting 0
ChrisnieM 4:784c4b53a3d4 155 // while the temperature conversion is in progress and 1 when the conversion is done
ChrisnieM 4:784c4b53a3d4 156 // - as were are not checking this (TODO), we use Tconv, as we would do for
ChrisnieM 4:784c4b53a3d4 157 // parasitic power
ChrisnieM 4:784c4b53a3d4 158 wait_ms(CONVERSION_TIME[resolution]);
ChrisnieM 4:784c4b53a3d4 159 }
ChrisnieM 4:784c4b53a3d4 160
ChrisnieM 4:784c4b53a3d4 161 if (readAndValidateData(data)) // issue Read Scratchpad commmand and get data
ChrisnieM 4:784c4b53a3d4 162 {
ChrisnieM 4:784c4b53a3d4 163 realTemp = calculateTemperature(data);
ChrisnieM 4:784c4b53a3d4 164 }
ChrisnieM 4:784c4b53a3d4 165
ChrisnieM 4:784c4b53a3d4 166 return realTemp;
ChrisnieM 4:784c4b53a3d4 167 }