[STM32U5] 【NUCLEO-U575ZI-Q测评】DAC

[复制链接]
602|1
 楼主| dql2015 发表于 2023-2-25 12:42 | 显示全部楼层 |阅读模式
STM32U5内置了DAC、运算放大器等常用模拟外设,其中运放具有多种输入源、可以配置成多种工作模式,这里将运放配置为电压跟随器模式,输入信号为DAC输出,这样能够提高带负载能力。
DAC配置:
11.png
运放配置:
22.png
主要代码:
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=288409]@file[/url]           : main.c
  5.   * [url=home.php?mod=space&uid=247401]@brief[/url]          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2023 STMicroelectronics.
  10.   * All rights reserved.
  11.   *
  12.   * This software is licensed under terms that can be found in the LICENSE file
  13.   * in the root directory of this software component.
  14.   * If no LICENSE file comes with this software, it is provided AS-IS.
  15.   *
  16.   ******************************************************************************
  17.   */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "dac.h"
  23. #include "opamp.h"
  24. #include "ucpd.h"
  25. #include "usart.h"
  26. #include "usb_otg.h"
  27. #include "gpio.h"

  28. /* Private includes ----------------------------------------------------------*/
  29. /* USER CODE BEGIN Includes */

  30. /* USER CODE END Includes */

  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */

  33. /* USER CODE END PTD */

  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. /* USER CODE END PD */

  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */

  39. /* USER CODE END PM */

  40. /* Private variables ---------------------------------------------------------*/

  41. /* USER CODE BEGIN PV */
  42. extern int32_t fft_test(void);

  43. uint32_t Wave_LUT[128] = {
  44.     2048, 2149, 2250, 2350, 2450, 2549, 2646, 2742, 2837, 2929, 3020, 3108, 3193, 3275, 3355,
  45.     3431, 3504, 3574, 3639, 3701, 3759, 3812, 3861, 3906, 3946, 3982, 4013, 4039, 4060, 4076,
  46.     4087, 4094, 4095, 4091, 4082, 4069, 4050, 4026, 3998, 3965, 3927, 3884, 3837, 3786, 3730,
  47.     3671, 3607, 3539, 3468, 3394, 3316, 3235, 3151, 3064, 2975, 2883, 2790, 2695, 2598, 2500,
  48.     2400, 2300, 2199, 2098, 1997, 1896, 1795, 1695, 1595, 1497, 1400, 1305, 1212, 1120, 1031,
  49.     944, 860, 779, 701, 627, 556, 488, 424, 365, 309, 258, 211, 168, 130, 97,
  50.     69, 45, 26, 13, 4, 0, 1, 8, 19, 35, 56, 82, 113, 149, 189,
  51.     234, 283, 336, 394, 456, 521, 591, 664, 740, 820, 902, 987, 1075, 1166, 1258,
  52.     1353, 1449, 1546, 1645, 1745, 1845, 1946, 2047
  53. };

  54. /* USER CODE END PV */

  55. /* Private function prototypes -----------------------------------------------*/
  56. void SystemClock_Config(void);
  57. static void SystemPower_Config(void);
  58. /* USER CODE BEGIN PFP */

  59. /* USER CODE END PFP */

  60. /* Private user code ---------------------------------------------------------*/
  61. /* USER CODE BEGIN 0 */
  62. uint32_t TM_DELAY_Init(void)
  63. {
  64.   uint32_t c;     
  65.         /* Enable TRC */
  66.         CoreDebug->DEMCR &= ~0x01000000;
  67.         CoreDebug->DEMCR |=  0x01000000;

  68.         /* Enable counter */
  69.         DWT->CTRL &= ~0x00000001;
  70.         DWT->CTRL |=  0x00000001;

  71.         /* Reset counter */
  72.         DWT->CYCCNT = 0;

  73.         /* Check if DWT has started */
  74.         c = DWT->CYCCNT;

  75.         /* 2 dummys */
  76.         __ASM volatile ("NOP");
  77.         __ASM volatile ("NOP");

  78.         /* Return difference, if result is zero, DWT has not started */
  79.         return (DWT->CYCCNT - c);
  80. }
  81. void Delay(__IO uint32_t micros)
  82. {
  83.         uint32_t start = DWT->CYCCNT;

  84.         /* Go to number of cycles for system */
  85.         micros *= (HAL_RCC_GetHCLKFreq() / 1000000);

  86.         /* Delay till end */
  87.         while ((DWT->CYCCNT - start) < micros);
  88. }
  89. /* USER CODE END 0 */

  90. /**
  91.   * @brief  The application entry point.
  92.   * @retval int
  93.   */
  94. int main(void)
  95. {
  96.   /* USER CODE BEGIN 1 */

  97.   /* USER CODE END 1 */

  98.   /* MCU Configuration--------------------------------------------------------*/

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

  101.   /* USER CODE BEGIN Init */

  102.   /* USER CODE END Init */

  103.   /* Configure the system clock */
  104.   SystemClock_Config();

  105.   /* Configure the System Power */
  106.   SystemPower_Config();

  107.   /* USER CODE BEGIN SysInit */

  108.   /* USER CODE END SysInit */

  109.   /* Initialize all configured peripherals */
  110.   MX_GPIO_Init();
  111.   MX_UCPD1_Init();
  112.   MX_USART1_UART_Init();
  113.   MX_USB_OTG_FS_PCD_Init();
  114.   MX_ADC1_Init();
  115.   MX_OPAMP1_Init();
  116.   MX_DAC1_Init();
  117.   /* USER CODE BEGIN 2 */
  118.         HAL_OPAMP_Start(&hopamp1);
  119.         HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
  120.         HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 2000);
  121.        
  122.         EventRecorderInitialize(EventRecordAll, 1U);
  123.         EventRecorderStart();

  124.         TM_DELAY_Init();
  125.        
  126.         printf("__FPU_PRESENT=%d __FPU_USED=%d\r\n",__FPU_PRESENT,__FPU_USED);
  127.        
  128.         //arm_cfft_f32_app();

  129.         int i=0;
  130.   /* USER CODE END 2 */

  131.   /* Infinite loop */
  132.   /* USER CODE BEGIN WHILE */
  133.   while (1)
  134.   {
  135.     /* USER CODE END WHILE */

  136.     /* USER CODE BEGIN 3 */
  137.                 //for(int i=0;i<128;i++)
  138.                 {
  139.                         HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, i);
  140.                         i+=100;
  141.                         if(i>4095)
  142.                                 i=0;
  143.                         HAL_Delay(100);
  144.                 }
  145.                 //i=0;       
  146.   }
  147.   /* USER CODE END 3 */
  148. }

  149. /**
  150.   * @brief System Clock Configuration
  151.   * @retval None
  152.   */
  153. void SystemClock_Config(void)
  154. {
  155.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  156.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  157.   /** Configure the main internal regulator output voltage
  158.   */
  159.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  160.   {
  161.     Error_Handler();
  162.   }

  163.   /** Initializes the CPU, AHB and APB buses clocks
  164.   */
  165.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI
  166.                               |RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_MSI;
  167.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  168.   RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  169.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  170.   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  171.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  172.   RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  173.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
  174.   RCC_OscInitStruct.LSIDiv = RCC_LSI_DIV1;
  175.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  176.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  177.   RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
  178.   RCC_OscInitStruct.PLL.PLLM = 1;
  179.   RCC_OscInitStruct.PLL.PLLN = 80;
  180.   RCC_OscInitStruct.PLL.PLLP = 2;
  181.   RCC_OscInitStruct.PLL.PLLQ = 2;
  182.   RCC_OscInitStruct.PLL.PLLR = 2;
  183.   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
  184.   RCC_OscInitStruct.PLL.PLLFRACN = 0;
  185.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  186.   {
  187.     Error_Handler();
  188.   }

  189.   /** Initializes the CPU, AHB and APB buses clocks
  190.   */
  191.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  192.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  193.                               |RCC_CLOCKTYPE_PCLK3;
  194.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  195.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  196.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  197.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  198.   RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  199.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  200.   {
  201.     Error_Handler();
  202.   }
  203. }

  204. /**
  205.   * @brief Power Configuration
  206.   * @retval None
  207.   */
  208. static void SystemPower_Config(void)
  209. {
  210.   HAL_PWREx_EnableVddIO2();

  211.   /*
  212.    * Switch to SMPS regulator instead of LDO
  213.    */
  214.   if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
  215.   {
  216.     Error_Handler();
  217.   }
  218. }

  219. /* USER CODE BEGIN 4 */

  220. /* USER CODE END 4 */

  221. /**
  222.   * @brief  This function is executed in case of error occurrence.
  223.   * @retval None
  224.   */
  225. void Error_Handler(void)
  226. {
  227.   /* USER CODE BEGIN Error_Handler_Debug */
  228.   /* User can add his own implementation to report the HAL error return state */
  229.   __disable_irq();
  230.   while (1)
  231.   {
  232.   }
  233.   /* USER CODE END Error_Handler_Debug */
  234. }

  235. #ifdef  USE_FULL_ASSERT
  236. /**
  237.   * @brief  Reports the name of the source file and the source line number
  238.   *         where the assert_param error has occurred.
  239.   * @param  file: pointer to the source file name
  240.   * @param  line: assert_param error line source number
  241.   * @retval None
  242.   */
  243. void assert_failed(uint8_t *file, uint32_t line)
  244. {
  245.   /* USER CODE BEGIN 6 */
  246.   /* User can add his own implementation to report the file name and line number,
  247.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  248.   /* USER CODE END 6 */
  249. }
  250. #endif /* USE_FULL_ASSERT */


Undshing 发表于 2024-1-12 16:28 | 显示全部楼层
这个dac跟pwm有什么区别啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

104

主题

384

帖子

8

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