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 *****************************************************************************/
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 
27  if (onewire_reset()) {
28  return 1; // no response
29  } else {
30  onewire_write_byte(DS1820_CMD_SKIP_ROM);
31  if (parasitic_power) {
32  //ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
33  uint8_t sreg_tmp=SREG; cli(); // Arduino workaround :-0
34  onewire_write_byte(DS1820_CMD_CONVERT_T);
35  ONEWIRE_STRONG_PU_ON
36  //}
37  SREG = sreg_tmp; // Arduino workaround :-0
38  } else {
39  onewire_write_byte(DS1820_CMD_CONVERT_T);
40  }
41  }
42  return 0;
43 }
44 
45 uint8_t ds18B20_read_temp(int16_t *temperature) {
46  int16_t temp;
47  uint8_t scratchpad[9];
48  uint8_t rc;
49 
50  ds18x20_read_scratchpad(scratchpad);
51  rc=onewire_crc(scratchpad, 9);
52  if (rc) {
53  return ONEWIRE_CRC_ERROR;
54  }
55 
56  temp = ((int16_t)scratchpad[1] << 8) | scratchpad[0];
57  // calculate temperature with 0.1 C resolution using fixed point arithmetic
58  // t(0.1C) = t(1/16C) * 10/16
59  temp = (temp * 10) >> 4;
60  *temperature = temp;
61  return ONEWIRE_OK;
62 }
63 
64 uint8_t ds18S20_read_temp(int16_t *temperature) {
65  int16_t temp;
66  uint8_t scratchpad[9];
67  uint8_t rc;
68 
69  ds18x20_read_scratchpad(scratchpad);
70  rc=onewire_crc(scratchpad, 9);
71  if (rc) {
72  return ONEWIRE_CRC_ERROR;
73  }
74 
75  temp = ((int16_t)scratchpad[1] << 8) | scratchpad[0];
76  temp &= ~1; // clear bit#0
77  temp <<= 3; // x8 -> resolution 1/16 C
78  // calculate extended resolution
79  // /16 must be omitted, since we are using a resolution of 1/16 degree C
80  temp = temp - 4 + (16-scratchpad[6]);
81  // calculate temperature with 0.1 C resolution using fixed point arithmetic
82  // t(0.1C) = t(1/16C) * 10/16
83  temp = (temp * 10) >> 4;
84  *temperature = temp;
85  return ONEWIRE_OK;
86 }
87 
88 void ds18x20_read_scratchpad(uint8_t *buffer) {
89  uint8_t i;
90 
91  onewire_write_byte(DS1820_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(DS1820_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 config) {
105 
106  onewire_write_byte(DS1820_CMD_WRITE_SCRATCHPAD);
107  onewire_write_byte(th);
108  onewire_write_byte(tl);
109  onewire_write_byte(config);
110 }
111 
112 void ds18x20_copy_scratchpad(uint8_t parasitic_power) {
113 
114  if (parasitic_power) {
115  //ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
116  uint8_t sreg_tmp=SREG; cli(); // Arduino workaround :-0
117  onewire_write_byte(DS1820_CMD_COPY_SCRATCHPAD);
118  ONEWIRE_STRONG_PU_ON
119  //}
120  SREG = sreg_tmp; // Arduino workaround :-0
121  } else {
122  onewire_write_byte(DS1820_CMD_COPY_SCRATCHPAD);
123  }
124  _delay_ms(10);
125  ONEWIRE_STRONG_PU_OFF
126 }
127 
128 void ds18x20_recall_E2(void) {
129  onewire_write_byte(DS1820_CMD_RECALL_E2);
130  _delay_ms(1);
131 }
132 
134  onewire_write_byte(DS1820_CMD_READ_POWER_SUPPLY);
135  return !onewire_read_bit();
136 }
uint8_t onewire_reset(void)
OneWire reset.
Definition: onewire.c:27
uint8_t ds18S20_read_temp(int16_t *temperature)
Read temperature from DS18S20 (9 bit + enhanced resolution, effective 12 bits)
Definition: ds18x20.c:64
void onewire_write_byte(uint8_t data)
write one byte
Definition: onewire.c:106
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.c:312
void ds18B20_write_scratchpad(int8_t th, int8_t tl, uint8_t config)
write th, tl and configuration of DS18B20
Definition: ds18x20.c:104
uint8_t ds18x20_convert_t(uint8_t parasitic_power)
start temperature conversion
Definition: ds18x20.c:25
uint8_t onewire_read_bit(void)
read one bit from bus
Definition: onewire.c:71
uint8_t onewire_read_byte(void)
read one byte
Definition: onewire.c:93
void ds18x20_copy_scratchpad(uint8_t parasitic_power)
copy scratchpad to EEPROM, busy waiting (10ms),
Definition: ds18x20.c:112
void ds18S20_write_scratchpad(int8_t th, int8_t tl)
write th and tl of DS18S20
Definition: ds18x20.c:97
uint8_t ds18B20_read_temp(int16_t *temperature)
Read temperature from DS18B20 (12 bit resolution)
Definition: ds18x20.c:45
uint8_t ds18x20_read_power_supply(void)
read power supply
Definition: ds18x20.c:133
void ds18x20_recall_E2(void)
copy EEPROM to scratchpad, busy waiting (1ms)
Definition: ds18x20.c:128
void ds18x20_read_scratchpad(uint8_t *buffer)
Read complete scratchpad of DS18x20 (9 bytes)
Definition: ds18x20.c:88