我使用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);
}
真心感谢!!! |