|
本帖最后由 wonderboy 于 2014-2-26 15:12 编辑
已经解决,SDA或者SCL有根针虚了,重新焊接后测试,数据刷新了!
Arduino通过I2C连接LCD2004和HM5883问题
上电后除只能显示一次,之后不论如何动传感器,LCD不刷新显示,各位老大能帮看看程序么?
[mw_shl_code=c,true]#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args) write(args);
#else
#define printByte(args) print(args,BYTE);
#endif
#define HM5883_ADDRESS 0x1E //0011110b, I2C 7bit address of HMC5883
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
Wire.beginTransmission(HM5883_ADDRESS); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
}
void loop()
{
Wire.begin();
int x, y, z; //triple axis data
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(HM5883_ADDRESS);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();
//Read data from each axis, 2 registers per axis
Wire.requestFrom(HM5883_ADDRESS, 6);
if (6 <= Wire.available()) {
x = Wire.read() << 8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read() << 8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read() << 8; //Y msb
y |= Wire.read(); //Y lsb
}
//Print out values of each axis
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("X: ");
lcd.print(x);
lcd.setCursor(0, 1);
lcd.print("Y: ");
lcd.print(y);
lcd.setCursor(0, 2);
lcd.print("Z: ");
lcd.print(z);
delay(250);
}[/mw_shl_code]
|
|