milesburton / Arduino-Temperature-Control-Library

Arduino Temperature Library
https://www.milesburton.com/w/index.php/Dallas_Temperature_Control_Library
975 stars 486 forks source link

Using a logic analyzer to analyze the runtime sequence routines. #253

Open mayjack0312 opened 5 months ago

mayjack0312 commented 5 months ago

I want to use a logic analyzer to measure the relevant timing parameters:

  1. The time to initialize the DS18B20 and monitor the online information
  2. The time to wait for the DS18B20 to release the bus
  3. The time to trigger the temperature conversion
  4. The time to send the read data command
  5. The time to read the register value and obtain the temperature value

According to the datasheet, their units should be in microseconds (us), but my actual measurements are in milliseconds (ms). How should I modify my measurement method? Here is my code:

#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.");
    }
  }
}
f770f4ff34d1b538ee5ee64843ae5db
mayjack0312 commented 5 months ago

Here are some segments obtained from the logic analyzer.

1535cc75ba5d763f2bb2944afa4a4fa ef055f752b068c0b55233db3a9b6040