Skip to main content
Explorer II
May 10, 2024
Question

Function to simulate positive phase shifting changes direction randomly.

  • May 10, 2024
  • 1 reply
  • 560 views

Hi,

I have the following function to simulate a positive phase shifting.

 

void TIM3_Configuration_positive_quadrature(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_OCInitTypeDef TIM_OCInitStructure;
	int Period;
	Period = 1000 / 1; //
	TIM_Cmd( TIM3, DISABLE );

	TIM_TimeBaseStructure.TIM_CounterMode =		TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_ClockDivision =	TIM_CKD_DIV1;
	TIM_TimeBaseStructure.TIM_Period =			1000 - 1;
	//the prescaler is divided by 2 because the toggle mode (see below) halves the frequency.
	TIM_TimeBaseStructure.TIM_Prescaler =		( TIM3_FREQ / lastSetFrequency ) / 2 - 1;

	TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );

	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OCInitStructure.TIM_Pulse = (Period * 1) / 4; // CH3 at 25%
	TIM_OC3Init(TIM3, &TIM_OCInitStructure);

	TIM_OC3PreloadConfig( TIM3, TIM_OCPreload_Enable );
	TIM_OCInitStructure.TIM_Pulse = (Period * 3) / 4; // CH4 at 75%, ie half cycle later
	TIM_OC4Init(TIM3, &TIM_OCInitStructure);
	TIM_OC4PreloadConfig( TIM3, TIM_OCPreload_Enable );

	TIM_Cmd( TIM3, ENABLE );
}

 

 

But when I set the frequency and call the function once I get the correct phase shifting, but If I call the function a second time the phase shifting goes from positive to negative, called a third time turns it from negative to positive, and so on.

Any reason and workaround behind this?

 

    This topic has been closed for replies.

    1 reply

    ST Employee
    August 6, 2024

    Hello @EmbeddedPepe

    The timer's counter needs to be reset when you reconfigure the timer each time the function is called: 

    TIM_SetCounter(TIM3, 0);