标题: Arduino单总线多路18b20测温源码 [打印本页]

作者: genhao2    时间: 2018-9-28 09:24
标题: Arduino单总线多路18b20测温源码
Arduino多路18b20测温,使用单总线协议,优点是只占一个io口,且测温路数可以任意设定。

源程序如下:
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>

  3. // Data wire is plugged into port 2 on the Arduino
  4. #define ONE_WIRE_BUS 2
  5. #define TEMPERATURE_PRECISION 12

  6. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  7. OneWire oneWire(ONE_WIRE_BUS);

  8. // Pass our oneWire reference to Dallas Temperature.
  9. DallasTemperature sensors(&oneWire);

  10. int numberOfDevices; // Number of temperature devices found

  11. DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

  12. void setup(void)
  13. {
  14.   // start serial port
  15.   Serial.begin(9600);
  16.   Serial.println("Dallas Temperature IC Control Library Demo");

  17.   // Start up the library
  18.   sensors.begin();
  19.   
  20.   // Grab a count of devices on the wire
  21.   numberOfDevices = sensors.getDeviceCount();
  22.   
  23.   // locate devices on the bus
  24.   Serial.print("Locating devices...");
  25.   
  26.   Serial.print("Found ");
  27.   Serial.print(numberOfDevices, DEC);
  28.   Serial.println(" devices.");

  29.   // report parasite power requirements
  30.   Serial.print("Parasite power is: ");
  31.   if (sensors.isParasitePowerMode()) Serial.println("ON");
  32.   else Serial.println("OFF");
  33.   
  34.   // Loop through each device, print out address
  35.   for(int i=0;i<numberOfDevices; i++)
  36.   {
  37.     // Search the wire for address
  38.     if(sensors.getAddress(tempDeviceAddress, i))
  39.         {
  40.                 Serial.print("Found device ");
  41.                 Serial.print(i, DEC);
  42.                 Serial.print(" with address: ");
  43.                 printAddress(tempDeviceAddress);
  44.                 Serial.println();
  45.                
  46.                 Serial.print("Setting resolution to ");
  47.                 Serial.println(TEMPERATURE_PRECISION,DEC);
  48.                
  49.                 // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  50.                 sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
  51.                
  52.                  Serial.print("Resolution actually set to: ");
  53.                 Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
  54.                 Serial.println();
  55.         }else{
  56.                 Serial.print("Found ghost device at ");
  57.                 Serial.print(i, DEC);
  58.                 Serial.print(" but could not detect address. Check power and cabling");
  59.         }
  60.   }

  61. }

  62. // function to print the temperature for a device
  63. void printTemperature(DeviceAddress deviceAddress)
  64. {
  65.   // method 1 - slower
  66.   //Serial.print("Temp C: ");
  67.   //Serial.print(sensors.getTempC(deviceAddress));
  68.   //Serial.print(" Temp F: ");
  69.   //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit

  70.   // method 2 - faster
  71.   float tempC = sensors.getTempC(deviceAddress);
  72.   Serial.print("Temp C: ");
  73.   Serial.print(tempC);
  74.   Serial.print(" Temp F: ");
  75.   Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
  76. }

  77. void loop(void)
  78. {
  79.   // call sensors.requestTemperatures() to issue a global temperature
  80.   // request to all devices on the bus
  81.   Serial.print("Requesting temperatures...");
  82.   sensors.requestTemperatures(); // Send the command to get temperatures
  83.   Serial.println("DONE");
  84.   
  85.   
  86.   // Loop through each device, print out temperature data
  87.   for(int i=0;i<numberOfDevices; i++)
  88.   {
  89.     // Search the wire for address
  90.     if(sensors.getAddress(tempDeviceAddress, i))
  91.         {
  92.                 // Output the device ID
  93.                 Serial.print("Temperature for device: ");
  94.                 Serial.println(i,DEC);
  95.                
  96.                 // It responds almost immediately. Let's print out the data
  97.                 printTemperature(tempDeviceAddress); // Use a simple function to print out the data
  98.         }
  99.         //else ghost device! Check your power requirements and cabling
  100.         
  101.   }
  102.   delay(10000);
  103.   Serial.println(":");
  104.   Serial.println(":");
  105.   Serial.println(":");
  106. }

  107. // function to print a device address
  108. void printAddress(DeviceAddress deviceAddress)
  109. {
  110.   for (uint8_t i = 0; i < 8; i++)
  111.   {
  112.     if (deviceAddress[i] < 16) Serial.print("0");
  113.     Serial.print(deviceAddress[i], HEX);
  114.   }
  115. }
复制代码

所有资料51hei提供下载:
18b20.zip (1.41 KB, 下载次数: 22)




作者: szzxl10    时间: 2018-9-29 14:39
谢谢分享




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1