summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Tittelbach <xro@realraum.at>2013-08-04 03:12:29 +0000
committerBernhard Tittelbach <xro@realraum.at>2013-08-04 03:12:29 +0000
commitbe76990656840614230e406f88a8d99c1d81cc6a (patch)
treeeb83cd78e00447b9b804cd3224a24e48b6f85e75
parentwhitespace cleanups (diff)
ds1820 onewire lib
git-svn-id: https://svn.spreadspace.org/avr/trunk@214 aa12f405-d877-488e-9caf-2d797e2a1cc7
-rw-r--r--lib/ds1820.c167
-rw-r--r--lib/ds1820.h61
-rw-r--r--lib/onewire.h23
-rw-r--r--usb-1wire/Makefile2
-rw-r--r--usb-1wire/usb-1wire.c138
5 files changed, 264 insertions, 127 deletions
diff --git a/lib/ds1820.c b/lib/ds1820.c
new file mode 100644
index 0000000..ca1ff16
--- /dev/null
+++ b/lib/ds1820.c
@@ -0,0 +1,167 @@
+/*
+ * spreadspace avr utils - usb-1wire example
+ *
+ *
+ * Copyright (C) 2013 Bernhard Tittelbach <xro@realraum.at>
+ * based on code from:
+ * http://www.pjrc.com/teensy/td_libs_OneWire.html
+ *
+ * This file is part of spreadspace avr utils.
+ *
+ * spreadspace avr utils is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * spreadspace avr utils is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "ds1820.h"
+#include "onewire.h"
+#include "util/delay.h"
+
+uint8_t owi_ds1820_addr_[MAX_OWI_DEVICES][8];
+uint8_t num_ds1820_found_;
+
+uint8_t ds1820_discover(void)
+{
+ uint8_t d=0;
+ num_ds1820_found_ = 0;
+ if (! owi_reset()) //if no devices on bus, return now
+ return 0;
+ owi_reset_search();
+ _delay_ms(250);
+
+ //Search only for DS1820 temp sensors
+ owi_target_search(DS1820_FAMILY_ID);
+
+ while ( owi_search(owi_ds1820_addr_[ d ]))
+ {
+ d++;
+ if ( d >= MAX_OWI_DEVICES)
+ break;
+ }
+ num_ds1820_found_ = d;
+ return num_ds1820_found_;
+}
+
+void ds1820_set_resolution(uint8_t d, uint8_t bits)
+{
+ uint8_t resolution;
+ if (d >= num_ds1820_found_ || bits < 9 || bits > 12)
+ return;
+
+ owi_reset();
+ owi_select(owi_ds1820_addr_[d]);
+
+ owi_write(DS1820_WRITE_SCRATCHPAD, 0);
+ owi_write(0xFF, 0);
+ owi_write(0x7F, 0);
+
+ resolution = (bits - 9) << 5;
+ owi_write(resolution | 0x1F, 0);
+}
+
+void ds1820_start_measuring(uint8_t d)
+{
+ owi_reset();
+ owi_select(owi_ds1820_addr_[d]);
+ owi_write(DS1820_START_CONVERSION, 1);
+}
+
+uint16_t ds1820_get_conversion_time_ms(uint8_t bits)
+{
+ switch (bits)
+ {
+ case 9: return DS1820_TCONV_MS_9BITS;
+ case 10: return DS1820_TCONV_MS_10BITS;
+ case 11: return DS1820_TCONV_MS_11BITS;
+ default: return DS1820_TCONV_MS_12BITS;
+ }
+}
+
+void ds1820_wait_conversion_time(uint8_t bits)
+{
+ switch (bits)
+ {
+ case 9: _delay_ms(DS1820_TCONV_MS_9BITS); break;
+ case 10: _delay_ms(DS1820_TCONV_MS_10BITS); break;
+ case 11: _delay_ms(DS1820_TCONV_MS_11BITS); break;
+ default: _delay_ms(DS1820_TCONV_MS_12BITS); break;
+ }
+}
+
+int16_t ds1820_read_temperature(uint8_t d)
+{
+ uint8_t data[9];
+ int16_t raw = 0;
+ uint8_t type_s=0;
+ uint8_t cfg, i;
+
+ owi_reset();
+ owi_select(owi_ds1820_addr_[d]);
+ owi_write(DS1820_READ_SCRATCHPAD, 0); // Read Scratchpad
+ for (i = 0; i < 9; i++) { // we need 9 bytes
+ data[i] = owi_read();
+ }
+ #if (ONEWIRE_CRC == 1)
+ uint8_t crc_result = owi_crc8(data,8);
+ if (crc_result != data[8])
+ return DS1820_ERROR;
+ #endif
+
+ // the first ROM byte indicates which chip
+ switch (owi_ds1820_addr_[d][0])
+ {
+ case 0x10:
+ type_s = 1;
+ break;
+ case 0x28:
+ case 0x22:
+ type_s = 0;
+ break;
+ default:
+ break;
+ }
+
+ // Convert the data to actual temperature
+ // because the result is a 16 bit signed integer, it should
+ // be stored to an "int16_t" type, which is always 16 bits
+ // even when compiled on a 32 bit processor.
+ raw = (data[1] << 8) | data[0];
+ if (type_s)
+ {
+ raw = raw << 3; // 9 bit resolution default
+ if (data[7] == 0x10) {
+ // "count remain" gives full 12 bit resolution
+ raw = (raw & 0xFFF0) + 12 - data[6];
+ }
+ } else {
+ cfg = (data[4] & 0x60);
+ // at lower res, the low bits are undefined, so let's zero them
+ if (cfg == DS1820_RESOLUTION_9BITS)
+ raw = raw & ~7; // 9 bit resolution, 93.75 ms
+ else if (cfg == DS1820_RESOLUTION_10BITS)
+ raw = raw & ~3; // 10 bit res, 187.5 ms
+ else if (cfg == DS1820_RESOLUTION_11BITS)
+ raw = raw & ~1; // 11 bit res, 375 ms
+ //// default is 12 bit resolution, 750 ms conversion time
+ }
+
+ return raw;
+}
+
+float ds1820_raw_temp_to_celsius(int16_t t)
+{
+ if ( t == DS1820_ERROR )
+ return DS1820_ERROR;
+ else
+ return ((float) t) / 16.0;
+}
diff --git a/lib/ds1820.h b/lib/ds1820.h
new file mode 100644
index 0000000..4c29d07
--- /dev/null
+++ b/lib/ds1820.h
@@ -0,0 +1,61 @@
+/*
+ * spreadspace avr utils - usb-1wire example
+ *
+ *
+ * Copyright (C) 2013 Bernhard Tittelbach <xro@realraum.at>
+ * based on code from:
+ * http://www.pjrc.com/teensy/td_libs_OneWire.html
+ *
+ * This file is part of spreadspace avr utils.
+ *
+ * spreadspace avr utils is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * spreadspace avr utils is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __DS1820__
+#define __DS1820__
+
+#include <stdint.h>
+
+#define DS1820_FAMILY_ID 0x10
+#define DS1820_START_CONVERSION 0x44
+#define DS1820_READ_SCRATCHPAD 0xbe
+#define DS1820_WRITE_SCRATCHPAD 0x4e
+#define DS1820_ERROR -1000 // Return code. Outside temperature range.
+#define DS1820_RESOLUTION_9BITS 0x00
+#define DS1820_RESOLUTION_10BITS 0x20
+#define DS1820_RESOLUTION_11BITS 0x40
+#define DS1820_RESOLUTION_12BITS 0x60
+#define DS1820_TCONV_MS_9BITS 96
+#define DS1820_TCONV_MS_10BITS 195
+#define DS1820_TCONV_MS_11BITS 390
+#define DS1820_TCONV_MS_12BITS 800
+
+#define DS2890_FAMILY_ID 0x2c
+#define DS2890_WRITE_CONTROL_REGISTER 0X55
+#define DS2890_RELEASE_CODE 0x96
+#define DS2890_WRITE_POSITION 0x0f
+
+#ifndef MAX_OWI_DEVICES
+#define MAX_OWI_DEVICES 8
+#endif
+
+uint8_t ds1820_discover(void);
+void ds1820_set_resolution(uint8_t d, uint8_t bits);
+void ds1820_start_measuring(uint8_t d);
+uint16_t ds1820_get_conversion_time_ms(uint8_t bits);
+void ds1820_wait_conversion_time(uint8_t bits);
+int16_t ds1820_read_temperature(uint8_t d);
+float ds1820_raw_temp_to_celsius(int16_t t);
+
+#endif \ No newline at end of file
diff --git a/lib/onewire.h b/lib/onewire.h
index 0cbd2a4..1410fde 100644
--- a/lib/onewire.h
+++ b/lib/onewire.h
@@ -103,29 +103,6 @@
#error "Please define I/O register types here"
#endif
-#define DS1820_FAMILY_ID 0x10
-#define DS1820_START_CONVERSION 0x44
-#define DS1820_READ_SCRATCHPAD 0xbe
-#define DS1820_WRITE_SCRATCHPAD 0x4e
-#define DS1820_ERROR -1000 // Return code. Outside temperature range.
-#define DS1820_RESOLUTION_9BITS 0x00
-#define DS1820_RESOLUTION_10BITS 0x20
-#define DS1820_RESOLUTION_11BITS 0x40
-#define DS1820_RESOLUTION_12BITS 0x60
-#define DS1820_TCONV_MS_9BITS 96
-#define DS1820_TCONV_MS_10BITS 195
-#define DS1820_TCONV_MS_11BITS 390
-#define DS1820_TCONV_MS_12BITS 800
-
-
-
-
-
-#define DS2890_FAMILY_ID 0x2c
-#define DS2890_WRITE_CONTROL_REGISTER 0X55
-#define DS2890_RELEASE_CODE 0x96
-#define DS2890_WRITE_POSITION 0x0f
-
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
diff --git a/usb-1wire/Makefile b/usb-1wire/Makefile
index 099fac3..1f08967 100644
--- a/usb-1wire/Makefile
+++ b/usb-1wire/Makefile
@@ -23,7 +23,7 @@
NAME := usb-1wire
BOARD_TYPE := teensy2
OBJ := $(NAME).o
-LIBS := util led lufa-descriptor-usbserial usbio onewire
+LIBS := util led lufa-descriptor-usbserial usbio onewire ds1820
EXTERNAL_LIBS := lufa
LUFA_PATH := ../contrib/LUFA-120219
diff --git a/usb-1wire/usb-1wire.c b/usb-1wire/usb-1wire.c
index 4480a0a..e96100f 100644
--- a/usb-1wire/usb-1wire.c
+++ b/usb-1wire/usb-1wire.c
@@ -33,28 +33,28 @@
#include "led.h"
#include "usbio.h"
+#define MAX_OWI_DEVICES 8
+
#include "onewire.h"
+#include "ds1820.h"
-#define MAX_OWI_DEVICES 3
uint8_t owi_addr_[MAX_OWI_DEVICES][8];
-uint8_t num_owi_dev_found_;
+uint8_t num_owi_dev_found_ = 0;
+uint8_t num_temp_sensors_ = 0;
void discoverOWIBus(void)
{
uint8_t d=0;
led_on();
- printf("Searching OW Bus ");
+ printf("Searching 1W Bus ");
num_owi_dev_found_ = 0;
if (owi_reset())
printf(" !something is there! ");
owi_reset_search();
_delay_ms(250);
- //Search only for DS1820 temp sensors
- owi_target_search(DS1820_FAMILY_ID);
-
while ( owi_search(owi_addr_[ d ]))
{
d++;
@@ -81,113 +81,45 @@ void discoverOWIBus(void)
led_off();
}
-void tempToUSB(uint8_t resolution)
+void tempToUSB(uint8_t bit_resolution)
{
- uint8_t data[9];
- int16_t celsius = 0;
- int16_t raw = 0;
- uint8_t type_s, cfg;
- uint8_t d=0;
- uint8_t cfg_res=12;
- uint8_t crc_result = 0;
+ uint8_t sensor_index = 0;
+ uint16_t raw_temp = 0;
+ double temp=0.0;
led_on();
- owi_reset();
- //we assume we do NOT use parasitic power, so we can issue
- //commands while other sensors are already busy converting
- for (d=0; d<num_owi_dev_found_; d++)
+ if (num_temp_sensors_ == 0)
{
- owi_select(owi_addr_[d]);
-
- owi_write(DS1820_WRITE_SCRATCHPAD, 0);
- owi_write(0xFF, 0);
- owi_write(0x7F, 0);
- owi_write(resolution | 0x1F, 0);
- owi_reset();
- owi_select(owi_addr_[d]);
- owi_write(DS1820_START_CONVERSION, 0);
+ printf("No DS1820 sensors ?? running bus discovery... \r\n");
+ num_temp_sensors_ = ds1820_discover();
}
+ printf("%d DS18x20 sensors on the bus\r\n", num_temp_sensors_);
- switch (resolution)
+ for (sensor_index=0; sensor_index < num_temp_sensors_; sensor_index++)
{
- case DS1820_RESOLUTION_9BITS: _delay_ms(DS1820_TCONV_MS_9BITS); break;
- case DS1820_RESOLUTION_10BITS: _delay_ms(DS1820_TCONV_MS_10BITS); break;
- case DS1820_RESOLUTION_11BITS: _delay_ms(DS1820_TCONV_MS_11BITS); break;
- default: _delay_ms(DS1820_TCONV_MS_12BITS); break;
+ ds1820_set_resolution(sensor_index, bit_resolution);
+ ds1820_start_measuring(sensor_index);
}
- if (owi_reset()) //reset is needed
- printf("Sensor present\r\n");
- else
- printf("Sensor NOT present\r\n");
+ ds1820_wait_conversion_time(bit_resolution);
- for (d=0; d<num_owi_dev_found_; d++)
+ for (sensor_index=0; sensor_index < num_temp_sensors_; sensor_index++)
{
- owi_select(owi_addr_[d]);
- owi_write(DS1820_READ_SCRATCHPAD, 0); // Read Scratchpad
- printf("Data: ");
- for (uint8_t i = 0; i < 9; i++) { // we need 9 bytes
- data[i] = owi_read();
- printf("%02x " , data[i]);
- }
- crc_result = owi_crc8(data,8);
- printf(" crc: %x is %s\r\n" , crc_result, (crc_result == data[8])? "OK": "ERRORENOUS");
-
- // the first ROM byte indicates which chip
- switch (owi_addr_[d][0])
+ raw_temp = ds1820_read_temperature(sensor_index);
+ printf("DS1820 #%d: ", sensor_index);
+ if (raw_temp == DS1820_ERROR)
{
- case 0x10:
- //~ Serial.println(" Chip = DS18S20"); // or old DS1820
- type_s = 1;
- break;
- case 0x28:
- //~ Serial.println(" Chip = DS18B20");
- type_s = 0;
- break;
- case 0x22:
- //~ Serial.println(" Chip = DS1822");
- type_s = 0;
- break;
- default:
- continue;
- }
-
- // Convert the data to actual temperature
- // because the result is a 16 bit signed integer, it should
- // be stored to an "int16_t" type, which is always 16 bits
- // even when compiled on a 32 bit processor.
- raw = (data[1] << 8) | data[0];
- if (type_s)
- {
- raw = raw << 3; // 9 bit resolution default
- if (data[7] == 0x10) {
- // "count remain" gives full 12 bit resolution
- raw = (raw & 0xFFF0) + 12 - data[6];
- }
+ printf("CRC comm error\r\n");
} else {
- cfg = (data[4] & 0x60);
- // at lower res, the low bits are undefined, so let's zero them
- if (cfg == 0x00)
- {
- raw = raw & ~7; // 9 bit resolution, 93.75 ms
- cfg_res = 9;
- }
- else if (cfg == 0x20)
- {
- raw = raw & ~3; // 10 bit res, 187.5 ms
- cfg_res = 10;
- }
- else if (cfg == 0x40)
- {
- raw = raw & ~1; // 11 bit res, 375 ms
- cfg_res = 11;
- }
- //// default is 12 bit resolution, 750 ms conversion time
+ temp = ds1820_raw_temp_to_celsius( raw_temp );
+ printf("raw: %d, celsius (float): %f, celsius (int): %d.%d, %d bit resolution\r\n",
+ raw_temp,
+ temp ,
+ raw_temp / 16, 10 * (raw_temp % 16) / 16,
+ bit_resolution
+ );
}
- celsius = raw / 16;
-
- printf("Temperature: raw %d, celsius: %d, resolution: %d bits\r\n", raw, celsius, cfg_res);
}
led_off();
}
@@ -197,10 +129,11 @@ void handle_cmd(uint8_t cmd)
{
switch(cmd) {
case '0': discoverOWIBus(); break;
- case '1': tempToUSB(DS1820_RESOLUTION_9BITS); break;
- case '2': tempToUSB(DS1820_RESOLUTION_10BITS); break;
- case '3': tempToUSB(DS1820_RESOLUTION_11BITS); break;
- case '4': tempToUSB(DS1820_RESOLUTION_12BITS); break;
+ case '1': num_temp_sensors_ = ds1820_discover(); break;
+ case '2': tempToUSB(9); break;
+ case '3': tempToUSB(10); break;
+ case '4': tempToUSB(11); break;
+ case '5': tempToUSB(12); break;
case 'r': reset2bootloader(); break;
default: printf("error\r\n"); return;
}
@@ -216,7 +149,6 @@ int main(void)
led_init();
usbio_init();
sei();
- DDRC &= ~7;
owi_init(7, &PINC);
for(;;)