DS18x20: DS18S20 support, bugfixes (#255)

This commit is contained in:
Ruslan V. Uss 2016-10-28 18:29:47 +06:00 committed by sheinz
parent 5b12ba54dc
commit 5a14ab31e4
2 changed files with 39 additions and 13 deletions

View file

@ -6,4 +6,10 @@ INC_DIRS += $(ds18b20_ROOT)..
# args for passing into compile rule generation # args for passing into compile rule generation
ds18b20_SRC_DIR = $(ds18b20_ROOT) 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)) $(eval $(call component_compile_rules,ds18b20))

View file

@ -18,6 +18,15 @@
#define os_sleep_ms(x) vTaskDelay(((x) + portTICK_RATE_MS - 1) / portTICK_RATE_MS) #define os_sleep_ms(x) vTaskDelay(((x) + portTICK_RATE_MS - 1) / portTICK_RATE_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) { uint8_t ds18b20_read_all(uint8_t pin, ds_sensor_t *result) {
onewire_addr_t addr; onewire_addr_t addr;
onewire_search_t search; onewire_search_t search;
@ -28,7 +37,7 @@ uint8_t ds18b20_read_all(uint8_t pin, ds_sensor_t *result) {
while ((addr = onewire_search_next(&search, pin)) != ONEWIRE_NONE) { while ((addr = onewire_search_next(&search, pin)) != ONEWIRE_NONE) {
uint8_t crc = onewire_crc8((uint8_t *)&addr, 7); uint8_t crc = onewire_crc8((uint8_t *)&addr, 7);
if (crc != (addr >> 56)){ if (crc != (addr >> 56)){
printf("CRC check failed: %02X %02X\n", (unsigned)(addr >> 56), crc); debug("CRC check failed: %02X %02X\n", (unsigned)(addr >> 56), crc);
return 0; return 0;
} }
@ -49,11 +58,11 @@ uint8_t ds18b20_read_all(uint8_t pin, ds_sensor_t *result) {
get[k]=onewire_read(pin); get[k]=onewire_read(pin);
} }
//printf("\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]); //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); crc = onewire_crc8(get, 8);
if (crc != get[8]){ if (crc != get[8]){
printf("CRC check failed: %02X %02X\n", get[8], crc); debug("CRC check failed: %02X %02X\n", get[8], crc);
return 0; return 0;
} }
@ -64,7 +73,7 @@ uint8_t ds18b20_read_all(uint8_t pin, ds_sensor_t *result) {
float temperature; float temperature;
temperature = (temp * 625.0)/10000; temperature = (temp * 625.0)/10000;
//printf("Got a DS18B20 Reading: %d.%02d\n", (int)temperature, (int)(temperature - (int)temperature) * 100); //debug("Got a DS18B20 Reading: %d.%02d\n", (int)temperature, (int)(temperature - (int)temperature) * 100);
result[sensor_id].id = sensor_id; result[sensor_id].id = sensor_id;
result[sensor_id].value = temperature; result[sensor_id].value = temperature;
sensor_id++; sensor_id++;
@ -91,11 +100,11 @@ float ds18b20_read_single(uint8_t pin) {
get[k]=onewire_read(pin); get[k]=onewire_read(pin);
} }
//printf("\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]); //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); uint8_t crc = onewire_crc8(get, 8);
if (crc != get[8]){ if (crc != get[8]){
printf("CRC check failed: %02X %02X", get[8], crc); debug("CRC check failed: %02X %02X", get[8], crc);
return 0; return 0;
} }
@ -108,7 +117,7 @@ float ds18b20_read_single(uint8_t pin) {
temperature = (temp * 625.0)/10000; temperature = (temp * 625.0)/10000;
return temperature; return temperature;
//printf("Got a DS18B20 Reading: %d.%02d\n", (int)temperature, (int)(temperature - (int)temperature) * 100); //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) { bool ds18b20_measure(int pin, ds18b20_addr_t addr, bool wait) {
@ -156,7 +165,7 @@ bool ds18b20_read_scratchpad(int pin, ds18b20_addr_t addr, uint8_t *buffer) {
expected_crc = onewire_crc8(buffer, 8); expected_crc = onewire_crc8(buffer, 8);
if (crc != expected_crc) { if (crc != expected_crc) {
printf("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); 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 false;
} }
@ -172,8 +181,16 @@ float ds18b20_read_temperature(int pin, ds18b20_addr_t addr) {
} }
temp = scratchpad[1] << 8 | scratchpad[0]; temp = scratchpad[1] << 8 | scratchpad[0];
return ((float)temp * 625.0)/10000; 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) { float ds18b20_measure_and_read(int pin, ds18b20_addr_t addr) {
@ -200,10 +217,13 @@ int ds18b20_scan_devices(int pin, ds18b20_addr_t *addr_list, int addr_count) {
onewire_search_start(&search); onewire_search_start(&search);
while ((addr = onewire_search_next(&search, pin)) != ONEWIRE_NONE) { while ((addr = onewire_search_next(&search, pin)) != ONEWIRE_NONE) {
if (found < addr_count) { uint8_t family_id = (uint8_t)addr;
addr_list[found] = addr; if (family_id == DS18B20_FAMILY_ID || family_id == DS18S20_FAMILY_ID) {
if (found < addr_count) {
addr_list[found] = addr;
}
found++;
} }
found++;
} }
return found; return found;
} }