zoukankan      html  css  js  c++  java
  • STM32基于LL库——DMA传输双路ADC数据

    /**
      ******************************************************************************
      * File Name          : ADC.c
      * Description        : This file provides code for the configuration
      *                      of the ADC instances.
      ******************************************************************************
      * @attention
      *
      * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
      * All rights reserved.</center></h2>
      *
      * This software component is licensed by ST under BSD 3-Clause license,
      * the "License"; You may not use this file except in compliance with the
      * License. You may obtain a copy of the License at:
      *                        opensource.org/licenses/BSD-3-Clause
      *
      ******************************************************************************
      */
    
    /* Includes ------------------------------------------------------------------*/
    #include "adc.h"
    
    /* USER CODE BEGIN 0 */
    uint16_t ADC_ConvertedValue[2]={0,0};
    /* USER CODE END 0 */
    
    /* ADC1 init function */
    void MX_ADC1_Init(void)
    {
      LL_ADC_InitTypeDef ADC_InitStruct = {0};
      LL_ADC_CommonInitTypeDef ADC_CommonInitStruct = {0};
      LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
    
      LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
    
      /* Peripheral clock enable */
      LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
      
      LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
      /**ADC1 GPIO Configuration  
      PA1   ------> ADC1_IN1
      PA2   ------> ADC1_IN2 
      */
      GPIO_InitStruct.Pin = LL_GPIO_PIN_1|LL_GPIO_PIN_2;
      GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
      LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
      /* ADC1 DMA Init */
      
      /* ADC1 Init */
      LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_1, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
    
      LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PRIORITY_HIGH);
    
      LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
    
      LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PERIPH_NOINCREMENT);
    
      LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
    
      LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_HALFWORD);
    
      LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_HALFWORD);
    
      /* ADC1 interrupt Init */
      NVIC_SetPriority(ADC1_2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
      NVIC_EnableIRQ(ADC1_2_IRQn);
    
      /** Common config 
      */
      ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
      ADC_InitStruct.SequencersScanMode = LL_ADC_SEQ_SCAN_ENABLE;
      LL_ADC_Init(ADC1, &ADC_InitStruct);
      ADC_CommonInitStruct.Multimode = LL_ADC_MULTI_INDEPENDENT;
      LL_ADC_CommonInit(__LL_ADC_COMMON_INSTANCE(ADC1), &ADC_CommonInitStruct);
      ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
      ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_ENABLE_2RANKS;
      ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
      ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_CONTINUOUS;
      ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_UNLIMITED;
      LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
      /** Configure Regular Channel 
      */
      LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_1);
      LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_1, LL_ADC_SAMPLINGTIME_1CYCLE_5);
      /** Configure Regular Channel 
      */
      LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_2, LL_ADC_CHANNEL_2);
      LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_2, LL_ADC_SAMPLINGTIME_1CYCLE_5);
    
    }
    
    /* USER CODE BEGIN 1 */
    
    void ADC_DMA_Config_Start(void)
    {
    	LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PRIORITY_LOW);
    	LL_DMA_SetMemoryAddress(DMA1,LL_DMA_CHANNEL_1,(uint32_t)ADC_ConvertedValue);
    	LL_DMA_SetPeriphAddress(DMA1,LL_DMA_CHANNEL_1,LL_ADC_DMA_GetRegAddr(ADC1,LL_ADC_DMA_REG_REGULAR_DATA));
    	LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_1, sizeof(ADC_ConvertedValue));
    	LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_1);
    	
    	LL_ADC_Enable(ADC1);
    	LL_ADC_StartCalibration(ADC1);
    	while(LL_ADC_IsCalibrationOnGoing(ADC1));
    	
    	LL_ADC_REG_StartConversionSWStart(ADC1);
    }
    
    /* USER CODE END 1 */
    
    /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    
    
    /**
      ******************************************************************************
      * File Name          : dma.c
      * Description        : This file provides code for the configuration
      *                      of all the requested memory to memory DMA transfers.
      ******************************************************************************
      * @attention
      *
      * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
      * All rights reserved.</center></h2>
      *
      * This software component is licensed by ST under BSD 3-Clause license,
      * the "License"; You may not use this file except in compliance with the
      * License. You may obtain a copy of the License at:
      *                        opensource.org/licenses/BSD-3-Clause
      *
      ******************************************************************************
      */
    
    /* Includes ------------------------------------------------------------------*/
    #include "dma.h"
    
    /* USER CODE BEGIN 0 */
    
    /* USER CODE END 0 */
    
    /*----------------------------------------------------------------------------*/
    /* Configure DMA                                                              */
    /*----------------------------------------------------------------------------*/
    
    /* USER CODE BEGIN 1 */
    
    /* USER CODE END 1 */
    
    /** 
      * Enable DMA controller clock
      */
    void MX_DMA_Init(void) 
    {
    
      /* Init with LL driver */
      /* DMA controller clock enable */
      LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
    
      /* DMA interrupt init */
      /* DMA1_Channel1_IRQn interrupt configuration */
      NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
      NVIC_EnableIRQ(DMA1_Channel1_IRQn);
    
    }
    
    /* USER CODE BEGIN 2 */
    
    /* USER CODE END 2 */
    
    /**
      * @}
      */
    
    /**
      * @}
      */
    
    /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    
    
    /* USER CODE BEGIN Header */
    /**
      ******************************************************************************
      * @file           : main.c
      * @brief          : Main program body
      ******************************************************************************
      * @attention
      *
      * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
      * All rights reserved.</center></h2>
      *
      * This software component is licensed by ST under BSD 3-Clause license,
      * the "License"; You may not use this file except in compliance with the
      * License. You may obtain a copy of the License at:
      *                        opensource.org/licenses/BSD-3-Clause
      *
      ******************************************************************************
      */
    /* USER CODE END Header */
    
    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    #include "adc.h"
    #include "dma.h"
    #include "tim.h"
    #include "usart.h"
    #include "gpio.h"
    
    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    
    /* USER CODE END Includes */
    
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
    
    /* USER CODE END PTD */
    
    /* Private define ------------------------------------------------------------*/
    /* USER CODE BEGIN PD */
    /* USER CODE END PD */
    
    /* Private macro -------------------------------------------------------------*/
    /* USER CODE BEGIN PM */
    
    /* USER CODE END PM */
    
    /* Private variables ---------------------------------------------------------*/
    
    /* USER CODE BEGIN PV */
    float ADC_ConvertedValueLocal[2]; 
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    /* USER CODE BEGIN PFP */
    
    /* USER CODE END PFP */
    
    /* Private user code ---------------------------------------------------------*/
    /* USER CODE BEGIN 0 */
    
    /* USER CODE END 0 */
    
    /**
      * @brief  The application entry point.
      * @retval int
      */
    int main(void)
    {
      /* USER CODE BEGIN 1 */
    
      /* USER CODE END 1 */
    
      /* MCU Configuration--------------------------------------------------------*/
    
      /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
      
    
      LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_AFIO);
      LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
    
      NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
    
      /* System interrupt init*/
    
      /** DISABLE: JTAG-DP Disabled and SW-DP Disabled 
      */
      LL_GPIO_AF_DisableRemap_SWJ();
    
      /* USER CODE BEGIN Init */
    
      /* USER CODE END Init */
    
      /* Configure the system clock */
      SystemClock_Config();
    
      /* USER CODE BEGIN SysInit */
    
      /* USER CODE END SysInit */
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
      MX_DMA_Init();
      MX_ADC1_Init();
      MX_TIM3_Init();
      MX_USART1_UART_Init();
      /* USER CODE BEGIN 2 */
    	printf("STM32CubeMX Test...
    ");
    	ADC_DMA_Config_Start();
    	LL_TIM_EnableIT_UPDATE(TIM3);
    	LL_TIM_EnableCounter(TIM3);
      /* USER CODE END 2 */
    
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
      while (1)
      {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
    		LL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);
    		LL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
    		LL_mDelay(200);
    		printf("ADC->CH1 Value:%d
    ",ADC_ConvertedValue[0]);
    		printf("ADC->CH2 Value:%d
    ",ADC_ConvertedValue[1]);
    		LL_mDelay(200);
      }
      /* USER CODE END 3 */
    }
    
    /**
      * @brief System Clock Configuration
      * @retval None
      */
    void SystemClock_Config(void)
    {
      LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
    
       if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
      {
        Error_Handler();  
      }
      LL_RCC_HSE_Enable();
    
       /* Wait till HSE is ready */
      while(LL_RCC_HSE_IsReady() != 1)
      {
        
      }
      LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
      LL_RCC_PLL_Enable();
    
       /* Wait till PLL is ready */
      while(LL_RCC_PLL_IsReady() != 1)
      {
        
      }
      LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
      LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
      LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
      LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
    
       /* Wait till System clock is ready */
      while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
      {
      
      }
      LL_Init1msTick(72000000);
      LL_SetSystemCoreClock(72000000);
      LL_RCC_SetADCClockSource(LL_RCC_ADC_CLKSRC_PCLK2_DIV_8);
    }
    
    /* USER CODE BEGIN 4 */
    
    /* USER CODE END 4 */
    
    /**
      * @brief  This function is executed in case of error occurrence.
      * @retval None
      */
    void Error_Handler(void)
    {
      /* USER CODE BEGIN Error_Handler_Debug */
      /* User can add his own implementation to report the HAL error return state */
    
      /* USER CODE END Error_Handler_Debug */
    }
    
    #ifdef  USE_FULL_ASSERT
    /**
      * @brief  Reports the name of the source file and the source line number
      *         where the assert_param error has occurred.
      * @param  file: pointer to the source file name
      * @param  line: assert_param error line source number
      * @retval None
      */
    void assert_failed(uint8_t *file, uint32_t line)
    { 
      /* USER CODE BEGIN 6 */
      /* User can add his own implementation to report the file name and line number,
         tex: printf("Wrong parameters value: file %s on line %d
    ", file, line) */
      /* USER CODE END 6 */
    }
    #endif /* USE_FULL_ASSERT */
    
    /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    
    
  • 相关阅读:
    phpQuery—基于jQuery的PHP实现
    php 知乎爬虫
    windows下安装php5.5的redis扩展
    Redis 安装
    使用AngularJS创建应用的5个框架
    Redis能干啥?细看11种Web应用场景
    前端开发必须知道的JS之闭包及应用
    javascript深入理解js闭包
    day16<集合框架+>
    day15<集合框架>
  • 原文地址:https://www.cnblogs.com/hhsxy/p/14018394.html
Copyright © 2011-2022 走看看