Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 10265|回復: 8
打印 上一主題 下一主題

兩個溫度感測器同時感測兩處溫度

[複製鏈接]
跳轉到指定樓層
1#
發表於 2015-12-15 13:16:21 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
想請問各位先進,有什麼方法可以讓 兩個溫度感測器同時感測兩處溫度嗎
底下為網路上找到的溫度感測器程式 感測器為 ds1821
目前推測 卡在 此處 小弟不知 val 代表什麼 在下方的主程式loop裡 不見val的蹤影
想請問大家 有什麼方法能修改以下程式 使其可以同時讀取2個感測器的溫度呢?  
{
    unsigned int val = 0;
    for (int i = 0; i < count; i++)
    {
        val |= (unsigned int)(ds.read() << i * 8);
    }
    return val;
}





#include <OneWire.h>
#include <Wire.h>

OneWire  ds(7);  // on pin 10
unsigned int readBytes(int count)
{
    unsigned int val = 0;
    for (int i = 0; i < count; i++)
    {
        val |= (unsigned int)(ds.read() << i * 8);
    }
    return val;
}





void setup(void) {
  Serial.begin(9600);
  ds.reset();

}



void loop(void) {

  byte temp_read = 0;
  unsigned int count_remain = 0;
  unsigned int count_per_c = 0;
  byte configuration_register = 0;

  ds.reset();
  ds.write(0xEE); //Start Converting the temperatures  

   do {
        delay(1);
        configuration_register = 0;
        ds.reset();
        ds.write(0xAC);

        // Read the configuration Register from the DS1821
        configuration_register = readBytes(1);
    } while ((configuration_register & (1 << 7)) == 0); // If Bit #8 is 1 then we are finished converting the temp


    // Get Temp
    ds.reset();
    ds.write(0xAA);
    temp_read = readBytes(1); ;

    // Get Count Remaining
    ds.reset();
    ds.write(0xA0);
    count_remain = readBytes(2);

    // Load The Counter to populate the slope accumulator
    ds.reset();
    ds.write(0x41);

    // Read Count Per Deg
    ds.reset();
    ds.write(0xA0);
    count_per_c = readBytes(2);

    // If we are reading above the 200 mark then we are below 0 and need to compensate the calculation
    if (temp_read >= 200) temp_read -= 256;

    float highResTemp = (float)temp_read - .5 + (((float)count_per_c - (float)count_remain) / (float)count_per_c);

    highResTemp = ( highResTemp);
    Serial.print(highResTemp);
    Serial.println(" degree ");

   delay(1000);
}
2#
發表於 2015-12-15 19:18:46 | 只看該作者
val 回傳在3個地方
count_per_c = readBytes(2);
temp_read = readBytes(1);
count_remain = readBytes(2);
如果你要用兩個溫度計
可在程式上再宣告
OneWire  ds1(8);
其中的名稱或接腳請自行修改
然後再複製一份 ds 變成 ds1 即可
3#
 樓主| 發表於 2015-12-16 17:49:54 | 只看該作者
回復 2# 超新手


大大您好,感謝您的幫忙,小弟將程式碼修改後如下
#include <OneWire.h>
#include <Wire.h>

OneWire  ds(7);  // on pin 7
OneWire  ds1(10);  // on pin 10
unsigned int readBytes(int count)
{
    unsigned int val = 0;
    for (int i = 0; i < count; i++)
    {
        val |= (unsigned int)(ds.read() << i * 8);
    }
    return val;
}





void setup(void) {
  Serial.begin(9600);
  ds.reset();
  ds1.reset();

}



