cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f301 comparator

Neal Jing
Associate
Posted on March 28, 2017 at 12:59

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);

  }

}
1 REPLY 1
Imen.D
ST Employee
Posted on March 28, 2017 at 14:57

Hi

nealjing@eptco.com

,

I suggest, as a first step, to run one of the COMP working example available inthe STM32CubeF3 firmware package with a similar type of your project, it will help you to configure and develop your project:

STM32Cube_FW_F3_V1.7.0\Projects\STM32303C_EVAL\Examples\COMP

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen