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?