找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 6149|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

Arduino單總線多路18b20測溫源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:398397 發(fā)表于 2018-9-28 09:24 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
Arduino多路18b20測溫,使用單總線協(xié)議,優(yōu)點是只占一個io口,且測溫路數(shù)可以任意設(shè)定。

源程序如下:
  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. }
復(fù)制代碼

所有資料51hei提供下載:
18b20.zip (1.41 KB, 下載次數(shù): 22)



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:243394 發(fā)表于 2018-9-29 14:39 | 只看該作者
謝謝分享
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表