OneWire + DS18X20 Library
Basic functions for OneWire operations + specific DS18x20 operations
 All Functions Groups Pages
ds18x20.cpp
1 /*****************************************************************************
2 
3  DS18x20 library
4 
5  Copyright (C) 2016 Falk Brunner
6 
7 *****************************************************************************/
8 
9 /*
10 * ----------------------------------------------------------------------------
11 * "THE BEER-WARE LICENSE" (Revision 42):
12 * <Falk.Brunner@gmx.de> wrote this file. As long as you retain this notice you
13 * can do whatever you want with this stuff. If we meet some day, and you think
14 * this stuff is worth it, you can buy me a beer in return. Falk Brunner
15 * ----------------------------------------------------------------------------
16 */
17 
18 #include <util/delay.h>
19 #include <avr/interrupt.h>
20 #include <util/atomic.h>
21 
22 #include "onewire.h"
23 #include "ds18x20.h"
24 
25 uint8_t ds18x20_convert_t(uint8_t parasitic_power) {
26  uint8_t rc;
27 
28  rc = onewire_reset();
29  if (rc) {
30  return rc;
31  } else {
32  onewire_write_byte(DS18x20_CMD_SKIP_ROM);
33  if (parasitic_power) {
34  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
35  onewire_write_byte(DS18x20_CMD_CONVERT_T);
36  ONEWIRE_STRONG_PU_ON
37  }
38  } else {
39  onewire_write_byte(DS18x20_CMD_CONVERT_T);
40  }
41  }
42  return ONEWIRE_OK;
43 }
44 
45 uint8_t ds18B20_read_temp(int16_t *temperature) {
46  int16_t temp;
47  uint8_t scratchpad[9];
48 
49  ds18x20_read_scratchpad(scratchpad);
50  if (onewire_crc(scratchpad, 9)) {
51  return ONEWIRE_CRC_ERROR;
52  }
53 
54  temp = ((int16_t)scratchpad[1] << 8) | scratchpad[0];
55  // zero undefined LSBs, depending on current resolution
56  switch((scratchpad[4] >> 5) & 3) {
57  case 0: temp &= ~7; break; // 9 Bit
58  case 1: temp &= ~3; break; // 10 Bit
59  case 2: temp &= ~1; break; // 11 Bit
60  }
61  // calculate temperature with 0.1 C resolution using fixed point arithmetic
62  // t(0.1C) = t(1/16C) * 10/16
63  *temperature = (temp * 10) >> 4;
64  return ONEWIRE_OK;
65 }
66 
67 uint8_t ds18S20_read_temp(int16_t *temperature) {
68  int16_t temp;
69  uint8_t scratchpad[9];
70 
71  ds18x20_read_scratchpad(scratchpad);
72  if (onewire_crc(scratchpad, 9)) {
73  return ONEWIRE_CRC_ERROR;
74  }
75 
76  temp = ((int16_t)scratchpad[1] << 8) | scratchpad[0];
77  temp &= ~1; // clear bit#0
78  temp <<= 3; // x8 -> resolution 1/16 C
79  // calculate extended resolution according to data sheet
80  // /16 must be omitted, since we are using a resolution of 1/16 degree C
81  temp = temp - 4 + (16-scratchpad[6]);
82  // calculate temperature with 0.1 C resolution using fixed point arithmetic
83  // t(0.1C) = t(1/16C) * 10/16
84  *temperature = (temp * 10) >> 4;
85  return ONEWIRE_OK;
86 }
87 
88 void ds18x20_read_scratchpad(uint8_t *buffer) {
89  uint8_t i;
90 
91  onewire_write_byte(DS18x20_CMD_READ_SCRATCHPAD);
92  for (i=0; i<9; i++) {
93  buffer[i]=onewire_read_byte();
94  }
95 }
96 
97 void ds18S20_write_scratchpad(int8_t th, int8_t tl) {
98 
99  onewire_write_byte(DS18x20_CMD_WRITE_SCRATCHPAD);
100  onewire_write_byte(th);
101  onewire_write_byte(tl);
102 }
103 
104 void ds18B20_write_scratchpad(int8_t th, int8_t tl, uint8_t adc_resolution) {
105  uint8_t cfg;
106 
107  onewire_write_byte(DS18x20_CMD_WRITE_SCRATCHPAD);
108  onewire_write_byte(th);
109  onewire_write_byte(tl);
110  switch(adc_resolution) {
111  case 9: cfg = 0x00; break;
112  case 10: cfg = 0x20; break;
113  case 11: cfg = 0x40; break;
114  default: cfg = 0x60; break; // 12 bit
115  }
116  onewire_write_byte(cfg);
117 }
118 
119 void ds18x20_copy_scratchpad(uint8_t parasitic_power) {
120 
121  if (parasitic_power) {
122  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
123  onewire_write_byte(DS18x20_CMD_COPY_SCRATCHPAD);
124  ONEWIRE_STRONG_PU_ON
125  }
126  } else {
127  onewire_write_byte(DS18x20_CMD_COPY_SCRATCHPAD);
128  }
129  _delay_ms(10);
130  ONEWIRE_STRONG_PU_OFF
131 }
132 
133 void ds18x20_recall_E2(void) {
134  onewire_write_byte(DS18x20_CMD_RECALL_E2);
135  _delay_ms(1);
136 }
137 
139  onewire_write_byte(DS18x20_CMD_READ_POWER_SUPPLY);
140  return !onewire_read_bit();
141 }
void ds18B20_write_scratchpad(int8_t th, int8_t tl, uint8_t adc_resolution)
write th, tl and configuration of DS18B20
Definition: ds18x20.cpp:104
uint8_t onewire_reset(void)
OneWire reset.
Definition: onewire.cpp:27
uint8_t ds18S20_read_temp(int16_t *temperature)
Read temperature from DS18S20 (9 bit + enhanced resolution, effective 12 bits)
Definition: ds18x20.cpp:67
void onewire_write_byte(uint8_t data)
write one byte
Definition: onewire.cpp:100
uint8_t onewire_crc(const uint8_t *data, uint8_t cnt)
calculate CRC over data array, fast version, 0.3ms for 8 bytes @1MHz
Definition: onewire.cpp:287
uint8_t ds18x20_convert_t(uint8_t parasitic_power)
start temperature conversion
Definition: ds18x20.cpp:25
uint8_t onewire_read_bit(void)
read one bit from bus
Definition: onewire.cpp:67
uint8_t onewire_read_byte(void)
read one byte
Definition: onewire.cpp:87
void ds18x20_copy_scratchpad(uint8_t parasitic_power)
copy scratchpad to EEPROM, busy waiting (10ms),
Definition: ds18x20.cpp:119
void ds18S20_write_scratchpad(int8_t th, int8_t tl)
write th and tl of DS18S20
Definition: ds18x20.cpp:97
uint8_t ds18B20_read_temp(int16_t *temperature)
Read temperature from DS18B20 (9-12 bit resolution)
Definition: ds18x20.cpp:45
uint8_t ds18x20_read_power_supply(void)
read power supply
Definition: ds18x20.cpp:138
void ds18x20_recall_E2(void)
copy EEPROM to scratchpad, busy waiting (1ms)
Definition: ds18x20.cpp:133
void ds18x20_read_scratchpad(uint8_t *buffer)
Read complete scratchpad of DS18x20 (9 bytes)
Definition: ds18x20.cpp:88