Dallas' DS1820 family temperature sensor. For more details see [https://developer.mbed.org/users/hudakz/code/DS1820/wiki/Homepage]
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
}
}
}
DS1820.cpp
- Committer:
- hudakz
- Date:
- 2015-10-28
- Revision:
- 13:b593a82ce790
- Parent:
- 8:8dfdd1603e4d
- Child:
- 14:b02fa18b294a
File content as of revision 13:b593a82ce790:
/*
* Dallas' DS1820 family temperature sensor.
* This library depends on the OneWire library (Dallas' 1-Wire bus protocol implementation)
* available at <http://developer.mbed.org/users/hudakz/code/OneWire/>
*
* Example of use:
*
* #include "DS1820.h"
*
* Serial serial(USBTX, USBRX);
*
* int main() {
* DS1820 ds1820(PA_9); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
*
* if(ds1820.begin()) {
* ds1820.startConversion();
* wait(1.0);
* while(1) {
* serial.printf("temp = %3.1f\r\n", ds1820.read()); // read temperature
* ds1820.startConversion(); // start temperature conversion
* wait(1.0); // let DS1820 complete the temperature conversion
* }
* } else
* serial.printf("No DS1820 sensor found!\r\n");
* }
*
*
* Note: Don't forget to connect a 4.7k Ohm resistor
* between the DS1820's data pin and the +3.3V pin
*
*/
#include "DS1820.h"
#define DEBUG 0
#if DEBUG
extern Serial serial;
#endif
/**
* @brief Constructs a generic DS1820 sensor
* @note begin() must be called to detect and initialize the actual model
* @param pin: Name of data pin
* @retval
*/
DS1820::DS1820(PinName pin) :
oneWire(pin) {
present = false;
model_s = false;
}
/**
* @brief Constructs a specific model
* @note No need to call begin() to detect and initialize the model
* @param model: One character model name: 'S', 's', 'B' or 'b'
* pin: Name of data pin
* @retval
*/
DS1820::DS1820(char model, PinName pin) :
oneWire(pin) {
if((model == 'S') or (model == 's')) {
present = true;
model_s = true;
}
else if((model == 'B') or (model == 'b')) {
present = true;
model_s = false;
}
else
present = false;
}
/**
* @brief Detects and initializes the actual DS1820 model
* @note
* @param
* @retval true: if a DS1820 family sensor was detected and initialized
false: otherwise
*/
bool DS1820::begin(void) {
oneWire.reset_search();
wait_ms(250);
if(!oneWire.search(addr)) {
#if DEBUG
serial.printf("No addresses.\r\n");
#endif
oneWire.reset_search();
wait_ms(250);
return false;
}
#if DEBUG
serial.printf("ROM =");
for(uint8_t i = 0; i < 8; i++) {
serial.printf(" %x", addr[i]);
}
serial.printf("\r\n");
#endif
if(OneWire::crc8(addr, 7) == addr[7]) {
present = true;
// the first ROM byte indicates which chip
switch(addr[0]) {
case 0x10:
model_s = true;
#if DEBUG
serial.printf("DS18S20 or old DS1820\r\n");
#endif
break;
case 0x28:
model_s = false;
#if DEBUG
serial.printf("DS18B20\r\n");
#endif
break;
case 0x22:
model_s = false;
#if DEBUG
serial.printf("DS1822\r\n");
#endif
break;
default:
present = false;
#if DEBUG
serial.printf("Device doesn't belong to the DS1820 family\r\n");
#endif
return false;
}
return true;
}
else {
#if DEBUG
serial.printf("Invalid CRC!\r\n");
#endif
return false;
}
}
/**
* @brief Informs about presence of a DS1820 sensor.
* @note begin() shall be called before using this function
* if a generic DS1820 instance was created by the user.
* No need to call begin() for a specific DS1820 instance.
* @param
* @retval true: when a DS1820 sensor is present
* false: otherwise
*/
bool DS1820::isPresent(void) {
return present;
}
/**
* @brief Sets temperature-to-digital conversion resolution.
* @note The configuration register allows the user to set the resolution
* of the temperature-to-digital conversion to 9, 10, 11, or 12 bits.
* Defaults to 12-bit resolution for DS18B20.
* DS18S20 allows only 9-bit resolution.
* @param res: Resolution of the temperature-to-digital conversion in bits.
* @retval
*/
void DS1820::setResolution(uint8_t res) {
// keep resolution within limits
if(res > 12)
res = 12;
if(res < 9)
res = 9;
if(model_s)
res = 9;
oneWire.reset();
oneWire.skip();
oneWire.write(0xBE); // to read Scratchpad
for(uint8_t i = 0; i < 9; i++) // read Scratchpad bytes
data[i] = oneWire.read();
data[4] |= (res - 9) << 5; // update configuration byte (set resolution)
oneWire.reset();
oneWire.skip();
oneWire.write(0x4E); // to write into Scratchpad
for(uint8_t i = 2; i < 5; i++) // write three bytes (2nd, 3rd, 4th) into Scratchpad
oneWire.write(data[i]);
}
/**
* @brief Starts temperature conversion
* @note The time to complete the converion depends on the selected resolution:
* 9-bit resolution -> max conversion time = 93.75ms
* 10-bit resolution -> max conversion time = 187.5ms
* 11-bit resolution -> max conversion time = 375ms
* 12-bit resolution -> max conversion time = 750ms
* @param
* @retval
*/
void DS1820::startConversion(void) {
if(present) {
oneWire.reset();
oneWire.skip();
oneWire.write(0x44); //start temperature conversion
}
}
/**
* @brief Reads temperature from the chip's Scratchpad
* @note
* @param
* @retval Floating point temperature value
*/
float DS1820::read(void) {
if(present) {
oneWire.reset();
oneWire.skip();
oneWire.write(0xBE); // to read Scratchpad
for(uint8_t i = 0; i < 9; i++) // read Scratchpad bytes
data[i] = oneWire.read();
// Convert the raw bytes to a 16-bit unsigned value
uint16_t* p_word = reinterpret_cast < uint16_t * > (&data[0]);
#if DEBUG
serial.printf("raw = %#x\r\n", *p_word);
#endif
if(model_s) {
*p_word = *p_word << 3; // 9-bit resolution
if(data[7] == 0x10) {
// "count remain" gives full 12-bit resolution
*p_word = (*p_word & 0xFFF0) + 12 - data[6];
}
}
else {
uint8_t cfg = (data[4] & 0x60); // default 12-bit resolution
// at lower resolution, the low bits are undefined, so let's clear them
if(cfg == 0x00)
*p_word = *p_word &~7; // 9-bit resolution
else
if(cfg == 0x20)
*p_word = *p_word &~3; // 10-bit resolution
else
if(cfg == 0x40)
*p_word = *p_word &~1; // 11-bit resolution
}
// Convert the raw bytes to a 16-bit signed fixed point value :
// 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment
// and the LSB of the 16-bit binary number represents 1/256th of a unit).
*p_word = *p_word << 4;
// Convert to floating point value
return(toFloat(*p_word));
}
else
return 0;
}
/**
* @brief Reads temperature from chip's scratchpad.
* @note Verifies data integrity by calculating cyclic redundancy check (CRC).
* If the calculated CRC dosn't match the one stored in chip's scratchpad register
* the temperature variable is not updated and CRC error code is returned.
* @param temp: The temperature variable to be updated by this routine.
* (It's passed as reference to floating point.)
* @retval error code:
* 0 - no errors ('temp' contains the temperature measured)
* 1 - sensor not present ('temp' is not updated)
* 2 - CRC error ('temp' is not updated)
*/
uint8_t DS1820::read(float& temp) {
if(present) {
oneWire.reset();
oneWire.skip();
oneWire.write(0xBE); // to read Scratchpad
for(uint8_t i = 0; i < 9; i++) // reading scratchpad registers
data[i] = oneWire.read();
if(oneWire.crc8(data, 8) != data[8]) // if calculated CRC does not match the stored one
return 2; // return with CRC error
// Convert the raw bytes to a 16bit unsigned value
uint16_t* p_word = reinterpret_cast < uint16_t * > (&data[0]);
#if DEBUG
serial.printf("raw = %#x\r\n", *p_word);
#endif
if(model_s) {
*p_word = *p_word << 3; // 9 bit resolution, max conversion time = 750ms
if(data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
*p_word = (*p_word & 0xFFF0) + 12 - data[6];
}
// Convert the raw bytes to a 16bit signed fixed point value :
// 1 sign bit, 7 integer bits, 8 fractional bits (two's compliment
// and the LSB of the 16bit binary number represents 1/256th of a unit).
*p_word = *p_word << 4;
// Convert to floating point value
temp = toFloat(*p_word);
return 0; // return with no errors
}
else {
uint8_t cfg = (data[4] & 0x60); // default 12bit resolution, max conversion time = 750ms
// at lower resolution, the low bits are undefined, so let's clear them
if(cfg == 0x00)
*p_word = *p_word &~7; // 9bit resolution, max conversion time = 93.75ms
else
if(cfg == 0x20)
*p_word = *p_word &~3; // 10bit resolution, max conversion time = 187.5ms
else
if(cfg == 0x40)
*p_word = *p_word &~1; // 11bit resolution, max conversion time = 375ms
// Convert the raw bytes to a 16bit signed fixed point value :
// 1 sign bit, 7 integer bits, 8 fractional bits (two's compliment
// and the LSB of the 16bit binary number represents 1/256th of a unit).
*p_word = *p_word << 4;
// Convert to floating point value
temp = toFloat(*p_word);
return 0; // return with no errors
}
}
else
return 1; // error, sensor is not present
}
/**
* @brief Converts a 16-bit signed fixed point value to floating point value
* @note The 16-bit unsigned integer represnts actually
* a 16-bit signed fixed point value:
* 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment
* and the LSB of the 16-bit binary number represents 1/256th of a unit).
* @param 16-bit unsigned integer
* @retval Floating point value
*/
float DS1820::toFloat(uint16_t word) {
if(word & 0x8000)
return (-float(uint16_t(~word + 1)) / 256.0f);
else
return (float(word) / 256.0f);
}
