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:
Mon Dec 28 21:15:27 2020 +0000
Revision:
24:d683d826dccd
Parent:
23:74a4ff420541
DS1820: Added 1-wire bus implementation over UART.

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 24:d683d826dccd 7 *
hudakz 20:98c261bcb399 8 * Single sensor.
hudakz 6:518950e436be 9 *
hudakz 20:98c261bcb399 10 * #include "mbed.h"
hudakz 20:98c261bcb399 11 * #include "DS1820.h"
hudakz 24:d683d826dccd 12 *
hudakz 20:98c261bcb399 13 * Serial pc(USBTX, USBRX);
hudakz 20:98c261bcb399 14 * DigitalOut led(LED1);
hudakz 20:98c261bcb399 15 * OneWire oneWire(D8); // substitute D8 with actual mbed pin name connected 1-wire bus
hudakz 20:98c261bcb399 16 * float temp = 0;
hudakz 20:98c261bcb399 17 * int result = 0;
hudakz 24:d683d826dccd 18 *
hudakz 20:98c261bcb399 19 * int main()
hudakz 20:98c261bcb399 20 * {
hudakz 20:98c261bcb399 21 * pc.printf("\r\n--Starting--\r\n");
hudakz 20:98c261bcb399 22 * if (ds1820.begin()) {
hudakz 20:98c261bcb399 23 * while (1) {
hudakz 20:98c261bcb399 24 * ds1820.startConversion(); // start temperature conversion from analog to digital
hudakz 24:d683d826dccd 25 * ThisThread::sleep_for(1000ms);// let DS1820 complete the temperature conversion
hudakz 24:d683d826dccd 26 * result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
hudakz 20:98c261bcb399 27 * switch (result) {
hudakz 24:d683d826dccd 28 * case 0: // no errors -> 'temp' contains the value of measured temperature
hudakz 20:98c261bcb399 29 * pc.printf("temp = %3.1f%cC\r\n", temp, 176);
hudakz 20:98c261bcb399 30 * break;
hudakz 24:d683d826dccd 31 *
hudakz 24:d683d826dccd 32 * case 1: // no sensor present -> 'temp' is not updated
hudakz 20:98c261bcb399 33 * pc.printf("no sensor present\n\r");
hudakz 20:98c261bcb399 34 * break;
hudakz 24:d683d826dccd 35 *
hudakz 24:d683d826dccd 36 * case 2: // CRC error -> 'temp' is not updated
hudakz 20:98c261bcb399 37 * pc.printf("CRC error\r\n");
hudakz 20:98c261bcb399 38 * }
hudakz 24:d683d826dccd 39 *
hudakz 20:98c261bcb399 40 * led = !led;
hudakz 20:98c261bcb399 41 * }
hudakz 20:98c261bcb399 42 * }
hudakz 20:98c261bcb399 43 * else
hudakz 20:98c261bcb399 44 * pc.printf("No DS1820 sensor found!\r\n");
hudakz 20:98c261bcb399 45 * }
hudakz 24:d683d826dccd 46 *
hudakz 24:d683d826dccd 47 *
hudakz 20:98c261bcb399 48 * More sensors connected to the same 1-wire bus.
hudakz 24:d683d826dccd 49 *
hudakz 20:98c261bcb399 50 * #include "mbed.h"
hudakz 20:98c261bcb399 51 * #include "DS1820.h"
hudakz 24:d683d826dccd 52 *
hudakz 20:98c261bcb399 53 * #define SENSORS_COUNT 64 // number of DS1820 sensors to be connected to the 1-wire bus (max 256)
hudakz 24:d683d826dccd 54 *
hudakz 20:98c261bcb399 55 * Serial pc(USBTX, USBRX);
hudakz 20:98c261bcb399 56 * DigitalOut led(LED1);
hudakz 20:98c261bcb399 57 * OneWire oneWire(D8); // substitute D8 with actual mbed pin name connected to the DS1820 data pin
hudakz 20:98c261bcb399 58 * DS1820* ds1820[SENSORS_COUNT];
hudakz 20:98c261bcb399 59 * int sensors_found = 0; // counts the actually found DS1820 sensors
hudakz 20:98c261bcb399 60 * float temp = 0;
hudakz 20:98c261bcb399 61 * int result = 0;
hudakz 24:d683d826dccd 62 *
hudakz 3:a250babd0a9f 63 * int main() {
hudakz 20:98c261bcb399 64 * int i = 0;
hudakz 24:d683d826dccd 65 *
hudakz 20:98c261bcb399 66 * pc.printf("\r\n Starting \r\n");
hudakz 20:98c261bcb399 67 * //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
hudakz 20:98c261bcb399 68 * for(i = 0; i < SENSORS_COUNT; i++) {
hudakz 20:98c261bcb399 69 * ds1820[i] = new DS1820(&oneWire);
hudakz 20:98c261bcb399 70 * if(!ds1820[i]->begin()) {
hudakz 20:98c261bcb399 71 * delete ds1820[i];
hudakz 20:98c261bcb399 72 * break;
hudakz 20:98c261bcb399 73 * }
hudakz 20:98c261bcb399 74 * }
hudakz 24:d683d826dccd 75 *
hudakz 20:98c261bcb399 76 * sensors_found = i;
hudakz 24:d683d826dccd 77 *
hudakz 20:98c261bcb399 78 * if (sensors_found == 0) {
hudakz 20:98c261bcb399 79 * pc.printf("No DS1820 sensor found!\r\n");
hudakz 20:98c261bcb399 80 * return -1;
hudakz 20:98c261bcb399 81 * }
hudakz 20:98c261bcb399 82 * else
hudakz 20:98c261bcb399 83 * pc.printf("Found %d sensors.\r\n", sensors_found);
hudakz 24:d683d826dccd 84 *
hudakz 20:98c261bcb399 85 * while(1) {
hudakz 20:98c261bcb399 86 * pc.printf("-------------------\r\n");
hudakz 20:98c261bcb399 87 * for(i = 0; i < sensors_found; i++)
hudakz 24:d683d826dccd 88 * ds1820[i]->startConversion(); // start temperature conversion from analog to digital
hudakz 24:d683d826dccd 89 * ThisThread::sleep_for(1000ms); // let DS1820s complete the temperature conversion
hudakz 20:98c261bcb399 90 * for(int i = 0; i < sensors_found; i++) {
hudakz 20:98c261bcb399 91 * if(ds1820[i]->isPresent())
hudakz 20:98c261bcb399 92 * pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature
hudakz 20:98c261bcb399 93 * }
hudakz 20:98c261bcb399 94 * }
hudakz 3:a250babd0a9f 95 * }
hudakz 24:d683d826dccd 96 *
hudakz 3:a250babd0a9f 97 */
hudakz 0:433af64321d5 98 #include "DS1820.h"
hudakz 0:433af64321d5 99
hudakz 24:d683d826dccd 100 //#define DEBUG 1
hudakz 20:98c261bcb399 101
hudakz 20:98c261bcb399 102 //* Initializing static members
hudakz 24:d683d826dccd 103 uint8_t DS1820:: _lastAddr[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
hudakz 24:d683d826dccd 104
hudakz 20:98c261bcb399 105 /**
hudakz 20:98c261bcb399 106 * @brief Constructs a generic DS1820 sensor
hudakz 20:98c261bcb399 107 * @note begin() must be called to detect and initialize the actual model
hudakz 24:d683d826dccd 108 * @param gpioPin: Name of the GPIO pin
hudakz 20:98c261bcb399 109 * @retval
hudakz 20:98c261bcb399 110 */
hudakz 24:d683d826dccd 111 DS1820::DS1820(PinName gpioPin, int samplePoint_us /*=13*/ )
hudakz 24:d683d826dccd 112 {
hudakz 24:d683d826dccd 113 _oneWire = new OneWire(gpioPin, samplePoint_us);
hudakz 24:d683d826dccd 114 _present = false;
hudakz 24:d683d826dccd 115 _model_s = false;
hudakz 24:d683d826dccd 116 }
hudakz 24:d683d826dccd 117
hudakz 24:d683d826dccd 118 /**
hudakz 24:d683d826dccd 119 * @brief Constructs a generic DS1820 sensor
hudakz 24:d683d826dccd 120 * @note begin() must be called to detect and initialize the actual model
hudakz 24:d683d826dccd 121 * @param txPin: UART's Tx pin
hudakz 24:d683d826dccd 122 * @param rxPin: UART's Rx pin
hudakz 24:d683d826dccd 123 * @retval
hudakz 24:d683d826dccd 124 */
hudakz 24:d683d826dccd 125 DS1820::DS1820(PinName txPin, PinName rxPin)
hudakz 24:d683d826dccd 126 {
hudakz 24:d683d826dccd 127 _oneWire = new OneWire(txPin, rxPin);
hudakz 24:d683d826dccd 128 _present = false;
hudakz 24:d683d826dccd 129 _model_s = false;
hudakz 20:98c261bcb399 130 }
hudakz 0:433af64321d5 131
hudakz 0:433af64321d5 132 /**
hudakz 0:433af64321d5 133 * @brief Constructs a generic DS1820 sensor
hudakz 0:433af64321d5 134 * @note begin() must be called to detect and initialize the actual model
hudakz 3:a250babd0a9f 135 * @param pin: Name of data pin
hudakz 0:433af64321d5 136 * @retval
hudakz 0:433af64321d5 137 */
hudakz 24:d683d826dccd 138 DS1820::DS1820(OneWire* oneWire) :
hudakz 24:d683d826dccd 139 _oneWire(oneWire)
hudakz 24:d683d826dccd 140 {
hudakz 24:d683d826dccd 141 _present = false;
hudakz 24:d683d826dccd 142 _model_s = false;
hudakz 0:433af64321d5 143 }
hudakz 0:433af64321d5 144
hudakz 0:433af64321d5 145 /**
hudakz 0:433af64321d5 146 * @brief Detects and initializes the actual DS1820 model
hudakz 0:433af64321d5 147 * @note
hudakz 0:433af64321d5 148 * @param
hudakz 0:433af64321d5 149 * @retval true: if a DS1820 family sensor was detected and initialized
hudakz 0:433af64321d5 150 false: otherwise
hudakz 0:433af64321d5 151 */
hudakz 24:d683d826dccd 152 bool DS1820::begin(void)
hudakz 24:d683d826dccd 153 {
hudakz 20:98c261bcb399 154 #if DEBUG
hudakz 20:98c261bcb399 155 printf("lastAddr =");
hudakz 24:d683d826dccd 156 for (uint8_t i = 0; i < 8; i++) {
hudakz 20:98c261bcb399 157 printf(" %x", lastAddr[i]);
hudakz 20:98c261bcb399 158 }
hudakz 24:d683d826dccd 159
hudakz 20:98c261bcb399 160 printf("\r\n");
hudakz 20:98c261bcb399 161 #endif
hudakz 24:d683d826dccd 162 if (!_oneWire->search(_lastAddr))
hudakz 24:d683d826dccd 163 {
hudakz 0:433af64321d5 164 #if DEBUG
hudakz 17:9ff584b9809f 165 printf("No addresses.\r\n");
hudakz 0:433af64321d5 166 #endif
hudakz 24:d683d826dccd 167 _oneWire->reset_search();
hudakz 24:d683d826dccd 168 #if MBED_MAJOR_VERSION == 2
hudakz 24:d683d826dccd 169 wait_ms(250);
hudakz 24:d683d826dccd 170 #else
hudakz 24:d683d826dccd 171 ThisThread::sleep_for(250ms);
hudakz 24:d683d826dccd 172 #endif
hudakz 0:433af64321d5 173 return false;
hudakz 0:433af64321d5 174 }
hudakz 24:d683d826dccd 175
hudakz 20:98c261bcb399 176 for (int i = 0; i < 8; i++)
hudakz 24:d683d826dccd 177 _addr[i] = _lastAddr[i];
hudakz 0:433af64321d5 178
hudakz 0:433af64321d5 179 #if DEBUG
hudakz 17:9ff584b9809f 180 printf("ROM =");
hudakz 24:d683d826dccd 181 for (uint8_t i = 0; i < 8; i++) {
hudakz 17:9ff584b9809f 182 printf(" %x", addr[i]);
hudakz 0:433af64321d5 183 }
hudakz 24:d683d826dccd 184
hudakz 17:9ff584b9809f 185 printf("\r\n");
hudakz 0:433af64321d5 186 #endif
hudakz 24:d683d826dccd 187 if (OneWire::crc8(_addr, 7) == _addr[7]) {
hudakz 24:d683d826dccd 188 _present = true;
hudakz 0:433af64321d5 189
hudakz 0:433af64321d5 190 // the first ROM byte indicates which chip
hudakz 24:d683d826dccd 191 switch (_addr[0]) {
hudakz 24:d683d826dccd 192 case 0x10:
hudakz 24:d683d826dccd 193 _model_s = true;
hudakz 24:d683d826dccd 194 #if DEBUG
hudakz 24:d683d826dccd 195 printf("DS18S20 or old DS1820\r\n");
hudakz 24:d683d826dccd 196 #endif
hudakz 24:d683d826dccd 197 break;
hudakz 0:433af64321d5 198
hudakz 24:d683d826dccd 199 case 0x28:
hudakz 24:d683d826dccd 200 _model_s = false;
hudakz 24:d683d826dccd 201 #if DEBUG
hudakz 24:d683d826dccd 202 printf("DS18B20\r\n");
hudakz 24:d683d826dccd 203 #endif
hudakz 24:d683d826dccd 204 break;
hudakz 0:433af64321d5 205
hudakz 24:d683d826dccd 206 case 0x22:
hudakz 24:d683d826dccd 207 _model_s = false;
hudakz 24:d683d826dccd 208 #if DEBUG
hudakz 24:d683d826dccd 209 printf("DS1822\r\n");
hudakz 24:d683d826dccd 210 #endif
hudakz 24:d683d826dccd 211 break;
hudakz 0:433af64321d5 212
hudakz 24:d683d826dccd 213 default:
hudakz 24:d683d826dccd 214 _present = false;
hudakz 24:d683d826dccd 215 #if DEBUG
hudakz 24:d683d826dccd 216 printf("Device doesn't belong to the DS1820 family\r\n");
hudakz 24:d683d826dccd 217 #endif
hudakz 24:d683d826dccd 218 return false;
hudakz 0:433af64321d5 219 }
hudakz 24:d683d826dccd 220
hudakz 0:433af64321d5 221 return true;
hudakz 0:433af64321d5 222 }
hudakz 24:d683d826dccd 223 else
hudakz 24:d683d826dccd 224 {
hudakz 24:d683d826dccd 225 #if DEBUG
hudakz 17:9ff584b9809f 226 printf("Invalid CRC!\r\n");
hudakz 24:d683d826dccd 227 #endif
hudakz 0:433af64321d5 228 return false;
hudakz 0:433af64321d5 229 }
hudakz 0:433af64321d5 230 }
hudakz 0:433af64321d5 231
hudakz 0:433af64321d5 232 /**
hudakz 8:8dfdd1603e4d 233 * @brief Informs about presence of a DS1820 sensor.
hudakz 8:8dfdd1603e4d 234 * @note begin() shall be called before using this function
hudakz 24:d683d826dccd 235 * if a generic DS1820 instance was created by the user.
hudakz 8:8dfdd1603e4d 236 * No need to call begin() for a specific DS1820 instance.
hudakz 8:8dfdd1603e4d 237 * @param
hudakz 8:8dfdd1603e4d 238 * @retval true: when a DS1820 sensor is present
hudakz 8:8dfdd1603e4d 239 * false: otherwise
hudakz 8:8dfdd1603e4d 240 */
hudakz 24:d683d826dccd 241 bool DS1820::isPresent(void)
hudakz 24:d683d826dccd 242 {
hudakz 24:d683d826dccd 243 return _present;
hudakz 8:8dfdd1603e4d 244 }
hudakz 8:8dfdd1603e4d 245
hudakz 8:8dfdd1603e4d 246 /**
hudakz 6:518950e436be 247 * @brief Sets temperature-to-digital conversion resolution.
hudakz 4:adf4e7972d73 248 * @note The configuration register allows the user to set the resolution
hudakz 6:518950e436be 249 * of the temperature-to-digital conversion to 9, 10, 11, or 12 bits.
hudakz 6:518950e436be 250 * Defaults to 12-bit resolution for DS18B20.
hudakz 6:518950e436be 251 * DS18S20 allows only 9-bit resolution.
hudakz 6:518950e436be 252 * @param res: Resolution of the temperature-to-digital conversion in bits.
hudakz 4:adf4e7972d73 253 * @retval
hudakz 4:adf4e7972d73 254 */
hudakz 24:d683d826dccd 255 void DS1820::setResolution(uint8_t res)
hudakz 24:d683d826dccd 256 {
hudakz 4:adf4e7972d73 257 // keep resolution within limits
hudakz 24:d683d826dccd 258
hudakz 24:d683d826dccd 259 if (res > 12)
hudakz 4:adf4e7972d73 260 res = 12;
hudakz 24:d683d826dccd 261 if (res < 9)
hudakz 24:d683d826dccd 262 res = 9;
hudakz 24:d683d826dccd 263 if (_model_s)
hudakz 4:adf4e7972d73 264 res = 9;
hudakz 24:d683d826dccd 265
hudakz 24:d683d826dccd 266 _oneWire->reset();
hudakz 24:d683d826dccd 267 _oneWire->select(_addr);
hudakz 24:d683d826dccd 268 _oneWire->write_byte(0xBE); // to read Scratchpad
hudakz 24:d683d826dccd 269 for (uint8_t i = 0; i < 9; i++) // read Scratchpad bytes
hudakz 24:d683d826dccd 270 _data[i] = _oneWire->read_byte();
hudakz 4:adf4e7972d73 271
hudakz 24:d683d826dccd 272 _data[4] |= (res - 9) << 5; // update configuration byte (set resolution)
hudakz 24:d683d826dccd 273 _oneWire->reset();
hudakz 24:d683d826dccd 274 _oneWire->select(_addr);
hudakz 24:d683d826dccd 275 _oneWire->write_byte(0x4E); // to write into Scratchpad
hudakz 24:d683d826dccd 276 for (uint8_t i = 2; i < 5; i++) // write three bytes (2nd, 3rd, 4th) into Scratchpad
hudakz 24:d683d826dccd 277 _oneWire->write_byte(_data[i]);
hudakz 4:adf4e7972d73 278 }
hudakz 4:adf4e7972d73 279
hudakz 4:adf4e7972d73 280 /**
hudakz 0:433af64321d5 281 * @brief Starts temperature conversion
hudakz 6:518950e436be 282 * @note The time to complete the converion depends on the selected resolution:
hudakz 6:518950e436be 283 * 9-bit resolution -> max conversion time = 93.75ms
hudakz 6:518950e436be 284 * 10-bit resolution -> max conversion time = 187.5ms
hudakz 6:518950e436be 285 * 11-bit resolution -> max conversion time = 375ms
hudakz 6:518950e436be 286 * 12-bit resolution -> max conversion time = 750ms
hudakz 0:433af64321d5 287 * @param
hudakz 0:433af64321d5 288 * @retval
hudakz 0:433af64321d5 289 */
hudakz 24:d683d826dccd 290 void DS1820::startConversion(void)
hudakz 24:d683d826dccd 291 {
hudakz 24:d683d826dccd 292 if (_present) {
hudakz 24:d683d826dccd 293 _oneWire->reset();
hudakz 24:d683d826dccd 294 _oneWire->select(_addr);
hudakz 24:d683d826dccd 295 _oneWire->write_byte(0x44); //start temperature conversion
hudakz 0:433af64321d5 296 }
hudakz 0:433af64321d5 297 }
hudakz 0:433af64321d5 298
hudakz 0:433af64321d5 299 /**
hudakz 6:518950e436be 300 * @brief Reads temperature from the chip's Scratchpad
hudakz 0:433af64321d5 301 * @note
hudakz 0:433af64321d5 302 * @param
hudakz 0:433af64321d5 303 * @retval Floating point temperature value
hudakz 0:433af64321d5 304 */
hudakz 24:d683d826dccd 305 float DS1820::read(void)
hudakz 24:d683d826dccd 306 {
hudakz 24:d683d826dccd 307 if (_present) {
hudakz 24:d683d826dccd 308 _oneWire->reset();
hudakz 24:d683d826dccd 309 _oneWire->select(_addr);
hudakz 24:d683d826dccd 310 _oneWire->write_byte(0xBE); // to read Scratchpad
hudakz 24:d683d826dccd 311 for (uint8_t i = 0; i < 9; i++) // reading scratchpad registers
hudakz 24:d683d826dccd 312 _data[i] = _oneWire->read_byte();
hudakz 0:433af64321d5 313
hudakz 6:518950e436be 314 // Convert the raw bytes to a 16-bit unsigned value
hudakz 24:d683d826dccd 315 uint16_t* p_word = reinterpret_cast < uint16_t * > (&_data[0]);
hudakz 0:433af64321d5 316
hudakz 0:433af64321d5 317 #if DEBUG
hudakz 17:9ff584b9809f 318 printf("raw = %#x\r\n", *p_word);
hudakz 24:d683d826dccd 319 #endif
hudakz 24:d683d826dccd 320 if (_model_s) {
hudakz 6:518950e436be 321 *p_word = *p_word << 3; // 9-bit resolution
hudakz 24:d683d826dccd 322 if (_data[7] == 0x10) {
hudakz 0:433af64321d5 323
hudakz 6:518950e436be 324 // "count remain" gives full 12-bit resolution
hudakz 24:d683d826dccd 325 *p_word = (*p_word & 0xFFF0) + 12 - _data[6];
hudakz 0:433af64321d5 326 }
hudakz 0:433af64321d5 327 }
hudakz 0:433af64321d5 328 else {
hudakz 24:d683d826dccd 329 uint8_t cfg = (_data[4] & 0x60); // default 12-bit resolution
hudakz 24:d683d826dccd 330
hudakz 4:adf4e7972d73 331 // at lower resolution, the low bits are undefined, so let's clear them
hudakz 24:d683d826dccd 332 if (cfg == 0x00)
hudakz 6:518950e436be 333 *p_word = *p_word &~7; // 9-bit resolution
hudakz 0:433af64321d5 334 else
hudakz 24:d683d826dccd 335 if (cfg == 0x20)
hudakz 6:518950e436be 336 *p_word = *p_word &~3; // 10-bit resolution
hudakz 0:433af64321d5 337 else
hudakz 24:d683d826dccd 338 if (cfg == 0x40)
hudakz 6:518950e436be 339 *p_word = *p_word &~1; // 11-bit resolution
hudakz 0:433af64321d5 340 }
hudakz 6:518950e436be 341
hudakz 6:518950e436be 342 // Convert the raw bytes to a 16-bit signed fixed point value :
hudakz 6:518950e436be 343 // 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment
hudakz 6:518950e436be 344 // and the LSB of the 16-bit binary number represents 1/256th of a unit).
hudakz 6:518950e436be 345 *p_word = *p_word << 4;
hudakz 24:d683d826dccd 346
hudakz 6:518950e436be 347 // Convert to floating point value
hudakz 6:518950e436be 348 return(toFloat(*p_word));
hudakz 0:433af64321d5 349 }
hudakz 0:433af64321d5 350 else
hudakz 0:433af64321d5 351 return 0;
hudakz 0:433af64321d5 352 }
hudakz 0:433af64321d5 353
hudakz 0:433af64321d5 354 /**
hudakz 13:b593a82ce790 355 * @brief Reads temperature from chip's scratchpad.
hudakz 13:b593a82ce790 356 * @note Verifies data integrity by calculating cyclic redundancy check (CRC).
hudakz 13:b593a82ce790 357 * If the calculated CRC dosn't match the one stored in chip's scratchpad register
hudakz 13:b593a82ce790 358 * the temperature variable is not updated and CRC error code is returned.
hudakz 13:b593a82ce790 359 * @param temp: The temperature variable to be updated by this routine.
hudakz 13:b593a82ce790 360 * (It's passed as reference to floating point.)
hudakz 13:b593a82ce790 361 * @retval error code:
hudakz 13:b593a82ce790 362 * 0 - no errors ('temp' contains the temperature measured)
hudakz 13:b593a82ce790 363 * 1 - sensor not present ('temp' is not updated)
hudakz 13:b593a82ce790 364 * 2 - CRC error ('temp' is not updated)
hudakz 13:b593a82ce790 365 */
hudakz 24:d683d826dccd 366 uint8_t DS1820::read(float& temp)
hudakz 24:d683d826dccd 367 {
hudakz 24:d683d826dccd 368 if (_present) {
hudakz 24:d683d826dccd 369 _oneWire->reset();
hudakz 24:d683d826dccd 370 _oneWire->select(_addr);
hudakz 24:d683d826dccd 371 _oneWire->write_byte(0xBE); // to read Scratchpad
hudakz 24:d683d826dccd 372 for (uint8_t i = 0; i < 9; i++) // reading scratchpad registers
hudakz 24:d683d826dccd 373 _data[i] = _oneWire->read_byte();
hudakz 13:b593a82ce790 374
hudakz 24:d683d826dccd 375 if (_oneWire->crc8(_data, 8) != _data[8]) // if calculated CRC does not match the stored one
hudakz 17:9ff584b9809f 376 {
hudakz 17:9ff584b9809f 377 #if DEBUG
hudakz 24:d683d826dccd 378 for (uint8_t i = 0; i < 9; i++)
hudakz 17:9ff584b9809f 379 printf("data[%d]=0x%.2x\r\n", i, data[i]);
hudakz 24:d683d826dccd 380 #endif
hudakz 13:b593a82ce790 381 return 2; // return with CRC error
hudakz 17:9ff584b9809f 382 }
hudakz 13:b593a82ce790 383
hudakz 13:b593a82ce790 384 // Convert the raw bytes to a 16bit unsigned value
hudakz 24:d683d826dccd 385 uint16_t* p_word = reinterpret_cast < uint16_t * > (&_data[0]);
hudakz 13:b593a82ce790 386
hudakz 13:b593a82ce790 387 #if DEBUG
hudakz 17:9ff584b9809f 388 printf("raw = %#x\r\n", *p_word);
hudakz 13:b593a82ce790 389 #endif
hudakz 24:d683d826dccd 390 if (_model_s) {
hudakz 24:d683d826dccd 391 *p_word = *p_word << 3; // 9 bit resolution, max conversion time = 750ms
hudakz 24:d683d826dccd 392 if (_data[7] == 0x10) {
hudakz 13:b593a82ce790 393
hudakz 13:b593a82ce790 394 // "count remain" gives full 12 bit resolution
hudakz 24:d683d826dccd 395 *p_word = (*p_word & 0xFFF0) + 12 - _data[6];
hudakz 13:b593a82ce790 396 }
hudakz 13:b593a82ce790 397
hudakz 13:b593a82ce790 398 // Convert the raw bytes to a 16bit signed fixed point value :
hudakz 13:b593a82ce790 399 // 1 sign bit, 7 integer bits, 8 fractional bits (two's compliment
hudakz 13:b593a82ce790 400 // and the LSB of the 16bit binary number represents 1/256th of a unit).
hudakz 13:b593a82ce790 401 *p_word = *p_word << 4;
hudakz 24:d683d826dccd 402
hudakz 13:b593a82ce790 403 // Convert to floating point value
hudakz 13:b593a82ce790 404 temp = toFloat(*p_word);
hudakz 24:d683d826dccd 405 return 0; // return with no errors
hudakz 13:b593a82ce790 406 }
hudakz 13:b593a82ce790 407 else {
hudakz 24:d683d826dccd 408 uint8_t cfg = (_data[4] & 0x60); // default 12bit resolution, max conversion time = 750ms
hudakz 13:b593a82ce790 409
hudakz 13:b593a82ce790 410 // at lower resolution, the low bits are undefined, so let's clear them
hudakz 24:d683d826dccd 411 if (cfg == 0x00)
hudakz 24:d683d826dccd 412 *p_word = *p_word &~7; // 9bit resolution, max conversion time = 93.75ms
hudakz 13:b593a82ce790 413 else
hudakz 24:d683d826dccd 414 if (cfg == 0x20)
hudakz 24:d683d826dccd 415 *p_word = *p_word &~3; // 10bit resolution, max conversion time = 187.5ms
hudakz 13:b593a82ce790 416 else
hudakz 24:d683d826dccd 417 if (cfg == 0x40)
hudakz 24:d683d826dccd 418 *p_word = *p_word &~1; // 11bit resolution, max conversion time = 375ms
hudakz 13:b593a82ce790 419
hudakz 13:b593a82ce790 420 // Convert the raw bytes to a 16bit signed fixed point value :
hudakz 14:b02fa18b294a 421 // 1 sign bit, 7 integer bits, 8 fractional bits (two's complement
hudakz 13:b593a82ce790 422 // and the LSB of the 16bit binary number represents 1/256th of a unit).
hudakz 13:b593a82ce790 423 *p_word = *p_word << 4;
hudakz 24:d683d826dccd 424
hudakz 13:b593a82ce790 425 // Convert to floating point value
hudakz 13:b593a82ce790 426 temp = toFloat(*p_word);
hudakz 24:d683d826dccd 427 return 0; // return with no errors
hudakz 13:b593a82ce790 428 }
hudakz 13:b593a82ce790 429 }
hudakz 13:b593a82ce790 430 else
hudakz 24:d683d826dccd 431 return 1; // error, sensor is not present
hudakz 13:b593a82ce790 432 }
hudakz 13:b593a82ce790 433
hudakz 13:b593a82ce790 434 /**
hudakz 6:518950e436be 435 * @brief Converts a 16-bit signed fixed point value to floating point value
hudakz 6:518950e436be 436 * @note The 16-bit unsigned integer represnts actually
hudakz 6:518950e436be 437 * a 16-bit signed fixed point value:
hudakz 14:b02fa18b294a 438 * 1 sign bit, 7 integer bits, 8 fractional bits (two’s complement
hudakz 24:d683d826dccd 439 * and the LSB of the 16-bit binary number represents 1/256th of a unit).
hudakz 6:518950e436be 440 * @param 16-bit unsigned integer
hudakz 4:adf4e7972d73 441 * @retval Floating point value
hudakz 0:433af64321d5 442 */
hudakz 24:d683d826dccd 443 float DS1820::toFloat(uint16_t word)
hudakz 24:d683d826dccd 444 {
hudakz 24:d683d826dccd 445 if (word & 0x8000)
hudakz 24:d683d826dccd 446 return(-float(uint16_t(~word + 1)) / 256.0f);
hudakz 0:433af64321d5 447 else
hudakz 24:d683d826dccd 448 return(float(word) / 256.0f);
hudakz 0:433af64321d5 449 }