esp32-ds18b20
ESP32-compatible C library for Maxim Integrated DS18B20 Programmable Resolution 1-Wire Digital Thermometer.
|
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_system.h"
#include "esp_log.h"
#include "ds18b20.h"
#include "owb.h"
Macros | |
#define | DS18B20_FUNCTION_TEMP_CONVERT 0x44 |
Initiate a single temperature conversion. | |
#define | DS18B20_FUNCTION_SCRATCHPAD_WRITE 0x4E |
Write 3 bytes of data to the device scratchpad at positions 2, 3 and 4. | |
#define | DS18B20_FUNCTION_SCRATCHPAD_READ 0xBE |
Read 9 bytes of data (including CRC) from the device scratchpad. | |
#define | DS18B20_FUNCTION_SCRATCHPAD_COPY 0x48 |
Copy the contents of the scratchpad to the device EEPROM. | |
#define | DS18B20_FUNCTION_EEPROM_RECALL 0xB8 |
Restore alarm trigger values and configuration data from EEPROM to the scratchpad. | |
#define | DS18B20_FUNCTION_POWER_SUPPLY_READ 0xB4 |
Determine if a device is using parasitic power. | |
Functions | |
DS18B20_Info * | ds18b20_malloc (void) |
Construct a new device info instance. New instance should be initialised before calling other functions. More... | |
void | ds18b20_free (DS18B20_Info **ds18b20_info) |
Delete an existing device info instance. More... | |
void | ds18b20_init (DS18B20_Info *ds18b20_info, const OneWireBus *bus, OneWireBus_ROMCode rom_code) |
Initialise a device info instance with the specified GPIO. More... | |
void | ds18b20_init_solo (DS18B20_Info *ds18b20_info, const OneWireBus *bus) |
Initialise a device info instance as a solo device on the bus. More... | |
void | ds18b20_use_crc (DS18B20_Info *ds18b20_info, bool use_crc) |
Enable or disable use of CRC checks on device communications. More... | |
bool | ds18b20_set_resolution (DS18B20_Info *ds18b20_info, DS18B20_RESOLUTION resolution) |
Set temperature measurement resolution. More... | |
DS18B20_RESOLUTION | ds18b20_read_resolution (DS18B20_Info *ds18b20_info) |
Update and return the current temperature measurement resolution from the device. More... | |
bool | ds18b20_convert (const DS18B20_Info *ds18b20_info) |
Start a temperature measurement conversion on a single device. More... | |
void | ds18b20_convert_all (const OneWireBus *bus) |
Start temperature conversion on all connected devices. More... | |
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. More... | |
DS18B20_ERROR | ds18b20_read_temp (const DS18B20_Info *ds18b20_info, float *value) |
Read last temperature measurement from device. More... | |
DS18B20_ERROR | ds18b20_convert_and_read_temp (const DS18B20_Info *ds18b20_info, float *value) |
Convert, wait and read current temperature from device. More... | |
DS18B20_ERROR | ds18b20_check_for_parasite_power (const OneWireBus *bus, bool *present) |
Check OneWire bus for presence of parasitic-powered devices. More... | |
Resolution is cached in the DS18B20_Info object to avoid querying the hardware every time a temperature conversion is required. However this can result in the cached value becoming inconsistent with the hardware value, so care must be taken.
DS18B20_Info* ds18b20_malloc | ( | void | ) |
Construct a new device info instance. New instance should be initialised before calling other functions.
void ds18b20_free | ( | DS18B20_Info ** | ds18b20_info | ) |
Delete an existing device info instance.
[in] | ds18b20_info | Pointer to device info instance. |
[in,out] | ds18b20_info | Pointer to device info instance that will be freed and set to NULL. |
void ds18b20_init | ( | DS18B20_Info * | ds18b20_info, |
const OneWireBus * | bus, | ||
OneWireBus_ROMCode | rom_code | ||
) |
Initialise a device info instance with the specified GPIO.
[in] | ds18b20_info | Pointer to device info instance. |
[in] | bus | Pointer to initialised 1-Wire bus instance. |
[in] | rom_code | Device-specific ROM code to identify a 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.
This is subject to the requirement that this device is the ONLY device on the bus. This allows for faster commands to be used without ROM code addressing.
NOTE: if additional devices are added to the bus, operation will cease to work correctly.
[in] | ds18b20_info | Pointer to device info instance. |
[in] | bus | Pointer to initialised 1-Wire bus instance. |
void ds18b20_use_crc | ( | DS18B20_Info * | ds18b20_info, |
bool | use_crc | ||
) |
Enable or disable use of CRC checks on device communications.
[in] | ds18b20_info | Pointer to device info instance. |
[in] | use_crc | True to enable CRC checks, false to disable. |
bool ds18b20_set_resolution | ( | DS18B20_Info * | ds18b20_info, |
DS18B20_RESOLUTION | resolution | ||
) |
Set temperature measurement resolution.
This programs the hardware to the specified resolution and sets the cached value to be the same. If the program fails, the value currently in hardware is used to refresh the cache.
[in] | ds18b20_info | Pointer to device info instance. |
[in] | resolution | Selected resolution. |
DS18B20_RESOLUTION ds18b20_read_resolution | ( | DS18B20_Info * | ds18b20_info | ) |
Update and return the current temperature measurement resolution from the device.
[in] | ds18b20_info | Pointer to device info instance. |
bool ds18b20_convert | ( | const DS18B20_Info * | ds18b20_info | ) |
Start a temperature measurement conversion on a single device.
[in] | ds18b20_info | Pointer to device info instance. |
void ds18b20_convert_all | ( | const OneWireBus * | bus | ) |
Start temperature conversion on all connected devices.
This should be followed by a sufficient delay to ensure all devices complete their conversion before the measurements are read.
[in] | bus | Pointer to initialised bus instance. |
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.
[in] | ds18b20_info | Pointer to device info instance. |
DS18B20_ERROR ds18b20_read_temp | ( | const DS18B20_Info * | ds18b20_info, |
float * | value | ||
) |
Read last temperature measurement from device.
This is typically called after ds18b20_start_mass_conversion(), provided enough time has elapsed to ensure that all devices have completed their conversions.
[in] | ds18b20_info | Pointer to device info instance. Must be initialised first. |
[out] | value | Pointer to the measurement value returned by the device, in degrees Celsius. |
DS18B20_ERROR ds18b20_convert_and_read_temp | ( | const DS18B20_Info * | ds18b20_info, |
float * | value | ||
) |
Convert, wait and read current temperature from device.
[in] | ds18b20_info | Pointer to device info instance. Must be initialised first. |
[out] | value | Pointer to the measurement value returned by the device, in degrees Celsius. |
DS18B20_ERROR ds18b20_check_for_parasite_power | ( | const OneWireBus * | bus, |
bool * | present | ||
) |
Check OneWire bus for presence of parasitic-powered devices.
[in] | bus | Pointer to initialised bus instance. |
[out] | present | Result value, true if a parasitic-powered device was detected. |