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

[复制链接]
463|1
 楼主| 比神乐 发表于 2023-2-26 13:35 | 显示全部楼层 |阅读模式
今天搞了一下DAC,通道1,PA4。
原理图:
2.jpg
代码:
  1. #include "main.h"

  2. /* Private includes ----------------------------------------------------------*/
  3. /* USER CODE BEGIN Includes */

  4. /* USER CODE END Includes */

  5. /* Private typedef -----------------------------------------------------------*/
  6. /* USER CODE BEGIN PTD */

  7. /* USER CODE END PTD */

  8. /* Private define ------------------------------------------------------------*/
  9. /* USER CODE BEGIN PD */

  10. /* USER CODE END PD */

  11. /* Private macro -------------------------------------------------------------*/
  12. /* USER CODE BEGIN PM */

  13. /* USER CODE END PM */

  14. /* Private variables ---------------------------------------------------------*/

  15. DAC_HandleTypeDef hdac1;

  16. /* USER CODE BEGIN PV */
  17. /* Private variables ---------------------------------------------------------*/
  18. __IO uint32_t UserButtonStatus = 0;  /* set to 1 after Wkup/Tamper push-button interrupt  */
  19. /* USER CODE END PV */

  20. /* Private function prototypes -----------------------------------------------*/
  21. void SystemClock_Config(void);
  22. static void SystemPower_Config(void);
  23. static void MX_ICACHE_Init(void);
  24. static void MX_GPIO_Init(void);
  25. static void MX_DAC1_Init(void);
  26. /* USER CODE BEGIN PFP */
  27. /* Private function prototypes -----------------------------------------------*/
  28. /* USER CODE END PFP */

  29. /* Private user code ---------------------------------------------------------*/
  30. /* USER CODE BEGIN 0 */

  31. /* USER CODE END 0 */

  32. /**
  33.   * [url=home.php?mod=space&uid=247401]@brief[/url]  The application entry point.
  34.   * @retval int
  35.   */
  36. int main(void)
  37. {
  38.   /* USER CODE BEGIN 1 */
  39.   DAC_ChannelConfTypeDef sConfig;

  40.   /* STM32U5xx HAL library initialization:
  41.        - Configure the Flash prefetch
  42.        - Configure the Systick to generate an interrupt each 1 msec
  43.        - Set NVIC Group Priority to 3
  44.        - Low Level Initialization
  45.      */
  46.   /* USER CODE END 1 */

  47.   /* MCU Configuration--------------------------------------------------------*/

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

  50.   /* USER CODE BEGIN Init */

  51.   /* USER CODE END Init */

  52.   /* Configure the system clock */
  53.   SystemClock_Config();

  54.   /* Configure the System Power */
  55.   SystemPower_Config();

  56.   /* USER CODE BEGIN SysInit */

  57.   /* USER CODE END SysInit */

  58.   /* Initialize all configured peripherals */
  59.   MX_ICACHE_Init();
  60.   MX_GPIO_Init();
  61.   MX_DAC1_Init();
  62.   /* USER CODE BEGIN 2 */

  63.   /* Configure LED3 */
  64.   BSP_LED_Init(LED3);

  65.   /* Initialize the Wkup/Tamper push-button.
  66.      It is used for changing the gain */
  67.   BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
  68.   /*##########################################################################*/
  69.   /*## NORMAL POWER MODE #####################################################*/

  70.   /*##-0- Set DAC Channel1 DHR register ######################################*/
  71.   if (HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_8B_R, 0xFF) != HAL_OK)
  72.   {
  73.     /* Setting value Error */
  74.     Error_Handler();
  75.   }

  76.   /*##-1- Enable DAC Channel1 ################################################*/
  77.   if (HAL_DAC_Start(&hdac1, DAC_CHANNEL_1) != HAL_OK)
  78.   {
  79.     /* Start Error */
  80.     Error_Handler();
  81.   }

  82.   /*##########################################################################*/
  83.   /*## LOW POWER MODE ########################################################*/

  84.   /* Wait UserButtonStatus pushed */
  85.   while (UserButtonStatus == 0)
  86.   {
  87.   }
  88.   UserButtonStatus = 0;


  89.   if (HAL_DAC_Stop(&hdac1, DAC_CHANNEL_1) != HAL_OK)
  90.   {
  91.     /* Start Error */
  92.     Error_Handler();
  93.   }

  94.   /*## DeInit the DAC peripheral ##########################################*/
  95.   if (HAL_DAC_DeInit(&hdac1) != HAL_OK)
  96.   {
  97.     /* Initialization Error */
  98.     Error_Handler();
  99.   }

  100.   /*## Configure the DAC peripheral #######################################*/
  101.   if (HAL_DAC_Init(&hdac1) != HAL_OK)
  102.   {
  103.     /* Initialization Error */
  104.     Error_Handler();
  105.   }

  106.   /*## Configure DAC channel1 #############################################*/
  107.   sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_ENABLE;

  108.   if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  109.   {
  110.     /* Channel configuration Error */
  111.     Error_Handler();
  112.   }

  113.   /*## Set DAC Channel1 DHR register ######################################*/
  114.   if (HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_8B_R, 0xFF) != HAL_OK)
  115.   {
  116.     /* Setting value Error */
  117.     Error_Handler();
  118.   }

  119.   /*## Enable DAC Channel1 ################################################*/
  120.   if (HAL_DAC_Start(&hdac1, DAC_CHANNEL_1) != HAL_OK)
  121.   {
  122.     /* Start Error */
  123.     Error_Handler();
  124.   }

  125.   /*Suspend Tick increment to prevent wakeup by Systick interrupt.
  126.   Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/
  127.   HAL_SuspendTick();

  128.   /* Enter SLEEP Mode , wake up is done once Key push button is pressed */  
  129. HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
  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.   }
  138.   /* USER CODE END 3 */
  139. }

  140. /**
  141.   * @brief System Clock Configuration
  142.   * @retval None
  143.   */
  144. void SystemClock_Config(void)
  145. {
  146.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  147.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  148.   /** Configure the main internal regulator output voltage
  149.   */
  150.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  151.   {
  152.     Error_Handler();
  153.   }

  154.   /** Initializes the CPU, AHB and APB busses clocks
  155.   */
  156.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI
  157.                               |RCC_OSCILLATORTYPE_MSI;
  158.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  159.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  160.   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  161.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  162.   RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  163.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
  164.   RCC_OscInitStruct.LSIDiv = RCC_LSI_DIV1;
  165.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  166.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  167.   RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
  168.   RCC_OscInitStruct.PLL.PLLM = 1;
  169.   RCC_OscInitStruct.PLL.PLLN = 80;
  170.   RCC_OscInitStruct.PLL.PLLP = 2;
  171.   RCC_OscInitStruct.PLL.PLLQ = 2;
  172.   RCC_OscInitStruct.PLL.PLLR = 2;
  173.   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
  174.   RCC_OscInitStruct.PLL.PLLFRACN = 0;
  175.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  176.   {
  177.     Error_Handler();
  178.   }

  179.   /** Initializes the CPU, AHB and APB busses clocks
  180.   */
  181.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  182.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  183.                               |RCC_CLOCKTYPE_PCLK3;
  184.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  185.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  186.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  187.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  188.   RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  189.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  190.   {
  191.     Error_Handler();
  192.   }
  193. }

  194. /**
  195.   * @brief Power Configuration
  196.   * @retval None
  197.   */
  198. static void SystemPower_Config(void)
  199. {

  200.   /*
  201.    * Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
  202.    */
  203.   HAL_PWREx_DisableUCPDDeadBattery();

  204.   /*
  205.    * Switch to SMPS regulator instead of LDO
  206.    */
  207.   if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
  208.   {
  209.     Error_Handler();
  210.   }
  211. }

  212. /**
  213.   * @brief DAC1 Initialization Function
  214.   * @param None
  215.   * @retval None
  216.   */
  217. static void MX_DAC1_Init(void)
  218. {

  219.   /* USER CODE BEGIN DAC1_Init 0 */

  220.   /* USER CODE END DAC1_Init 0 */

  221.   DAC_ChannelConfTypeDef sConfig = {0};
  222.   DAC_AutonomousModeConfTypeDef sAutonomousMode = {0};

  223.   /* USER CODE BEGIN DAC1_Init 1 */

  224.   /* USER CODE END DAC1_Init 1 */

  225.   /** DAC Initialization
  226.   */
  227.   hdac1.Instance = DAC1;
  228.   if (HAL_DAC_Init(&hdac1) != HAL_OK)
  229.   {
  230.     Error_Handler();
  231.   }

  232.   /** DAC channel OUT1 config
  233.   */
  234.   sConfig.DAC_HighFrequency = DAC_HIGH_FREQUENCY_INTERFACE_MODE_DISABLE;
  235.   sConfig.DAC_DMADoubleDataMode = DISABLE;
  236.   sConfig.DAC_SignedFormat = DISABLE;
  237.   sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_ENABLE;
  238.   sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  239.   sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  240.   sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_EXTERNAL;
  241.   sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
  242.   sConfig.DAC_SampleAndHoldConfig.DAC_SampleTime = 20;
  243.   sConfig.DAC_SampleAndHoldConfig.DAC_HoldTime = 10;
  244.   sConfig.DAC_SampleAndHoldConfig.DAC_RefreshTime = 5;
  245.   if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  246.   {
  247.     Error_Handler();
  248.   }

  249.   /** Configure Autonomous Mode
  250.   */
  251.   sAutonomousMode.AutonomousModeState = DAC_AUTONOMOUS_MODE_DISABLE;
  252.   if (HAL_DACEx_SetConfigAutonomousMode(&hdac1, &sAutonomousMode) != HAL_OK)
  253.   {
  254.     Error_Handler();
  255.   }
  256.   /* USER CODE BEGIN DAC1_Init 2 */

  257.   /* USER CODE END DAC1_Init 2 */

  258. }

  259. /**
  260.   * @brief ICACHE Initialization Function
  261.   * @param None
  262.   * @retval None
  263.   */
  264. static void MX_ICACHE_Init(void)
  265. {

  266.   /* USER CODE BEGIN ICACHE_Init 0 */

  267.   /* USER CODE END ICACHE_Init 0 */

  268.   /* USER CODE BEGIN ICACHE_Init 1 */

  269.   /* USER CODE END ICACHE_Init 1 */

  270.   /** Enable instruction cache in 1-way (direct mapped cache)
  271.   */
  272.   if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)
  273.   {
  274.     Error_Handler();
  275.   }
  276.   if (HAL_ICACHE_Enable() != HAL_OK)
  277.   {
  278.     Error_Handler();
  279.   }
  280.   /* USER CODE BEGIN ICACHE_Init 2 */

  281.   /* USER CODE END ICACHE_Init 2 */

  282. }

  283. /**
  284.   * @brief GPIO Initialization Function
  285.   * @param None
  286.   * @retval None
  287.   */
  288. static void MX_GPIO_Init(void)
  289. {

  290.   /* GPIO Ports Clock Enable */
  291.   __HAL_RCC_GPIOA_CLK_ENABLE();

  292. }

  293. /* USER CODE BEGIN 4 */
  294. /**
  295.   * @brief  EXTI line detection callbacks
  296.   * @param  GPIO_Pin: Specifies the pins connected EXTI line
  297.   * @retval None
  298.   */
  299. void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
  300. {
  301.   if (GPIO_Pin == BUTTON_USER_PIN)
  302.   {
  303.     UserButtonStatus = 1;
  304.   }
  305. }

  306. /* USER CODE END 4 */

  307. /**
  308.   * @brief  This function is executed in case of error occurrence.
  309.   * @retval None
  310.   */
  311. void Error_Handler(void)
  312. {
  313.   /* USER CODE BEGIN Error_Handler_Debug */
  314.   /* Turn LED3 on */
  315.   BSP_LED_On(LED3);
  316.   while(1)
  317.   {
  318.   }
  319.   /* USER CODE END Error_Handler_Debug */
  320. }

  321. #ifdef  USE_FULL_ASSERT
  322. /**
  323.   * @brief  Reports the name of the source file and the source line number
  324.   *         where the assert_param error has occurred.
  325.   * @param  file: pointer to the source file name
  326.   * @param  line: assert_param error line source number
  327.   * @retval None
  328.   */
  329. void assert_failed(uint8_t *file, uint32_t line)
  330. {
  331.   /* USER CODE BEGIN 6 */
  332.   /* User can add his own implementation to report the file name and line number,
  333.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  334.   /* USER CODE END 6 */
  335. }
  336. #endif /* USE_FULL_ASSERT */
效果图:
0.jpg 1.jpg
EmmaTT 发表于 2024-1-12 16:32 | 显示全部楼层
这个内部集成了几个dac啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

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