cancel
Showing results for 
Search instead for 
Did you mean: 

Function to simulature quadratures changes quadrature direction randomly.

EmbeddedPepe
Associate III

Hi,

I have these two functions ( see below ) that simulate, respectively, a positive and negative quadrature.

 

 

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

void TIM3_Configuration_negative_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 =		( 84000 / 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 * 3) / 4; // CH3 at 75%
	TIM_OC3Init(TIM3, &TIM_OCInitStructure);
	TIM_OCInitStructure.TIM_Pulse = (Period * 1) / 4; // CH4 at 25%, ie half cycle later
	TIM_OC4Init(TIM3, &TIM_OCInitStructure);

	TIM_Cmd( TIM3, ENABLE );


}

 

 

The first time I set the frequency and call the positive quadrature function I get the wanted phase shift; but when I call the function to set the frequency and call the positive quadrature function once again I get negative phase shifting, if I call the negative quadrature function I get positive quadrature instead; so they basically switch. 

I've checked that the negative quadrature function doesn't get called.

Edit: If I change Frequency, only once, and call the positive quadrature twice i get positive and then negative quadrature.

Is there any workaround?

1 ACCEPTED SOLUTION

Accepted Solutions
EmbeddedPepe
Associate III

Fixed by setting the mode to the previous one (PWM1) when the frequency function get called.

View solution in original post

2 REPLIES 2
EmbeddedPepe
Associate III

This is how the waveform looks like when I  change frequency after calling positive Quadrature function (the frequency becomes half as well)

20240509_120759.jpg

EmbeddedPepe
Associate III

Fixed by setting the mode to the previous one (PWM1) when the frequency function get called.