termistor bib 01
This commit is contained in:
parent
c91e18f6db
commit
4f6656d37e
169 changed files with 6210 additions and 0 deletions
15
embadet/components/ds18b20/component.mk
Normal file
15
embadet/components/ds18b20/component.mk
Normal file
|
@ -0,0 +1,15 @@
|
|||
# Component makefile for extras/ds18b20
|
||||
|
||||
# expected anyone using bmp driver includes it as 'ds18b20/ds18b20.h'
|
||||
INC_DIRS += $(ds18b20_ROOT)..
|
||||
|
||||
# args for passing into compile rule generation
|
||||
ds18b20_SRC_DIR = $(ds18b20_ROOT)
|
||||
|
||||
# users can override this setting and get console debug output
|
||||
DS18B20_DEBUG ?= 0
|
||||
ifeq ($(DS18B20_DEBUG),1)
|
||||
ds18b20_CFLAGS = $(CFLAGS) -DDS18B20_DEBUG
|
||||
endif
|
||||
|
||||
$(eval $(call component_compile_rules,ds18b20))
|
244
embadet/components/ds18b20/ds18b20.c
Normal file
244
embadet/components/ds18b20/ds18b20.c
Normal file
|
@ -0,0 +1,244 @@
|
|||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "math.h"
|
||||
|
||||
|
||||
#include "ds18b20.h"
|
||||
|
||||
#define DS18B20_WRITE_SCRATCHPAD 0x4E
|
||||
#define DS18B20_READ_SCRATCHPAD 0xBE
|
||||
#define DS18B20_COPY_SCRATCHPAD 0x48
|
||||
#define DS18B20_READ_EEPROM 0xB8
|
||||
#define DS18B20_READ_PWRSUPPLY 0xB4
|
||||
#define DS18B20_SEARCHROM 0xF0
|
||||
#define DS18B20_SKIP_ROM 0xCC
|
||||
#define DS18B20_READROM 0x33
|
||||
#define DS18B20_MATCHROM 0x55
|
||||
#define DS18B20_ALARMSEARCH 0xEC
|
||||
#define DS18B20_CONVERT_T 0x44
|
||||
|
||||
#define os_sleep_ms(x) vTaskDelay(((x) + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS)
|
||||
|
||||
#define DS18B20_FAMILY_ID 0x28
|
||||
#define DS18S20_FAMILY_ID 0x10
|
||||
|
||||
#ifdef DS18B20_DEBUG
|
||||
#define debug(fmt, ...) printf("%s" fmt "\n", "DS18B20: ", ## __VA_ARGS__);
|
||||
#else
|
||||
#define debug(fmt, ...)
|
||||
#endif
|
||||
|
||||
uint8_t ds18b20_read_all(uint8_t pin, ds_sensor_t *result) {
|
||||
onewire_addr_t addr;
|
||||
onewire_search_t search;
|
||||
uint8_t sensor_id = 0;
|
||||
|
||||
onewire_search_start(&search);
|
||||
|
||||
while ((addr = onewire_search_next(&search, pin)) != ONEWIRE_NONE) {
|
||||
uint8_t crc = onewire_crc8((uint8_t *)&addr, 7);
|
||||
if (crc != (addr >> 56)){
|
||||
debug("CRC check failed: %02X %02X\n", (unsigned)(addr >> 56), crc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
onewire_reset(pin);
|
||||
onewire_select(pin, addr);
|
||||
onewire_write(pin, DS18B20_CONVERT_T);
|
||||
|
||||
onewire_power(pin);
|
||||
vTaskDelay(750 / portTICK_PERIOD_MS);
|
||||
|
||||
onewire_reset(pin);
|
||||
onewire_select(pin, addr);
|
||||
onewire_write(pin, DS18B20_READ_SCRATCHPAD);
|
||||
|
||||
uint8_t get[10];
|
||||
|
||||
for (int k=0;k<9;k++){
|
||||
get[k]=onewire_read(pin);
|
||||
}
|
||||
|
||||
//debug("\n ScratchPAD DATA = %X %X %X %X %X %X %X %X %X\n",get[8],get[7],get[6],get[5],get[4],get[3],get[2],get[1],get[0]);
|
||||
crc = onewire_crc8(get, 8);
|
||||
|
||||
if (crc != get[8]){
|
||||
debug("CRC check failed: %02X %02X\n", get[8], crc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t temp_msb = get[1]; // Sign byte + lsbit
|
||||
uint8_t temp_lsb = get[0]; // Temp data plus lsb
|
||||
uint16_t temp = temp_msb << 8 | temp_lsb;
|
||||
|
||||
float temperature;
|
||||
|
||||
temperature = (temp * 625.0)/10000;
|
||||
//debug("Got a DS18B20 Reading: %d.%02d\n", (int)temperature, (int)(temperature - (int)temperature) * 100);
|
||||
result[sensor_id].id = sensor_id;
|
||||
result[sensor_id].value = temperature;
|
||||
sensor_id++;
|
||||
}
|
||||
return sensor_id;
|
||||
}
|
||||
|
||||
float ds18b20_read_single(uint8_t pin) {
|
||||
|
||||
onewire_reset(pin);
|
||||
onewire_skip_rom(pin);
|
||||
onewire_write(pin, DS18B20_CONVERT_T);
|
||||
|
||||
onewire_power(pin);
|
||||
vTaskDelay(750 / portTICK_PERIOD_MS);
|
||||
|
||||
onewire_reset(pin);
|
||||
onewire_skip_rom(pin);
|
||||
onewire_write(pin, DS18B20_READ_SCRATCHPAD);
|
||||
|
||||
uint8_t get[10];
|
||||
|
||||
for (int k=0;k<9;k++){
|
||||
get[k]=onewire_read(pin);
|
||||
}
|
||||
|
||||
//debug("\n ScratchPAD DATA = %X %X %X %X %X %X %X %X %X\n",get[8],get[7],get[6],get[5],get[4],get[3],get[2],get[1],get[0]);
|
||||
uint8_t crc = onewire_crc8(get, 8);
|
||||
|
||||
if (crc != get[8]){
|
||||
debug("CRC check failed: %02X %02X", get[8], crc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t temp_msb = get[1]; // Sign byte + lsbit
|
||||
uint8_t temp_lsb = get[0]; // Temp data plus lsb
|
||||
|
||||
uint16_t temp = temp_msb << 8 | temp_lsb;
|
||||
|
||||
float temperature;
|
||||
|
||||
temperature = (temp * 625.0)/10000;
|
||||
return temperature;
|
||||
//debug("Got a DS18B20 Reading: %d.%02d\n", (int)temperature, (int)(temperature - (int)temperature) * 100);
|
||||
}
|
||||
|
||||
bool ds18b20_measure(int pin, ds18b20_addr_t addr, bool wait) {
|
||||
if (!onewire_reset(pin)) {
|
||||
return false;
|
||||
}
|
||||
if (addr == DS18B20_ANY) {
|
||||
onewire_skip_rom(pin);
|
||||
} else {
|
||||
onewire_select(pin, addr);
|
||||
}
|
||||
taskENTER_CRITICAL();
|
||||
onewire_write(pin, DS18B20_CONVERT_T);
|
||||
// For parasitic devices, power must be applied within 10us after issuing
|
||||
// the convert command.
|
||||
onewire_power(pin);
|
||||
taskEXIT_CRITICAL();
|
||||
|
||||
if (wait) {
|
||||
os_sleep_ms(750);
|
||||
onewire_depower(pin);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ds18b20_read_scratchpad(int pin, ds18b20_addr_t addr, uint8_t *buffer) {
|
||||
uint8_t crc;
|
||||
uint8_t expected_crc;
|
||||
|
||||
if (!onewire_reset(pin)) {
|
||||
return false;
|
||||
}
|
||||
if (addr == DS18B20_ANY) {
|
||||
onewire_skip_rom(pin);
|
||||
} else {
|
||||
onewire_select(pin, addr);
|
||||
}
|
||||
onewire_write(pin, DS18B20_READ_SCRATCHPAD);
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
buffer[i] = onewire_read(pin);
|
||||
}
|
||||
crc = onewire_read(pin);
|
||||
|
||||
expected_crc = onewire_crc8(buffer, 8);
|
||||
if (crc != expected_crc) {
|
||||
debug("CRC check failed reading scratchpad: %02x %02x %02x %02x %02x %02x %02x %02x : %02x (expected %02x)\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], crc, expected_crc);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
float ds18b20_read_temperature(int pin, ds18b20_addr_t addr) {
|
||||
uint8_t scratchpad[8];
|
||||
int16_t temp;
|
||||
|
||||
if (!ds18b20_read_scratchpad(pin, addr, scratchpad)) {
|
||||
return NAN;
|
||||
}
|
||||
|
||||
temp = scratchpad[1] << 8 | scratchpad[0];
|
||||
|
||||
float res;
|
||||
if ((uint8_t)addr == DS18B20_FAMILY_ID) {
|
||||
res = ((float)temp * 625.0)/10000;
|
||||
}
|
||||
else {
|
||||
temp = ((temp & 0xfffe) << 3) + (16 - scratchpad[6]) - 4;
|
||||
res = ((float)temp * 625.0)/10000 - 0.25;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
float ds18b20_measure_and_read(int pin, ds18b20_addr_t addr) {
|
||||
if (!ds18b20_measure(pin, addr, true)) {
|
||||
return NAN;
|
||||
}
|
||||
return ds18b20_read_temperature(pin, addr);
|
||||
}
|
||||
|
||||
bool ds18b20_measure_and_read_multi(int pin, ds18b20_addr_t *addr_list, int addr_count, float *result_list) {
|
||||
if (!ds18b20_measure(pin, DS18B20_ANY, true)) {
|
||||
for (int i=0; i < addr_count; i++) {
|
||||
result_list[i] = NAN;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return ds18b20_read_temp_multi(pin, addr_list, addr_count, result_list);
|
||||
}
|
||||
|
||||
int ds18b20_scan_devices(int pin, ds18b20_addr_t *addr_list, int addr_count) {
|
||||
onewire_search_t search;
|
||||
onewire_addr_t addr;
|
||||
int found = 0;
|
||||
|
||||
onewire_search_start(&search);
|
||||
while ((addr = onewire_search_next(&search, pin)) != ONEWIRE_NONE) {
|
||||
uint8_t family_id = (uint8_t)addr;
|
||||
if (family_id == DS18B20_FAMILY_ID || family_id == DS18S20_FAMILY_ID) {
|
||||
if (found < addr_count) {
|
||||
addr_list[found] = addr;
|
||||
}
|
||||
found++;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
bool ds18b20_read_temp_multi(int pin, ds18b20_addr_t *addr_list, int addr_count, float *result_list) {
|
||||
bool result = true;
|
||||
|
||||
for (int i = 0; i < addr_count; i++) {
|
||||
result_list[i] = ds18b20_read_temperature(pin, addr_list[i]);
|
||||
if (isnan(result_list[i])) {
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
157
embadet/components/ds18b20/ds18b20.h
Normal file
157
embadet/components/ds18b20/ds18b20.h
Normal file
|
@ -0,0 +1,157 @@
|
|||
#ifndef DRIVER_DS18B20_H_
|
||||
#define DRIVER_DS18B20_H_
|
||||
#include <onewire/onewire.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @file ds18b20.h
|
||||
*
|
||||
* Communicate with the DS18B20 family of one-wire temperature sensor ICs.
|
||||
*
|
||||
*/
|
||||
|
||||
typedef onewire_addr_t ds18b20_addr_t;
|
||||
|
||||
/** An address value which can be used to indicate "any device on the bus" */
|
||||
#define DS18B20_ANY ONEWIRE_NONE
|
||||
|
||||
/** Find the addresses of all DS18B20 devices on the bus.
|
||||
*
|
||||
* Scans the bus for all devices and places their addresses in the supplied
|
||||
* array. If there are more than `addr_count` devices on the bus, only the
|
||||
* first `addr_count` are recorded.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the DS18B20 bus
|
||||
* @param addr_list A pointer to an array of ds18b20_addr_t values. This
|
||||
* will be populated with the addresses of the found
|
||||
* devices.
|
||||
* @param addr_count Number of slots in the `addr_list` array. At most this
|
||||
* many addresses will be returned.
|
||||
*
|
||||
* @returns The number of devices found. Note that this may be less than,
|
||||
* equal to, or more than `addr_count`, depending on how many DS18B20 devices
|
||||
* are attached to the bus.
|
||||
*/
|
||||
int ds18b20_scan_devices(int pin, ds18b20_addr_t *addr_list, int addr_count);
|
||||
|
||||
/** Tell one or more sensors to perform a temperature measurement and
|
||||
* conversion (CONVERT_T) operation. This operation can take up to 750ms to
|
||||
* complete.
|
||||
*
|
||||
* If `wait=true`, this routine will automatically drive the pin high for the
|
||||
* necessary 750ms after issuing the command to ensure parasitically-powered
|
||||
* devices have enough power to perform the conversion operation (for
|
||||
* non-parasitically-powered devices, this is not necessary but does not
|
||||
* hurt). If `wait=false`, this routine will drive the pin high, but will
|
||||
* then return immediately. It is up to the caller to wait the requisite time
|
||||
* and then depower the bus using onewire_depower() or by issuing another
|
||||
* command once conversion is done.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the DS18B20 device
|
||||
* @param addr The 64-bit address of the device on the bus. This can be set
|
||||
* to ::DS18B20_ANY to send the command to all devices on the bus
|
||||
* at the same time.
|
||||
* @param wait Whether to wait for the necessary 750ms for the DS18B20 to
|
||||
* finish performing the conversion before returning to the
|
||||
* caller (You will normally want to do this).
|
||||
*
|
||||
* @returns `true` if the command was successfully issued, or `false` on error.
|
||||
*/
|
||||
bool ds18b20_measure(int pin, ds18b20_addr_t addr, bool wait);
|
||||
|
||||
/** Read the value from the last CONVERT_T operation.
|
||||
*
|
||||
* This should be called after ds18b20_measure() to fetch the result of the
|
||||
* temperature measurement.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the DS18B20 device
|
||||
* @param addr The 64-bit address of the device to read. This can be set
|
||||
* to ::DS18B20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
*
|
||||
* @returns The temperature in degrees Celsius, or NaN if there was an error.
|
||||
*/
|
||||
float ds18b20_read_temperature(int pin, ds18b20_addr_t addr);
|
||||
|
||||
/** Read the value from the last CONVERT_T operation for multiple devices.
|
||||
*
|
||||
* This should be called after ds18b20_measure() to fetch the result of the
|
||||
* temperature measurement.
|
||||
*
|
||||
* @param pin The GPIO pin connected to the DS18B20 bus
|
||||
* @param addr_list A list of addresses for devices to read.
|
||||
* @param addr_count The number of entries in `addr_list`.
|
||||
* @param result_list An array of floats to hold the returned temperature
|
||||
* values. It should have at least `addr_count` entries.
|
||||
*
|
||||
* @returns `true` if all temperatures were fetched successfully, or `false`
|
||||
* if one or more had errors (the temperature for erroring devices will be
|
||||
* returned as NaN).
|
||||
*/
|
||||
bool ds18b20_read_temp_multi(int pin, ds18b20_addr_t *addr_list, int addr_count, float *result_list);
|
||||
|
||||
/** Perform a ds18b20_measure() followed by ds18b20_read_temperature()
|
||||
*
|
||||
* @param pin The GPIO pin connected to the DS18B20 device
|
||||
* @param addr The 64-bit address of the device to read. This can be set
|
||||
* to ::DS18B20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
*
|
||||
* @returns The temperature in degrees Celsius, or NaN if there was an error.
|
||||
*/
|
||||
float ds18b20_measure_and_read(int pin, ds18b20_addr_t addr);
|
||||
|
||||
/** Perform a ds18b20_measure() followed by ds18b20_read_temp_multi()
|
||||
*
|
||||
* @param pin The GPIO pin connected to the DS18B20 bus
|
||||
* @param addr_list A list of addresses for devices to read.
|
||||
* @param addr_count The number of entries in `addr_list`.
|
||||
* @param result_list An array of floats to hold the returned temperature
|
||||
* values. It should have at least `addr_count` entries.
|
||||
*
|
||||
* @returns `true` if all temperatures were fetched successfully, or `false`
|
||||
* if one or more had errors (the temperature for erroring devices will be
|
||||
* returned as NaN).
|
||||
*/
|
||||
bool ds18b20_measure_and_read_multi(int pin, ds18b20_addr_t *addr_list, int addr_count, float *result_list);
|
||||
|
||||
/** Read the scratchpad data for a particular DS18B20 device.
|
||||
*
|
||||
* This is not generally necessary to do directly. It is done automatically
|
||||
* as part of ds18b20_read_temperature().
|
||||
*
|
||||
* @param pin The GPIO pin connected to the DS18B20 device
|
||||
* @param addr The 64-bit address of the device to read. This can be set
|
||||
* to ::DS18B20_ANY to read any device on the bus (but note
|
||||
* that this will only work if there is exactly one device
|
||||
* connected, or they will corrupt each others' transmissions)
|
||||
* @param buffer An 8-byte buffer to hold the read data.
|
||||
*
|
||||
* @returns `true` if the data was read successfully, or `false` on error.
|
||||
*/
|
||||
bool ds18b20_read_scratchpad(int pin, ds18b20_addr_t addr, uint8_t *buffer);
|
||||
|
||||
// The following are obsolete/deprecated APIs
|
||||
|
||||
typedef struct {
|
||||
uint8_t id;
|
||||
float value;
|
||||
} ds_sensor_t;
|
||||
|
||||
// Scan all ds18b20 sensors on bus and return its amount.
|
||||
// Result are saved in array of ds_sensor_t structure.
|
||||
uint8_t ds18b20_read_all(uint8_t pin, ds_sensor_t *result);
|
||||
|
||||
// This method is just to demonstrate how to read
|
||||
// temperature from single dallas chip.
|
||||
float ds18b20_read_single(uint8_t pin);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DRIVER_DS18B20_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue