Skip to main content
Visitor II
August 3, 2020
Solved

How to get second DAC working on F072RB board

  • August 3, 2020
  • 3 replies
  • 1851 views

I'm working on a simple, bare metal application where right now I am passing in values read from the two ADCs and then write the same values to the corresponding DACs. I have confirmed by stepping through the code and using a volt meter.

The configuration of the DAC 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 */

}

My pass through function is as follows:

void PassThrough(void)

{

  uint16_t value;

  HAL_ADC_Start(&hadc);

  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);

  value = HAL_ADC_GetValue(&hadc);

  HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value);

  HAL_ADC_Start(&hadc);

  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);

  value = HAL_ADC_GetValue(&hadc);

  HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value);

}

The value in DAC2 _OUT is garbage. There is something fundamentally incorrect in the configuration. I've tried figuring it out by searching online and in the samples that I downloaded. Any times greatly appreciated.

    This topic has been closed for replies.
    Best answer by waclawek.jan

    By "read out and check" I mean, check yourself, against the expected values based on reading the given (DAC) chapter in Reference Manual (RM).

    DAC_CR is 0x0001, i.e. you don't have enabled DAC2 (DAC_CR.EN2 = 0).

    JW

    3 replies

    Super User
    August 3, 2020

    > The value in DAC2 _OUT is garbage.

    What do you mean by garbage? Read out and check/post the DAC registers' content, explaining how do they depart from the expectations.

    JW

    KKoem.1Author
    Visitor II
    August 3, 2020

    0693W000003ByrVQAS.jpgHi JW,

    By garbage I mean that the voltage read on my voltmeter does not match the value read from the ADC that I pass to it. I'm quite new to this, so please excuse dumb responses to your questions. I did a screen grab.

    At the first ADC, the input voltage is 4.95V which is read as a value of 4000 (0xFA0). This is written to the first DAC, and a value of 4.89 V is read back (close enough). On the second ADC, the input voltage is 2.37 V which is read as a value of 1855 (0x73F), but I read 0.68 Volts.

    Did I display the contents of the DAC register correctly?

    Thanks,

    KKoem1

    Graduate II
    August 3, 2020

    Inputs not rates for near 5V operation

    Super User
    August 3, 2020

    By "read out and check" I mean, check yourself, against the expected values based on reading the given (DAC) chapter in Reference Manual (RM).

    DAC_CR is 0x0001, i.e. you don't have enabled DAC2 (DAC_CR.EN2 = 0).

    JW

    KKoem.1Author
    Visitor II
    August 4, 2020

    JW,

    Got it and I see the issue where the DAC_CR.EN2 is 0: bit 16 in 14.10.1 in the RM0091 for the STM32f0x2 (and others).

    I'm scratching my head as I'm comparing to other sample code I'm finding online.

    The HAL function is a bit of a mystery. I would assume that the clocks/registers would be enabled via the code I showed above.

    Would you recommend against using the Hal functions and instead enable the register directory? I was using the STMCubeMX to generate some of the code.

    Thanks,

    K

    Super User
    August 14, 2020

    > Would you recommend against using the Hal functions

    By using *any* "library" you accept its limitations. The "library" has been written with some usage in mind, and expects to be used in some certain way. Cube/HAL caters for the "most usual" cases. Users often find, that Cube/HAL is providing great value as long as you don't depart from the "usual cases"; departing from that appears to result in ordeal. Many users try to bend Cube/HAL to their purpose, or combine it with other approaches.

    One could also expect a certain level of documentation. And of course one may hit bugs and errors.

    The ultimate decision is yours.

    I don't Cube.

    JW