I'm trying to enable or disable two DAC outputs of STM32F407 board. STM32CubeMX tools shows the following options:
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:
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?
1 Answer 1
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.
sConfig
struct. That same struct is then used inHAL_DAC_ConfigChannel
for bothDAC_CHANNEL_1
andDAc_CHANNEL_2
. Just look at the comments in the code which CUBE_MX generated for you... \$\endgroup\$