转载地址:http://www.cnblogs.com/bhlsheji/p/4912151.html
今天接着上次的mbed工程又玩弄了一番,实现了基于mbed RTOS以及三轴重力传感器的鼠标,当然了,由于FRDM25Z上没有按键,仅仅实现了鼠标的移动。 在这个系统中更是充分体现了mbed的方便快捷,分分钟钟几行代码搞定了一个飞鼠的设计。
实现步骤: 1.新建工程,导入相应的库(在线有提供的库):
导入库时通过import 导入;
2.为了好玩我把TSI(触摸按键),mbed RTOS、LED、Timer都导入了,其实这个功能用不到RTOS。可导入的库有:
3.新建并编写main函数:终于可以上代码了:
- #include "mbed.h"
- #include "rtos.h"
-
- #include "MMA8451Q.h"
- #include "USBMouse.h"
- #include "tsi_sensor.h"
- //MMA8451Q definiation
- #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
- PinName const SDA = PTE25;
- PinName const SCL = PTE24;
- #elif defined (TARGET_KL05Z)
- PinName const SDA = PTB4;
- PinName const SCL = PTB3;
- #elif defined (TARGET_K20D50M)
- PinName const SDA = PTB1;
- PinName const SCL = PTB0;
- #else
- #error TARGET NOT DEFINED
- #endif
-
- #define MMA8451_I2C_ADDRESS (0x1d<<1)
-
- //
- #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
- #define ELEC0 9
- #define ELEC1 10
- #elif defined (TARGET_KL05Z)
- #define ELEC0 9
- #define ELEC1 8
- #else
- #error TARGET NOT DEFINED
- #endif
-
- MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
-
- Ticker tick;
- PwmOut rled(LED1);
- PwmOut gled(LED2);
- PwmOut bled(LED3);
-
- TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
- USBMouse mouse;
- //InterruptIn sw2(SW2);
-
- //thread pointer
- Thread *thread2;
-
- float x=0, y=0, z=0,t=0;
-
- //intruppt
- void sw2_press(void)
- {
- thread2->signal_set(0x1);
- }
-
- void led_thread(void const *argument)
- {
- while (true) {
- rled = abs(1.0f - x -t);
- gled = abs(1.0f - y -t);
- bled = abs(1.0f - z -t);
- Thread::wait(50);
- }
- }
-
- void scan_thread(void const *argument)
- {
- while (true) {
- Thread::signal_wait(0x1);
- // x = abs(acc.getAccX());
- // y = abs(acc.getAccY());
- // z = abs(acc.getAccZ());
- x = acc.getAccX();
- y = acc.getAccY();
- z = acc.getAccZ();
- t = tsi.readPercentage();
- }
- }
-
- int main()
- {
- Thread thread(led_thread);
- thread2 = new Thread(scan_thread);
-
- // sw2.fall(&sw2_press);
- tick.attach(&sw2_press,0.001); //scan MMA8451Q ervery 0.01s
- while (true) {
- Thread::wait(1);
- mouse.move( y*127,x*127);
- printf("\r\n 0.1 seconds passed: %f,%f,%f,%f \r\n", x,y,z,t);
- fflush(stdout);
- }
- }
复制代码
OK ,大功告成,连接USB线到板子上的USB KL25Z到PC,PC识别USB输入设备,鼠标动了。。。。
|