Dallas' DS1820 family temperature sensor. For more details see [https://developer.mbed.org/users/hudakz/code/DS1820/wiki/Homepage]

Dependencies:   OneWire

Dependents:   BLE_nRF24L01 frdm_serialfgmp gather_sensor_data UltiSaverController ... more

Some programs using the DS1820 library:

Import programDS1820_Hello

Simple DS1820 sensor demo showing how to use the DS1820 library [https://developer.mbed.org/users/hudakz/code/DS1820/]

Import programBLE_nRF24L01

Bluetooth Low Energy (BLE) beacon with nRF24L01(+). Data is received and displayed by Android device (Android app source code is attached).

Examples of use:

Single DS1820 sensor

/*
 * Single DS1820 sensor GPIO driven
 *
 * Note: Don't forget to connect a 4.7k Ohm resistor 
 *       between the DS1820's data pin and the +3.3V pin
 *
 *           ----------------
 *          |                |   ----------------------->  +3.3V
 *          |   MBED BOARD   |  |
 *          |                |  |   ------
 *          |          +3.3V |--o--| 4.7k |-------
 *          |                |      ------        |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |           GPIO |--------------------o----->  1-wire bus/line
 *          |                |
 *          |                |
 *          |            GND |-------------------------->  GND
 *          |                |
 *           ----------------
 *
 */
 
#include "mbed.h"
#include "DS1820.h"
 
Serial serial(USBTX, USBRX);
 
int main() {
    DS1820  ds1820(D8);    // substitute D8 with actual mbed pin name connected to the DS1820 data pin    
                             
    if(ds1820.begin()) {
        while(1) {
            ds1820.startConversion();     // start temperature conversion
            ThisThread::sleep_for(1000);  // let DS1820 complete the temperature conversion
            serial.printf("temp = %3.1f\r\n", ds1820.read());     // read temperature
        }
    } else
        serial.printf("No DS1820 sensor found!\r\n");
}
 

Single DS1820 sensor. Data integrity is assured by performing CRC.

/*
 * Single DS1820 sensor GPIO driven + performing CRC
 *
 * Note: Don't forget to connect a 4.7k Ohm resistor 
 *       between the DS1820's data pin and the +3.3V pin
 *
 *           ----------------
 *          |                |   ----------------------->  +3.3V
 *          |   MBED BOARD   |  |
 *          |                |  |   ------
 *          |          +3.3V |--o--| 4.7k |-------
 *          |                |      ------        |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |           GPIO |--------------------o----->  1-wire bus/line
 *          |                |
 *          |                |
 *          |            GND |-------------------------->  GND
 *          |                |
 *           ----------------
 *
 */
 
#include "mbed.h"
#include "DS1820.h"
 
Serial serial(USBTX, USBRX);
 
int main() {
    DS1820  ds1820(D8);    // substitute D8 with actual mbed pin name connected to the DS1820 data pin   
    float   temp = 0;
    int     error = 0; 
                             
    if(ds1820.begin()) {
        while(1) {
            ds1820.startConversion();    // start temperature conversion
            ThisThread::sleep_for(1000); // let DS1820 complete the temperature conversion
            error = ds1820.read(temp);   // read temperature from DS1820 and perform cyclic redundancy check (CRC)
            switch(error) {
            case 0:    // no errors -> 'temp' contains the value of measured temperature
                serial.printf("temp = %3.1f\r\n", temp);
                break;
            case 1:    // no sensor present -> 'temp' is not updated
                serial.printf("no sensor present\n\r");
                break;
            case 2:    // CRC error -> 'temp' is not updated
                serial.printf("CRC error\r\n");
            } 
        }
    } else
        serial.printf("No DS1820 sensor found!\r\n");
}
 

Several DS1820 sensors connected to the same 1-wire bus.

/*
 * Multiple sensors GPIO driven
 *
 * Note: Don't forget to connect a 4.7k Ohm resistor 
 *       between the 1-wire bus data line and the +3.3V rail
 *
 *           ----------------
 *          |                |   ----------------------->  +3.3V
 *          |   MBED BOARD   |  |
 *          |                |  |   ------
 *          |          +3.3V |--o--| 4.7k |-------
 *          |                |      ------        |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |           GPIO |--------------------o----->  1-wire bus/line
 *          |                |
 *          |                |
 *          |            GND |-------------------------->  GND
 *          |                |
 *           ----------------
 *
 */

#include "mbed.h"
#include "DS1820.h"

#define     MAX_SENSOSRS   32   // max number of DS1820 sensors to be connected to the 1-wire bus (max 256)

DS1820*     ds1820[MAX_SENSOSRS];
Serial      pc(USBTX, USBRX);
DigitalOut  led(LED1);
OneWire     oneWire(D8);        // substitute D8 with the actual pin name connected to the 1-wire bus
int         sensorsFound = 0;   // counts the actually found DS1820 sensors

int main()
{
    pc.printf("\r\n--Starting--\r\n");
    
    //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
    for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) {
        ds1820[sensorsFound] = new DS1820(&oneWire);
        if (!ds1820[sensorsFound]->begin()) {
            delete ds1820[sensorsFound];
            break;
        }
    }

    switch (sensorsFound) {
        case 0:
            pc.printf("No DS1820 sensor found!\r\n");
            return -1;

        case 1:
            pc.printf("One DS1820 sensor found.\r\n");
            break;

        default:
            pc.printf("Found %d DS1820 sensors.\r\n", sensorsFound);
    }

    while (1) {
        pc.printf("----------------\r\n");
        for (int i = 0; i < sensorsFound; i++)
            ds1820[i]->startConversion();        // start temperature conversion from analog to digital
        ThisThread::sleep_for(1000);             // let DS1820 sensors complete the temperature conversion
        for (int i = 0; i < sensorsFound; i++) {
            if (ds1820[i]->isPresent())
                pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature
        }
    }
}


