分享一个3D鼠标的方案
包括软件,原理图
单片机源程序如下:
- /*
- [3D空间鼠标固件-发送端]
- MCU :STM32F103C8T6
- IMU :MPU6050
- RF2.4G:NRF24L01
- [ 秋阳 2013-11 ]
- */
- #include "stm32f10x.h"
- #include "stdlib.h"
- #include "math.h"
- #include "delay.h"
- #include "NRF24L01.H"
- #include "MPU6050.H"
- #include "IIC.H"
- #include "spi.H"
- #include "ALL_config.h"
- #include "var_global.h"
- #include "Cal.h"
- #include "KEY.h"
- //-----------------------------------------------------------------//
- u16 dir_time=0;
- u8 RC_CON=0;
- float Button=0;
- u16 TO=0;
- u16 C_count,L_count;
- u8 D_Clk;
- u8 clk1,clk2;
- //---------------------------------------------------------------------//
- int main()
- {
- RCC_Configuration(); //配置系统时钟
- delay_ms(50);
- I2C_Configuration();
- GPIO_Configuration();
- SPIx_Init();
- //设置为发射模式
- NRF24L01Int();
- NRFSetTxMode(TxDate);
- //设置IMU
- IIC_Reboot();
- IIC_Reboot();
- Init_MPU6050();
- //设置陀螺仪零点
- Set_Offset();
- //设置5ms中断
- TIM3_INT();
- while(1)
- {
- //简单闪灯
- GPIO_ResetBits(GPIOB,GPIO_Pin_0);
- Delay_s(5);
- GPIO_SetBits(GPIOB,GPIO_Pin_0);
- Delay_s(5);
- }
- }
- //----------------------------5ms中断--------------------------------------
- void TIM3_IRQHandler()
- {
- TIM_ClearITPendingBit(TIM3,TIM_FLAG_Update);
- dir_time++;
- //读取传感器
- READ_MPU6050();
- Cal_TsData();
- sensfusion6UpdateQ(-GRY_F.X,-GRY_F.Y,GRY_F.Z,ACC_AVG.X*100,ACC_AVG.Y*100,ACC_AVG.Z*100,0.005f);
- sensfusion6GetEulerRPY(&Q_ANGLE.Roll, &Q_ANGLE.Pitch, &Q_ANGLE.Yaw);
- //----------------单击双击优化,防止双击时指针漂移---------------------------------
- //-无线数据包标志:【左单1】【 左双2】 【左长3】 【右单4】 【左右长5】
- if(dir_time>=5) //按键扫描周期:5ms*5=25ms
- {
- dir_time=0;
- Get_KEY(); //取得按键状态
- if (!K_Trg && !K_TrgR) Button=0;
- if (K_Trg) {Button=1;D_Clk=1;} //左键键按下,双击计时开始
- if (K_TrgR)Button=4; //右键按下
- if (K_Cont) L_count++;else L_count=0;
- if (D_Clk && C_count<15) C_count++; else {D_Clk=0;C_count=0;} //双击超时
- if (K_Trg && C_count>2) {Button=2;C_count=0;D_Clk=0;}//第一下点完,未超时且又被触发,为左键双击
- if (L_count>15) Button=3;//左键长按
- if( K_Cont && K_ContR) Button=5; //左右键按下
- }
- //------------------------------------------------------
- if(Button==1)clk1++;
- if(Button==2)clk2++;
- Send_RFdata(); // 发送RF数据包
- }
复制代码
Keil代码下载:
STM32空中3D鼠标.7z
(1.6 MB, 下载次数: 34)
|