OneWire + DS18X20 Library
Basic functions for OneWire operations + specific DS18x20 operations
 All Functions Groups Pages
ds18x20.c
1 /*****************************************************************************
2 
3  DS18x20 library
4 
5  Copyright (C) 2016 Falk Brunner
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  You can contact the author at Falk.Brunner@gmx.de
22 
23 *****************************************************************************/
24 
25 #include <util/delay.h>
26 #include <avr/interrupt.h>
27 #include <util/atomic.h>
28 
29 #include "onewire.h"
30 #include "ds18x20.h"
31 
32 uint8_t ds18x20_convert_t(uint8_t parasitic_power) {
33 
34  if (onewire_reset()) {
35  return 1; // no response
36  } else {
37  onewire_write_byte(DS1820_CMD_SKIP_ROM);
38  if (parasitic_power) {
39  //ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
40  uint8_t sreg_tmp=SREG; cli(); // Arduino workaround :-0
41  onewire_write_byte(DS1820_CMD_CONVERT_T);
42  ONEWIRE_STRONG_PU_ON
43  //}
44  SREG = sreg_tmp; // Arduino workaround :-0
45  } else {
46  onewire_write_byte(DS1820_CMD_CONVERT_T);
47  }
48  }
49  return 0;
50 }
51 
52 int16_t ds18x20_read_temp(void) {
53  uint8_t lsb, msb;
54  int16_t temp;
55 
56  onewire_write_byte(DS1820_CMD_READ_SCRATCHPAD);
57  lsb = onewire_read_byte();
58  msb = onewire_read_byte();
59  onewire_reset(); // terminate read process
60 
61  temp = ((int16_t)msb << 8) | lsb;
62 
63  return temp;
64 }
65 
66 int16_t ds18B20_read_temp(void) {
67  int16_t temp;
68 
69  temp = ds18x20_read_temp();
70 
71  // calculate temperature with 0,1 C resolution
72  //
73  // t(0.1C) = t(1/16C) * 0.625
74  // = t(1/16C) * 625 / 1000
75  // = t(1/16C) * 625 * 1.024 / 1024
76  // = t(1/16C) * 640 / 1024
77 
78  temp = (temp*640L) >> 10;
79  return temp;
80 }
81 
82 int16_t ds18S20_read_temp(void) {
83  int16_t temp;
84 
85  temp = ds18x20_read_temp();
86 
87  // calculate temperature with 0,1 C resolution
88  //
89  // t(0.1C) = t(1/2C) * 5
90 
91  temp = temp*5;
92  return temp;
93 }
94 
95 void ds18x20_read_scratchpad(uint8_t *buffer) {
96  uint8_t i;
97 
98  onewire_write_byte(DS1820_CMD_READ_SCRATCHPAD);
99  for (i=0; i<9; i++) {
100  buffer[i]=onewire_read_byte();
101  }
102 }
103 
104 void ds18S20_write_scratchpad(int8_t th, int8_t tl) {
105 
106  onewire_write_byte(DS1820_CMD_WRITE_SCRATCHPAD);
107  onewire_write_byte(th);
108  onewire_write_byte(tl);
109 }
110 
111 void ds18B20_write_scratchpad(uint8_t th, uint8_t tl, uint8_t config) {
112 
113  onewire_write_byte(DS1820_CMD_WRITE_SCRATCHPAD);
114  onewire_write_byte(th);
115  onewire_write_byte(tl);
116  onewire_write_byte(config);
117 }
118 
119 void ds18x20_copy_scratchpad(uint8_t parasitic_power) {
120 
121  if (parasitic_power) {
122  //ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
123  uint8_t sreg_tmp=SREG; cli(); // Arduino workaround :-0
124  onewire_write_byte(DS1820_CMD_COPY_SCRATCHPAD);
125  ONEWIRE_STRONG_PU_ON
126  //}
127  SREG = sreg_tmp; // Arduino workaround :-0
128  } else {
129  onewire_write_byte(DS1820_CMD_COPY_SCRATCHPAD);
130  }
131  _delay_ms(10);
132  ONEWIRE_STRONG_PU_OFF
133 }
134 
135 void ds18x20_recall_E2(void) {
136  onewire_write_byte(DS1820_CMD_RECALL_E2);
137  _delay_ms(1);
138 }
139 
141  onewire_write_byte(DS1820_CMD_READ_POWER_SUPPLY);
142  return !onewire_read_bit();
143 }
uint8_t onewire_reset(void)
OneWire reset.
Definition: onewire.c:19
void onewire_write_byte(uint8_t data)
write one byte
Definition: onewire.c:98
void ds18B20_write_scratchpad(uint8_t th, uint8_t tl, uint8_t config)
write th, tl and configuration of DS18B20
Definition: ds18x20.c:111
uint8_t ds18x20_convert_t(uint8_t parasitic_power)
start temperature conversion
Definition: ds18x20.c:32
int16_t ds18B20_read_temp(void)
Read temperature from DS18B20 (12 bit resolution)
Definition: ds18x20.c:66
uint8_t onewire_read_bit(void)
read one bit from bus
Definition: onewire.c:63
uint8_t onewire_read_byte(void)
read one byte
Definition: onewire.c:85
void ds18x20_copy_scratchpad(uint8_t parasitic_power)
copy scratchpad to EEPROM, busy waiting (10ms),
Definition: ds18x20.c:119
void ds18S20_write_scratchpad(int8_t th, int8_t tl)
write th and tl of DS18S20
Definition: ds18x20.c:104
int16_t ds18x20_read_temp(void)
Read temperature from DS18x20.
Definition: ds18x20.c:52
int16_t ds18S20_read_temp(void)
Read temperature from DS18S20 (9 bit resolution)
Definition: ds18x20.c:82
uint8_t ds18x20_read_power_supply(void)
read power supply
Definition: ds18x20.c:140
void ds18x20_recall_E2(void)
copy EEPROM to scratchpad, busy waiting (1ms)
Definition: ds18x20.c:135
void ds18x20_read_scratchpad(uint8_t *buffer)
Read complete scratchpad of DS18x20 (9 bytes)
Definition: ds18x20.c:95