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

stm32f7 adc dma 读到的数据为0?

[复制链接]
whyil 提问时间:2017-8-23 19:41 /
悬赏10ST金币未解决
  1. #include "dma.h"

  2. #include "adc.h"

  3. DMA_HandleTypeDef   ADC1_ch5_DMA_Handler;      //DMA句柄

  4. //初始化DMA2
  5. //这里的传输形式是固定的,这点要根据不同的情况来修改
  6. //DMA_Streamx:DMA数据流,DMA1_Stream0~7/DMA2_Stream0~7
  7. //chx:DMA通道选择,[url=home.php?mod=space&uid=144993]@ref[/url] DMA_channel DMA_CHANNEL_0~DMA_CHANNEL_7
  8. void DMA2_ADC1_Ch0_Stream0_Init(void)
  9. {
  10.     __HAL_RCC_DMA2_CLK_ENABLE();//DMA2时钟使能  
  11.      
  12.     //DMA配置
  13.     ADC1_ch5_DMA_Handler.Instance=DMA2_Stream0;                            //数据流0
  14.     ADC1_ch5_DMA_Handler.Init.Channel=DMA_CHANNEL_0;                       //通道0
  15.     ADC1_ch5_DMA_Handler.Init.Direction=DMA_PERIPH_TO_MEMORY ;             //外设到存储器
  16.     ADC1_ch5_DMA_Handler.Init.PeriphInc=DMA_PINC_DISABLE;                  //外设非增量模式
  17.     ADC1_ch5_DMA_Handler.Init.MemInc=DMA_MINC_DISABLE;                     //存储器非增量模式
  18.     ADC1_ch5_DMA_Handler.Init.PeriphDataAlignment=DMA_PDATAALIGN_HALFWORD ;    //外设数据长度:32位
  19.     ADC1_ch5_DMA_Handler.Init.MemDataAlignment=DMA_MDATAALIGN_HALFWORD ;       //存储器数据长度:32位
  20.     ADC1_ch5_DMA_Handler.Init.Mode=DMA_CIRCULAR;                            //循环模式
  21.     ADC1_ch5_DMA_Handler.Init.Priority=DMA_PRIORITY_MEDIUM;               //中等优先级
  22.     ADC1_ch5_DMA_Handler.Init.FIFOMode=DMA_FIFOMODE_DISABLE;              
  23.     ADC1_ch5_DMA_Handler.Init.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL;      
  24.     ADC1_ch5_DMA_Handler.Init.MemBurst=DMA_MBURST_SINGLE;                 //存储器突发单次传输
  25.     ADC1_ch5_DMA_Handler.Init.PeriphBurst=DMA_PBURST_SINGLE;              //外设突发单次传输
  26.      
  27.     HAL_DMA_Init(&ADC1_ch5_DMA_Handler);
  28.      
  29.     __HAL_LINKDMA(&ADC1_Handler,DMA_Handle,ADC1_ch5_DMA_Handler);    //将DMA与ADC1联系起来(发送DMA)
  30.      
  31.         /*##-4- Configure the NVIC for DMA #########################################*/
  32.         /* NVIC configuration for DMA transfer complete interrupt */
  33.         HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
  34.         HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);  
  35. }
