Skip to main content
Explorer
November 29, 2024
Solved

Code error when passing parameters to timer function

  • November 29, 2024
  • 1 reply
  • 727 views

https://community.st.com/t5/stm32-mcus-products/can-pwm-settings-affect-other-timer-settings/m-p/748254#M267471

void TIM2_Configuration(u32 Intervalms)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

	TIM_TimeBaseStructure.TIM_Prescaler = 16000-1;

	//do not work
	//TIM_TimeBaseStructure.TIM_Period 	 = Intervalms - 1;

	//do work
	TIM_TimeBaseStructure.TIM_Period 	 = 500 - 1;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	TIM_Cmd(TIM2, ENABLE);

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
	NVIC_InitTypeDef NVIC_InitStructure;

	NVIC_InitStructure.NVIC_IRQChannel 					 = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority 		 = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd 				 = ENABLE;

	NVIC_Init(&NVIC_InitStructure);
}

 In the last article, I said that the code worked or did not work depending on the location of TIM2_Configuration. However, when I removed the parameter from TIM2_Configuration and changed the period initialization to 500-1, the code worked properly regardless of the order. Does anyone know what causes this phenomenon to occur?

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Check the value of 

    TIM_TimeBaseStructure.TIM_Period 

    after setting it:

    TIM_TimeBaseStructure.TIM_Period 	 = Intervalms - 1;

     

    1 reply

    mƎALLEmAnswer
    Technical Moderator
    November 29, 2024

    Check the value of 

    TIM_TimeBaseStructure.TIM_Period 

    after setting it:

    TIM_TimeBaseStructure.TIM_Period 	 = Intervalms - 1;