我遇到了一些试图获得328p arduino代码编译的问题。这里的想法是使用请求处理程序从主设备接收I2C总线上的命令,并从DS1820温度探头或霍尔效应传感器流量计发送主临时设备。首先,我试图使用指针,我不确定我是否在其实现标记附近,或者这里有没有优势。其次,DeviceAddress格式给我带来了一些麻烦,我不确定我在那里可能会做错什么。代码有点乱,但我做了很多评论,包括编译器提出的问题/问题。我意识到函数printSensorAtemp和printSensorBtemp有很大不同,包含这两个函数的意图是了解我应该采取哪个方向。ds1820地址,指针和I2C
#include <OneWire.h> //for temperature probes
#include <Wire.h> //for I2C
#include <DallasTemperature.h> //also needed for temperature probes
//#include <I2C_Anything.h> //aids in sending floats over I2C
// Data wire of temperature probes is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// data wire from flowmeter is plugged into pin 4 on the Arduino
#define hallSensor 4
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
DeviceAddress SensorA = { 0x28, 0x94, 0xE2, 0xDF, 0x02, 0x00, 0x00, 0xFE };
DeviceAddress SensorB = { 0x28, 0x6B, 0xDF, 0xDF, 0x02, 0x00, 0x00, 0xC0 };
char val = ' ';
int Calc; //this is the placeholder for the calculated flow to be sent via I2C, is it an int? seedstudio has it as being DEC...
volatile float tempToPrint;
int NbTopsFan;
float Calc;
int CMD;
void setup()
{
Wire.begin(2); // join i2c bus with address #2 (slave)
pinMode(hallSensor, INPUT); // sets the pinMode for the variable hallSensor, set above, to pin 4
pinMode(ONE_WIRE_BUS, INPUT);
Serial.begin(9600);
//attachInterrupt(0, rpm, RISING); // double check this -- where is the interrupt coming from? what format is RPM?
Wire.onRequest(requestEvent); //declares that when I2C sends an onRequest, the function
//requestEvent() gets called
Wire.read(receiveEvent); // declares that when I2C sends a command, the variable
//CMD gets updated by this function
// Start up the one Wire sensorlibrary
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(SensorA, 10);
sensors.setResolution(SensorB, 10);
}
void receiveEvent(int HowMany)
{
if(Wire.available() > 0)
{
CMD = Wire.read();
}
}
void requestEvent()
{
switch(CMD)
{
case 0x01: printSensorATemp(); break; // need to set bytes and types within(),
// otherwise compiler indicates too few arguments.
// At 4 bytes for a float, would that cover a
// string too? Not sure how to make this agree with
// the different data types in this function.
case 0x02: printHSensorBTemp(); break;
case 0x03: printFlow(); break;
default: break; // do nothing
}
}
void loop()
{}
void printFlow()
{
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
Calc = (NbTopsFan * 60/7.5); //(Pulse frequency x 60)/7.5Q, = flow rate
//in L/hour
Wire.write(Calc)
}
void printSensorATemp(DeviceAddress SensorA)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00)
{
send("Error getting SensorA temp");
}
else
{
send("SensorA Temp");
*tempToPrint = DallasTemperature::toFahrenheit(tempC);
if (*howMany >= (sizeof *tempToPrint)
{
I2C_writeAnything([glow]*[/glow]tempToPrint);
}
}
void printSensorBTemp(DeviceAddress hotLiquorTank)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00)
{
send("Error getting SensorB temperature");
}
else {
send("SensorB temp");
send(DallasTemperature::toFahrenheit(tempC));
}
}
}
}
这里是I2C_anything,在IDE的另一个选项卡中设置。
//#include <Arduino.h>
#include <Wire.h>
template <typename T> int I2C_writeAnything (const T& value)
{
const byte * p = (const byte*) &value;
unsigned int i;
for (i = 0; i < sizeof value; i++)
Wire.write(*p++);
return i;
} // end of I2C_writeAnything
template <typename T> int I2C_readAnything(T& value)
{
byte * p = (byte*) &value;
unsigned int i;
for (i = 0; i < sizeof value; i++)
*p++ = Wire.read();
return i;
} // end of I2C_readAnything
template <typename T> int I2C_singleWriteAnything (const T& value)
{
int size = sizeof value;
byte vals[size];
const byte* p = (const byte*) &value;
unsigned int i;
for (i = 0; i < sizeof value; i++) {
vals[i] = *p++;
}
// {
//Wire.write(vals, size);
//return i;
//}
克里斯,你真棒。这里有一些非常好的答案,这些答案告诉我很多关于我在这里做错了什么,并给我一个关于如何进行的更好的想法。我所做的大部分工作都来自示例代码,虽然它对该代码中缺少irt变量的文档/注释感到不满,但它非常酷,我可以将一些编写得不好的代码放在那里,学习一下,希望其他人也会。我将执行这些更改并回报。 –