3
\$\begingroup\$

I'm trying to enable or disable two DAC outputs of STM32F407 board. STM32CubeMX tools shows the following options:

enter image description here

Above I choose both output buffers as Enable.

And the auto generated code is as follows:

static void MX_DAC_Init(void)
{

  /* USER CODE BEGIN DAC_Init 0 */

  /* USER CODE END DAC_Init 0 */

  DAC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN DAC_Init 1 */

  /* USER CODE END DAC_Init 1 */
  /** DAC Initialization
  */
  hdac.Instance = DAC;
  if (HAL_DAC_Init(&hdac) != HAL_OK)
  {
    Error_Handler();
  }
  /** DAC channel OUT1 config
  */
  sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /** DAC channel OUT2 config
  */
  if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN DAC_Init 2 */

  /* USER CODE END DAC_Init 2 */

}

And in this HAL document, it says:

enter image description here

My question is in the generated code there is no separate codes for DAC1 and DAC2. There is only:

sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;

How come one line of code sets both DAC output buffer types?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ The line of code you're looking at is only loading values into the sConfig struct. That same struct is then used in HAL_DAC_ConfigChannel for both DAC_CHANNEL_1 and DAc_CHANNEL_2. Just look at the comments in the code which CUBE_MX generated for you... \$\endgroup\$
    – brhans
    Feb 28, 2022 at 13:00

1 Answer 1

7
\$\begingroup\$

It's only a matter of how the code is written.

The structure sConfig is created once, and used twice when calling HAL_DAC_ConfigChannel(), once for each channel. Since both channels have the same settings, this works fine.

If the two channels had different settings, sConfig would have been modified between the two usages.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.