目录
1、PS2手柄介绍
2、PS2库文件添加
3、Auduino代码
4、测试
5、测试程序和库文件下载连接
1、PS2手柄介绍
PS2手柄介绍见下面博客
STM32中使用PS2手柄_stm32 ps2-CSDN博客
PS2手柄按钮分布
2、PS2库文件添加
库文件和测试程序一起打包提供下载
3、Auduino代码
#include <PS2X_lib.h> //PS2手柄
PS2X ps2x; // create PS2 Controller Class
/*************************************************************
* PS2手柄接线方式:
* arduino 手柄接收器
* 5 MOSI
* 6 MISO
* 7 CS
* 8 SCLK
*************************************************************/
#define PS2_DAT 5 //MOSI
#define PS2_CMD 6 //MISO
#define PS2_SEL 7 //CS
#define PS2_CLK 8 //SCLK
/*************************************************************/
unsigned char servo,PS2_LY,PS2_LX,PS2_RY,PS2_RX,PS2_KEY; //定义L侧Y轴,X轴以及R侧Y轴,X轴的变量
void (* resetFunc) (void) = 0; // Reset func
void setup() {
char error;
Serial.begin(9600); //开启串口,波特率9600
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, false, false);//PS2控制
}
/******函数功能:主循环程序体*******/
void loop(){
delay(2000);
ps2x.read_gamepad(false, 0); //read controller and set large motor to spin at 'vibrate' speed
PS2_LX=ps2x.Analog(PSS_LX); //读取L侧X轴的模拟值
PS2_LY=ps2x.Analog(PSS_LY); //读取L侧Y轴的模拟值
PS2_RX=ps2x.Analog(PSS_RX); //读取R侧X轴的模拟值
PS2_RY=ps2x.Analog(PSS_RY); //读取R侧Y轴的模拟值
Serial.print("PS2_LX:");
Serial.print(PS2_LX);
Serial.print(" PS2_LY:");
Serial.print(PS2_LY);
Serial.print(" PS2_RX:");
Serial.print(PS2_RX);
Serial.print(" PS2_RY:");
Serial.print(PS2_RY);
if(ps2x.Button(PSB_TRIANGLE))
Serial.println(" PSB_TRIANGLE");
else if(ps2x.Button(PSB_CROSS))
Serial.println(" PSB_CROSS");
else if(ps2x.Button(PSB_CIRCLE))
Serial.println(" PSB_CIRC");
else if(ps2x.Button(PSB_SQUARE))
Serial.println(" PSB_SQUARE");
else if(ps2x.Button(PSB_PAD_UP))
Serial.println(" PSB_PAD_UP");
else if(ps2x.Button(PSB_PAD_DOWN))
Serial.println(" PSB_PAD_DOWN");
else if(ps2x.Button(PSB_PAD_RIGHT))
Serial.println(" PSB_PAD_RIGHT");
else if(ps2x.Button(PSB_PAD_LEFT))
Serial.println(" PSB_PAD_LEFT");
else if(ps2x.Button(PSB_R1))
Serial.println(" PSB_R1");
else if(ps2x.Button(PSB_R2))
Serial.println(" PSB_R2");
else if(ps2x.Button(PSB_SELECT))
Serial.println(" PSB_SELECT");
else if(ps2x.Button(PSB_START))
Serial.println(" PSB_START");
else if(ps2x.Button(PSB_L1))
Serial.println(" PSB_L1");
else if(ps2x.Button(PSB_L2))
Serial.println(" PSB_L2");
else Serial.println(" KEY_RELEASE");
}
4、测试
PS2手柄和合单片机连接
串口调试助手
两个摇杆的x坐标、y坐标数值和方向见前面图片示意。
手柄没有任何按键按下,按键值:KEY_RELEASE
5、测试程序和库文件下载连接
https://download.csdn.net/download/panjinliang066333/89875915
库文件