esp32-ds18b20
ESP32-compatible C library for Maxim Integrated DS18B20 Programmable Resolution 1-Wire Digital Thermometer.
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
ds18b20.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2017 David Antliff
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
34 #ifndef DS18B20_H
35 #define DS18B20_H
36 
37 #include "owb.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
46 typedef enum
47 {
49  DS18B20_OK = 0,
55 
59 typedef enum
60 {
67 
72 typedef struct
73 {
74  bool init;
75  bool solo;
76  bool use_crc;
77  const OneWireBus * bus;
78  OneWireBus_ROMCode rom_code;
80 } DS18B20_Info;
81 
88 
94 void ds18b20_free(DS18B20_Info ** ds18b20_info);
95 
102 void ds18b20_init(DS18B20_Info * ds18b20_info, const OneWireBus * bus, OneWireBus_ROMCode rom_code);
103 
115 void ds18b20_init_solo(DS18B20_Info * ds18b20_info, const OneWireBus * bus);
116 
122 void ds18b20_use_crc(DS18B20_Info * ds18b20_info, bool use_crc);
123 
134 bool ds18b20_set_resolution(DS18B20_Info * ds18b20_info, DS18B20_RESOLUTION resolution);
135 
142 
148 OneWireBus_ROMCode ds18b20_read_rom(DS18B20_Info * ds18b20_info);
149 
154 bool ds18b20_convert(const DS18B20_Info * ds18b20_info);
155 
163 void ds18b20_convert_all(const OneWireBus * bus);
164 
172 float ds18b20_wait_for_conversion(const DS18B20_Info * ds18b20_info);
173 
183 DS18B20_ERROR ds18b20_read_temp(const DS18B20_Info * ds18b20_info, float * value);
184 
191 DS18B20_ERROR ds18b20_convert_and_read_temp(const DS18B20_Info * ds18b20_info, float * value);
192 
200 DS18B20_ERROR ds18b20_check_for_parasite_power(const OneWireBus * bus, bool * present);
201 
202 #ifdef __cplusplus
203 }
204 #endif
205 
206 #endif // DS18B20_H
Structure containing information related to a single DS18B20 device connected via a 1-Wire bus...
Definition: ds18b20.h:72
Success.
Definition: ds18b20.h:49
A parameter or value is NULL.
Definition: ds18b20.h:53
bool use_crc
True if CRC checks are to be used when retrieving information from a device on the bus...
Definition: ds18b20.h:76
DS18B20_ERROR ds18b20_read_temp(const DS18B20_Info *ds18b20_info, float *value)
Read last temperature measurement from device.
Definition: ds18b20.c:523
void ds18b20_free(DS18B20_Info **ds18b20_info)
Delete an existing device info instance.
Definition: ds18b20.c:352
bool solo
True if device is intended to be the only one connected to the bus, otherwise false.
Definition: ds18b20.h:75
A One Wire Bus error occurred.
Definition: ds18b20.h:52
11-bit resolution, LSB bit 0 undefined
Definition: ds18b20.h:64
9-bit resolution, LSB bits 2,1,0 undefined
Definition: ds18b20.h:62
DS18B20_Info * ds18b20_malloc(void)
Construct a new device info instance. New instance should be initialised before calling other functio...
Definition: ds18b20.c:336
bool ds18b20_convert(const DS18B20_Info *ds18b20_info)
Start a temperature measurement conversion on a single device.
Definition: ds18b20.c:467
DS18B20_ERROR
Success and error codes.
Definition: ds18b20.h:46
12-bit resolution (default)
Definition: ds18b20.h:65
Invalid resolution.
Definition: ds18b20.h:61
An unknown error occurred, or the value was not set.
Definition: ds18b20.h:48
bool ds18b20_set_resolution(DS18B20_Info *ds18b20_info, DS18B20_RESOLUTION resolution)
Set temperature measurement resolution.
Definition: ds18b20.c:404
void ds18b20_convert_all(const OneWireBus *bus)
Start temperature conversion on all connected devices.
Definition: ds18b20.c:487
10-bit resolution, LSB bits 1,0 undefined
Definition: ds18b20.h:63
const OneWireBus * bus
Pointer to 1-Wire bus information relevant to this device.
Definition: ds18b20.h:77
DS18B20_ERROR ds18b20_check_for_parasite_power(const OneWireBus *bus, bool *present)
Check OneWire bus for presence of parasitic-powered devices.
Definition: ds18b20.c:579
DS18B20_RESOLUTION
Symbols for the supported temperature resolution of the device.
Definition: ds18b20.h:59
void ds18b20_init(DS18B20_Info *ds18b20_info, const OneWireBus *bus, OneWireBus_ROMCode rom_code)
Initialise a device info instance with the specified GPIO.
Definition: ds18b20.c:362
A device error occurred.
Definition: ds18b20.h:50
DS18B20_RESOLUTION ds18b20_read_resolution(DS18B20_Info *ds18b20_info)
Update and return the current temperature measurement resolution from the device. ...
Definition: ds18b20.c:443
float ds18b20_wait_for_conversion(const DS18B20_Info *ds18b20_info)
Wait for the maximum conversion time according to the current resolution of the device. In external power mode, the device or devices can signal when conversion has completed. In parasitic power mode, this is not possible, so a pre-calculated delay is performed.
Definition: ds18b20.c:503
void ds18b20_use_crc(DS18B20_Info *ds18b20_info, bool use_crc)
Enable or disable use of CRC checks on device communications.
Definition: ds18b20.c:395
DS18B20_ERROR ds18b20_convert_and_read_temp(const DS18B20_Info *ds18b20_info, float *value)
Convert, wait and read current temperature from device.
Definition: ds18b20.c:555
DS18B20_RESOLUTION resolution
Temperature measurement resolution per reading.
Definition: ds18b20.h:79
OneWireBus_ROMCode rom_code
The ROM code used to address this device on the bus.
Definition: ds18b20.h:78
OneWireBus_ROMCode ds18b20_read_rom(DS18B20_Info *ds18b20_info)
Read 64-bit ROM code from device - only works when there is a single device on the bus...
void ds18b20_init_solo(DS18B20_Info *ds18b20_info, const OneWireBus *bus)
Initialise a device info instance as a solo device on the bus.
Definition: ds18b20.c:378
A CRC error occurred.
Definition: ds18b20.h:51
bool init
True if struct has been initialised, otherwise false.
Definition: ds18b20.h:74