void loop(void) {

  byte temp_read = 0;
  unsigned int count_remain = 0;
  unsigned int count_per_c = 0;
  byte configuration_register = 0;

  ds.reset();
  ds.write(0xEE); //Start Converting the temperatures  

   do {
        delay(1);
        configuration_register = 0;
        ds.reset();
        ds.write(0xAC);

        // Read the configuration Register from the DS1821
        configuration_register = readBytes(1);
    } while ((configuration_register & (1 << 7)) == 0); // If Bit #8 is 1 then we are finished converting the temp


    // Get Temp
    ds.reset();
    ds.write(0xAA);
    temp_read = readBytes(1); ;

    // Get Count Remaining
    ds.reset();
    ds.write(0xA0);
    count_remain = readBytes(2);

    // Load The Counter to populate the slope accumulator
    ds.reset();
    ds.write(0x41);

    // Read Count Per Deg
    ds.reset();
    ds.write(0xA0);
    count_per_c = readBytes(2);

    // If we are reading above the 200 mark then we are below 0 and need to compensate the calculation
    if (temp_read >= 200) temp_read -= 256;

    float highResTemp = (float)temp_read - .5 + (((float)count_per_c - (float)count_remain) / (float)count_per_c);

    highResTemp = ( highResTemp);
    Serial.print(highResTemp);
    Serial.println(" degreesss ");

   delay(1000);
   byte temp_read = 0;
  unsigned int count_remain = 0;
  unsigned int count_per_c = 0;
  byte configuration_register = 0;

  ds1.reset();
  ds1.write(0xEE); //Start Converting the temperatures  

   do {
        delay(1);
        configuration_register = 0;
        ds1.reset();
        ds1.write(0xAC);

        // Read the configuration Register from the DS1821
        configuration_register = readBytes(1);
    } while ((configuration_register & (1 << 7)) == 0); // If Bit #8 is 1 then we are finished converting the temp


    // Get Temp
    ds1.reset();
    ds1.write(0xAA);
    temp_read = readBytes(1); ;

    // Get Count Remaining
    ds1.reset();
    ds1.write(0xA0);
    count_remain = readBytes(2);

    // Load The Counter to populate the slope accumulator
    ds1.reset();
    ds1.write(0x41);

    // Read Count Per Deg
    ds1.reset();
    ds1.write(0xA0);
    count_per_c = readBytes(2);

    // If we are reading above the 200 mark then we are below 0 and need to compensate the calculation
    if (temp_read >= 200) temp_read -= 256;

    float highResTemp = (float)temp_read - .5 + (((float)count_per_c - (float)count_remain) / (float)count_per_c);

    highResTemp = ( highResTemp);
    Serial.print(highResTemp);
    Serial.println(" degree ");

   delay(1000);

}


錯誤訊息如下


Arduino:1.6.1 (Windows 7), 板子:"Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

ds1821-1215.ino: In function 'void loop()':

ds1821-1215.ino:79:9: error: redeclaration of 'byte temp_read'

ds1821-1215.ino:31:8: error: 'byte temp_read' previously declared here

ds1821-1215.ino:80:16: error: redeclaration of 'unsigned int count_remain'

ds1821-1215.ino:32:16: error: 'unsigned int count_remain' previously declared here

ds1821-1215.ino:81:16: error: redeclaration of 'unsigned int count_per_c'

ds1821-1215.ino:33:16: error: 'unsigned int count_per_c' previously declared here

ds1821-1215.ino:82:8: error: redeclaration of 'byte configuration_register'

ds1821-1215.ino:34:8: error: 'byte configuration_register' previously declared here

ds1821-1215.ino:120:11: error: redeclaration of 'float highResTemp'

ds1821-1215.ino:72:11: error: 'float highResTemp' previously declared here

編譯時發生錯誤

想請問在設定的部分要如何修改呢?
4#
發表於 2015-12-16 19:38:02 | 只看該作者
本帖最後由 超新手 於 2015-12-16 19:55 編輯

你重覆宣告了相同的變數兩次
下面的那組程式(DS1), 拿掉宣告即可(byte,unsigned int, float)
void loop(void) {

  byte temp_read = 0;
  unsigned int count_remain = 0;
  unsigned int count_per_c = 0;
  byte configuration_register = 0;
.......
      float highResTemp = (float)temp_read - .5 + (((float)count_per_c - (float)count_remain) / (float)count_per_c);
      highResTemp = ( highResTemp);// 這行其實不需要
.........
   delay(1000);// 這行其實不需要
temp_read = 0;[size=13.3333px]//拿掉 byte
count_remain = 0;[size=13.3333px]//拿掉 unsigned int
count_per_c = 0;[size=13.3333px]//拿掉 unsigned int
configuration_register = 0;[size=13.3333px]//拿掉 byte
...........
highResTemp = (float)temp_read - .5 + (((float)count_per_c - (float)count_remain) / (float)count_per_c);[size=13.3333px]//拿掉 float
highResTemp = ( highResTemp);// 這行其實不需要
    Serial.print(highResTemp);
    Serial.println(" degree ");

   delay(1000);

}
另外還有 readBytes
也要複製一份給 DS1

在 DS1 那邊的程式, 要使用 DS1 的 readBytes
不可以用 DS 的 readBytes
5#
 樓主| 發表於 2015-12-21 10:07:17 | 只看該作者
