DAC out doesn't work properly on STM32H562ZI
Good morning everyone,
I need to drive an external pump using DAC out pheriperal.
I am working with an STM32H562ZI microcontroller and have followed the guide available at the following link: Getting started with DAC
Below is my current configuration:
/**
* @brief DAC1 Initialization Function
* @param None
* @retval None
*/
static void MX_DAC1_Init(void)
{
/* USER CODE BEGIN DAC1_Init 0 */
/* USER CODE END DAC1_Init 0 */
DAC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN DAC1_Init 1 */
/* USER CODE END DAC1_Init 1 */
/** DAC Initialization
*/
hdac1.Instance = DAC1;
if (HAL_DAC_Init(&hdac1) != HAL_OK)
{
Error_Handler();
}
/** DAC channel OUT1 config
*/
sConfig.DAC_HighFrequency = DAC_HIGH_FREQUENCY_INTERFACE_MODE_ABOVE_80MHZ;
sConfig.DAC_DMADoubleDataMode = DISABLE;
sConfig.DAC_SignedFormat = DISABLE;
sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_EXTERNAL;
sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN DAC1_Init 2 */
/* USER CODE END DAC1_Init 2 */
}After that, I implemented the following example and expected to obtain a stepped waveform. However, the result is not as expected: the DAC seems to skip some steps, producing an irregular curve as shown in the image below:
#include <stdbool.h>
bool setRaw(uint16_t value_raw)
{
while(HAL_DAC_STATE_READY != HAL_DAC_GetState(&hdac1));
while((hdac1.Instance->SR & DAC_SR_DAC1RDY) == 0x00);
do {
HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R,
value_raw);
} while(HAL_DAC_GetValue(&hdac1, DAC_CHANNEL_1) != value_raw);
return true;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DAC1_Init();
/* USER CODE BEGIN 2 */
HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
uint32_t timeBetweenVariation = 100;
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(timeBetweenVariation);
setRaw(0);
HAL_Delay(timeBetweenVariation);
setRaw(1000);
HAL_Delay(timeBetweenVariation);
setRaw(2000);
HAL_Delay(timeBetweenVariation);
setRaw(3000);
HAL_Delay(timeBetweenVariation);
setRaw(4095);
}
/* USER CODE END 3 */
}setRaw method performs other checks before doing HAL_DAC_SetValue. By performing the HAL_DAC_SetValue call directly doesn’t change.
On the other hand, when I run the provided example code, the generated waveform is correct:

using the example code insted seems the generated curve is correct:

Could you please help me understand what I might be missing?
Thank you in advance,
Mattia

