错误:Arduino中'{'标记之前的预期unqualified-id

时间:2015-04-12 03:49:44

标签: c++ arduino i2c arduino-uno arduino-ide

我在为两个I2C传感器编译代码时遇到了麻烦。我一直收到错误:expected unqualified-id before '{' token

以下是我要解决的代码:

#include <i2cmaster.h>
#include "Wire.h"    // imports the wire library for talking over I2C 

int led = 13;
//before void setup
#define SENSOR_ADDR_OFF_OFF  (0x4B)
#define SENSOR_ADDR_OFF_ON   (0x4A)
#define SENSOR_ADDR_ON_OFF   (0x49)
#define SENSOR_ADDR_ON_ON    (0x4m8)

// Set the sensor address here
const uint8_t sensorAddr = SENSOR_ADDR_OFF_OFF;
//void setup begins here
void setup()
{
// Start the serial port for output
Serial.begin(9600);

pinMode(led, OUTPUT);

  // Join the I2C bus as master
  Wire.begin();

// Set up the ADC on the sensor (reset everything)


i2c_init(); //Initialise the i2c bus
PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups

}
//void loop begins here
void loop(){
int dev = 0x5A<<1;
int data_low = 0;
int data_high = 0;
int pec = 0;

i2c_start_wait(dev+I2C_WRITE);
i2c_write(0x07);

// read
i2c_rep_start(dev+I2C_READ);
data_low = i2c_readAck(); //Read 1 byte and then send ack
data_high = i2c_readAck(); //Read 1 byte and then send ack
pec = i2c_readNak();
i2c_stop();

//This converts high and low bytes together and processes temperature, MSB              is   a error bit and is ignored for temps
double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
double tempData = 0x0000; // zero out the data
int frac; // data past the decimal point

// This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
tempData = (double)(((data_high & 0x007F) << 8) + data_low);
tempData = (tempData * tempFactor)-0.01;

float celcius = tempData - 273.15;
float fahrenheit = (celcius*1.8) + 32;

Serial.print("Celcius: ");
Serial.println(celcius);

Serial.print("Fahrenheit: ");
Serial.println(fahrenheit);
}
uint8_t left;
uint8_t right;

{
if ((ReadByte(sensorAddr, 0x0, &left) == 0) &&
   (ReadByte(sensorAddr, 0x1, &right) == 0))
{
   // Use a threshold (value from 0-255) to determine if sensor detected a     dark
  // or light surface; the threshold should be modified according to the
  // environment on which the sensor will be used
{    
Serial.print("Left: ");
Serial.println(left);
}{
Serial.print("Right: ");
Serial.println(right);
}}  
delay(1000);
}

// Read a byte on the i2c interface
int ReadByte(uint8_t addr, uint8_t reg, uint8_t *data)
{
// Do an i2c write to set the register that we want to read from
Wire.beginTransmission(addr);
Wire.write(reg);
Wire.endTransmission();

//Read a byte from the device
Wire.requestFrom(addr, (uint8_t)1);
if (Wire.available())
{
  *data = Wire.read();
}
else
{
  // Read nothing back
  return -1;
}

return 0;
}

// Write a byte on the i2c interface
void WriteByte(uint8_t addr, uint8_t reg, byte data)
}
{
// Begin the write sequence
Wire.beginTransmission(addr);

// First byte is to set the register pointer
Wire.write(reg);

// Write the data byte
Wire.write(data);

// End the write sequence; bytes are actually transmitted now
Wire.endTransmission();
}
}    

1 个答案:

答案 0 :(得分:2)

你有这个函数,它以错误的括号类型开头。

void WriteByte(uint8_t addr, uint8_t reg, byte data)
} // Remove this, it's wrong
{ // Opening brace
    ... the rest of your code ...
} // Closing brace
} // Another closing brace, but I don't know why (I'd just remove it too)

你应该有。

void WriteByte(uint8_t addr, uint8_t reg, byte data)
{ // Opening brace
    ... the rest of your code ...
} // Closing brace