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

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、付费专栏及课程。

余额充值