AnsweredAssumed Answered

Configure Timer for Output Compare

Question asked by botha.antonie on Feb 22, 2017

I want to configure TIM3 on my STM32F072 to count the number of pulses from the built-in comparator, and then Toggle Pin PA7 when the compare value was reached. I have a multiplexed sine signal coming into the comparator, and would like to use TIM3 clocked from the comparator as a low resolution counter, and TIM2 as a High-Resolution counter. 


I have set timer 2 as a slave to TIM3 in Trigger Mode, such that TIM2 should start running when TIM3 is enabled and received an edge from the comparator, when the low resolution counter reaches LSCVal (lets say LSCVal = 500) I want it to toggle a pin so that I can route that pin to an Input Capture channel on TIM2 to get an accurate value of what the high resolution counts in TIM2 were when TIM3 reached the desired LSCVal. 


First prize would be if I could do this internally, but I cannot find a way to do that. 


I am not getting an output on my OutputCompare Pin currently, I am not sure if the TIM3 isn't set up correctly, or just not routed to the output correctly?


Comparator Setup:

/* COMP1 init function */
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_TIM3IC1;
  hcomp1.Init.Hysteresis = COMP_HYSTERESIS_MEDIUM;
  hcomp1.Init.Mode = COMP_MODE_HIGHSPEED;
  hcomp1.Init.WindowMode = COMP_WINDOWMODE_DISABLE;
  hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_NONE;
  if (HAL_COMP_Init(&hcomp1) != HAL_OK)




TIM3 Setup:


/* TIM3 init function */
void MX_TIM3_Init(void)
  TIM_SlaveConfigTypeDef sSlaveConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_OC_InitTypeDef sConfigOC;

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 0;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 65535;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)

  if (HAL_TIM_OC_Init(&htim3) != HAL_OK)

  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
  sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
  sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING;
  sSlaveConfig.TriggerFilter = 2;
  if (HAL_TIM_SlaveConfigSynchronization(&htim3, &sSlaveConfig) != HAL_OK)

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC2REF;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)

  sConfigOC.Pulse = 500;
  if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)



I start the timer with:

               __HAL_TIM_SetCounter(&htim2, 0);                                                            // Reset the High Speed Counter to Zero
               __HAL_TIM_SetCounter(&htim3, 0);                                                            // Set this counter to Zero
               HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);
               HAL_TIM_OC_Start(&htim3, TIM_CHANNEL_2);          


I am triggering my oscilloscope on PA7 (TIM3 Channel 2 Output??) but the pin never toggles.


Any ideas?

Non HAL help will also be appreciated.