cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L476 comparator constantly generates an interrupt

ARa.2
Associate II

Hi. I configured comparator with neccessary settings using CubeIDE and LL library and got next generated piece of code:

  COMP_InitStruct.PowerMode = LL_COMP_POWERMODE_HIGHSPEED;
  COMP_InitStruct.InputPlus = LL_COMP_INPUT_PLUS_IO1;
  COMP_InitStruct.InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
  COMP_InitStruct.InputHysteresis = LL_COMP_HYSTERESIS_HIGH;
  COMP_InitStruct.OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
  COMP_InitStruct.OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
  LL_COMP_Init(COMP1, &COMP_InitStruct);
  LL_COMP_SetCommonWindowMode(__LL_COMP_COMMON_INSTANCE(COMP1), LL_COMP_WINDOWMODE_DISABLE);

I also got generated code to use interrupt:

  LL_EXTI_ClearFlag_0_31(LL_EXTI_LINE_21);
  LL_EXTI_EnableRisingTrig_0_31(LL_EXTI_LINE_21);
  LL_EXTI_DisableFallingTrig_0_31(LL_EXTI_LINE_21);
  LL_EXTI_DisableEvent_0_31(LL_EXTI_LINE_21);
  LL_EXTI_EnableIT_0_31(LL_EXTI_LINE_21);

Then I used 3 configuration of connection INP(input plus):

1) INP do not connected to anything;

2) INP connect to GND;

3) INP connect to DAC output pin with maximum value (4095) (I think it correspond to Vref).

In config 1) I enable comparator and got constantly generated interrupt (one after another) (I clear flag in IRQ)

In config 2) I enable comparator and got once generated interrupt

In config 3) I enable comparator and got constantly generated interrupt again.

Maybe I don't understand how comparator works, but in my opinion it must generate interrupt once only when INP(+) > INP(-) (Vref in my case). And that's all. So

In config 1) I must not get interrupt

In config 2) I must not get interrupt

In config 3) ... I can or can't get interrupt? Anyway, I must not get constantly generated interrupt

What is the problem?

2 REPLIES 2
Piranha
Chief II

Show the ISR (interrupt service routine) code.

...
LL_EXTI_ClearFlag_0_31(LL_EXTI_LINE_21);
LL_EXTI_EnableIT_0_31(LL_EXTI_LINE_21);

It's more logical to clear the flags after the configuration and before enabling the interrupt. Don't expect a sane design from the HAL/Cube team...

There is nothing interesting:

void COMP_IRQHandler(void)
{
	LL_EXTI_ClearFlag_0_31(LL_EXTI_LINE_21);
}

I also check flag of LL_EXTI_LINE_21 and it is set.

I partially solved the problem with config 3. When testing with DAC I disable its output buffer, but when I enable it I got only one interrupt immediately after turning on the comparator. I also test it with different values of DAC. And that's seems to be OK. It generate interrupt after INP > INM. But I think I must not get interrupt only after enabling comparator. Changing the order of initialization did nothing. Also, interrupts keep coming when the input pin is not connected to anything (config 2).

UPDATE:

I must have made a mistake somewhere, because now with config 3 it works even with the disabled output buffer (but again: I got interrupt just after enable comparator).