回復 4# 超新手


   大大您好,這幾天反覆試了許多次,還是無法編譯成功,以下是修改過後的程式碼&錯誤訊息   此外 大大也提到 >>>  readBytes  也要複製一份給 DS1  而此處的readBytes 並非ds 的readBytes
   小弟不太瞭解 此處要如何修改呢?  此處程式在開頭即存在,並非在loop中 想請問要如何讓 readBytes 有兩個呢?
{
    unsigned int val = 0;
    for (int i = 0; i < count; i++)
    {
        val |= (unsigned int)(ds.read() << i * 8);
    }
    return val;
}


主程式

#include <OneWire.h>
#include <Wire.h>

OneWire  ds(7);  // on pin 7
OneWire  ds1(10);  // on pin 10
unsigned int readBytes(int count)
{
    unsigned int val = 0;
    for (int i = 0; i < count; i++)
    {
        val |= (unsigned int)(ds.read() << i * 8);
    }
    return val;
}





void setup(void) {
  Serial.begin(9600);
  ds.reset();
  ds1.reset();

}



void loop(void) {

  temp_read = 0;
  count_remain = 0;
  count_per_c = 0;
  configuration_register = 0;

  ds.reset();
  ds.write(0xEE); //Start Converting the temperatures  

   do {
        delay(1);
        configuration_register = 0;
        ds.reset();
        ds.write(0xAC);

        // Read the configuration Register from the DS1821
        configuration_register = readBytes(1);
    } while ((configuration_register & (1 << 7)) == 0); // If Bit #8 is 1 then we are finished converting the temp


    // Get Temp
    ds.reset();
    ds.write(0xAA);
    temp_read = readBytes(1); ;

    // Get Count Remaining
    ds.reset();
    ds.write(0xA0);
    count_remain = readBytes(2);

    // Load The Counter to populate the slope accumulator
    ds.reset();
    ds.write(0x41);

    // Read Count Per Deg
    ds.reset();
    ds.write(0xA0);
    count_per_c = readBytes(2);

    // If we are reading above the 200 mark then we are below 0 and need to compensate the calculation
    if (temp_read >= 200) temp_read -= 256;

    highResTemp = temp_read - .5 + ((count_per_c - count_remain) / count_per_c);

    highResTemp = ( highResTemp);
    Serial.print(highResTemp);
    Serial.println(" degreesss ");




  ds1.reset();
  ds1.write(0xEE); //Start Converting the temperatures  

   do {
        delay(1);
        configuration_register = 0;
        ds1.reset();
        ds1.write(0xAC);

        // Read the configuration Register from the DS1821
        configuration_register = readBytes(1);
    } while ((configuration_register & (1 << 7)) == 0); // If Bit #8 is 1 then we are finished converting the temp


    // Get Temp
    ds1.reset();
    ds1.write(0xAA);
    temp_read = readBytes(1); ;

    // Get Count Remaining
    ds1.reset();
    ds1.write(0xA0);
    count_remain = readBytes(2);

    // Load The Counter to populate the slope accumulator
    ds1.reset();
    ds1.write(0x41);

    // Read Count Per Deg
    ds1.reset();
    ds1.write(0xA0);
    count_per_c = readBytes(2);

    // If we are reading above the 200 mark then we are below 0 and need to compensate the calculation
    if (temp_read >= 200) temp_read -= 256;

    highResTemp = ()temp_read - .5 + ((()count_per_c - ()count_remain) / ()count_per_c);


    Serial.print(highResTemp);
    Serial.println(" degree ");

   delay(1000);

}


Arduino:1.6.1 (Windows 7), 板子:"Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

temp-test1221.ino: In function 'void loop()':

temp-test1221.ino:31:3: error: 'temp_read' was not declared in this scope

temp-test1221.ino:32:3: error: 'count_remain' was not declared in this scope

temp-test1221.ino:33:3: error: 'count_per_c' was not declared in this scope

temp-test1221.ino:34:3: error: 'configuration_register' was not declared in this scope

temp-test1221.ino:72:5: error: 'highResTemp' was not declared in this scope

temp-test1221.ino:117:20: error: expected primary-expression before ')' token

temp-test1221.ino:117:21: error: expected ';' before 'temp_read'

編譯時發生錯誤

  麻煩大大了 謝謝
6#
發表於 2015-12-21 15:40:57 | 只看該作者
本帖最後由 超新手 於 2015-12-21 15:42 編輯

你把兩組宣告(DS 和 DS1)都拿掉了, 所以有問題
我只說, 拿掉下面那一組(DS1)宣告
你好像又改一些東西, 沒很仔細看
原來的程式沒太大問題.
如果以下程式有問題, 請參照你自己最初的程式修改回去即可
#include <OneWire.h>
#include <Wire.h>

