现在原来的基础上添加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();给注释掉

或者在STM32CubeMX配置的时候将SW接口的直接开启

 

 

下载编译

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

 

关于STM32的ADC更多参考:

AN3116应用笔记  STM32™ ADC 模式及其应用 .PDF

http://www.st.com/content/ccc/resource/technical/document/application_note/c4/63/a9/f4/ae/f2/48/5d/CD00258017.pdf/files/CD00258017.pdf/jcr:content/translations/zh.CD00258017.pdf

原版

STM32's ADC modes and their applications (AN3116)

http://www.st.com/content/ccc/resource/technical/document/application_note/c4/63/a9/f4/ae/f2/48/5d/CD00258017.pdf/files/CD00258017.pdf/jcr:content/translations/en.CD00258017.pdf

posted on 2017-05-17 22:53  陌鉎こ城sHi  阅读(965)  评论(0编辑  收藏  举报