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 Jul 20 08:09:58 2020 +0000
Revision:
23:74a4ff420541
Parent:
22:16537aa25bd9
Child:
24:d683d826dccd
Dallas' DS1820 family temperature sensor library

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