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 void ds18x20_convert_t(uint8_t parasitic_power) {
26 
27  if (parasitic_power) {
28  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
29  onewire_write_byte(DS18x20_CMD_CONVERT_T);
30  ONEWIRE_STRONG_PU_ON
31  }
32  } else {
33  onewire_write_byte(DS18x20_CMD_CONVERT_T);
34  }
35 }
36 
37 uint8_t ds18B20_read_temp(int16_t *temperature) {
38  int16_t temp;
39  uint8_t scratchpad[9];
40 
41  ds18x20_read_scratchpad(scratchpad);
42  if (onewire_crc(scratchpad, 9)) {
43  return ONEWIRE_CRC_ERROR;
44  }
45 
46  temp = ((int16_t)scratchpad[1] << 8) | scratchpad[0];
47  // zero undefined LSBs, depending on current resolution
48  switch((scratchpad[4] >> 5) & 3) {
49  case 0: temp &= ~7; break; // 9 Bit
50  case 1: temp &= ~3; break; // 10 Bit
51  case 2: temp &= ~1; break; // 11 Bit
52  }
53  // calculate temperature with 0.1 C resolution using fixed point arithmetic
54  // t(0.1C) = t(1/16C) * 10/16
55  *temperature = (temp * 10) >> 4;
56  return ONEWIRE_OK;
57 }
58 
59 uint8_t ds18S20_read_temp(int16_t *temperature) {
60  int16_t temp;
61  uint8_t scratchpad[9];
62 
63  ds18x20_read_scratchpad(scratchpad);
64  if (onewire_crc(scratchpad, 9)) {
65  return ONEWIRE_CRC_ERROR;
66  }
67 
68  temp = ((int16_t)scratchpad[1] << 8) | scratchpad[0];
69  temp &= ~1; // clear bit#0
70  temp <<= 3; // x8 -> resolution 1/16 C
71  // calculate extended resolution according to data sheet
72  // /16 must be omitted, since we are already using a resolution of 1/16 degree C
73  temp = temp - 4 + (16-scratchpad[6]);
74  // calculate temperature with 0.1 C resolution using fixed point arithmetic
75  // t(0.1C) = t(1/16C) * 10/16
76  *temperature = (temp * 10) >> 4;
77  return ONEWIRE_OK;
78 }
79 
80 void ds18x20_read_scratchpad(uint8_t *buffer) {
81  uint8_t i;
82 
83  onewire_write_byte(DS18x20_CMD_READ_SCRATCHPAD);
84  for (i=0; i<9; i++) {
85  buffer[i]=onewire_read_byte();
86  }
87 }
88 
89 void ds18S20_write_scratchpad(int8_t tl, int8_t th) {
90 
91  onewire_write_byte(DS18x20_CMD_WRITE_SCRATCHPAD);
94 }
95 
96 void ds18B20_write_scratchpad(int8_t tl, int8_t th, uint8_t adc_resolution) {
97  uint8_t cfg;
98 
99  onewire_write_byte(DS18x20_CMD_WRITE_SCRATCHPAD);
100  onewire_write_byte(th);
101  onewire_write_byte(tl);
102  switch(adc_resolution) {
103  case 9: cfg = 0x00; break;
104  case 10: cfg = 0x20; break;
105  case 11: cfg = 0x40; break;
106  default: cfg = 0x60; break; // 12 bit
107  }
108  onewire_write_byte(cfg);
109 }
110 
111 void ds18x20_copy_scratchpad(uint8_t parasitic_power) {
112 
113  if (parasitic_power) {
114  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
115  onewire_write_byte(DS18x20_CMD_COPY_SCRATCHPAD);
116  ONEWIRE_STRONG_PU_ON
117  }
118  } else {
119  onewire_write_byte(DS18x20_CMD_COPY_SCRATCHPAD);
120  }
121  _delay_ms(10);
122  ONEWIRE_STRONG_PU_OFF
123 }
124 
125 void ds18x20_recall_E2(void) {
126  onewire_write_byte(DS18x20_CMD_RECALL_E2);
127  _delay_ms(1);
128 }
129 
131  onewire_write_byte(DS18x20_CMD_READ_POWER_SUPPLY);
132  return !onewire_read_bit();
133 }
void ds18x20_convert_t(uint8_t parasitic_power)
start temperature conversion
Definition: ds18x20.cpp:25
void ds18B20_write_scratchpad(int8_t tl, int8_t th, uint8_t adc_resolution)
write tl, th and configuration of DS18B20
Definition: ds18x20.cpp:96
uint8_t ds18S20_read_temp(int16_t *temperature)
Read temperature from DS18S20 (9 bit + enhanced resolution, effective 12 bits)
Definition: ds18x20.cpp:59
void onewire_write_byte(uint8_t data)
write one byte
Definition: onewire.cpp:96
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:286
void ds18S20_write_scratchpad(int8_t tl, int8_t th)
write tl and th of DS18S20
Definition: ds18x20.cpp:89
uint8_t onewire_read_bit(void)
read one bit from bus
Definition: onewire.cpp:64
uint8_t onewire_read_byte(void)
read one byte
Definition: onewire.cpp:83
void ds18x20_copy_scratchpad(uint8_t parasitic_power)
copy scratchpad to EEPROM, busy waiting (10ms),
Definition: ds18x20.cpp:111
uint8_t ds18B20_read_temp(int16_t *temperature)
Read temperature from DS18B20 (9-12 bit resolution)
Definition: ds18x20.cpp:37
uint8_t ds18x20_read_power_supply(void)
read power supply
Definition: ds18x20.cpp:130
void ds18x20_recall_E2(void)
copy EEPROM to scratchpad, busy waiting (1ms)
Definition: ds18x20.cpp:125
void ds18x20_read_scratchpad(uint8_t *buffer)
Read complete scratchpad of DS18x20 (9 bytes)
Definition: ds18x20.cpp:80