莫言恐鸣 发表于 2012-9-5 16:40:53

使用Arduino Leonardo连接两个传感器不能工作,求大神指导

我使用Arduino Leonardo板子连接了BMA180加速度计和PS2鼠标芯片,参照的是http://www.geek-workshop.com/thread-457-1-1.html和http://www.geek-workshop.com/thread-1114-1-1.html两个例子。但是在setup()中只能初始化其中的一个,也就是不能同时初始化。端口没有冲突,怀疑是库中发生了冲突。求大神看看怎么处理。

代码如下:

#include <Wire.h>
#include <ps2.h>
PS2 mouse(6, 5);


void mouse_init()
{
mouse.write(0xff);// reset
mouse.read();// ack byte
mouse.read();// blank */
mouse.read();// blank */
mouse.write(0xf0);// remote mode
mouse.read();// ack
delayMicroseconds(100);
}

void AccelerometerInit()
{
Wire.beginTransmission(0x40); // address of the accelerometer
// reset the accelerometer
Wire.write(0x10);
Wire.write(0xB6);
Wire.endTransmission();
delay(10);

Wire.beginTransmission(0x40); // address of the accelerometer
// low pass filter, range settings
Wire.write(0x0D);
Wire.write(0x10);
Wire.endTransmission();

Wire.beginTransmission(0x40); // address of the accelerometer
Wire.write(0x20); // read from here
Wire.endTransmission();
Wire.requestFrom(0x40, 1);
byte data = Wire.read();
Wire.beginTransmission(0x40); // address of the accelerometer
Wire.write(0x20);
Wire.write(data & 0x0F); // low pass filter to 10 Hz
Wire.endTransmission();

Wire.beginTransmission(0x40); // address of the accelerometer
Wire.write(0x35); // read from here
Wire.endTransmission();
Wire.requestFrom(0x40, 1);
data = Wire.read();
Wire.beginTransmission(0x40); // address of the accelerometer
Wire.write(0x35);
Wire.write((data & 0xF1) | 0x04); // range +/- 2g
Wire.endTransmission();
}

void setup()
{
//AccelerometerInit();
mouse_init();
Wire.begin();
Mouse.begin();
}
//this function can not be settled in the front of setup
void AccelerometerRead()
{
Wire.beginTransmission(0x40); // address of the accelerometer
Wire.write(0x02); // set read pointer to data
Wire.endTransmission();
Wire.requestFrom(0x40, 6);

short mx = Wire.read();
mx += Wire.read() << 8;
mx=double(mx)/1024;
short my = Wire.read();
my += Wire.read() << 8;
my=double(my)/1024;
short mz = Wire.read();
mz += Wire.read() << 8;
mz=double(mz)/16348;
Mouse.move(mx,-my,0);
}

void loop()
{
char mstat;
char mx;
char my;
/* get a reading from the mouse */
mouse.write(0xeb);// give me data!
mouse.read();      // ignore ack
mstat = mouse.read();
mx = mouse.read();
my = mouse.read();
//if((mx=my=0))
//{
//    AccelerometerInit();
//    AccelerometerRead();
//    angle();
//    int mx=bma180.x-4096*sin(Qx);
//    int my=bma180.y-4096*sin(Qy);
//    mx=bma180.x;
//    my=bma180.y;
//    Mouse.move(mx,-my,0);
//}
Mouse.move(mx,-my,0);
//if(mstat==char(1001))
//Mouse.press(MOUSE_LEFT);
delay(10);
}

真心感谢!!!

水乐天 发表于 2012-9-5 17:06:43

我跟你一样的问题哦

水乐天 发表于 2012-9-5 17:07:03

<Wire.h> 用了这个库之后 PMW不能用了

莫言恐鸣 发表于 2012-9-5 17:52:56

水乐天 发表于 2012-9-5 17:06 static/image/common/back.gif
我跟你一样的问题哦

那你怎么解决的呢

莫言恐鸣 发表于 2012-9-5 17:53:59

水乐天 发表于 2012-9-5 17:07 static/image/common/back.gif
用了这个库之后 PMW不能用了

喔,谢谢指导。我试试

┏ωǒ┛菰独 发表于 2012-9-5 18:20:59

我有个建议,不同时使用两个库, 先初始化一个,然后用过之后, 关掉这个库. 在使用另一个.

这样就不会冲突了

那么setup中就不需要代码了, 全在loop中就行了
页: [1]
查看完整版本: 使用Arduino Leonardo连接两个传感器不能工作,求大神指导