smyhz 发表于 2016-9-14 23:58:40

关于2块GY-30同时显示的问题

本帖最后由 smyhz 于 2016-9-15 00:00 编辑

想用2个GY-30做个光照比较,但是改BH1750程序时,无法加入第二个BH1750
程序如下
//VCC-5v
//GND-GND
//SCL-SCL(A5)
//SDA-SDA(A4)
//ADD-GND   ADD接VCC为0x5c ADD接GND为0x23
//*/
#include "Wire.h"
#include "math.h"
int BH1750address1 = 0x23;//BH1750 I2C地址、
int BH1750address2 = 0x5c;
byte buff;
void setup()
{
Wire.begin();
Serial.begin(9600);
}
void loop()
{
Serial.print( BH17501() );
Serial.print( “/” );
Serial.print( BH17502() );
Serial.println("");
}
double BH17501() //BH1750设备操作
double BH17502()
{
int ia = 0;
int ib = 0;
double vala = 0;
double valb = 0
                //开始I2C读写操作
Wire.beginTransmission(BH1750address1);
Wire.beginTransmission(BH1750address2);
Wire.write(0x10);//1lx reolution 120ms//发送命令
Wire.endTransmission();
delay(200);
//读取数据
Wire.beginTransmission(BH1750address1);
Wire.beginTransmission(BH1750address2);
Wire.requestFrom(BH1750address1, 2);
Wire.requestFrom(BH1750address2, 2);
while (Wire.available()) //
{
    buff = Wire.read(); // receive one byte
    buff = Wire.read();
    ia++;
    ib++;
}
Wire.endTransmission();
if (2 == ia)
{
    vala = ((buff << 8) | buff) / 1.2;
}
return vala;
if (2 == ib)
{
    valb = ((buff << 8) | buff) / 1.2;
}
return vala;

}
主要是看不懂BH1750()是从double里读取的还是其他地方读出来的
问题简介如下

gy-30:24: error: expected initializer before 'double'

double BH17502()

^

exit status 1
expected initializer before 'double'
谢谢各位大神了

smyhz 发表于 2016-9-26 15:29:13

好吧,没人回。。。
研究了好久终于弄好了
顺便发发
#include "Wire.h" //IIC库
#include "math.h"
int BH1750address1 = 0x23;//芯片地址为16位23
int BH1750address2 = 0x5c;
byte buff;

void setup()
{
Wire.begin();
Serial.begin(9600);

}

void loop()
{
int ia;
int ib;
int sumcd;
uint16_t vala = 0;
uint16_t valb = 0;
BH1750a_Init(BH1750address1);
BH1750b_Init(BH1750address2);
delay(1000);
if (2 == BH1750a_Read(BH1750address1))
{
    vala = ((buff << 8) | buff) / 12 - 3;

}
if (2 == BH1750b_Read(BH1750address2))
{
    valb = ((buff << 8) | buff) / 12;


}

delay(150);
Serial.print(vala, DEC);
Serial.print("/");
Serial.print(valb, DEC);
Serial.println("");


}
int BH1750a_Read(int address1) //
{
int ia = 0;
Wire.beginTransmission(address1);
Wire.requestFrom(address1, 2);
while (Wire.available()) //
{
    buff = Wire.read();// read one byte
    ia++;
}
Wire.endTransmission();
return ia;
}
int BH1750b_Read(int address2) //
{
int ib = 0;
Wire.beginTransmission(address2);
Wire.requestFrom(address2, 2);
while (Wire.available()) //
{
    buff = Wire.read();// read one byte
    ib++;
}
Wire.endTransmission();
return ib;
}


void BH1750a_Init(int address1)
{
Wire.beginTransmission(address1);
Wire.write(0x10);//1lx reolution 120ms
Wire.endTransmission();
}
void BH1750b_Init(int address2)
{
Wire.beginTransmission(address2);
Wire.write(0x10);//1lx reolution 120ms
Wire.endTransmission();
}

123liyao 发表于 2018-5-31 22:14:30

请问这个芯片地址是怎么确定的?我很想请教下
页: [1]
查看完整版本: 关于2块GY-30同时显示的问题