cancel
Showing results for 
Search instead for 
Did you mean: 

I want to measure the number of impulse for signal so I use a timer en mode ETR (signal like a horloge en extern de timer) But I don't know how Create this task

Anaghim
Associate II
 
2 REPLIES 2

Not sure which STM32 part you're using specifically.

Most support External Timer modes, using ETR or CHx as a counting clock source, similarly Encoder Modes with CH1/CH2

Most of the HAL examples have at least one using an external source, some have internal mappings to LSE/LSI

I can't say I'm familiar with external halogen timers.

This is an example using the older SPL implementation

// STM32F429I-DISCO TIM3 (CH1 PA6) External Count Demo - sourcer32@gmail.com
 
#include "stm32f429i_discovery.h"
 
//****************************************************************************
 
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
  /* GPIOA clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
 
  /*  pin configuration */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6,  GPIO_AF_TIM3); // PA6 TIM3_CH1
}
 
//****************************************************************************
 
void TIM3_Configuration(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  int i;
 
  /* Enable TIM3 Peripheral clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
 
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_Period = 100 - 1; // 100 (16-bit counter)
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
 
  TIM_TIxExternalClockConfig(TIM3, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
 
  TIM_Cmd(TIM3, ENABLE);
 
  // Likely will interrupt initially unless we clear it
 
  for(i=0; i<3; i++)
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
      TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
 
  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
 
  // Could also use compare mode, set the counter maximal and advance CCRx 100 ticks at a time
}
 
//****************************************************************************
 
void TIM3_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
 
    /* Toggle LED3 */
    STM_EVAL_LEDToggle(LED3);
  }
}
 
//****************************************************************************
 
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
 
  /* Enable the TIM3 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
 
//****************************************************************************
 
int main(void)
{
  /* Initialize Leds mounted on STM32F429I-DISCO board */
  STM_EVAL_LEDInit(LED3);
 
  /* Turn on LED3 */
  STM_EVAL_LEDOn(LED3);
 
  NVIC_Configuration();
 
  GPIO_Configuration();
 
  TIM3_Configuration();
 
  while(1); // Do not exit
}
 
//******************************************************************************
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
 
//******************************************************************************

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

HAL, something along the lines of

TIM_HandleTypeDef htim4;
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
 
htim4.Instance = TIM4;
htim4.Init.Prescaler = 0;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = 65535;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htim4);
 
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_ETRMODE2;
sClockSourceConfig.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
sClockSourceConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
sClockSourceConfig.ClockFilter = 0;
HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig);
 
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig);

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