久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 6183|回復: 1
收起左側

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

[復制鏈接]
ID:398397 發表于 2018-9-28 09:24 | 顯示全部樓層 |閱讀模式
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)



回復

使用道具 舉報

ID:243394 發表于 2018-9-29 14:39 | 顯示全部樓層
謝謝分享
回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产精品久久久久久亚洲调教 | 亚洲视频免费在线观看 | 91在线视频观看 | 成人国产一区二区三区精品麻豆 | 亚洲免费网址 | 亚洲精品一区中文字幕乱码 | 欧美国产精品一区二区三区 | 亚洲国产区 | 欧美视频在线一区 | 在线免费视频一区 | 成年人在线视频 | 一级毛片成人免费看a | 国产一级淫片免费视频 | 国产在线观看不卡一区二区三区 | 欧美精品在线一区二区三区 | 久久久久久久国产 | 欧美日韩视频在线 | 日本一区二区三区在线观看 | 99爱视频 | 在线精品一区二区三区 | 99热这里都是精品 | 91在线网| 一区二区在线免费观看 | 精品久久香蕉国产线看观看亚洲 | 日韩欧美中文字幕在线观看 | 国产日韩精品视频 | 中文字幕一区二区三区日韩精品 | 国产在线97 | 成人妇女免费播放久久久 | 碰碰视频| 日中文字幕在线 | 日韩色图在线观看 | 黄色一级免费 | 一区视频 | 精品久久久av | 一区二区三区在线观看免费视频 | 国产日韩免费视频 | 欧美日韩一二三区 | 一区二区三区免费 | 久久久久久久久国产成人免费 | 亚洲精品综合 |