-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDS18B20.c
140 lines (120 loc) · 3.5 KB
/
DS18B20.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* DS18B20.c
* maxim1wire
*
* Library for Maxim 1-wire based devices.
*
* Copyright (c) 2009, Jon Nall, All rights reserved.
*
* This file is part of maxim1wire.
*
* maxim1wire is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* maxim1wire 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with maxim1wire. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DS18B20.h"
#include <util/crc16.h>
uint8_t ds18b20_check_spad_crc(uint8_t* spad)
{
uint16_t crc = 0;
for(uint8_t i = 0; i < SPAD_MAX_BYTES; ++i)
{
crc = _crc_ibutton_update(crc, spad[i]);
}
return (crc == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
uint8_t ds18b20_read_spad(uint8_t* spad)
{
if(onewire_select_device_and_issue_command(CMD_18B20_READ_SPAD, DS18B20_FAMILY_CODE) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
for(uint8_t i = 0; i < SPAD_MAX_BYTES; i++)
{
spad[i] = onewire_readbyte();
}
#ifdef ONEWIRE_CHECK_CRC
return ds18b20_check_spad_crc(spad);
#else
return EXIT_SUCCESS;
#endif // ONEWIRE_CHECK_CRC
}
uint8_t ds18b20_write_spad(uint8_t* spad)
{
if(onewire_select_device_and_issue_command(CMD_18B20_WRITE_SPAD, DS18B20_FAMILY_CODE) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
onewire_writebyte(spad[SPAD_ALARM_HIGH]);
onewire_writebyte(spad[SPAD_ALARM_LOW]);
onewire_writebyte(spad[SPAD_CONFIG]);
return EXIT_SUCCESS;
}
#if ONEWIRE_USE_MACROS == 0
uint8_t ds18b20_read_temperature(int16_t* temperature)
{
*temperature = 0;
if(onewire_select_device_and_issue_command(CMD_18B20_CONV_TEMP, DS18B20_FAMILY_CODE) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
while(1)
{
const uint8_t done = onewire_readbyte();
// There might be a 1 bit anywhere in the output
// so just check != 0
if(done != 0)
{
break;
}
}
uint8_t spad[SPAD_MAX_BYTES];
if(ds18b20_read_spad(&(spad[0])) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
*temperature = spad[SPAD_TEMP_MSB];
*temperature <<= 8;
*temperature |= spad[SPAD_TEMP_LSB];
return EXIT_SUCCESS;
}
uint8_t ds18b20_set_resolution(const uint8_t resolution, const uint8_t store_to_eeprom)
{
uint8_t spad[SPAD_MAX_BYTES];
if(ds18b20_read_spad(&(spad[0])) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
uint8_t config = spad[SPAD_CONFIG];
if(((config >> 5) & 3) != resolution)
{
// Set resolution to desired value.
config &= ~(0x3 << 5);
config |= (resolution << 5);
spad[SPAD_CONFIG] = config;
if(ds18b20_write_spad(&(spad[0])) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
// Save these new values to EEPROM to save this step
// next time
if(store_to_eeprom != 0)
{
if(onewire_select_device_and_issue_command(CMD_18B20_COPY_SPAD, DS18B20_FAMILY_CODE) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
}
}
return EXIT_SUCCESS;
}
#endif //ONEWIRE_USE_MACROS