stm32f7 adc dma 读到的数据为0?

2019-07-20 12:22发布

[mw_shl_code=c,true]#include "dma.h"

#include "adc.h"

DMA_HandleTypeDef         ADC1_ch5_DMA_Handler;      //DMA句柄

//初始化DMA2
//这里的传输形式是固定的,这点要根据不同的情况来修改
//DMA_StreamxMA数据流,DMA1_Stream0~7/DMA2_Stream0~7
//chxMA通道选择,@ref DMA_channel DMA_CHANNEL_0~DMA_CHANNEL_7
void DMA2_ADC1_Ch0_Stream0_Init(void)
{
    __HAL_RCC_DMA2_CLK_ENABLE();//DMA2时钟使能       
   
    //DMA配置
    ADC1_ch5_DMA_Handler.Instance=DMA2_Stream0;                            //数据流0
    ADC1_ch5_DMA_Handler.Init.Channel=DMA_CHANNEL_0;                       //通道0
    ADC1_ch5_DMA_Handler.Init.Direction=DMA_PERIPH_TO_MEMORY ;             //外设到存储器
    ADC1_ch5_DMA_Handler.Init.PeriphInc=DMA_PINC_DISABLE;                  //外设非增量模式
    ADC1_ch5_DMA_Handler.Init.MemInc=DMA_MINC_DISABLE;                     //存储器非增量模式
    ADC1_ch5_DMA_Handler.Init.PeriphDataAlignment=DMA_PDATAALIGN_HALFWORD ;    //外设数据长度:32位
    ADC1_ch5_DMA_Handler.Init.MemDataAlignment=DMA_MDATAALIGN_HALFWORD ;       //存储器数据长度:32位
    ADC1_ch5_DMA_Handler.Init.Mode=DMA_CIRCULAR;                            //循环模式
    ADC1_ch5_DMA_Handler.Init.Priority=DMA_PRIORITY_MEDIUM;               //中等优先级
    ADC1_ch5_DMA_Handler.Init.FIFOMode=DMA_FIFOMODE_DISABLE;              
    ADC1_ch5_DMA_Handler.Init.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL;      
    ADC1_ch5_DMA_Handler.Init.MemBurst=DMA_MBURST_SINGLE;                 //存储器突发单次传输
    ADC1_ch5_DMA_Handler.Init.PeriphBurst=DMA_PBURST_SINGLE;              //外设突发单次传输
   
    HAL_DMA_Init(&ADC1_ch5_DMA_Handler);
       
    __HAL_LINKDMA(&ADC1_Handler,DMA_Handle,ADC1_ch5_DMA_Handler);    //将DMA与ADC1联系起来(发送DMA)
       
                /*##-4- Configure the NVIC for DMA #########################################*/
                /* NVIC configuration for DMA transfer complete interrupt */
                HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
                HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);       
}

[/mw_shl_code]


[mw_shl_code=c,true]#include "adc.h"

#include "dma.h"

ADC_HandleTypeDef ADC1_Handler;        //ADC1 句柄


uint32_t ADC1_data;        //缓冲


//adc 底层配置 时钟 IO
void HAL_ADC_MspInit (ADC_HandleTypeDef * hadc){
       
                GPIO_InitTypeDef GPIO_Initure;
       
                __HAL_RCC_ADC1_CLK_ENABLE();                        //开启ADC1时钟
                __HAL_RCC_GPIOA_CLK_ENABLE();                        //开启GPIOA时钟
       
                GPIO_Initure.Pin = GPIO_PIN_5;        //PA5
                GPIO_Initure.Mode = GPIO_MODE_ANALOG;        //模拟输入
                GPIO_Initure.Pull = GPIO_NOPULL;        //无上下拉
                HAL_GPIO_Init(GPIOA,&GPIO_Initure);        //初始化GPIO
}


//adc1 初始化
void adc1_init(void){
       
        ADC_ChannelConfTypeDef ADC1_Channel;        //ADC1通道配置
       
        ADC1_Handler.Instance = ADC1;        //ADC1
       
        ADC1_Handler.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;        //4分频
        ADC1_Handler.Init.Resolution = ADC_RESOLUTION_12B;        //12位模式
        ADC1_Handler.Init.ScanConvMode = DISABLE;        //单通道模式
        ADC1_Handler.Init.EOCSelection = DISABLE;        //关闭EOC中断
        ADC1_Handler.Init.ContinuousConvMode = ENABLE;        //开启连续转换
        ADC1_Handler.Init.DMAContinuousRequests = ENABLE;        //开启DMA
        ADC1_Handler.Init.NbrOfConversion = 1;        //规则通道数
        ADC1_Handler.Init.DiscontinuousConvMode = DISABLE;        //间断模式
        ADC1_Handler.Init.NbrOfDiscConversion =  0;        //注入通道数
        ADC1_Handler.Init.ExternalTrigConv = ADC_SOFTWARE_START;        //软件触发
        ADC1_Handler.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;        //软件触发
       
        HAL_ADC_Init (&ADC1_Handler);        //ADC1初始化
       
        //ADC通道配置
        ADC1_Channel.Channel = ADC_CHANNEL_5;        //通道5
        ADC1_Channel.Rank = 1;        //规则通道数
        ADC1_Channel.SamplingTime = ADC_SAMPLETIME_3CYCLES;        //转换时间
        ADC1_Channel.Offset = 0;        //保留

        HAL_ADC_ConfigChannel(&ADC1_Handler,&ADC1_Channel);        //通道配置
       
//        HAL_ADC_Start(&ADC1_Handler);//开启ADC       

        HAL_ADC_Start_DMA(&ADC1_Handler,&ADC1_data,1);        //开启DMA传输
}






[/mw_shl_code]

[mw_shl_code=applescript,true]#include "main.h"

#include "sys.h"
#include "delay.h"
#include "usart.h"

#include "led.h"
#include "adc.h"
#include "dma.h"

#include <string.h>

//初始化
static void init(void){
       
        Cache_Enable();                //使能CPU的L1-Cache

        HAL_Init();                                //初始化HAL库

  Stm32_Clock_Init(432,25,2,9);   //设置时钟,216Mhz
  
        delay_init(216);                //延时初始化
       
        uart_init(115200);                        //串口初始化
       
        led_init();        //初始化led
       

        DMA2_ADC1_Ch0_Stream0_Init();        //初始化dma2
       
        adc1_init();        //初始化adc1
}


int main(void)
{
        init();        //初始化
       
  while (1)
  {
                delay_ms(1000);
               
                printf("adc1 ch5:%d ",ADC1_data);
               
//                ADC1_data = 0;
        }
}




[/mw_shl_code]

[mw_shl_code=applescript,true]/**
  ******************************************************************************
  * @file    Templates/Src/stm32f7xx.c
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    22-April-2016
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&#169; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  *
  * 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 "main.h"
#include "stm32f7xx_it.h"

#include "adc.h"
   
/** @addtogroup STM32F7xx_HAL_Examples
  * @{
  */

/** @addtogroup Templates
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M7 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief   This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
  HAL_IncTick();
}

/******************************************************************************/
/*                 STM32F7xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f7xx.s).                                               */
/******************************************************************************/


void DMA2_Stream0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(ADC1_Handler.DMA_Handle);
}


/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/


/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
[/mw_shl_code]



我的ADC1在debug的时候一直在转换,也触发了DMA的发送完成中断,但我打印出来的数据为什么全部都是0啊?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。