cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F072RB Comparator to Timer2 Frequency Counter

abotha
Associate III
Posted on January 16, 2017 at 11:41

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();
 }
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

#timer-channels #stm32f072 #comparator #timer #input-capture
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on January 16, 2017 at 12:53

Look at the TIMx_SMCR.TS description in RM: from the channels (inputs) of the given timer, only TI1FP1 and TI2FP2 can be used as TRGI (thus subsequently as source for external clock mode 1).

JW

View solution in original post

4 REPLIES 4
Posted on January 16, 2017 at 12:02

The comparator is connected to Input Capture 4 (i.e. Channel 4) which can't be used as external clock.

Why don't you use the TIM1/IC1 or TIM3/IC1 connection?

JW

Posted on January 16, 2017 at 12:27

Hi JW, what would be different changing from one Input Capture to another?

Is that the mode that I should be on to achieve what I described (counting pulses in a given time period)?

Posted on January 16, 2017 at 12:53

Look at the TIMx_SMCR.TS description in RM: from the channels (inputs) of the given timer, only TI1FP1 and TI2FP2 can be used as TRGI (thus subsequently as source for external clock mode 1).

JW

Posted on January 16, 2017 at 15:37

Thank you very much, it works!