复制代码
  1. #include "adc.h"

  2. #include "dma.h"

  3. ADC_HandleTypeDef ADC1_Handler; //ADC1 句柄


  4. uint32_t ADC1_data; //缓冲


  5. //adc 底层配置 时钟 IO
  6. void HAL_ADC_MspInit (ADC_HandleTypeDef * hadc){
  7.      
  8.         GPIO_InitTypeDef GPIO_Initure;
  9.      
  10.         __HAL_RCC_ADC1_CLK_ENABLE();            //开启ADC1时钟
  11.         __HAL_RCC_GPIOA_CLK_ENABLE();           //开启GPIOA时钟
  12.      
  13.         GPIO_Initure.Pin = GPIO_PIN_5;  //PA5
  14.         GPIO_Initure.Mode = GPIO_MODE_ANALOG;   //模拟输入
  15.         GPIO_Initure.Pull = GPIO_NOPULL;    //无上下拉
  16.         HAL_GPIO_Init(GPIOA,&GPIO_Initure); //初始化GPIO
  17. }


  18. //adc1 初始化
  19. void adc1_init(void){
  20.      
  21.     ADC_ChannelConfTypeDef ADC1_Channel;    //ADC1通道配置
  22.      
  23.     ADC1_Handler.Instance = ADC1;   //ADC1
  24.      
  25.     ADC1_Handler.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;    //4分频
  26.     ADC1_Handler.Init.Resolution = ADC_RESOLUTION_12B;  //12位模式
  27.     ADC1_Handler.Init.ScanConvMode = DISABLE;   //单通道模式
  28.     ADC1_Handler.Init.EOCSelection = DISABLE;   //关闭EOC中断
  29.     ADC1_Handler.Init.ContinuousConvMode = ENABLE;  //开启连续转换
  30.     ADC1_Handler.Init.DMAContinuousRequests = ENABLE;   //开启DMA
  31.     ADC1_Handler.Init.NbrOfConversion = 1;  //规则通道数
  32.     ADC1_Handler.Init.DiscontinuousConvMode = DISABLE;  //间断模式
  33.     ADC1_Handler.Init.NbrOfDiscConversion =  0; //注入通道数
  34.     ADC1_Handler.Init.ExternalTrigConv = ADC_SOFTWARE_START;    //软件触发
  35.     ADC1_Handler.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; //软件触发
  36.      
  37.     HAL_ADC_Init (&ADC1_Handler);   //ADC1初始化
  38.      
  39.     //ADC通道配置
  40.     ADC1_Channel.Channel = ADC_CHANNEL_5;   //通道5
  41.     ADC1_Channel.Rank = 1;  //规则通道数
  42.     ADC1_Channel.SamplingTime = ADC_SAMPLETIME_3CYCLES; //转换时间
  43.     ADC1_Channel.Offset = 0;    //保留

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

  47.     HAL_ADC_Start_DMA(&ADC1_Handler,&ADC1_data,1);  //开启DMA传输
  48. }
复制代码
  1. #include "main.h"

  2. #include "sys.h"
  3. #include "delay.h"
  4. #include "usart.h"

  5. #include "led.h"
  6. #include "adc.h"
  7. #include "dma.h"

  8. #include <string.h>

  9. //初始化
  10. static void init(void){
  11.      
  12.     Cache_Enable();     //使能CPU的L1-Cache

  13.     HAL_Init();             //初始化HAL库

  14.   Stm32_Clock_Init(432,25,2,9);   //设置时钟,216Mhz
  15.    
  16.     delay_init(216);                //延时初始化
  17.      
  18.     uart_init(115200);              //串口初始化
  19.      
  20.     led_init(); //初始化led
  21.      

  22.     DMA2_ADC1_Ch0_Stream0_Init();   //初始化dma2
  23.      
  24.     adc1_init();    //初始化adc1
  25. }


  26. int main(void)
  27. {
  28.     init(); //初始化
  29.      
  30.   while (1)
  31.   {
  32.         delay_ms(1000);
  33.          
  34.         printf("adc1 ch5:%d\r\n",ADC1_data);
  35.          
  36. //      ADC1_data = 0;
  37.     }
  38. }
