cancel
Showing results for 
Search instead for 
Did you mean: 

Problem using DAC_OUT2 as COMP1 inverting input, STM32L4

Ricardo Hassan
Associate III
Posted on March 12, 2018 at 16:31

Hi,

    I am having a problem using COMP1 on a STM32l496 chip.  I am doing a proof of concept on a Nucleo board.  I'm using a signal generator to put a 1KHz sine wave on the IN+ pin, and I'm using the DAC as the IN- signal, with the value set to 0.  I'm using CubeMX to generate code for this.  When I run this setup, I get no changes on the comparator.  When I configure the project to use an external pin as IN-, and attach that pin to ground, I get the expected toggling output from the comparator.  When I look at the DAC output, by configuring it's output to go to both an on-chip peripheral and external pin and looking at the pin, the output looks fine.  Does anyone have any idea what I may be doing wrong?  I'm including a code snippet of the COMP1 and DAC setup, and also the lines to show they are running.

// from main()

HAL_COMP_Start(&hcomp1);

HAL_DAC_Start(&hdac1, DAC_CHANNEL_2);

HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, 100);

/* COMP1 init function */

static void MX_COMP1_Init(void)

{

hcomp1.Instance = COMP1;

hcomp1.Init.InvertingInput = COMP_INPUT_MINUS_DAC1_CH2;

hcomp1.Init.NonInvertingInput = COMP_INPUT_PLUS_IO2;

hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;

hcomp1.Init.Hysteresis = COMP_HYSTERESIS_NONE;

hcomp1.Init.BlankingSrce = COMP_BLANKINGSRC_NONE;

hcomp1.Init.Mode = COMP_POWERMODE_HIGHSPEED;

hcomp1.Init.WindowMode = COMP_WINDOWMODE_DISABLE;

hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_NONE;

if (HAL_COMP_Init(&hcomp1) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

/* DAC1 init function */

static void MX_DAC1_Init(void)

{

DAC_ChannelConfTypeDef sConfig;

/**DAC Initialization

*/

hdac1.Instance = DAC1;

if (HAL_DAC_Init(&hdac1) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**DAC channel OUT2 config

*/

sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;

sConfig.DAC_Trigger = DAC_TRIGGER_NONE;

sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;

sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;

sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;

if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

Ricardo

1 ACCEPTED SOLUTION

Accepted Solutions
Ricardo Hassan
Associate III
Posted on April 02, 2018 at 19:41

I finally figured this out.  I had the gpio pins of the comparator configured incorrectly.  The input pin needs to have the alternate function set so that it's properly internally routed to the comparator, and the output needed to be set to analog.  The hint was when I looked at the COMP1 status register, and I could see that it was changing even though the output pin was not.  Trivial no doubt, but it was a real blocker till I got it.

Ricardo

View solution in original post

1 REPLY 1
Ricardo Hassan
Associate III
Posted on April 02, 2018 at 19:41

I finally figured this out.  I had the gpio pins of the comparator configured incorrectly.  The input pin needs to have the alternate function set so that it's properly internally routed to the comparator, and the output needed to be set to analog.  The hint was when I looked at the COMP1 status register, and I could see that it was changing even though the output pin was not.  Trivial no doubt, but it was a real blocker till I got it.

Ricardo