你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F429 ADC采样,测试可以获取正确值

[复制链接]
xhzheng 提问时间:2017-1-18 16:33 /

main.c
MX_ADC1_Init();
uhADCxConvertedValue=Get_Adc_Average(ADC_CHANNEL_10,20);



adc.c


/**
  ******************************************************************************
  * File Name          : ADC.c
  * Description        : This file provides code for the configuration
  *                      of the ADC instances.
  ******************************************************************************
  *
  * COPYRIGHT(c) 2016 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "adc.h"

#include "gpio.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

ADC_HandleTypeDef hadc1;
ADC_ChannelConfTypeDef sConfig;
/* ADC1 init function */
void MX_ADC1_Init(void)
{
  //ADC_ChannelConfTypeDef sConfig;

    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
    */
        hadc1.Instance=ADC1;
        hadc1.Init.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4;   
        hadc1.Init.Resolution=ADC_RESOLUTION_12B;            
        hadc1.Init.DataAlign=ADC_DATAALIGN_RIGHT;            
        hadc1.Init.ScanConvMode=DISABLE;                     
        hadc1.Init.EOCSelection=DISABLE;                     
        hadc1.Init.ContinuousConvMode=DISABLE;               
        hadc1.Init.NbrOfConversion=1;                        
        hadc1.Init.DiscontinuousConvMode=DISABLE;            
        hadc1.Init.NbrOfDiscConversion=0;                     
        hadc1.Init.ExternalTrigConv=ADC_SOFTWARE_START;      
        hadc1.Init.ExternalTrigConvEdge=ADC_EXTERNALTRIGCONVEDGE_NONE;
        hadc1.Init.DMAContinuousRequests=DISABLE;            
       
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_10;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
        sConfig.Offset       = 0;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
}

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{
        GPIO_InitTypeDef          GPIO_InitStruct;
  if(adcHandle->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspInit 0 */

  /* USER CODE END ADC1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_ADC1_CLK_ENABLE();
                __HAL_RCC_GPIOC_CLK_ENABLE();

    /**ADC1 GPIO Configuration   
    PC0     ------> ADC1_IN10
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  }
}

void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
{
        /*##-1- Reset peripherals ##################################################*/
  __HAL_RCC_ADC_FORCE_RESET();
  __HAL_RCC_ADC_RELEASE_RESET();

  /*##-2- Disable peripherals and GPIO Clocks ################################*/
  /* De-initialize the ADC Channel GPIO pin */
  HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0);

}

/* USER CODE BEGIN 1 */

//ADC Value;
//ch: channel 0~16;
//return: adc single value;
double Get_Adc(uint32_t ch)   
{
    ADC_ChannelConfTypeDef ADC1_ChanConf;

    ADC1_ChanConf.Channel=ch;                                   
    ADC1_ChanConf.Rank=1;                                       
    ADC1_ChanConf.SamplingTime=ADC_SAMPLETIME_480CYCLES;        //sampling time
    ADC1_ChanConf.Offset=0;                 
    HAL_ADC_ConfigChannel(&hadc1,&ADC1_ChanConf);        
       
    HAL_ADC_Start(&hadc1);                               //start adc
       
    HAL_ADC_PollForConversion(&hadc1,10);               

        return (uint16_t)HAL_ADC_GetValue(&hadc1);                
}

//Get ADC average value;
//times:sampling times;
//return: average value;
uint16_t Get_Adc_Average(uint32_t ch,uint8_t times)
{
        uint32_t temp_val=0;
        uint8_t t;
        for(t=0;t<times;t++)
        {
                temp_val+=Get_Adc(ch);
                //delay_ms(5);
                HAL_Delay(5);
        }
        return temp_val/times;
}

/* USER CODE END 1 */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


评分

参与人数 1 ST金币 +10 收起 理由
zero99 + 10

查看全部评分

收藏 1 评论4 发布时间:2017-1-18 16:33

举报

4个回答
xhzheng 回答时间:2017-1-18 16:33:57
顶一下咯;
队长shiwo 回答时间:2017-1-20 12:47:53
谢谢分享 学习了
zero99 回答时间:2017-1-23 13:15:32
学习下
五哥1 回答时间:2017-2-6 11:29:42
cube  ,HAL库很高大上
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版