复制代码
  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=288409]@file[/url]    Templates/Src/stm32f7xx.c
  4.   * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  5.   * [url=home.php?mod=space&uid=895143]@version[/url] V1.0.0
  6.   * [url=home.php?mod=space&uid=212281]@date[/url]    22-April-2016
  7.   * @brief   Main Interrupt Service Routines.
  8.   *          This file provides template for all exceptions handler and
  9.   *          peripherals interrupt service routine.
  10.   ******************************************************************************
  11.   * @attention
  12.   *
  13.   * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  14.   *
  15.   * Redistribution and use in source and binary forms, with or without modification,
  16.   * are permitted provided that the following conditions are met:
  17.   *   1. Redistributions of source code must retain the above copyright notice,
  18.   *      this list of conditions and the following disclaimer.
  19.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  20.   *      this list of conditions and the following disclaimer in the documentation
  21.   *      and/or other materials provided with the distribution.
  22.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  23.   *      may be used to endorse or promote products derived from this software
  24.   *      without specific prior written permission.
  25.   *
  26.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.   *
  37.   ******************************************************************************
  38.   */

  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41. #include "stm32f7xx_it.h"

  42. #include "adc.h"
  43.    
  44. /** @addtogroup STM32F7xx_HAL_Examples
  45.   * @{
  46.   */

  47. /** @addtogroup Templates
  48.   * @{
  49.   */

  50. /* Private typedef -----------------------------------------------------------*/
  51. /* Private define ------------------------------------------------------------*/
  52. /* Private macro -------------------------------------------------------------*/
  53. /* Private variables ---------------------------------------------------------*/

  54. /* Private function prototypes -----------------------------------------------*/
  55. /* Private functions ---------------------------------------------------------*/

  56. /******************************************************************************/
  57. /*            Cortex-M7 Processor Exceptions Handlers                         */
  58. /******************************************************************************/

  59. /**
  60.   * @brief   This function handles NMI exception.
  61.   * @param  None
  62.   * @retval None
  63.   */
  64. void NMI_Handler(void)
  65. {
  66. }

  67. /**
  68.   * @brief  This function handles Hard Fault exception.
  69.   * @param  None
  70.   * @retval None
  71.   */
  72. void HardFault_Handler(void)
  73. {
  74.   /* Go to infinite loop when Hard Fault exception occurs */
  75.   while (1)
  76.   {
  77.   }
  78. }

  79. /**
  80.   * @brief  This function handles Memory Manage exception.
  81.   * @param  None
  82.   * @retval None
  83.   */
  84. void MemManage_Handler(void)
  85. {
  86.   /* Go to infinite loop when Memory Manage exception occurs */
  87.   while (1)
  88.   {
  89.   }
  90. }

  91. /**
  92.   * @brief  This function handles Bus Fault exception.
  93.   * @param  None
  94.   * @retval None
  95.   */
  96. void BusFault_Handler(void)
  97. {
  98.   /* Go to infinite loop when Bus Fault exception occurs */
  99.   while (1)
  100.   {
  101.   }
  102. }

  103. /**
  104.   * @brief  This function handles Usage Fault exception.
  105.   * @param  None
  106.   * @retval None
  107.   */
  108. void UsageFault_Handler(void)
  109. {
  110.   /* Go to infinite loop when Usage Fault exception occurs */
  111.   while (1)
  112.   {
  113.   }
  114. }

  115. /**
  116.   * @brief  This function handles SVCall exception.
  117.   * @param  None
  118.   * @retval None
  119.   */
  120. void SVC_Handler(void)
  121. {
  122. }

  123. /**
  124.   * @brief  This function handles Debug Monitor exception.
  125.   * @param  None
  126.   * @retval None
  127.   */
  128. void DebugMon_Handler(void)
  129. {
  130. }

  131. /**
  132.   * @brief  This function handles PendSVC exception.
  133.   * @param  None
  134.   * @retval None
  135.   */
  136. void PendSV_Handler(void)
  137. {
  138. }

  139. /**
  140.   * @brief  This function handles SysTick Handler.
  141.   * @param  None
  142.   * @retval None
  143.   */
  144. void SysTick_Handler(void)
  145. {
  146.   HAL_IncTick();
  147. }

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


  154. void DMA2_Stream0_IRQHandler(void)
  155. {
  156.   HAL_DMA_IRQHandler(ADC1_Handler.DMA_Handle);
  157. }


  158. /**
  159.   * @brief  This function handles PPP interrupt request.
  160.   * @param  None
  161.   * @retval None
  162.   */
  163. /*void PPP_IRQHandler(void)
  164. {
  165. }*/


  166. /**
  167.   * @}
  168.   */

  169. /**
  170.   * @}
  171.   */

  172. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
我的ADC1 连续转换通道5  debug的时候adc模块在转换数据也在变化  dma2的中断也触发了,但ADC1_data的数据一直都是0

收藏 2 评论2 发布时间:2017-8-23 19:41

举报

2个回答
五哥1 回答时间:2018-5-25 23:42:54
用CUBEMX直接生成的?请看看ADC1的设置是否有问题

评分

参与人数 1蝴蝶豆 +2 收起 理由
zero99 + 2

查看全部评分

tryfly 回答时间:2018-10-7 16:54:22
怎么解决的??我也用cube生成是多路ADC DMA ,都不知道怎么获得结果数值。

所属标签

相似问题

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版