Skip to content

Commit 11a7cfe

Browse files
committed
Added HardCoded ROMID's (Untested)
1 parent bbe9dd9 commit 11a7cfe

4 files changed

Lines changed: 165 additions & 10 deletions

File tree

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ build_src_filter =
134134
-<main.cpp>
135135
-<main_test_ethernet.cpp>
136136
-<main_can_test.cpp>
137-
+<TempSensDriverTest.cpp>
137+
+<TempSensHardROMTest.cpp>
138138
+<DS2480B.cpp>
139139

140140
build_flags =

src/DS2480B.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ void DS2480B::depower()
162162

163163
#if ONEWIRE_SEARCH
164164

165-
void DS2480B::reset_search()
165+
//Removing Search Algorithm
166+
167+
/* void DS2480B::reset_search()
166168
{
167169
LastDiscrepancy = 0;
168170
LastDeviceFlag = FALSE;
@@ -204,6 +206,7 @@ uint8_t DS2480B::search(uint8_t *newAddr)
204206
_port->write((uint8_t)0x00);
205207
}
206208
209+
<<<<<<< HEAD
207210
for(int i = 0; i < 16; i++){ //Second, read the returned bytes
208211
ROM[i] = _port->read();
209212
}
@@ -215,6 +218,20 @@ uint8_t DS2480B::search(uint8_t *newAddr)
215218
//return;
216219
return ROM[16];
217220
}
221+
=======
222+
if (!search_result || !ROM_NO[0])
223+
{
224+
Serial.println("No Device Found");
225+
LastDiscrepancy = 0;
226+
LastDeviceFlag = FALSE;
227+
LastFamilyDiscrepancy = 0;
228+
search_result = FALSE;
229+
}
230+
for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i];
231+
return search_result;
232+
} */
233+
234+
>>>>>>> 65b1665 (Added HardCoded ROMID's (Untested))
218235
219236
#endif
220237

src/TempSensDriverTest.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,31 @@ void loop(void) {
2929
byte i;
3030
byte present = 0;
3131
byte data[12];
32-
byte addr[8] = {};
32+
byte addr[8]{};
33+
3334
float celsius, fahrenheit;
3435
35-
// Checks if devices (temp sensors) exist on one wire bus
36-
if (!ds.search(addr)) {
37-
Serial.println("No more addresses.");
38-
Serial.println();
39-
ds.reset_search();
40-
delay(1000);
41-
return;
36+
//not really syre what this for loop is for, looks like its printing the ID's but addr is empty?
37+
for(i = 0; i < 8; i++) {
38+
Serial.write(' ');
39+
Serial.print(addr[i], HEX);
4240
}
41+
4342
43+
// Checks if devices (temp sensors) exist on one wire bus
44+
//Removed because no search algorithm
45+
/* if (!ds.search(addr)) {
46+
Serial.println("No more addresses.");
47+
Serial.println();
48+
ds.reset_search();
49+
delay(1000);
50+
return;
51+
}
52+
*/
53+
4454
// Prints out ROM ID of each temp sensor
4555
// For loop iterates 8 times for each byte of ROM ID and prints each byte
56+
4657
Serial.print("ROM =");
4758
for(i = 0; i < 8; i++) {
4859
Serial.write(' ');
@@ -55,7 +66,11 @@ void loop(void) {
5566
Serial.println("CRC is not valid!");
5667
return;
5768
}
69+
5870
Serial.println();
71+
72+
// We know the chip is DS18B20, just clarifying through Serial Monitor
73+
Serial.println("Chip = DS18B20");
5974

6075
// Reset the one-wire bus
6176
ds.reset();

src/TempSensHardROMTest.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//This file is a rewrite of "TempSensDriverTest.cpp" that does not rely on the search algorithm.
2+
3+
#include <Arduino.h>
4+
#include "DS2480B.h"
5+
6+
// Create DS2480B object, passing Serial2 by reference
7+
DS2480B ds(Serial2);
8+
9+
void setup(void) {
10+
// Begin serial communication
11+
Serial.begin(115200);
12+
Serial2.begin(9600); // DS2480B communicates at 19200 baud
13+
14+
delay(1000); // Wait for serial to initialize
15+
Serial.println("Serial Successfully Initialized");
16+
// ds.begin function resets transceiver and puts it in command mode
17+
ds.begin();
18+
ds.reset();
19+
Serial.println("DS2480B initialized!");
20+
}
21+
22+
void loop(void) {
23+
byte i;
24+
byte present = 0;
25+
byte data[12];
26+
byte addr[8]{};
27+
28+
int RomIDArr[48] = {
29+
40,215,97,17,17,0,0,213, //RomID1 [0-7]
30+
40,198,156,16,17,0,0,124, //RomID2 [8-15]
31+
40,148,57,17,17,0,0,82, //RomID3 [16-23]
32+
40,22,55,17,17,0,0,116, //RomID4 [24-31]
33+
40,34,54,17,17,0,0,136, //RomID5 [32-39]
34+
40,246,127,16,17,0,0,5 //RomID6 [40-47]
35+
};
36+
37+
float celsius, fahrenheit;
38+
39+
for (int i = 0; i < 6; i++){
40+
//First lets load the rom ID into addr[]
41+
42+
for (int j = 0; j < 8; j++){
43+
addr[j] = (byte)(RomIDArr[j + (8 * i)]); //casts into a byte and writes into corresponding addr.
44+
}
45+
46+
Serial.print("ROM =");
47+
for(i = 0; i < 8; i++) {
48+
Serial.write(' ');
49+
Serial.print(addr[i], HEX);
50+
}
51+
Serial.println("We are looking at ROMID:" + ' ' +(i + 1));
52+
53+
//Rest of code should be identical from here on
54+
55+
// The last byte of ROM ID should equal the CRC (derived from first 7 bytes)
56+
// This checks if the CRCs align
57+
if (DS2480B::crc8(addr, 7) != addr[7]) {
58+
Serial.println("CRC is not valid!");
59+
return;
60+
}
61+
62+
Serial.println();
63+
64+
// We know the chip is DS18B20, just clarifying through Serial Monitor
65+
Serial.println("Chip = DS18B20");
66+
67+
// Reset the one-wire bus
68+
ds.reset();
69+
70+
// Brief delay to allow bus to stabilize
71+
delay(100);
72+
73+
// Reset again and select the device
74+
ds.reset();
75+
ds.select(addr);
76+
ds.write(0x44); // Start temperature conversion command
77+
78+
// Wait for conversion to complete
79+
// DS18B20 takes ~750ms for 12-bit conversion (default resolution)
80+
// Adjustable based on your resolution needs:
81+
// - 93.75ms for 9-bit
82+
// - 187.5ms for 10-bit
83+
// - 375ms for 11-bit
84+
// - 750ms for 12-bit (default)
85+
delay(1000);
86+
87+
// Read the temperature data
88+
present = ds.reset();
89+
ds.select(addr);
90+
ds.write(0xBE); // Read Scratchpad command
91+
92+
Serial.print(" Data = ");
93+
Serial.print(present, HEX);
94+
Serial.print(" ");
95+
96+
// Read 9 bytes of data from the scratchpad
97+
for(i = 0; i < 9; i++) {
98+
data[i] = ds.read();
99+
Serial.print(data[i], HEX);
100+
Serial.print(" ");
101+
}
102+
103+
Serial.print(" CRC=");
104+
Serial.print(DS2480B::crc8(data, 8), HEX);
105+
Serial.println();
106+
107+
// Convert the data to actual temperature
108+
// The result is a 16-bit signed integer stored in bytes 0 and 1
109+
// Byte 0 = LSB (lower 8 bits), Byte 1 = MSB (upper 8 bits)
110+
int16_t raw = (data[1] << 8) | data[0];
111+
112+
// DS18B20 always returns 12-bit resolution by default
113+
// Each LSB = 0.0625°C, so divide by 16
114+
celsius = (float)raw / 16.0;
115+
fahrenheit = celsius * 1.8 + 32.0;
116+
117+
Serial.print(" Temperature = ");
118+
Serial.print(celsius);
119+
Serial.print(" Celsius, ");
120+
Serial.print(fahrenheit);
121+
Serial.println(" Fahrenheit");
122+
}
123+
}

0 commit comments

Comments
 (0)