Several DS1820 sensors connected to the same 1-wire bus. UART is used to implement the bus

/*
 * Multiple sensors UART driven:
 * 
 *          UART is driving the 1-Wire Bus Master according to Maxim Integrated application note
 *
 *          https://www.maximintegrated.com/en/design/technical-documents/tutorials/2/214.html
 *
 *          In addition to the 4.7k Ohm resistor between the 1-wire data bus/line and the +3.3V pin,
 *          a 470 Ohm resistor shall be tied to the UART's tx and rx pin. UART's rx pin is then used
 *          as 1-wire data bus/line.
 *
 *           ----------------
 *          |                |   ----------------------->  +3.3V
 *          |   MBED BOARD   |  |
 *          |                |  |   ------
 *          |          +3.3V |--o--| 4.7k |-------
 *          |                |      ------        |
 *          |                |      ------        |
 *          |        UART TX |-----|  470 |---    |
 *          |                |      ------    |   |
 *          |                |                |   |
 *          |        UART RX |----------------o---o----->  1-wire bus/line
 *          |                |
 *          |                |
 *          |            GND |-------------------------->  GND
 *          |                |
 *           ----------------
 *
 */
#include "mbed.h"
#include "DS1820.h"

#define MAX_SENSOSRS    32      // max number of DS1820 sensors to be connected to the 1-wire bus (max 256)
DS1820*     ds1820[MAX_SENSOSRS];
DigitalOut  led(LED1);
OneWire     oneWire(p9, p10);       // LPC1768 (UART Tx pin, UART Rx pin)
//OneWire     oneWire(PA_0, PA_1);    // NUCLE0-F446RE (UART Tx pin, UART Rx pin)
int         sensorsFound = 0;       // counts the actually found DS1820 sensors

/**
 * @brief
 * @note
 * @param
 * @retval
 */
