C++ 软I²;C平函数
我使用软实现读取一组Sensirion SHT21传感器。我正试图找出一种让传感器响应的方法,看看它们是否真的连接到了设备上。我使用的是Arduino,这意味着我所有的代码都是C/C++ 我正在使用的库是 用于读取传感器的代码如下所示:C++ 软I²;C平函数,c++,arduino,i2c,C++,Arduino,I2c,我使用软实现读取一组Sensirion SHT21传感器。我正试图找出一种让传感器响应的方法,看看它们是否真的连接到了设备上。我使用的是Arduino,这意味着我所有的代码都是C/C++ 我正在使用的库是 用于读取传感器的代码如下所示: #include <Ports.h> #include <PortsSHT21.h> //Define soft I²C channels for three sensors SHT21 hsensor2 (2); // pins A1
#include <Ports.h>
#include <PortsSHT21.h>
//Define soft I²C channels for three sensors
SHT21 hsensor2 (2); // pins A1 and D5 - Sensor 2
//define variables for temp data
float h, t;
void setup() {}
void loop()
{
// Get data from sensor soft I²C
hsensor2.measure(SHT21::HUMI);
hsensor2.measure(SHT21::TEMP);
hsensor2.calculate(h, t);
float hum2 = (h);
float temp2 = (t);
}
#包括
#包括
//为三个传感器定义软I²C通道
SHT21 hsensor2(2);//针脚A1和D5-传感器2
//为临时数据定义变量
浮动h,t;
void setup(){}
void循环()
{
//从传感器软I²C获取数据
hsensor2.测量(SHT21::HUMI);
hs传感器2.测量(SHT21::温度);
hsensor2.计算(h,t);
浮态腐殖质2=(h);
浮点数2=(t);
}
大代码块是measure()函数的代码。请注意,它在某一点返回0,而不执行connReset()。这应该是一种检测有效设备的方法,例如
bool hasHUMI;
if (hsensor2.measure(SHT21::HUMI))
{
hasHUMI=true;
}
或
或
在进行读取之前,代码应该将h和t清除为0,以便测试有效值。像这样
void loop()
{
h=0.00f;
t=0.00f;
// Get data from sensor soft I²C
hsensor2.measure(SHT21::HUMI);
hsensor2.measure(SHT21::TEMP);
hsensor2.calculate(h, t);
float hum2 = (h);
float temp2 = (t);
if (h>0) {
}
if (t>0) {
}
}
如果没有,则可以制作(复制)自己版本的measure()
函数,该函数在meas[type]
中测试有效的返回值。在读取之前,您需要将meas[type]
设置为已知的无效值(例如0
)
h
您链接到的库有一个成员bool device2c::isPresent(),isPresent()不起作用。我已经尝试过,它只会拾取硬连线的i2c设备
void loop()
{
h=0.00f;
t=0.00f;
// Get data from sensor soft I²C
hsensor2.measure(SHT21::HUMI);
hsensor2.measure(SHT21::TEMP);
hsensor2.calculate(h, t);
float hum2 = (h);
float temp2 = (t);
if (h>0) {
}
if (t>0) {
}
}
uint8_t SHT21::measure(uint8_t type, void (*delayFun)()) {
start();
writeByte(type == TEMP? MEASURE_TEMP : MEASURE_HUMI)
for (uint8_t i = 0; i < 250; ++i) {
if (!digiRead()) {
meas[type] = readByte(1) << 8;
meas[type] |= readByte(1);
uint8_t flipped = 0;
for (uint8_t j = 0x80; j != 0; j >>= 1) {
flipped >>= 1;
}
if (readByte(0) != flipped)
break;
return 0;
}
if (delayFun)
delayFun();
else
delay(1);
}
connReset();
return 1;
}
uint8_t SHT21::measureTest(uint8_t type, void (*delayFun)()) {
}
uint8_t measureTest(uint8_t type, void (*delayFun)() =0);