码迷,mamicode.com
首页 > 其他好文 > 详细

利用STM32CubeMX来生成USB_HID_Mouse工程【添加ADC】(1)

时间:2017-05-18 01:26:38      阅读:405      评论:0      收藏:0      [点我收藏+]

标签:send   private   repo   单位   png   扩展   span   键盘   disable   

现在原来的基础上添加ADC的功能。

现在(利用STM32CubeMX来生成USB_HID_Mouse工程)基础上新增硬件

JoyStick Shield 游戏摇杆扩展板

技术分享

与STM32F103C8的连接

目前使用

JoyStick Shield   STM32F103C8

                           X----PA1(ADC1_IN1)

                           Y----PA2(ADC1_IN2)

好了我们现在STM32CubeMX来打开之前的工程

技术分享

现在我们先设置ADC1_IN1

技术分享

让我们来看其adc的默认配置

技术分享

现在直接生成工程。

会发现在原来的工程基础上多了一些ADC的初始化函数等。

现在我们在main.C新增

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
uint16_t AD_X_Value = 0;
/* USER CODE END PV */
/* USER CODE BEGIN 3 */
  
  /*##-1- Start the conversion process #######################################*/   
   HAL_ADC_Start(&hadc1);//<为启动ADC装换
  /*##-2- Wait for the end of conversion #####################################*/ 
    /**
     *   Before starting a new conversion, you need to check the current state of
     *   the peripheral; if it’s busy you need to wait for the end of current
     *   conversion before starting a new one.
     *   For simplicity reasons, this example is just waiting till the end of the
     *   conversion, but application may perform other tasks while conversion
     *   operation is ongoing. 
     */
    HAL_ADC_PollForConversion(&hadc1, 50);//<表示等待转换完成,第二个参数表示超时时间,单位ms.
    /* Check if the continous conversion of regular channel is finished */
    if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC)) 
    {

        /*##-3- Get the converted value of regular channel  ######################*/
        AD_X_Value = HAL_ADC_GetValue(&hadc1);
        #ifdef RTT_LOG_ENABLED
        loge("AD_X_Value %d",AD_X_Value);
        #endif //RTT_LOG_ENABLED
      
    }
    #if 0
  // Send HID report
    mouseHID.x = 10;
    USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)&mouseHID, sizeof(struct mouseHID_t));
    #endif 
    HAL_Delay(1000);
  }
  /* USER CODE END 3 */

在加上我喜欢的RTT【不知道RTT的可以参考 [转]使用RTT(Real-Time Terminal)

别忘记在main.c加上

/* USER CODE BEGIN Includes */
#include "usbd_hid.h"

#ifdef RTT_LOG_ENABLED
#include "rtt_log.h"
#endif //RTT_LOG_ENABLED

/* USER CODE END Includes */

将在HAL_MspInit()代码中 __HAL_AFIO_REMAP_SWJ_DISABLE();给注释掉

下载编译

我们可以看到x轴变化的时候数据

技术分享

 

利用STM32CubeMX来生成USB_HID_Mouse工程【添加ADC】(1)

标签:send   private   repo   单位   png   扩展   span   键盘   disable   

原文地址:http://www.cnblogs.com/libra13179/p/6869987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!