cancel
Showing results for 
Search instead for 
Did you mean: 

L031K6] How to Use comparator in comparating external source vs external source mode ?

DU2
Associate II

I can successfully comparating ( My boards' vref ) VS( output voltage )

but later I want to set up voltage by potentiometer and comparating it by output voltage

in other words, I want to Comparator Compare

(Other voltage output in my circuit) vs (Another voltage output in fields)

and finally I want to use this comparator in stop mode & make interrupt (I already made it)

in that case, what should I put in my comparator code & HAL options ?

I put my configuration codes. thank a lot.

 ----------------------------------------------------------------------------

this is my code about comparating VREF vs output port(PA3)

static void MX_COMP2_Init(void)

{

  hcomp2.Instance = COMP2;

  hcomp2.Init.NonInvertingInput = COMP_INPUT_PLUS_IO1; // +PA3

  hcomp2.Init.InvertingInput = COMP_INPUT_MINUS_3_4VREFINT; //-VREF, 0.8v (0.825v)

  

  hcomp2.Init.LPTIMConnection = COMP_LPTIMCONNECTION_IN1_ENABLED;

  

  hcomp2.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED ;

  hcomp2.Init.Mode = COMP_POWERMODE_ULTRALOWPOWER;  

  hcomp2.Init.WindowMode = COMP_WINDOWMODE_DISABLE;

  hcomp2.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING;

 if (HAL_COMP_Init(&hcomp2) != HAL_OK)

 {

  Error_Handler();

 }

  

  if(HAL_COMP_Start_IT(&hcomp2) != HAL_OK){

   

  while(1){

  }

 }

  

  

 HAL_COMPEx_EnableVREFINT();

}

-----------------------------------------------------------------------------------------

This is my msp code

void HAL_COMP_MspInit(COMP_HandleTypeDef* hcomp)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 if(hcomp->Instance==COMP2)

 {

 /* USER CODE BEGIN COMP2_MspInit 0 */

 /* USER CODE END COMP2_MspInit 0 */

  

  __HAL_RCC_GPIOB_CLK_ENABLE();

  /**COMP2 GPIO Configuration   

  PB4   ------> COMP2_INP 

  */

  GPIO_InitStruct.Pin = GPIO_PIN_4;

  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* COMP2 interrupt Init */

  HAL_NVIC_SetPriority(ADC1_COMP_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(ADC1_COMP_IRQn);

 /* USER CODE BEGIN COMP2_MspInit 1 */

 /* USER CODE END COMP2_MspInit 1 */

 }

}

-----------------------------------------------------------------------------------

Thanks for read

1 REPLY 1

Which STM32?

Generally, the COMP chapter in RM (Reference Manual) to your STM32 describes, how to connect COMP + and - to individual input terminals. Read it.

JW