cancel
Showing results for 
Search instead for 
Did you mean: 

Function to simulate positive phase shifting changes direction randomly.

EmbeddedPepe
Associate III

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?

 

1 REPLY 1
Sarra.S
ST Employee

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);

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.