PS2手柄移植-hal库

在这里插入图片描述
组件分为手柄,接收器,转接板,(手柄和接收器非匹配,是通用的)

特点:
1、自动休眠省电模式:在开启无配对状态下,30s后启动省点模式;开启并配对完成的状态下,5分钟无按键按下,然后启动省点模式。此时只要按下START就能激活!
2、遥控距离:手柄采用的是2.4G无线技术,遥控距离可达15米左右。

连接图

  •       DI->PB12 
    
  •       DO->PB13    
    
  •       CS->PB14 
    
  •       CLK->PB15
    

只要将移植文件加到工程中即可

然后在main.c中像这样添加

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "delay.h"
#include "pstwo.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#define go 			printf("go/r/n")
#define back 		printf("back/r/n")
#define stop		printf("stop/r/n")
#define turnleft	printf("turnleft/r/n")	
#define turnright	printf("turnright/r/n")	
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
   uint8_t key=0;
  /* USER CODE END 1 */
  

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  delay_init(72);
 // printf("aaa/r/n");
   PS2_Init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  		key=PS2_DataKey();
		
		 switch(key)
		 {
			 case 5:go;break;
			 case 7:back;break;
			 case 8:turnleft;break;
			 case 6:turnright;break;
			 
			 default:stop;break;			 
		 }
		if(key!=0)                   //有按键按下
    	{
			printf("%d  is  pressed  \r\n",key);
    	}
		printf(" %5d %5d %5d %5d\r\n",PS2_AnologData(PSS_LX),PS2_AnologData(PSS_LY),
		                              PS2_AnologData(PSS_RX),PS2_AnologData(PSS_RY) );
       HAL_Delay(500);
		
	  
//   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
//   delay_us(5);
//   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
//   delay_us(5); 
	  
  }
  /* USER CODE END 3 */
}

测试结果
在这里插入图片描述
移植文件

发布了33 篇原创文章 · 获赞 4 · 访问量 5081

猜你喜欢

转载自blog.csdn.net/qq_42967008/article/details/100061248
今日推荐