-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathds18b20.ino
More file actions
executable file
·99 lines (88 loc) · 2.9 KB
/
ds18b20.ino
File metadata and controls
executable file
·99 lines (88 loc) · 2.9 KB
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
/* ========================================================================== */
/* ds18b20.ino */
/* */
/* Code for reading OneWire Temperature devices/averaging ADC channels */
/* */
/* */
/* */
/* ========================================================================== */
// Variables
#include <OneWire.h>
#include <DallasTemperature.h>
#define MAX_SENSORS 2
// Variables
byte SensorCount=0; // Number of temperature devices found
unsigned long CheckDS18B20s=0;
byte GettingTemperature=0;
OneWire oneWire(WIREBUS); // OneWire port
DallasTemperature sensors(&oneWire); // Pass oneWire reference to Dallas Temperature object
void Setupds18b20(void)
{
DeviceAddress Thermometer;
uint8_t i, j;
sensors.begin();
// Grab a count of devices on the wire
SensorCount = sensors.getDeviceCount();
Serial.print(SensorCount);
Serial.println(F(" DS18B20's on bus"));
SensorCount = min(SensorCount, MAX_SENSORS);
if (SensorCount > 0)
{
sensors.setResolution(9);
}
if (SensorCount == 1)
{
// Only one device, so get its address and update if necessary
sensors.getAddress(Thermometer, 0);
if (memcmp(Thermometer, Settings.DS18B20_Address, 8) != 0)
{
memcpy(Settings.DS18B20_Address, Thermometer, 8);
SaveSettings();
Serial.println(F("Stored new DS18B20 address"));
}
}
for (j=0; j<SensorCount; j++)
{
sensors.getAddress(Thermometer, j);
for (i=0; i<8; i++)
{
if (Thermometer[i] < 0x10)
{
Serial.print('0');
}
Serial.print((unsigned int)(Thermometer[i]), HEX);
Serial.print(' ');
}
if (memcmp(Thermometer, Settings.DS18B20_Address, 8) == 0)
{
GPS.InternalTemperature = j;
Serial.println(F("<-- INTERNAL"));
}
else
{
Serial.println(F(" External"));
}
}
}
void Checkds18b20(void)
{
if (millis() >= CheckDS18B20s)
{
if (GettingTemperature)
{
int i;
for (i=0; i<SensorCount; i++)
{
GPS.Temperatures[i] = sensors.getTempCByIndex(i);
Serial.print(F("Temp")); Serial.print(i); Serial.print("="); Serial.println(GPS.Temperatures[i]);
}
CheckDS18B20s = millis() + 10000L;
}
else
{
sensors.requestTemperatures(); // Send the command to get temperature
CheckDS18B20s = millis() + 1000L; // Leave 1 second (takes 782ms) for readings to happen
}
GettingTemperature = !GettingTemperature;
}
}