我遇到了许多试图获取328p arduino编译代码的问题。这里的想法是使用请求处理程序从主设备接收I2C总线上的命令,并从DS1820温度探头或霍尔效应传感器流量计发送主温度。首先,我试图使用指针,我不确定我的实现是否接近标记,或者这里是否有任何优势。第二,DeviceAddress格式给我带来了一些麻烦,我不确定那里我可能做错了什么。代码有点乱,但我做了很多评论,包括编译器带来的问题/问题。我意识到printSensorAtemp和printSensorBtemp函数差别很大,并且包含两者的意图是了解我应该采取的方向。
#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;
//}
答案 0 :(得分:0)
首先是一些简单的语法内容:
}
太多。Wire.write(Calc)
之后的printFlow
。I2C_anything.h
中I2C_singleWriteAnything
最后遗失了}
。现在有些错误:
NbTopsFan
是一个全局变量,但只在printFlow
中使用过。此外,它始终设置为0,这意味着此计算结果(NbTopsFan * 60 / 7.5)
也将始终为0.您必须问自己:NbTopsFan
应该来自哪里以及如何设置?< / LI>
Calc
也是一个全局变量,没有明显的原因 - 它只在printFlow
中使用过,因此它应该是该函数中的局部变量。 您的评论询问Calc
的类型是什么 - 嗯,它由(NbTopsFan * 60 / 7.5)
计算,7.5
是double
,因此结果应为{{} 1}}。可以说arduino平台的上下文中可能不存在double
,编译器可能会把它变成浮点数(意味着编译器不符合标准,但很多嵌入式平台编译器都没有)。我不知道编译器或平台所以这有点推测。你应该能够写double
,在这种情况下结果肯定是(NbTopsFan * 60 / 7.5f)
。总之,您可以删除全局变量float
并将Calc
写为:
printFlow()
你仍然需要弄清楚void printFlow()
{
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
int flowRate = (NbTopsFan * 60 / 7.5); // (Pulse frequency x 60) / 7.5Q, = flow rate in L/hour
Wire.write(flowRate);
}
在NbTopsFan
中,您编写printSensorATemp
,但sensors.getTempC(deviceAddress)
不是任何地方的现有变量。你的意思是写deviceAddress
吗?
sensors.getTempC(SensorA)
你可能想写printSensorBTemp
。在sensors.getTempC(hotLiquorTank)
中,您编写printSensorATemp
,表示“取消引用*tempToPrint = DallasTemperature::toFahrenheit(tempC);
,并将返回的值存储在其指向的位置。” Howver tempToPrint
不是指针 - 它是一个你不能解除引用的浮点数。你可以把它写成:
tempToPrint
此外,如果您将其转换为华氏度,那么为什么不首先致电var tempF = DallasTemperature::toFahrenheit(tempC);
I2C_writeAnything(&tempF);
?
这至少可以帮助你编译。