==========================================================
原创文章转载请注明:blog.csdn.net/guo8113
==========================================================================
说明:
今天接着上次的mbedproject又玩弄了一番。实现了基于mbed RTOS以及三轴重力传感器的鼠标,当然了。因为FRDM25Z上没有按键,只实现了鼠标的移动。
在这个系统中更是充分体现了mbed的方便快捷,分分钟钟几行代码搞定了一个飞鼠的设计。
mbed的在线编译工具不会用的參考mbed试玩—高速开发MCU应用(基于FRDM-KL25Z)。
实现步骤:
1.新建project,导入对应的库(在线有提供的库):
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VvODExMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
导入库时通过import 导入;
2.为了好玩我把TSI(触摸按键)。mbed RTOS、LED、Timer都导入了,事实上这个功能用不到RTOS。可导入的库有:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VvODExMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
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输入设备,鼠标动了。。。。
版权声明:本文博主原创文章,博客,未经同意不得转载。