int main()
{
    printf("\r\n--Starting--\r\n");

    //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
    for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) {
        ds1820[sensorsFound] = new DS1820(&oneWire);
        if (!ds1820[sensorsFound]->begin()) {
            delete ds1820[sensorsFound];
            break;
        }
    }

    switch (sensorsFound) {
        case 0:
            printf("No DS1820 sensor found!\r\n");
            return -1;

        case 1:
            printf("One DS1820 sensor found.\r\n");
            break;

        default:
            printf("Found %d DS1820 sensors.\r\n", sensorsFound);
    }

    while (1) {
        led = !led;

        printf("----------------\r\n");
        for (int i = 0; i < sensorsFound; i++)
            ds1820[i]->startConversion();   // start temperature conversion from analog to digital
        #if (MBED_MAJOR_VERSION > 5)
            ThisThread::sleep_for(1s);
        #else
            wait(1);
        #endif


        for (int i = 0; i < sensorsFound; i++) {
            if (ds1820[i]->isPresent())
                printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176);   // read temperature
        }
    }
}
Committer:
hudakz
Date:
Sun Jan 20 10:11:31 2019 +0000
Revision:
14:b02fa18b294a
Parent:
13:b593a82ce790
Child:
17:9ff584b9809f
Improved One-Wire.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 3:a250babd0a9f 1 /*
hudakz 3:a250babd0a9f 2 * Dallas' DS1820 family temperature sensor.
hudakz 3:a250babd0a9f 3 * This library depends on the OneWire library (Dallas' 1-Wire bus protocol implementation)
hudakz 6:518950e436be 4 * available at <http://developer.mbed.org/users/hudakz/code/OneWire/>
hudakz 3:a250babd0a9f 5 *
hudakz 3:a250babd0a9f 6 * Example of use:
hudakz 3:a250babd0a9f 7 *
hudakz 3:a250babd0a9f 8 * #include "DS1820.h"
hudakz 6:518950e436be 9 *
hudakz 6:518950e436be 10 * Serial serial(USBTX, USBRX);
hudakz 3:a250babd0a9f 11 *
hudakz 3:a250babd0a9f 12 * int main() {
hudakz 6:518950e436be 13 * DS1820 ds1820(PA_9); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
hudakz 3:a250babd0a9f 14 *
hudakz 3:a250babd0a9f 15 * if(ds1820.begin()) {
hudakz 3:a250babd0a9f 16 * ds1820.startConversion();
hudakz 3:a250babd0a9f 17 * wait(1.0);
hudakz 3:a250babd0a9f 18 * while(1) {
hudakz 6:518950e436be 19 * serial.printf("temp = %3.1f\r\n", ds1820.read()); // read temperature
hudakz 6:518950e436be 20 * ds1820.startConversion(); // start temperature conversion
hudakz 6:518950e436be 21 * wait(1.0); // let DS1820 complete the temperature conversion
hudakz 3:a250babd0a9f 22 * }
hudakz 3:a250babd0a9f 23 * } else
hudakz 3:a250babd0a9f 24 * serial.printf("No DS1820 sensor found!\r\n");
hudakz 3:a250babd0a9f 25 * }
hudakz 6:518950e436be 26 *
hudakz 6:518950e436be 27 *
hudakz 6:518950e436be 28 * Note: Don't forget to connect a 4.7k Ohm resistor
hudakz 6:518950e436be 29 * between the DS1820's data pin and the +3.3V pin
hudakz 6:518950e436be 30 *
hudakz 3:a250babd0a9f 31 */
hudakz 3:a250babd0a9f 32
hudakz 0:433af64321d5 33 #include "DS1820.h"
hudakz 0:433af64321d5 34
hudakz 1:e4689408d617 35 #define DEBUG 0
hudakz 0:433af64321d5 36
hudakz 0:433af64321d5 37 #if DEBUG
hudakz 0:433af64321d5 38 extern Serial serial;
hudakz 0:433af64321d5 39 #endif
hudakz 0:433af64321d5 40
hudakz 0:433af64321d5 41 /**
hudakz 0:433af64321d5 42 * @brief Constructs a generic DS1820 sensor
hudakz 0:433af64321d5 43 * @note begin() must be called to detect and initialize the actual model
hudakz 3:a250babd0a9f 44 * @param pin: Name of data pin
hudakz 0:433af64321d5 45 * @retval
hudakz 0:433af64321d5 46 */
hudakz 0:433af64321d5 47 DS1820::DS1820(PinName pin) :
hudakz 0:433af64321d5 48 oneWire(pin) {
hudakz 8:8dfdd1603e4d 49 present = false;
hudakz 8:8dfdd1603e4d 50 model_s = false;
hudakz 0:433af64321d5 51 }
hudakz 0:433af64321d5 52
hudakz 0:433af64321d5 53 /**
hudakz 0:433af64321d5 54 * @brief Constructs a specific model
hudakz 0:433af64321d5 55 * @note No need to call begin() to detect and initialize the model
hudakz 3:a250babd0a9f 56 * @param model: One character model name: 'S', 's', 'B' or 'b'
hudakz 3:a250babd0a9f 57 * pin: Name of data pin
hudakz 0:433af64321d5 58 * @retval
hudakz 0:433af64321d5 59 */
hudakz 0:433af64321d5 60 DS1820::DS1820(char model, PinName pin) :
hudakz 0:433af64321d5 61 oneWire(pin) {
hudakz 0:433af64321d5 62 if((model == 'S') or (model == 's')) {
hudakz 8:8dfdd1603e4d 63 present = true;
hudakz 8:8dfdd1603e4d 64 model_s = true;
hudakz 0:433af64321d5 65 }
hudakz 0:433af64321d5 66 else if((model == 'B') or (model == 'b')) {
hudakz 8:8dfdd1603e4d 67 present = true;
hudakz 8:8dfdd1603e4d 68 model_s = false;
hudakz 0:433af64321d5 69 }
hudakz 0:433af64321d5 70 else
hudakz 8:8dfdd1603e4d 71 present = false;
hudakz 0:433af64321d5 72 }
hudakz 0:433af64321d5 73
hudakz 0:433af64321d5 74 /**
hudakz 0:433af64321d5 75 * @brief Detects and initializes the actual DS1820 model
hudakz 0:433af64321d5 76 * @note
hudakz 0:433af64321d5 77 * @param
hudakz 0:433af64321d5 78 * @retval true: if a DS1820 family sensor was detected and initialized
hudakz 0:433af64321d5 79 false: otherwise
hudakz 0:433af64321d5 80 */
hudakz 0:433af64321d5 81 bool DS1820::begin(void) {
hudakz 0:433af64321d5 82 oneWire.reset_search();
hudakz 0:433af64321d5 83 wait_ms(250);
hudakz 0:433af64321d5 84 if(!oneWire.search(addr)) {
hudakz 0:433af64321d5 85 #if DEBUG
hudakz 0:433af64321d5 86 serial.printf("No addresses.\r\n");
hudakz 0:433af64321d5 87 #endif
hudakz 0:433af64321d5 88 oneWire.reset_search();
hudakz 0:433af64321d5 89 wait_ms(250);
hudakz 0:433af64321d5 90 return false;
hudakz 0:433af64321d5 91 }
hudakz 0:433af64321d5 92
hudakz 0:433af64321d5 93 #if DEBUG
hudakz 0:433af64321d5 94 serial.printf("ROM =");
hudakz 0:433af64321d5 95 for(uint8_t i = 0; i < 8; i++) {
hudakz 0:433af64321d5 96 serial.printf(" %x", addr[i]);
hudakz 0:433af64321d5 97 }
hudakz 0:433af64321d5 98 serial.printf("\r\n");
hudakz 0:433af64321d5 99 #endif
hudakz 0:433af64321d5 100
hudakz 0:433af64321d5 101 if(OneWire::crc8(addr, 7) == addr[7]) {
hudakz 8:8dfdd1603e4d 102 present = true;
hudakz 0:433af64321d5 103
hudakz 0:433af64321d5 104 // the first ROM byte indicates which chip
hudakz 0:433af64321d5 105 switch(addr[0]) {
hudakz 0:433af64321d5 106 case 0x10:
hudakz 8:8dfdd1603e4d 107 model_s = true;
hudakz 0:433af64321d5 108 #if DEBUG
hudakz 0:433af64321d5 109 serial.printf("DS18S20 or old DS1820\r\n");
hudakz 0:433af64321d5 110 #endif
hudakz 0:433af64321d5 111 break;
hudakz 0:433af64321d5 112
hudakz 0:433af64321d5 113 case 0x28:
hudakz 8:8dfdd1603e4d 114 model_s = false;
hudakz 0:433af64321d5 115 #if DEBUG
hudakz 0:433af64321d5 116 serial.printf("DS18B20\r\n");
hudakz 0:433af64321d5 117 #endif
hudakz 0:433af64321d5 118 break;
hudakz 0:433af64321d5 119
hudakz 0:433af64321d5 120 case 0x22:
hudakz 8:8dfdd1603e4d 121 model_s = false;
hudakz 0:433af64321d5 122 #if DEBUG
hudakz 0:433af64321d5 123 serial.printf("DS1822\r\n");
hudakz 0:433af64321d5 124 #endif
hudakz 0:433af64321d5 125 break;
hudakz 0:433af64321d5 126
hudakz 0:433af64321d5 127 default:
hudakz 8:8dfdd1603e4d 128 present = false;
hudakz 0:433af64321d5 129 #if DEBUG
hudakz 0:433af64321d5 130 serial.printf("Device doesn't belong to the DS1820 family\r\n");
hudakz 2:b7ad1da7331a 131 #endif
hudakz 0:433af64321d5 132 return false;
hudakz 0:433af64321d5 133 }
hudakz 0:433af64321d5 134 return true;
hudakz 0:433af64321d5 135 }
hudakz 2:b7ad1da7331a 136 else {
hudakz 0:433af64321d5 137 #if DEBUG
hudakz 0:433af64321d5 138 serial.printf("Invalid CRC!\r\n");
hudakz 2:b7ad1da7331a 139 #endif
hudakz 0:433af64321d5 140 return false;
hudakz 0:433af64321d5 141 }
hudakz 0:433af64321d5 142 }
hudakz 0:433af64321d5 143
hudakz 0:433af64321d5 144 /**
hudakz 8:8dfdd1603e4d 145 * @brief Informs about presence of a DS1820 sensor.
hudakz 8:8dfdd1603e4d 146 * @note begin() shall be called before using this function
hudakz 8:8dfdd1603e4d 147 * if a generic DS1820 instance was created by the user.
hudakz 8:8dfdd1603e4d 148 * No need to call begin() for a specific DS1820 instance.
hudakz 8:8dfdd1603e4d 149 * @param
hudakz 8:8dfdd1603e4d 150 * @retval true: when a DS1820 sensor is present
hudakz 8:8dfdd1603e4d 151 * false: otherwise
hudakz 8:8dfdd1603e4d 152 */
hudakz 8:8dfdd1603e4d 153 bool DS1820::isPresent(void) {
hudakz 8:8dfdd1603e4d 154 return present;
hudakz 8:8dfdd1603e4d 155 }
hudakz 8:8dfdd1603e4d 156
hudakz 8:8dfdd1603e4d 157 /**
hudakz 6:518950e436be 158 * @brief Sets temperature-to-digital conversion resolution.
hudakz 4:adf4e7972d73 159 * @note The configuration register allows the user to set the resolution
hudakz 6:518950e436be 160 * of the temperature-to-digital conversion to 9, 10, 11, or 12 bits.
hudakz 6:518950e436be 161 * Defaults to 12-bit resolution for DS18B20.
hudakz 6:518950e436be 162 * DS18S20 allows only 9-bit resolution.
hudakz 6:518950e436be 163 * @param res: Resolution of the temperature-to-digital conversion in bits.
hudakz 4:adf4e7972d73 164 * @retval
hudakz 4:adf4e7972d73 165 */
hudakz 4:adf4e7972d73 166 void DS1820::setResolution(uint8_t res) {
hudakz 4:adf4e7972d73 167 // keep resolution within limits
hudakz 4:adf4e7972d73 168 if(res > 12)
hudakz 4:adf4e7972d73 169 res = 12;
hudakz 4:adf4e7972d73 170 if(res < 9)
hudakz 4:adf4e7972d73 171 res = 9;
hudakz 8:8dfdd1603e4d 172 if(model_s)
hudakz 4:adf4e7972d73 173 res = 9;
hudakz 4:adf4e7972d73 174
hudakz 4:adf4e7972d73 175 oneWire.reset();
hudakz 4:adf4e7972d73 176 oneWire.skip();
hudakz 4:adf4e7972d73 177 oneWire.write(0xBE); // to read Scratchpad
hudakz 4:adf4e7972d73 178 for(uint8_t i = 0; i < 9; i++) // read Scratchpad bytes
hudakz 6:518950e436be 179 data[i] = oneWire.read();
hudakz 4:adf4e7972d73 180
hudakz 4:adf4e7972d73 181 data[4] |= (res - 9) << 5; // update configuration byte (set resolution)
hudakz 4:adf4e7972d73 182 oneWire.reset();
hudakz 4:adf4e7972d73 183 oneWire.skip();
hudakz 4:adf4e7972d73 184 oneWire.write(0x4E); // to write into Scratchpad
hudakz 4:adf4e7972d73 185 for(uint8_t i = 2; i < 5; i++) // write three bytes (2nd, 3rd, 4th) into Scratchpad
hudakz 4:adf4e7972d73 186 oneWire.write(data[i]);
hudakz 4:adf4e7972d73 187 }
hudakz 4:adf4e7972d73 188
hudakz 4:adf4e7972d73 189 /**
hudakz 0:433af64321d5 190 * @brief Starts temperature conversion
hudakz 6:518950e436be 191 * @note The time to complete the converion depends on the selected resolution:
hudakz 6:518950e436be 192 * 9-bit resolution -> max conversion time = 93.75ms
hudakz 6:518950e436be 193 * 10-bit resolution -> max conversion time = 187.5ms
hudakz 6:518950e436be 194 * 11-bit resolution -> max conversion time = 375ms
hudakz 6:518950e436be 195 * 12-bit resolution -> max conversion time = 750ms
hudakz 0:433af64321d5 196 * @param
hudakz 0:433af64321d5 197 * @retval
hudakz 0:433af64321d5 198 */
hudakz 0:433af64321d5 199 void DS1820::startConversion(void) {
hudakz 0:433af64321d5 200 if(present) {
hudakz 0:433af64321d5 201 oneWire.reset();
hudakz 0:433af64321d5 202 oneWire.skip();
hudakz 3:a250babd0a9f 203 oneWire.write(0x44); //start temperature conversion
hudakz 0:433af64321d5 204 }
hudakz 0:433af64321d5 205 }
hudakz 0:433af64321d5 206
hudakz 0:433af64321d5 207 /**
hudakz 6:518950e436be 208 * @brief Reads temperature from the chip's Scratchpad
hudakz 0:433af64321d5 209 * @note
hudakz 0:433af64321d5 210 * @param
hudakz 0:433af64321d5 211 * @retval Floating point temperature value
hudakz 0:433af64321d5 212 */
hudakz 0:433af64321d5 213 float DS1820::read(void) {
hudakz 0:433af64321d5 214 if(present) {
hudakz 0:433af64321d5 215 oneWire.reset();
hudakz 0:433af64321d5 216 oneWire.skip();
hudakz 0:433af64321d5 217 oneWire.write(0xBE); // to read Scratchpad
hudakz 6:518950e436be 218 for(uint8_t i = 0; i < 9; i++) // read Scratchpad bytes
hudakz 6:518950e436be 219 data[i] = oneWire.read();
hudakz 0:433af64321d5 220
hudakz 6:518950e436be 221 // Convert the raw bytes to a 16-bit unsigned value
hudakz 0:433af64321d5 222 uint16_t* p_word = reinterpret_cast < uint16_t * > (&data[0]);
hudakz 0:433af64321d5 223
hudakz 0:433af64321d5 224 #if DEBUG
hudakz 0:433af64321d5 225 serial.printf("raw = %#x\r\n", *p_word);
hudakz 0:433af64321d5 226 #endif
hudakz 0:433af64321d5 227
hudakz 8:8dfdd1603e4d 228 if(model_s) {
hudakz 6:518950e436be 229 *p_word = *p_word << 3; // 9-bit resolution
hudakz 0:433af64321d5 230 if(data[7] == 0x10) {
hudakz 0:433af64321d5 231
hudakz 6:518950e436be 232 // "count remain" gives full 12-bit resolution
hudakz 0:433af64321d5 233 *p_word = (*p_word & 0xFFF0) + 12 - data[6];
hudakz 0:433af64321d5 234 }
hudakz 0:433af64321d5 235 }
hudakz 0:433af64321d5 236 else {
hudakz 6:518950e436be 237 uint8_t cfg = (data[4] & 0x60); // default 12-bit resolution
hudakz 4:adf4e7972d73 238
hudakz 4:adf4e7972d73 239 // at lower resolution, the low bits are undefined, so let's clear them
hudakz 0:433af64321d5 240 if(cfg == 0x00)
hudakz 6:518950e436be 241 *p_word = *p_word &~7; // 9-bit resolution
hudakz 0:433af64321d5 242 else
hudakz 0:433af64321d5 243 if(cfg == 0x20)
hudakz 6:518950e436be 244 *p_word = *p_word &~3; // 10-bit resolution
hudakz 0:433af64321d5 245 else
hudakz 0:433af64321d5 246 if(cfg == 0x40)
hudakz 6:518950e436be 247 *p_word = *p_word &~1; // 11-bit resolution
hudakz 4:adf4e7972d73 248
hudakz 0:433af64321d5 249 }
hudakz 6:518950e436be 250
hudakz 6:518950e436be 251 // Convert the raw bytes to a 16-bit signed fixed point value :
hudakz 6:518950e436be 252 // 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment
hudakz 6:518950e436be 253 // and the LSB of the 16-bit binary number represents 1/256th of a unit).
hudakz 6:518950e436be 254 *p_word = *p_word << 4;
hudakz 6:518950e436be 255
hudakz 6:518950e436be 256 // Convert to floating point value
hudakz 6:518950e436be 257 return(toFloat(*p_word));
hudakz 0:433af64321d5 258 }
hudakz 0:433af64321d5 259 else
hudakz 0:433af64321d5 260 return 0;
hudakz 0:433af64321d5 261 }
hudakz 0:433af64321d5 262
hudakz 0:433af64321d5 263 /**
hudakz 13:b593a82ce790 264 * @brief Reads temperature from chip's scratchpad.
hudakz 13:b593a82ce790 265 * @note Verifies data integrity by calculating cyclic redundancy check (CRC).
hudakz 13:b593a82ce790 266 * If the calculated CRC dosn't match the one stored in chip's scratchpad register
hudakz 13:b593a82ce790 267 * the temperature variable is not updated and CRC error code is returned.
hudakz 13:b593a82ce790 268 * @param temp: The temperature variable to be updated by this routine.
hudakz 13:b593a82ce790 269 * (It's passed as reference to floating point.)
hudakz 13:b593a82ce790 270 * @retval error code:
hudakz 13:b593a82ce790 271 * 0 - no errors ('temp' contains the temperature measured)
hudakz 13:b593a82ce790 272 * 1 - sensor not present ('temp' is not updated)
hudakz 13:b593a82ce790 273 * 2 - CRC error ('temp' is not updated)
hudakz 13:b593a82ce790 274 */
hudakz 13:b593a82ce790 275 uint8_t DS1820::read(float& temp) {
hudakz 13:b593a82ce790 276 if(present) {
hudakz 13:b593a82ce790 277 oneWire.reset();
hudakz 13:b593a82ce790 278 oneWire.skip();
hudakz 13:b593a82ce790 279 oneWire.write(0xBE); // to read Scratchpad
hudakz 13:b593a82ce790 280 for(uint8_t i = 0; i < 9; i++) // reading scratchpad registers
hudakz 13:b593a82ce790 281 data[i] = oneWire.read();
hudakz 13:b593a82ce790 282
hudakz 13:b593a82ce790 283 if(oneWire.crc8(data, 8) != data[8]) // if calculated CRC does not match the stored one
hudakz 13:b593a82ce790 284 return 2; // return with CRC error
hudakz 13:b593a82ce790 285
hudakz 13:b593a82ce790 286 // Convert the raw bytes to a 16bit unsigned value
hudakz 13:b593a82ce790 287 uint16_t* p_word = reinterpret_cast < uint16_t * > (&data[0]);
hudakz 13:b593a82ce790 288
hudakz 13:b593a82ce790 289 #if DEBUG
hudakz 13:b593a82ce790 290 serial.printf("raw = %#x\r\n", *p_word);
hudakz 13:b593a82ce790 291 #endif
hudakz 13:b593a82ce790 292
hudakz 13:b593a82ce790 293 if(model_s) {
hudakz 13:b593a82ce790 294 *p_word = *p_word << 3; // 9 bit resolution, max conversion time = 750ms
hudakz 13:b593a82ce790 295 if(data[7] == 0x10) {
hudakz 13:b593a82ce790 296
hudakz 13:b593a82ce790 297 // "count remain" gives full 12 bit resolution
hudakz 13:b593a82ce790 298 *p_word = (*p_word & 0xFFF0) + 12 - data[6];
hudakz 13:b593a82ce790 299 }
hudakz 13:b593a82ce790 300
hudakz 13:b593a82ce790 301 // Convert the raw bytes to a 16bit signed fixed point value :
hudakz 13:b593a82ce790 302 // 1 sign bit, 7 integer bits, 8 fractional bits (two's compliment
hudakz 13:b593a82ce790 303 // and the LSB of the 16bit binary number represents 1/256th of a unit).
hudakz 13:b593a82ce790 304 *p_word = *p_word << 4;
hudakz 13:b593a82ce790 305 // Convert to floating point value
hudakz 13:b593a82ce790 306 temp = toFloat(*p_word);
hudakz 13:b593a82ce790 307 return 0; // return with no errors
hudakz 13:b593a82ce790 308 }
hudakz 13:b593a82ce790 309 else {
hudakz 13:b593a82ce790 310 uint8_t cfg = (data[4] & 0x60); // default 12bit resolution, max conversion time = 750ms
hudakz 13:b593a82ce790 311
hudakz 13:b593a82ce790 312 // at lower resolution, the low bits are undefined, so let's clear them
hudakz 13:b593a82ce790 313 if(cfg == 0x00)
hudakz 13:b593a82ce790 314 *p_word = *p_word &~7; // 9bit resolution, max conversion time = 93.75ms
hudakz 13:b593a82ce790 315 else
hudakz 13:b593a82ce790 316 if(cfg == 0x20)
hudakz 13:b593a82ce790 317 *p_word = *p_word &~3; // 10bit resolution, max conversion time = 187.5ms
hudakz 13:b593a82ce790 318 else
hudakz 13:b593a82ce790 319 if(cfg == 0x40)
hudakz 13:b593a82ce790 320 *p_word = *p_word &~1; // 11bit resolution, max conversion time = 375ms
hudakz 13:b593a82ce790 321
hudakz 13:b593a82ce790 322 // Convert the raw bytes to a 16bit signed fixed point value :
hudakz 14:b02fa18b294a 323 // 1 sign bit, 7 integer bits, 8 fractional bits (two's complement
hudakz 13:b593a82ce790 324 // and the LSB of the 16bit binary number represents 1/256th of a unit).
hudakz 13:b593a82ce790 325 *p_word = *p_word << 4;
hudakz 13:b593a82ce790 326 // Convert to floating point value
hudakz 13:b593a82ce790 327 temp = toFloat(*p_word);
hudakz 13:b593a82ce790 328 return 0; // return with no errors
hudakz 13:b593a82ce790 329 }
hudakz 13:b593a82ce790 330 }
hudakz 13:b593a82ce790 331 else
hudakz 13:b593a82ce790 332 return 1; // error, sensor is not present
hudakz 13:b593a82ce790 333 }
hudakz 13:b593a82ce790 334
hudakz 13:b593a82ce790 335 /**
hudakz 6:518950e436be 336 * @brief Converts a 16-bit signed fixed point value to floating point value
hudakz 6:518950e436be 337 * @note The 16-bit unsigned integer represnts actually
hudakz 6:518950e436be 338 * a 16-bit signed fixed point value:
hudakz 14:b02fa18b294a 339 * 1 sign bit, 7 integer bits, 8 fractional bits (two’s complement
hudakz 6:518950e436be 340 * and the LSB of the 16-bit binary number represents 1/256th of a unit).
hudakz 6:518950e436be 341 * @param 16-bit unsigned integer
hudakz 4:adf4e7972d73 342 * @retval Floating point value
hudakz 0:433af64321d5 343 */
hudakz 0:433af64321d5 344 float DS1820::toFloat(uint16_t word) {
hudakz 0:433af64321d5 345 if(word & 0x8000)
hudakz 0:433af64321d5 346 return (-float(uint16_t(~word + 1)) / 256.0f);
hudakz 0:433af64321d5 347 else
hudakz 0:433af64321d5 348 return (float(word) / 256.0f);
hudakz 0:433af64321d5 349 }
hudakz 0:433af64321d5 350
hudakz 8:8dfdd1603e4d 351