[STM32U5] 【NUCLEO-U575ZI-Q测评】ADC测试

[复制链接]
886|2
 楼主| dql2015 发表于 2023-2-25 14:06 | 显示全部楼层 |阅读模式
根据官网资料,stm32u5内置了高性能adc,特性如下:
The devices offer one fast 14-bit ADC (2.5 Msps), one 12-bit ADC (2.5 Msps)
14-bit ADC 2.5-Msps with hardware oversampling
12-bit ADC 2.5-Msps, with hardware oversampling,


stm32cubeu5固件包里面提供了4个ADC例程:
22.png

主要函数代码:
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=288409]@file[/url]    Examples/ADC/ADC_SingleConversion_TriggerSW_IT/Src/main.c
  5.   * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  6.   * [url=home.php?mod=space&uid=247401]@brief[/url]   Use ADC to convert a single channel at each SW start.
  7.   *          Conversion performed using programming model: interrupt
  8.   *          This example is based on the STM32U5xx ADC HAL API.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * Copyright (c) 2021 STMicroelectronics.
  13.   * All rights reserved.
  14.   *
  15.   * This software is licensed under terms that can be found in the LICENSE file
  16.   * in the root directory of this software component.
  17.   * If no LICENSE file comes with this software, it is provided AS-IS.
  18.   *
  19.   ******************************************************************************
  20.   */
  21. /* USER CODE END Header */
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "main.h"

  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */

  26. /* USER CODE END Includes */

  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */

  29. /* USER CODE END PTD */

  30. /* Private define ------------------------------------------------------------*/
  31. /* USER CODE BEGIN PD */

  32. /* Definitions of environment analog values */
  33.   /* Value of analog reference voltage (Vref+), connected to analog voltage   */
  34.   /* supply Vdda (unit: mV).                                                  */
  35.   #define VDDA_APPLI                       (3300UL)

  36.   /* Init variable out of expected ADC conversion data range */
  37.   #define VAR_CONVERTED_DATA_INIT_VALUE    (__LL_ADC_DIGITAL_SCALE(ADC1, LL_ADC_RESOLUTION_12B) + 1)

  38. /* USER CODE END PD */

  39. /* Private macro -------------------------------------------------------------*/
  40. /* USER CODE BEGIN PM */

  41. /* USER CODE END PM */

  42. /* Private variables ---------------------------------------------------------*/
  43. ADC_HandleTypeDef hadc1;

  44. /* USER CODE BEGIN PV */

  45. /* Variables for ADC conversion data */
  46. __IO uint16_t uhADCxConvertedData = VAR_CONVERTED_DATA_INIT_VALUE; /* ADC group regular conversion data */

  47. /* Variables for ADC conversion data computation to physical values */
  48. uint16_t uhADCxConvertedData_Voltage_mVolt = 0;  /* Value of voltage calculated from ADC conversion data (unit: mV) */

  49. /* Variable to report status of ADC group regular unitary conversion          */
  50. /*  0: ADC group regular unitary conversion is not completed                  */
  51. /*  1: ADC group regular unitary conversion is completed                      */
  52. /*  2: ADC group regular unitary conversion has not been started yet          */
  53. /*     (initial state)                                                        */
  54. __IO uint8_t ubAdcGrpRegularUnitaryConvStatus = 2; /* Variable set into ADC interruption callback */

  55. /* USER CODE END PV */

  56. /* Private function prototypes -----------------------------------------------*/
  57. void SystemClock_Config(void);
  58. static void SystemPower_Config(void);
  59. static void MX_GPIO_Init(void);
  60. static void MX_ICACHE_Init(void);
  61. static void MX_ADC1_Init(void);
  62. /* USER CODE BEGIN PFP */

  63. /* USER CODE END PFP */

  64. /* Private user code ---------------------------------------------------------*/
  65. /* USER CODE BEGIN 0 */

  66. /* USER CODE END 0 */

  67. /**
  68.   * @brief  The application entry point.
  69.   * @retval int
  70.   */
  71. int main(void)
  72. {
  73.   /* USER CODE BEGIN 1 */

  74.   /* USER CODE END 1 */

  75.   /* MCU Configuration--------------------------------------------------------*/

  76.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  77.   HAL_Init();

  78.   /* USER CODE BEGIN Init */

  79.   /* USER CODE END Init */

  80.   /* Configure the system clock */
  81.   SystemClock_Config();

  82.   /* Configure the System Power */
  83.   SystemPower_Config();

  84.   /* USER CODE BEGIN SysInit */

  85.   /* USER CODE END SysInit */

  86.   /* Initialize all configured peripherals */
  87.   MX_GPIO_Init();
  88.   MX_ICACHE_Init();
  89.   MX_ADC1_Init();
  90.   /* USER CODE BEGIN 2 */

  91.   /* Initialize LED on board */
  92.   BSP_LED_Init(LED1);

  93.   /* Perform ADC calibration */
  94.   if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK)
  95.   {
  96.     /* Calibration Error */
  97.     Error_Handler();
  98.   }

  99.   /* USER CODE END 2 */

  100.   /* Infinite loop */
  101.   /* USER CODE BEGIN WHILE */
  102.   while (1)
  103.   {
  104.     /* Start ADC group regular conversion */
  105.     if (HAL_ADC_Start_IT(&hadc1) != HAL_OK)
  106.     {
  107.       /* Error: ADC conversion start could not be performed */
  108.       Error_Handler();
  109.     }

  110.     /* For this example purpose, wait until conversion is done */
  111.     while (ubAdcGrpRegularUnitaryConvStatus != 1);

  112.     /* Reset status variable of ADC group regular unitary conversion */
  113.     ubAdcGrpRegularUnitaryConvStatus = 0;

  114.     /* Toggle LED at each ADC conversion */
  115.     BSP_LED_On(LED1);
  116.     HAL_Delay(LED_BLINK_SLOW);
  117.     BSP_LED_Off(LED1);
  118.     HAL_Delay(LED_BLINK_SLOW);

  119.     /* Note: ADC group regular conversions data are stored into array         */
  120.     /*       "uhADCxConvertedData"                                            */
  121.     /*       (for debug: see variable content into watch window).             */

  122.     /* Note: ADC conversion data are computed to physical values              */
  123.     /*       into array "uhADCxConvertedData_Voltage_mVolt" using             */
  124.     /*       ADC LL driver helper macro "__LL_ADC_CALC_DATA_TO_VOLTAGE()"     */
  125.     /*       (for debug: see variable content with debugger)                  */
  126.     /*       in IRQ handler callback function.                                */

  127.     /* USER CODE END WHILE */

  128.     /* USER CODE BEGIN 3 */
  129.   }
  130.   /* USER CODE END 3 */
  131. }

  132. /**
  133.   * @brief System Clock Configuration
  134.   * @retval None
  135.   */
  136. void SystemClock_Config(void)
  137. {
  138.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  139.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  140.   /** Configure the main internal regulator output voltage
  141.   */
  142.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  143.   {
  144.     Error_Handler();
  145.   }

  146.   /** Initializes the CPU, AHB and APB busses clocks
  147.   */
  148.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;
  149.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  150.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  151.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  152.   RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  153.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
  154.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  155.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  156.   RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
  157.   RCC_OscInitStruct.PLL.PLLM = 1;
  158.   RCC_OscInitStruct.PLL.PLLN = 80;
  159.   RCC_OscInitStruct.PLL.PLLP = 2;
  160.   RCC_OscInitStruct.PLL.PLLQ = 2;
  161.   RCC_OscInitStruct.PLL.PLLR = 2;
  162.   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
  163.   RCC_OscInitStruct.PLL.PLLFRACN = 0;
  164.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  165.   {
  166.     Error_Handler();
  167.   }

  168.   /** Initializes the CPU, AHB and APB busses clocks
  169.   */
  170.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  171.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  172.                               |RCC_CLOCKTYPE_PCLK3;
  173.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  174.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  175.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  176.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  177.   RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  178.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  179.   {
  180.     Error_Handler();
  181.   }
  182. }

  183. /**
  184.   * @brief Power Configuration
  185.   * @retval None
  186.   */
  187. static void SystemPower_Config(void)
  188. {

  189.   /*
  190.    * Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
  191.    */
  192.   HAL_PWREx_DisableUCPDDeadBattery();

  193.   /*
  194.    * Switch to SMPS regulator instead of LDO
  195.    */
  196.   if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
  197.   {
  198.     Error_Handler();
  199.   }
  200. }

  201. /**
  202.   * @brief ADC1 Initialization Function
  203.   * @param None
  204.   * @retval None
  205.   */
  206. static void MX_ADC1_Init(void)
  207. {

  208.   /* USER CODE BEGIN ADC1_Init 0 */

  209.   /* USER CODE END ADC1_Init 0 */

  210.   ADC_ChannelConfTypeDef sConfig = {0};

  211.   /* USER CODE BEGIN ADC1_Init 1 */

  212.   /* USER CODE END ADC1_Init 1 */

  213.   /** Common config
  214.   */
  215.   hadc1.Instance = ADC1;
  216.   hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
  217.   hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  218.   hadc1.Init.GainCompensation = 0;
  219.   hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  220.   hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  221.   hadc1.Init.LowPowerAutoWait = DISABLE;
  222.   hadc1.Init.ContinuousConvMode = DISABLE;
  223.   hadc1.Init.NbrOfConversion = 1;
  224.   hadc1.Init.DiscontinuousConvMode = DISABLE;
  225.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  226.   hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  227.   hadc1.Init.DMAContinuousRequests = DISABLE;
  228.   hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
  229.   hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
  230.   hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  231.   hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
  232.   hadc1.Init.OversamplingMode = DISABLE;
  233.   if (HAL_ADC_Init(&hadc1) != HAL_OK)
  234.   {
  235.     Error_Handler();
  236.   }

  237.   /** Configure Regular Channel
  238.   */
  239.   sConfig.Channel = ADC_CHANNEL_9;
  240.   sConfig.Rank = ADC_REGULAR_RANK_1;
  241.   sConfig.SamplingTime = ADC_SAMPLETIME_391CYCLES_5;
  242.   sConfig.SingleDiff = ADC_SINGLE_ENDED;
  243.   sConfig.OffsetNumber = ADC_OFFSET_NONE;
  244.   sConfig.Offset = 0;
  245.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  246.   {
  247.     Error_Handler();
  248.   }
  249.   /* USER CODE BEGIN ADC1_Init 2 */

  250.   /* USER CODE END ADC1_Init 2 */

  251. }

  252. /**
  253.   * @brief ICACHE Initialization Function
  254.   * @param None
  255.   * @retval None
  256.   */
  257. static void MX_ICACHE_Init(void)
  258. {

  259.   /* USER CODE BEGIN ICACHE_Init 0 */

  260.   /* USER CODE END ICACHE_Init 0 */

  261.   /* USER CODE BEGIN ICACHE_Init 1 */

  262.   /* USER CODE END ICACHE_Init 1 */

  263.   /** Enable instruction cache in 1-way (direct mapped cache)
  264.   */
  265.   if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)
  266.   {
  267.     Error_Handler();
  268.   }
  269.   if (HAL_ICACHE_Enable() != HAL_OK)
  270.   {
  271.     Error_Handler();
  272.   }
  273.   /* USER CODE BEGIN ICACHE_Init 2 */

  274.   /* USER CODE END ICACHE_Init 2 */

  275. }

  276. /**
  277.   * @brief GPIO Initialization Function
  278.   * @param None
  279.   * @retval None
  280.   */
  281. static void MX_GPIO_Init(void)
  282. {
  283.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  284.   /* GPIO Ports Clock Enable */
  285.   __HAL_RCC_GPIOA_CLK_ENABLE();
  286.   __HAL_RCC_GPIOC_CLK_ENABLE();

  287.   /*Configure GPIO pin Output Level */
  288.   HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);

  289.   /*Configure GPIO pin : LED1_Pin */
  290.   GPIO_InitStruct.Pin = LED1_Pin;
  291.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  292.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  293.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  294.   HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);

  295. }

  296. /* USER CODE BEGIN 4 */

  297. /******************************************************************************/
  298. /*   USER IRQ HANDLER TREATMENT                                               */
  299. /******************************************************************************/

  300. /**
  301.   * @brief  Conversion transfer complete callback
  302.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]   This function is executed when the transfer complete interrupt
  303.   *         is generated
  304.   * @retval None
  305.   */
  306. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
  307. {
  308.   /* Retrieve ADC conversion data */
  309.   uhADCxConvertedData = HAL_ADC_GetValue(hadc);

  310.   /* Computation of ADC conversions raw data to physical values           */
  311.   /* using helper macro.                                                  */
  312.   uhADCxConvertedData_Voltage_mVolt = __LL_ADC_CALC_DATA_TO_VOLTAGE(ADC1, VDDA_APPLI, uhADCxConvertedData, LL_ADC_RESOLUTION_12B);

  313.   /* Update status variable of ADC unitary conversion                     */
  314.   ubAdcGrpRegularUnitaryConvStatus = 1;
  315. }

  316. /**
  317.   * @brief  ADC error interruption callback
  318.   * @retval None
  319.   */
  320. void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  321. {
  322.   /* Note: Disable ADC interruption that caused this error before entering in
  323.            infinite loop below. */

  324.   /* In case of error due to overrun: Disable ADC group regular overrun interruption */
  325.   LL_ADC_DisableIT_OVR(ADC1);

  326.   /* Error reporting */
  327.   Error_Handler();
  328. }

  329. /* USER CODE END 4 */

  330. /**
  331.   * @brief  This function is executed in case of error occurrence.
  332.   * @retval None
  333.   */
  334. void Error_Handler(void)
  335. {
  336.   /* USER CODE BEGIN Error_Handler_Debug */
  337.   /* User can add his own implementation to report the HAL error return state */

  338.   /* Turn on LED and remain in infinite loop */
  339.   while (1)
  340.   {
  341.     BSP_LED_On(LED1);
  342.   }
  343.   /* USER CODE END Error_Handler_Debug */
  344. }

  345. #ifdef  USE_FULL_ASSERT
  346. /**
  347.   * @brief  Reports the name of the source file and the source line number
  348.   *         where the assert_param error has occurred.
  349.   * @param  file: pointer to the source file name
  350.   * @param  line: assert_param error line source number
  351.   * @retval None
  352.   */
  353. void assert_failed(uint8_t *file, uint32_t line)
  354. {
  355.   /* USER CODE BEGIN 6 */

  356.   /* User can add his own implementation to report the file name and line number,
  357.      ex: printf("Wrong parameters value: file %s on line %d", file, line) */

  358.   /* Infinite loop */
  359.   while (1)
  360.   {
  361.   }
  362.   /* USER CODE END 6 */
  363. }
  364. #endif /* USE_FULL_ASSERT */

测试效果:
11.png
Jacquetry 发表于 2024-1-12 16:02 | 显示全部楼层
内置adc精度怎么样啊
Jacquetry 发表于 2024-1-12 16:12 | 显示全部楼层
能不能代替外置的adc?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

104

主题

384

帖子

8

粉丝
快速回复 在线客服 返回列表 返回顶部