HAL-F429-ADC DMA 单通道

HAL-F429-ADC DMA 单通道_第1张图片

static void DMA_Config(void)
{   
  /*## -1- Enable DMA2 clock #################################################*/
  __HAL_RCC_DMA2_CLK_ENABLE();//开启时钟
	
  /*##-2- Select the DMA functional Parameters ###############################*/
  DmaHandle.Init.Channel = DMA_CHANNEL_1;                     /* DMA_CHANNEL_1 查数据手册                    */                     
  DmaHandle.Init.Direction = DMA_PERIPH_TO_MEMORY;          /* 数据传输方向               */           
  DmaHandle.Init.PeriphInc = DMA_PINC_DISABLE;               /*  只有一个外设 不用地址增量 */                 
  DmaHandle.Init.MemInc = DMA_MINC_DISABLE;                  /*  地址固定     */                   
  DmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; /* 外设数据大小为半个字 2个字节 */    
  DmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;    /* 数据大小为半个字 2个字节     */     
  DmaHandle.Init.Mode = DMA_CIRCULAR;                         /* 循环模式                 */  
  DmaHandle.Init.Priority = DMA_PRIORITY_HIGH;              /* priority level : high            */  
  DmaHandle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;            /* 直接传送模式 不使用FIFO               */        
  DmaHandle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL;	//半字突发 , 与数据大小一致  
  DmaHandle.Init.MemBurst = DMA_MBURST_SINGLE;                /* Memory burst                     */  
  DmaHandle.Init.PeriphBurst = DMA_PBURST_SINGLE;             /* Peripheral burst                 */
  
  /*##-3- Select the DMA instance to be used for the transfer : DMA2_Stream0 #*/
  DmaHandle.Instance = DMA2_Stream3;

  /*##-4- Select Callbacks functions called after Transfer complete and Transfer error */
//  //DmaHandle.StreamBaseAddress = ((uint32_t)ADC2+OX4C);//ADC外设基址
//	DmaHandle.XferCpltCallback  = TransferComplete;
//  DmaHandle.XferErrorCallback = TransferError;
//  
	__HAL_LINKDMA(&hadc2,DMA_Handle,DmaHandle);  
	//一下三个不设置同样有效果
	DmaHandle.Instance->PAR =  ((uint32_t)ADC2 + 0X4c);//外设地址
	DmaHandle.Instance->M0AR = (uint32_t)ADC_DMA_ConvertedValue;//内存缓冲地址
	DmaHandle.Instance->NDTR =2;//数据长度
	
  /*##-5- Initialize the DMA stream ##########################################*/
  if(HAL_DMA_Init(&DmaHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /*##-6- Configure NVIC for DMA transfer complete/error interrupts ##########*/
//  HAL_NVIC_SetPriority(DMA_STREAM_IRQ, 0, 0);
//  HAL_NVIC_EnableIRQ(DMA_STREAM_IRQ);

  /*##-7- Start the DMA transfer using the interrupt mode ####################*/
  /* Configure the source, destination and buffer size DMA fields and Start DMA Stream transfer */
  /* Enable All the DMA interrupts */
//测试用的是下面这行的
//HAL_DMA_Start_IT(&DmaHandle, (uint32_t)&ADC2->DR,(uint32_t)&ADC_DMA_ConvertedValue, 2)
//第四个参数为通道个数
  if(HAL_DMA_Start_IT(&DmaHandle, (uint32_t)&ADC2->DR,(uint32_t)&ADC_DMA_ConvertedValue, 1) != HAL_OK)    
  {
    /* Transfer error */
    Error_Handler();   
  }           
}
/* ADC2 init function */
static void MX_ADC2_Init(void)
{

  ADC_ChannelConfTypeDef sConfig;

	/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 
	*/
  hadc2.Instance = ADC2;	//配置ADC实例
  hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;//四分频 ,需要查文档在配置
  hadc2.Init.Resolution = ADC_RESOLUTION_12B;	//分辨率
  hadc2.Init.ScanConvMode = DISABLE;//关闭扫描模式,只采集一个通道。扫描模式:多个通道同时采集
  hadc2.Init.ContinuousConvMode = ENABLE;//连续采集模式,DISABLE :采集一次ADC就关闭了
  hadc2.Init.DiscontinuousConvMode = DISABLE;//是否转换规则通道序列组,扫描模式下无效
  hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;//禁止外部边沿触发模式
  hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;//软触发模式
  hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;//数据右对齐
  hadc2.Init.NbrOfConversion = 1;//使用到的通道个数
  //使用DMA
	hadc2.Init.DMAContinuousRequests = ENABLE;//使用DMA模式传递数据

  hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;//是否开启传输标志位
  if (HAL_ADC_Init(&hadc2) != HAL_OK)//初始化结构体
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
    */
  sConfig.Channel = ADC_CHANNEL_13;//配置通道13
  sConfig.Rank = 1;//转换等级
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;//采样时间
  if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)//初始化通道结构体
  {
    _Error_Handler(__FILE__, __LINE__);
  }
	
	
	//HAL_ADC_Start_IT(&hadc2);//开启中断
}
 while (1)
  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
	HAL_Delay(500);
         //注意这里声明的ADC_DMA_ConvertedValue类型为uint16_t
	printf("values is %d ",ADC_DMA_ConvertedValue);
  }

 

你可能感兴趣的:(F429之HAL库学习)