mbed系列--基于飞思卡尔FRDM KL25Z的鼠标设计快速实现

==========================================================

原创文章转载请注明:blog.csdn.net/guo8113
==========================================================================

说明:

今天接着上次的mbed工程又玩弄了一番,实现了基于mbed RTOS以及三轴重力传感器的鼠标,当然了,由于FRDM25Z上没有按键,仅仅实现了鼠标的移动。

在这个系统中更是充分体现了mbed的方便快捷,分分钟钟几行代码搞定了一个飞鼠的设计。

mbed的在线编译工具不会用的参考mbed试玩—快速开发MCU应用(基于FRDM-KL25Z)

实现步骤:

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输入设备,鼠标动了。。。。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
应用介绍: 蓝牙无线空中键鼠,能够同时实现传统的键盘和鼠标双功能。它的空中使用功能,可以将你从电脑、电视旁边彻底解放出来,只需要通过在空中挥动RC16空鼠,就迅速响应转换成在屏幕上的光标移动,使用3D陀螺仪完美结合,用户可以以360度随意精准操作。手持操作手感舒适、方便,完全避免了传统鼠标需要以静止的桌面为参照物操作或红外遥控器按键操作的弊端,让您躺着玩电脑、电视都不累,轻松休闲,完全 “掌”控你的电脑、电视娱乐 原理介绍: 飞思卡尔蓝牙飞鼠以Kinetis KL16单片机、加速度计、陀螺仪和电子罗盘为基础,并通过蓝牙与目标主机通信。使用了蓝牙 HID/HFP/SSP配置文件,并可以将鼠标和键盘的输入数据和传感器数据发送至目标主机。 它的关键特性如下: 6自由度/ 9自由度的鼠标和QWERTY键盘 操纵杆和6/9轴运动游戏 蓝牙连接: HID / HFP / SSP 红外遥控, 自学习 麦克风输入,头戴式耳机, 语音识别 指纹, 身份认证支持 近场通信, 快速蓝牙连接 无线充电(电路板上线圈为无线充电接收线圈) 飞思卡尔蓝牙飞鼠主要器件清单: 飞思卡尔 Kinetis MKL16Z256VLH4(MKL16Z256VLH4数据手册)(256KB FLASH / 32K RAM)(主控芯片) 飞思卡尔 MAG3110FCR2(MAG3110FCR2数据手册)(电子罗盘) 飞思卡尔 MMA8563FCR1(加速度计) 飞思卡尔 FXAS21000CQR1(FXAS21000CQR1数据手册)(陀螺仪) RDA5876A(RDA5876A数据手册),Compliant with Bluetooth 2.1 + EDR specification(蓝牙模块) 硬件系统框图: 软件架构: 附件内容包括: 源代码 飞思卡尔蓝牙飞鼠参考设计论文 bootloader工具软件以及CodeWarrior工程等内容 你可能感兴趣的项目设计: “空中鼠标”一款不用放在鼠标垫上的鼠标
Sure, I can help you with that! Here's an example code for adjusting the brightness of an RGB LED using the keys on the Freescale FRDM-KL25Z board. The code is written in C++ using the mbed development environment: ``` #include "mbed.h" // Define pins for the RGB LED PwmOut red(LED_RED); PwmOut green(LED_GREEN); PwmOut blue(LED_BLUE); // Define pins for the keys DigitalIn key1(PTC6); DigitalIn key2(PTC7); // Define the initial brightness values float brightness = 0.5f; float redBrightness = 0.5f; float greenBrightness = 0.5f; float blueBrightness = 0.5f; int main() { // Set the PWM frequency for the RGB LED red.period(0.001f); green.period(0.001f); blue.period(0.001f); while(1) { // Check if key 1 is pressed if (key1 == 0) { // Increase the brightness brightness += 0.1f; if (brightness > 1.0f) { brightness = 1.0f; } } // Check if key 2 is pressed if (key2 == 0) { // Decrease the brightness brightness -= 0.1f; if (brightness < 0.0f) { brightness = 0.0f; } } // Update the red brightness redBrightness = brightness; // Update the green brightness greenBrightness = 1.0f - brightness; // Update the blue brightness blueBrightness = fabs(brightness - 0.5f) * 2.0f; // Set the PWM duty cycle for the RGB LED red.write(redBrightness); green.write(greenBrightness); blue.write(blueBrightness); // Wait for a short time wait(0.1f); } } ``` Let me explain the code in detail: - We first define the pins for the RGB LED and the keys using the `PwmOut` and `DigitalIn` classes from the mbed library. - We then define the initial brightness values for the RGB LED, which are all set to 50%. - In the `main()` function, we set the PWM frequency for the RGB LED and start an infinite loop. - Inside the loop, we check if the first key is pressed. If it is, we increase the brightness by 10% and make sure it doesn't go above 100%. If the second key is pressed, we decrease the brightness by 10% and make sure it doesn't go below 0%. - We then calculate the new brightness values for the red, green, and blue channels based on the overall brightness value. The red brightness is set to the overall brightness, the green brightness is set to the complement of the overall brightness, and the blue brightness is set to a scaled version of the difference between the overall brightness and 50%. - Finally, we set the PWM duty cycles for the red, green, and blue channels using the `write()` method of the `PwmOut` class, and wait for a short time before starting the loop again. I hope this helps! Let me know if you have any questions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值