AnsweredAssumed Answered

STM32F072RB Comparator to Timer2 Frequency Counter

Question asked by botha.antonie on Jan 16, 2017
Latest reply on Jan 16, 2017 by botha.antonie

Good Day

 

I have an oscillator connected to Comparator 1, I want the output of Comparator 1 to internally go to timer 2. 

 

I would like to manually start TIM2, leave it for a few milliseconds, and come back and read how many times the comparator changed state. I then want to reset the value back to zero and it must start counting up again, one count for every transition of the from high to low on the comparator. 

 

How must I set up timer 2 to achieve this? I have it set-up as Slave Mode with External Input but I think it is incorrect.

 

I have set Comparator 1 as follows:

 

void MX_COMP1_Init(void){
hcomp1.Instance = COMP1;
hcomp1.Init.InvertingInput = COMP_INVERTINGINPUT_IO1;
hcomp1.Init.NonInvertingInput = COMP_NONINVERTINGINPUT_IO1;
hcomp1.Init.Output = COMP_OUTPUT_TIM2IC4;
hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
hcomp1.Init.Hysteresis = COMP_HYSTERESIS_LOW;
hcomp1.Init.Mode = COMP_MODE_HIGHSPEED;
hcomp1.Init.WindowMode = COMP_WINDOWMODE_DISABLE;
hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING;

if (HAL_COMP_Init(&hcomp1) != HAL_OK)
{ Error_Handler(); }
}

 

 

and Timer 2 like this:

 

/* TIM2 init function */
void MX_TIM2_Init(void)
{
  TIM_SlaveConfigTypeDef sSlaveConfig;
  TIM_MasterConfigTypeDef sMasterConfig;

  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 0;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 150000;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }

  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
  sSlaveConfig.InputTrigger = TIM_TS_TI1F_ED;
  sSlaveConfig.TriggerFilter = 5;
  if (HAL_TIM_SlaveConfigSynchronization(&htim2, &sSlaveConfig) != HAL_OK)
  {
    Error_Handler();
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }

}

Outcomes