2013-11-25 77 views
0

我遇到了一些试图获得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; 
//} 

回答

0

首先有的干脆语法的东西:

  1. 如果我没有那么算错,你有一对夫妇的}在你的主文件的末尾太多。
  2. 您错过了';'在Wire.write(Calc)之后printFlow
  3. 在您的I2C_anything.hI2C_singleWriteAnything最后缺少}

现在一些错误:

  1. NbTopsFan是一个全局变量,但在printFlow只使用过。此外,它始终设置为0,这意味着此计算结果(NbTopsFan * 60/7.5)也将始终为0.您必须问自己:NbTopsFan应该来自哪里?应该如何设置?
  2. Calc也是一个没有明显原因的全局变量 - 它只在printFlow中使用过,所以它应该是该函数中的局部变量。
  3. 您的留言询问什么类型的Calc是 - 嗯,这是由(NbTopsFan * 60/7.5)计算和7.5double所以结果应该是一个double。也就是说,在arduino平台的上下文中可能不存在double,编译器可能会将其变成浮点(意思是编译器不符合标准,但是很多嵌入式平台编译器不符合)。我不知道编译器或平台,所以这是有点推测。你应该能写(NbTopsFan * 60/7.5f)在这种情况下,结果肯定应该是float。总之,你可以删除全局变量CalcprintFlow()为:

    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

  4. 奥秘在printSensorATemp你写sensors.getTempC(deviceAddress)deviceAddress不是现有变量的任何地方。你的意思是写sensors.getTempC(SensorA)

  5. printSensorBTemp相同您可能要写sensors.getTempC(hotLiquorTank)
  6. printSensorATemp你写*tempToPrint = DallasTemperature::toFahrenheit(tempC);这意味着“解除参考tempToPrint并将返回的值存储在它指向的位置。” Howver tempToPrint不是一个指针 - 它是一个无法取消引用的浮点数。你可以只是把它写为:

    var tempF = DallasTemperature::toFahrenheit(tempC); 
    I2C_writeAnything(&tempF); 
    

    此外,如果你将其转换为华氏那么为什么不叫摆在首位sensors.getTempF()

这应该让你至少编译。

+0

克里斯,你真棒。这里有一些非常好的答案,这些答案告诉我很多关于我在这里做错了什么,并给我一个关于如何进行的更好的想法。我所做的大部分工作都来自示例代码,虽然它对该代码中缺少irt变量的文档/注释感到不满,但它非常酷,我可以将一些编写得不好的代码放在那里,学习一下,希望其他人也会。我将执行这些更改并回报。 –