DAC Output signal is inverted in STM32F767 Nucleo

Thread Starter

Xavier Pacheco Paulino

Joined Oct 21, 2015
728
Hi,

I'm having a problem with nucleo STM32F767. For example, if I write 0 to the input of the DAC, the output is 3.3V, and when I write 4095, the output is 0V.
What could be happening here?
 
Last edited:

Thread Starter

Xavier Pacheco Paulino

Joined Oct 21, 2015
728
I cannot see or think of anything that is obvious.
Can you post the DAC initialization code?
I don't see anything reasonable either. Some days ago, the DAC worked well, but suddenly it started to do that.

Code:
/* DAC init function */
static void MX_DAC_Init(void)
{

  DAC_ChannelConfTypeDef sConfig;

    /**DAC Initialization
    */
  hdac.Instance = DAC;
  if (HAL_DAC_Init(&hdac) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**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(__FILE__, __LINE__);
  }

}
Code:
HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac)
{
  /* Check DAC handle */
  if(hdac == NULL)
  {
     return HAL_ERROR;
  }
  /* Check the parameters */
  assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
 
  if(hdac->State == HAL_DAC_STATE_RESET)
  {
    /* Allocate lock resource and initialize it */
    hdac->Lock = HAL_UNLOCKED;
    /* Init the low level hardware */
    HAL_DAC_MspInit(hdac);
  }
 
  /* Initialize the DAC state*/
  hdac->State = HAL_DAC_STATE_BUSY;
 
  /* Set DAC error code to none */
  hdac->ErrorCode = HAL_DAC_ERROR_NONE;
 
  /* Initialize the DAC state*/
  hdac->State = HAL_DAC_STATE_READY;
 
  /* Return function status */
  return HAL_OK;
}
Code:
HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
{
  uint32_t tmp1 = 0, tmp2 = 0;
 
  /* Check the parameters */
  assert_param(IS_DAC_CHANNEL(Channel));
 
  /* Process locked */
  __HAL_LOCK(hdac);
 
  /* Change DAC state */
  hdac->State = HAL_DAC_STATE_BUSY;
 
  /* Enable the Peripheral */
  __HAL_DAC_ENABLE(hdac, Channel);
 
  if(Channel == DAC_CHANNEL_1)
  {
    tmp1 = hdac->Instance->CR & DAC_CR_TEN1;
    tmp2 = hdac->Instance->CR & DAC_CR_TSEL1;
    /* Check if software trigger enabled */
    if((tmp1 ==  DAC_CR_TEN1) && (tmp2 ==  DAC_CR_TSEL1))
    {
      /* Enable the selected DAC software conversion */
      hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1;
    }
  }
  else
  {
    tmp1 = hdac->Instance->CR & DAC_CR_TEN2;
    tmp2 = hdac->Instance->CR & DAC_CR_TSEL2;   
    /* Check if software trigger enabled */
    if((tmp1 == DAC_CR_TEN2) && (tmp2 == DAC_CR_TSEL2))
    {
      /* Enable the selected DAC software conversion*/
      hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG2;
    }
  }
 
  /* Change DAC state */
  hdac->State = HAL_DAC_STATE_READY;
 
  /* Process unlocked */
  __HAL_UNLOCK(hdac);
   
  /* Return function status */
  return HAL_OK;
}
 

MrChips

Joined Oct 2, 2009
29,251
I will look at your code later.

What are you using to measure your DAC output?
You don't by any chance have your oscilloscope input channel inverted?
 

MrChips

Joined Oct 2, 2009
29,251
There's your clue. You said it worked correctly some days ago. There is nothing in your code that I can see to cause an inversion. I think it has something to do with how you are making the voltage measurement. Try different values such as 1000 and 3000.
 
Top