AnsweredAssumed Answered

stm32f301 comparator

Question asked by nealjing@eptco.com on Mar 28, 2017
Latest reply on Mar 28, 2017 by Imen D

I'm trying to use stm32f301 comparator 2, but the output is always 0 no matter if the two inputs are higher or lower. I guess the configuration is wrong.

 

The main code is here. Thanks for the help in advance!

 

void ConfigureComp2(vod)
{
  GPIO_InitTypeDef GPIO_InitStruct;

  //init A7, A2 as analog
  GPIO_InitStruct.Pin = GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  GPIO_InitStruct.Pin = GPIO_PIN_2;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
  RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
 
  //COMP2->CSR |= COMP_CSR_COMPxOUTSEL_0;

  printf("after reset out %d\n", COMP2->CSR);

  COMP2->CSR |= COMP_CSR_COMPxINSEL_2;
  COMP2->CSR |= COMP_CSR_COMPxINSEL_1;
  printf("after input selection out %d\n", COMP2->CSR);

  //COMP2->CSR |= COMP_CSR_COMPxPOL;
  //printf("after pol out %d\n", COMP2->CSR);

  COMP2->CSR |= COMP_CSR_COMPxEN;
  printf("after enable, out %d\n", COMP2->CSR);

  for (int i=0; i<5; i++)
  {
    printf("comp2 i %d out %d\n", i, COMP2->CSR);
    printf("comp2 i %d out %d\n", i, COMP2->CSR & COMP2_CSR_COMP2OUT);
  }
}

Attachments

Outcomes