cancel
Showing results for 
Search instead for 
Did you mean: 

1s timer for counting pulse ?

antonius
Senior

Dear Members,

I want to count pulse from EXTI4, and

I tried to set 30 seconds for how many pulse ?

Please correct my setting :

void MX_TIM4_Init(void)
{
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};
 
  htim4.Instance = TIM4;
  htim4.Init.Prescaler = 0;
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim4.Init.Period = 10000;
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_OC_Init(&htim4) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_TIMING;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_OC_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_TIM_MspPostInit(&htim4);
 
}
void GPIO_EXTI4_Callback()
{
	
	count++;
}	
 
void Timer_4_Callback()
{
	printf("TIMER4Callback\r\n");
	 
 
	 seconds += 1;
 
	 if (seconds == 30)
	 {
		 minute = 1;
		 seconds = 0;
 		
 		 rpm=count;
 		
		 rps=(uint8_t)rpm/30;
        count=0;
	 }
	
}	

on main()

current_rps=rps;
		 if (minute==1)
		  {
				printf("BPS = %u",current_rps);
				minute=0;
			}	
		

my clock is 2.097MHz , TIMER4 STM32L152RE

Any clues ?

thanks

2 REPLIES 2

People normally use the SysTick at 1ms, and count off 1000 periods.

You could likely use a 16-bit timer with appropriate prescaler/period to count 2097000 cycles.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

any examples on how to use that method ?

pseudo code ?

Systick = 1000ms,

check how many counts ?

thanks