cancel
Showing results for 
Search instead for 
Did you mean: 

Comparator interrupt stm32l4

Vertinhol
Associate III

Hello I would like to use the internal comparator output as an interrupter on falling or rising edge, the problem is I don't know where to start, the reference manual says that I need to configure the EXTI register but how do I do that, and with which pins are the input comparator linked and I can change them?

Help me please.

2 REPLIES 2

You did not tell us which 'L4, so I will assume 'L476. Refer to manuals to your actual model.

> which pins are the input comparator linked and I can change them

See COMPx_CSR.INPSEL and .INMSEL fields, and COMP pins and internal signals subchapter of COMP chapter in RM. The selected pin must be set as Analog in respectiove GPIOx_MODER.

> I need to configure the EXTI register but how do I do that

In EXTI lines connections table, EXTI interrupt/event line mapping subchapter of EXTI chapter, find number of EXTI line to which COMPx output is connected - in 'L476, it's EXTI21 for COMP1 output and EXTI22 for COMP2 output.

Then, select interrupt upon rising or falling edge (or both), by setting respective bit in EXTI_RTSRx/EXTI_FTSRx and enable interrupt in EXTI_IMRx.

Then, find the respective interrupt in the vector table in NVIC chapter - for 'L476, there's one single interrupt vector for both COMP interrupts, and it's COMP1/COMP2 through EXTI lines 21/22 interrupts, acronym COMP - which means you will use COMP_IRQn as the symbol for NVIC-related function calls (i.e. NVIC_EnableIRQ(COMP_IRQn);) and the related ISR will be void COMP_IRQHandler(void) {}.

JW

Thank you for your answer, first of all as you guess it, I'm using a NUCLEO-L476RG, I'm trying to drive a BLDC motor and I need a comparator to detect the zero-crossing event on each phase, according to the RM I can connect the negative comparator input with DAC Channel1, DAC Channel2 or PC4 which I believe Channel1 and Channel2 are PA4 and PA5.

During the driving process I need to change constantly the comparator input pins and the interruption mode (falling or rising) so this can be done by changing the INMSEL and the EXTI bits, so when an interruption is detected all what's inside the function below will be executed is that right?

void COMP_IRQHandler(void)
{
  /* USER CODE BEGIN COMP_IRQn 0 */
 
  /* USER CODE END COMP_IRQn 0 */
  HAL_COMP_IRQHandler(&hcomp1);
  /* USER CODE BEGIN COMP_IRQn 1 */
 
  /* USER CODE END COMP_IRQn 1 */
}

Also can you give me an example on how to change these registers and do I need to Re-initialize the comparator each time when I only change the interruption mode and the input pin?