找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6748|回复: 6
收起左侧

基于STM32的PS2遥控手柄,平衡车程序源码

[复制链接]
ID:367409 发表于 2018-7-8 19:18 | 显示全部楼层 |阅读模式
基于STM32的PS2遥控手柄,平衡车程序源码

单片机源程序如下:
  1. /******************** (C) COPYRIGHT (2015)BST BALANCECAR **************************
  2. * 文件名  :main.c
  3. **********************************************************************************/
  4. //#include "stm32f10x.h"
  5. #include "mpu6050.h"
  6. #include "i2c_mpu6050.h"
  7. #include "motor.h"
  8. #include "upstandingcar.h"
  9. #include "SysTick.h"
  10. #include "led.h"
  11. #include "usart.h"
  12. #include "i2c.h"
  13. //#include "outputdata.h"
  14. #include "timer.h"
  15. #include "UltrasonicWave.h"
  16. //#include "stm32f10x_usart.h"
  17. #include "ps2.h"
  18. #include "delay.h"

  19. float gyz;
  20. int acc;
  21. int acc1;

  22. /*协议相关*/
  23. //extern u8 newLineReceived = 0;

  24. /*
  25. * 函数名:main
  26. * 描述  :主函数
  27. */
  28. int main(void)
  29. {       
  30.     u8 PS2_KEY = 0, X1=0,Y1=0,X2=0,Y2=0;
  31.         u16 num = 0;  
  32.        
  33.           SystemInit();                   //=====系统初始化
  34.         Timerx_Init(5000,7199);                                   //定时器TIM1
  35. //        UltrasonicWave_Configuration();            //超声波初始化设置 IO口及中断设置                            

  36.         //USART1_Config();                                                //串口1初始化 上位机
  37.         USART3_Config();                                                //串口3初始化 蓝牙与USART3公用相同IO口
  38.        
  39.         TIM2_PWM_Init();                                           //PWM输出初始化
  40.         MOTOR_GPIO_Config();                                  //电机IO口初始化
  41.           LED_GPIO_Config();
  42.        
  43. //   TIM3_External_Clock_CountingMode();           //左电机脉冲输出外部中断口PA7使用TIM3定时器用作为脉冲数计算
  44. //   TIM4_External_Clock_CountingMode();           //右电机脉冲输出外部中断口PB7使用TIM4定时器用作为脉冲数计算
  45.           TIM3_Encoder_Init();                       //编码器获取脉冲数 PA6 7
  46.           TIM4_Encoder_Init();                       //编码器获取脉冲数 PB6 7       
  47.         ////////////////////DMP/////////////////////////////////
  48.         i2cInit();                                                           //IIC初始化 用于挂靠在总线上的设备使用
  49.         delay_nms(10);                                                   //延时10ms
  50.         MPU6050_Init();                                                   //MPU6050 DMP陀螺仪初始化
  51.         PS2_Init();                                                           //PS2手柄初始化
  52.         SysTick_Init();                                                  //SysTick函数初始化       
  53.         CarUpstandInit();                                          //小车直立参数初始化
  54.         SysTick->CTRL |=  SysTick_CTRL_ENABLE_Msk;         //使能总算法时钟
  55.         PrintChar("FFFFFFF \n");
  56.         while (1)
  57.         {

  58. //                 GPIO_ResetBits(GPIOC, GPIO_Pin_13);
  59.                 MPU6050_Pose();                                                 //获取MPU6050角度状态
  60. //                 gy0=gyro[0];
  61. //                 UltrasonicWave_StartMeasure();           //调用超声波发送程序 给Trig脚 <10us 高电平                 
  62. //                 chaoshengbo();                               //计算超声波测距距离
  63. //      printf("%d",ucBluetoothValue);
  64. //                printf("\t");
  65. //                printf("%f",BST_fSpeedControlOutNew);
  66. //                printf("\t");
  67. //                printf("%f",BST_fCarAngle);
  68. //                printf("\t");
  69. //                 printf("%f",BST_fLeftMotorOut);
  70. //                printf("\t");
  71. //                printf("\n");
  72.                 num++;
  73.                 if(num == 500)
  74.                 {
  75.                         num =0;

  76.                         PS2_KEY = PS2_DataKey();         //手柄按键捕获处理
  77.                         switch(PS2_KEY)
  78.                         {
  79.                                 case PSB_SELECT:         PrintChar("PSB_SELECT \n");  break;
  80.                                 case PSB_L3:             g_newcarstate = enSTOP;  PrintChar("PSB_L3 \n");  break;  
  81.                                 case PSB_R3:             g_newcarstate = enSTOP;         PrintChar("PSB_R3 \n");  break;  
  82.                                 case PSB_START:          PrintChar("PSB_START \n");  break;  
  83.                                 case PSB_PAD_UP:         g_newcarstate = enRUN;   PrintChar("PSB_PAD_UP \n");  break;  
  84.                                 case PSB_PAD_RIGHT:        g_newcarstate = enRIGHT; PrintChar("PSB_PAD_RIGHT \n");  break;
  85.                                 case PSB_PAD_DOWN:        g_newcarstate = enBACK;  PrintChar("PSB_PAD_DOWN \n");  break;
  86.                                 case PSB_PAD_LEFT:        g_newcarstate = enLEFT;  PrintChar("PSB_PAD_LEFT \n");  break;
  87.                                 case PSB_L2:              PrintChar("PSB_L2 \n");  break;
  88.                                 case PSB_R2:              PrintChar("PSB_R2 \n");  break;
  89.                                 case PSB_L1:              PrintChar("PSB_L1 \n");  break;
  90.                                 case PSB_R1:              PrintChar("PSB_R1 \n");  break;     
  91.                                 case PSB_TRIANGLE:        PrintChar("PSB_TRIANGLE \n");  break;
  92.                                 case PSB_CIRCLE:          PrintChar("PSB_CIRCLE \n");  break;
  93.                                 case PSB_CROSS:           PrintChar("PSB_CROSS \n");  break;
  94.                                 case PSB_SQUARE:          PrintChar("PSB_SQUARE \n");  break;
  95.                                 default: g_newcarstate = enSTOP; break;
  96.                         }
  97.                         //获取模拟值
  98.                         if(PS2_KEY == PSB_L1 || PS2_KEY == PSB_R1)
  99.                         {
  100.                                 X1 = PS2_AnologData(PSS_LX);
  101.                                 Y1 = PS2_AnologData(PSS_LY);
  102.                                 X2 = PS2_AnologData(PSS_RX);
  103.                                 Y2 = PS2_AnologData(PSS_RY);
  104.                                 /*左摇杆*/
  105.                             if (Y1 < 5 && X1 > 80 && X1 < 180) //上
  106.                             {
  107.                               g_newcarstate = enRUN;
  108.                                  
  109.                             }
  110.                             else if (Y1 > 230 && X1 > 80 && X1 < 180) //下
  111.                             {
  112.                               g_newcarstate = enBACK;
  113.                                   
  114.                             }
  115.                             else if (X1 < 5 && Y1 > 80 && Y1 < 180) //左
  116.                             {
  117.                               g_newcarstate = enLEFT;

  118.                             }
  119.                             else if (Y1 > 80 && Y1 < 180 && X1 > 230)//右
  120.                             {
  121.                               g_newcarstate = enRIGHT;

  122.                             }
  123.                             else if (Y1 <= 80 && X1 <= 80) //左上
  124.                             {
  125.                               g_newcarstate = enUPLEFT;
  126.        
  127.                             }
  128.                             else if (Y1 <= 80 && X1 >= 180) //右上
  129.                             {
  130.                               g_newcarstate = enUPRIGHT;
  131.                        
  132.                             }
  133.                             else if (X1 <= 80 && Y1 >= 180) // 左下
  134.                             {
  135.                               g_newcarstate = enDOWNLEFT;
  136.                        
  137.                             }
  138.                             else if (Y1 >= 180 && X1 >= 180) //右下
  139.                             {
  140.                               g_newcarstate = enDOWNRIGHT;
  141.                                   
  142.                             }
  143.                             else//停
  144.                             {
  145.                               g_newcarstate = enSTOP;
  146.                             }

  147.                                  /*右摇杆*/
  148.                             if (X2 < 5 && Y2 > 110 && Y2 < 150) //左
  149.                             {
  150.                               g_newcarstate = enTLEFT;
  151.                             }
  152.                             else if (Y2 > 110 && Y2 < 150 && X2 > 230)//右
  153.                             {
  154.                               g_newcarstate = enTRIGHT;
  155.                             }
  156.                             else//归位
  157.                             {
  158.                               //
  159.                             }
  160.                         }
  161.                 }
  162.                
  163.        
  164.                 CarStateOut();               
  165.                          
  166.          }
  167.                                                                     
  168. }
复制代码

所有资料51hei提供下载:
PS2_STM32平衡车程序源码.rar (695.06 KB, 下载次数: 150)


回复

使用道具 举报

ID:1 发表于 2018-7-9 02:13 | 显示全部楼层
此贴需要补充说明资料后才可获得积分
回复

使用道具 举报

ID:333428 发表于 2018-7-29 12:23 | 显示全部楼层
楼主会用51连接PS2手柄吗
回复

使用道具 举报

ID:579146 发表于 2019-9-19 14:14 | 显示全部楼层
这个具体的波形是怎么样的?
回复

使用道具 举报

ID:614524 发表于 2019-9-21 19:36 | 显示全部楼层
单片机用的103还是f407
回复

使用道具 举报

ID:430148 发表于 2020-4-11 16:37 | 显示全部楼层
是stm32f103?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表