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;
}
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;
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;
}
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;
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;
大大您好,這幾天反覆試了許多次,還是無法編譯成功,以下是修改過後的程式碼&錯誤訊息 此外 大大也提到 >>> 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;
}
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;
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;
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;
}
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;
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;