OneWire  ds(7);  // on pin 7
OneWire  ds1(10);  // on pin 10
unsigned int readBytes(int count)
{
    unsigned int val = 0;
    for (int i = 0; i < count; i++)
    {
        val |= (unsigned int)(ds.read() << i * 8);
    }
    return val;
}
unsigned int readBytes1(int count)
{
    unsigned int val = 0;
    for (int i = 0; i < count; i++)
    {
        val |= (unsigned int)(ds1.read() << i * 8);
    }
    return val;
}




void setup(void) {
  Serial.begin(9600);
  ds.reset();
  ds1.reset();

}



void loop(void) {

  byte temp_read = 0;
  unsigned int count_remain = 0;
  unsigned int count_per_c = 0;
  byte configuration_register = 0;

  ds.reset();
  ds.write(0xEE); //Start Converting the temperatures  

   do {
        delay(1);
        configuration_register = 0;
        ds.reset();
        ds.write(0xAC);

        // Read the configuration Register from the DS1821
        configuration_register = readBytes(1);
    } while ((configuration_register & (1 << 7)) == 0); // If Bit #8 is 1 then we are finished converting the temp


    // Get Temp
    ds.reset();
    ds.write(0xAA);
    temp_read = readBytes(1); ;

    // Get Count Remaining
    ds.reset();
    ds.write(0xA0);
    count_remain = readBytes(2);

    // Load The Counter to populate the slope accumulator
    ds.reset();
    ds.write(0x41);

    // Read Count Per Deg
    ds.reset();
    ds.write(0xA0);
    count_per_c = readBytes(2);

    // If we are reading above the 200 mark then we are below 0 and need to compensate the calculation
    if (temp_read >= 200) temp_read -= 256;

    float highResTemp = temp_read - .5 + ((count_per_c - count_remain) / count_per_c);

    highResTemp = ( highResTemp);
    Serial.print(highResTemp);
    Serial.println(" degreesss ");


  temp_read = 0;
  count_remain = 0;
  count_per_c = 0;
  configuration_register = 0;

  ds1.reset();
  ds1.write(0xEE); //Start Converting the temperatures  

   do {
        delay(1);
        configuration_register = 0;
        ds1.reset();
        ds1.write(0xAC);

        // Read the configuration Register from the DS1821
        configuration_register = readBytes1(1);
    } while ((configuration_register & (1 << 7)) == 0); // If Bit #8 is 1 then we are finished converting the temp


    // Get Temp
    ds1.reset();
    ds1.write(0xAA);
    temp_read = readBytes1(1); ;

    // Get Count Remaining
    ds1.reset();
    ds1.write(0xA0);
    count_remain = readBytes1(2);

    // Load The Counter to populate the slope accumulator
    ds1.reset();
    ds1.write(0x41);

    // Read Count Per Deg
    ds1.reset();
    ds1.write(0xA0);
    count_per_c = readBytes1(2);

    // If we are reading above the 200 mark then we are below 0 and need to compensate the calculation
    if (temp_read >= 200) temp_read -= 256;

    highResTemp = temp_read - .5 + ((count_per_c - count_remain) / count_per_c);


    Serial.print(highResTemp);
    Serial.println(" degree ");

   delay(1000);

}
7#
 樓主| 發表於 2015-12-22 13:40:56 | 只看該作者
回復 6# 超新手


   感謝大大 可以拉~~~!!!  現在可同時顯示兩個感測器所量到的溫度



還有個小問題想請教一下, 要如何把溫度感測器的精度提升呢?
原本可量到小數點下兩位如25.58
現在的精度只到整數 後面小數點 .5 始終沒變化
應該是 要把變數宣告回 float 倍精度浮點數吧!?
想問的是 要改哪邊就好呢??

再次感謝大大的幫忙  真的在這卡了好久 1個多月了
真的很謝謝
8#
發表於 2015-12-22 15:25:58 | 只看該作者
DS1821 的解析度本來就只有 1 度C
9#
 樓主| 發表於 2015-12-24 16:25:05 | 只看該作者
回復 8# 超新手
   感謝 大大的幫忙 已順利完成 謝謝

   

12434472_1013284842069515_391913319_n.jpg (78.19 KB, 下載次數: 163)

12434472_1013284842069515_391913319_n.jpg
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-4-29 05:31 , Processed in 0.215581 second(s), 11 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表