Loading...
 

Zephyr project on STM32

   Zephyr Workbench, a VSCode extension to manage Zephyr on STM32.
It enables users to easily create, develop, and debug Zephyr applications.
Main features:
  • Install host dependencies.
  • Import toolchain and SDK.
  • Create, configure, build and manage apps.
  • Debug STM32.
You can directly download it from the VSCode marketplace
For more details, visit the Zephyr Workbench

System Workbench for STM32


STM32F4 disc ,pulse counter

Hello i have a technical question,
I am using STM32F4-discovery board as a prototype.
I want to have several inputs to count external pulses (sensors outputs).
I have manage to enable TIM5 by this code

void MX_TIM5_Init(void)
{
TIM_HandleTypeDef timer;
TIM_Base_InitTypeDef inizializza;
TIM_IC_InitTypeDef startclock;
TIM_ClockConfigTypeDef ClockConfig;
TIM_SlaveConfigTypeDef sSlaveConfigure;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_Encoder_InitTypeDef hEncoder1;

GPIO_InitTypeDef GPIO_InitStruct;
__TIM5_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;//GPIO_PULLDOWN
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);//A
timer.Instance = TIM5; timer.Init.Period = 0xFFFFFFFF;
timer.Init.Prescaler = 0x0000; timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; timer.Init.CounterMode = TIM_COUNTERMODE_UP; //TIM_COUNTERMODE_CENTERALIGNED3;//3 timer.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&timer);

sSlaveConfigure.SlaveMode = TIM_SLAVEMODE_DISABLE;
HAL_TIM_SlaveConfigSynchronization(&timer, &sSlaveConfigure);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&timer, &sMasterConfig);
ClockConfig.ClockFilter = 0;
ClockConfig.ClockPolarity = TIM_CLOCKPOLARITY_RISING;
ClockConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
ClockConfig.ClockSource = TIM_CLOCKSOURCE_TI2;
HAL_TIM_ConfigClockSource( &timer, &ClockConfig );
TIM5->CR1 |= TIM_CR1_ARPE; // autoreload on
TIM5->CR1 |= TIM_CR1_CEN; // TIM4->CR1 = 1; // enable timer
}


As a result when i apply pulses to portA pin0 i count the pulses ok as i check TIM5->CNT increases correctly.

Then i try to do the same thing with TIM2 portA pin 15.But the counter isn’t activated and doesnt count the pulses

The code is:


void MX_TIM2_Init(void) //was 10
{
TIM_HandleTypeDef timer;
TIM_Base_InitTypeDef inizializza;
TIM_IC_InitTypeDef startclock;
TIM_ClockConfigTypeDef ClockConfig;
TIM_SlaveConfigTypeDef sSlaveConfigure;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_Encoder_InitTypeDef hEncoder1;
GPIO_InitTypeDef GPIO_InitStruct;
__TIM2_CLK_ENABLE()
; __GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP
; GPIO_InitStruct.Pull = GPIO_NOPULL;//GPIO_PULLDOWN
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);//A
timer.Instance = TIM2;
timer.Init.Period = 0xFFFFFFFF;
timer.Init.Prescaler = 0x0000;
timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
timer.Init.CounterMode = TIM_COUNTERMODE_UP; //TIM_COUNTERMODE_CENTERALIGNED3;//3
timer.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&timer);

sSlaveConfigure.SlaveMode = TIM_SLAVEMODE_DISABLE;
HAL_TIM_SlaveConfigSynchronization(&timer, &sSlaveConfigure);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&timer, &sMasterConfig);
ClockConfig.ClockFilter = 0;
ClockConfig.ClockPolarity = TIM_CLOCKPOLARITY_RISING;
ClockConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
ClockConfig.ClockSource = TIM_CLOCKSOURCE_TI2;
HAL_TIM_ConfigClockSource( &timer, &ClockConfig );
TIM2->CR1 |= TIM_CR1_ARPE; // autoreload on
TIM2->CR1 |= TIM_CR1_CEN; // TIM4->CR1 = 1; // enable timer
}



Can you suggest me a solution in order to make TIM2 work and count the external pulses?

 

Newest Forum Posts

  1. SPI on Nucleo_STMH533RE by royjamil, 2025-05-04 20:13
  2. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-25 07:37
  3. SPI on Nucleo_STMH533RE by royjamil, 2025-03-23 11:31
  4. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-23 09:33
  5. Configuring DMA for ADC in SW? by sam.hodgson, 2025-03-04 12:58
  6. Build a project in "release" mode by info@creosrl.it, 2025-02-20 18:12
  7. Build a project in "release" mode by info@creosrl.it, 2025-02-20 17:05
  8. Build a project in "release" mode by tang, 2025-02-20 10:36
  9. Build a project in "release" mode by info@creosrl.it, 2025-02-19 17:35
  10. Fail to debug in Win 11 C/C++ by mortenlund, 2024-12-26 20:27

Last-Modified Blogs