cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103RBT6 external counter

DDong.0
Associate II

I'm using the STM32F103RBT6 to counter a external pulses with TIM3. But the counter is always ZERO. I can check the pulse with  oscillometer. It is about 1KHz. Here is the code. Could any one tell me what is the issue ? Thank you.

int main(void)
{
  /* USER CODE BEGIN 1 */
	u16 valComp,Tests;
 
  /* USER CODE END 1 */
  
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  
 
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_AFIO);
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
 
  NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
 
  /* System interrupt init*/
 
  /** NOJTAG: JTAG-DP Disabled and SW-DP Enabled 
  */
  LL_GPIO_AF_Remap_SWJ_NOJTAG();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_IWDG_Init();
  MX_TIM2_Init();
  MX_TIM3_Init();
  MX_TIM4_Init();
  MX_USART2_UART_Init();
  MX_I2C1_Init();
  /* USER CODE BEGIN 2 */
	OLED_Init();
  OLED_CLS();
  OLED_ShowStr(20,0,"power off");
 
	LL_TIM_EnableCounter(TIM2);
	LL_TIM_EnableCounter(TIM3);
	LL_TIM_EnableCounter(TIM4);
 
	LL_TIM_CC_EnableChannel(TIM3, LL_TIM_CHANNEL_CH1);
 
 
	LL_IWDG_Enable(IWDG);
	
	valComp=2048;									//周期4096 514us   2048代表�?�空比50%   数字越�? 高电平时间越短 为0时 �?有500ns高脉冲  为4096时 全高
  Tests=0;
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
		if (ISSET(KeyPress)) {
			
			OLED_Show16_8_Number(20, 2, valComp,4);
			
			if (valComp>100) {
				LL_TIM_OC_SetCompareCH3(TIM4, valComp);
				LL_TIM_OC_SetCompareCH4(TIM4, valComp);
				valComp=valComp-100;
			}
			else if (valComp==0) valComp=4096;				
			else {
				LL_TIM_OC_SetCompareCH3(TIM4, 0);
				LL_TIM_OC_SetCompareCH4(TIM4, 0);
				valComp=0;
			}
 
			POWTOG;
			if (IsPowOn) {
				OLED_ShowStr(20,0,"power on ");
				LL_TIM_CC_EnableChannel(TIM4, LL_TIM_CHANNEL_CH4);
			}
			else {
				OLED_ShowStr(20,0,"power off");
				LL_TIM_CC_DisableChannel(TIM4, LL_TIM_CHANNEL_CH4);
			}
			RESETBIT(KeyPress);
		}
		
		if (ISSET(StartTest)) {
			Tests++;
			OLED_Show16_8_Number(80, 4, Tests,4);
			OLED_Show16_8_Number(20, 4, LL_TIM_GetCounter(TIM3),4);
			LL_TIM_DisableCounter(TIM3);
			LL_TIM_SetCounter(TIM3, 0);
			LL_TIM_EnableCounter(TIM3);
			RESETBIT(StartTest);
		}
		
  }
  /* USER CODE END 3 */
}

/**
  * @brief TIM3 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM3_Init(void)
{
 
  /* USER CODE BEGIN TIM3_Init 0 */
 
  /* USER CODE END TIM3_Init 0 */
 
  LL_TIM_InitTypeDef TIM_InitStruct = {0};
 
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
  
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
  /**TIM3 GPIO Configuration  
  PB4   ------> TIM3_CH1 
  */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_4;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
  /* TIM3 interrupt Init */
  NVIC_SetPriority(TIM3_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
  NVIC_EnableIRQ(TIM3_IRQn);
 
  /* USER CODE BEGIN TIM3_Init 1 */
 
  /* USER CODE END TIM3_Init 1 */
  TIM_InitStruct.Prescaler = 0;
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = 65535;
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  LL_TIM_Init(TIM3, &TIM_InitStruct);
  LL_TIM_EnableARRPreload(TIM3);
  LL_TIM_SetTriggerInput(TIM3, LL_TIM_TS_TI1FP1);
  LL_TIM_SetClockSource(TIM3, LL_TIM_CLOCKSOURCE_EXT_MODE1);
  LL_TIM_IC_SetFilter(TIM3, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV32_N8);
  LL_TIM_IC_SetPolarity(TIM3, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);
  LL_TIM_DisableIT_TRIG(TIM3);
  LL_TIM_DisableDMAReq_TRIG(TIM3);
  LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_UPDATE);
  LL_TIM_EnableMasterSlaveMode(TIM3);
  /* USER CODE BEGIN TIM3_Init 2 */
	LL_TIM_EnableIT_UPDATE(TIM3);
	LL_TIM_EnableIT_CC1(TIM3);
	LL_TIM_EnableIT_TRIG(TIM3);
 
  /* USER CODE END TIM3_Init 2 */
 
}

4 REPLIES 4

Read out and check the relevant GPIO/AFIO and TIM registers' content.

JW

DDong.0
Associate II

Thanks for you reminding.

I found the issue. The cubemx did NOT do the TIM3 remap. I added code as below in linie 33 in the ​MX_TIM3_Init(void). It works fine now.

LL_GPIO_AF_RemapPartial_TIM3();

So is this a CubeMX bug? Can you please post the .ioc file and tell us the version of CubeMX you are using?

JW

@Amel NASRI​ 

DDong.0
Associate II

The CubeMX is version 5.3.0. The ioc file is as below.​