I want to use the Uno board and a logic analyzer to analyze several timings of the DS18B20:
- The time to initialize the DS18B20 and monitor the online information
- The time to wait for the DS18B20 to release the bus
- The time to trigger the temperature conversion
- The time to send the read data command
- The time to read the register value and obtain the temperature value
My wiring method is using D2 for transmission, and a 10K resistor is connected between VCC and DQ.
Below is my code, but the output results are always incorrect.
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress DS18B20_ID;
void setup() {
Serial.begin(9600);
unsigned long initStartTime = micros();
sensors.begin();
unsigned long initEndTime = micros();
Serial.print("Initialization Time: ");
Serial.print(initEndTime - initStartTime);
Serial.println(" us");
}
void loop() {
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
command.trim();
if (command == "GET") {
unsigned long waitStartTime = micros();
delayMicroseconds(550);
unsigned long waitEndTime = micros();
unsigned long convStartTime = micros();
sensors.requestTemperatures();
unsigned long convEndTime = micros();
unsigned long readCmdStartTime = micros();
float temperature = sensors.getTempCByIndex(0);
unsigned long readCmdEndTime = micros();
unsigned long readRegStartTime = micros();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
unsigned long readRegEndTime = micros();
Serial.print("Wait for Bus Release Time: ");
Serial.print(waitEndTime - waitStartTime);
Serial.println(" us");
Serial.print("Trigger Conversion Time: ");
Serial.print(convEndTime - convStartTime);
Serial.println(" us");
Serial.print("Send Read Command Time: ");
Serial.print(readCmdEndTime - readCmdStartTime);
Serial.println(" us");
Serial.print("Read Register Value Time: ");
Serial.print(readRegEndTime - readRegStartTime);
Serial.println(" us");
} else {
Serial.println("Unknown command.");
}
}
}
I looked up the manuals and related articles online, and the data below are the correct ones.
data | time (us) |
---|---|
The time to initialize the DS18B20 and monitor the online information | 610 |
The time to wait for the DS18B20 to release the bus | 550 |
The time to trigger the temperature conversion | 887 |
The time to send the read data command | 886 |
The time to read the register value and obtain the temperature value | 560 |
Please help me.