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