AnsweredAssumed Answered

How to calibrate the DACs on STM32 using the HAL library ?

Question asked by Jnana Panuganti on Feb 8, 2017
Latest reply on Feb 14, 2017 by Imen D



I am using the the HAL library for calibrating the DAC on STM32L4.


I read the documentation for  DAC on page 286 (DOCID027704 Rev 4) in HAL L4 user manual. I am still confused about how to correctly use the HAL DAC calibration APIs.


My question is how does the HAL_DACEx_SelfCalibrate() API work? Does it automatically apply the trimming offset? or do I also need to use the APIs below?




If yes should I be manually adding the TrimOffset before making a call to HAL_DAC_SetValue, something like below?

trim_offset = HAL_DACEx_GetTrimOffset(&hdac1, DAC_CHANNEL_2)

HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_8B_R, input_data + trim_offset )?


Just for reference I have posted my current code below.


Below is my current calibration function:

void DAC_SelfCalibrate(void)
   DAC_ChannelConfTypeDef sConfig;
   sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
   sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
   sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE;
   sConfig.DAC_UserTrimming = DAC_TRIMMING_USER;

   //Calibrate DAC channel 1
   if (HAL_DACEx_SelfCalibrate(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)


   //Calibrate DAC channel 2
   sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE;
   if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK)


and here is my  code to set the DAC


int set_dac_val(uint8_t input_index, uint8_t input_data)

   //set dac channel 1
   if(input_index==0) {
      HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_8B_R, input_data);
   //set dac channel 2

   else if(input_index==1)

      HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_8B_R, input_data);
   return 0;


Below is my sequence of calls for initialization.




HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
HAL_DAC_Start(&hdac1, DAC_CHANNEL_2);



I have a similar question about HAL based ADC calibration and usage of calibration factor. I will post that under a separate thread.