2024-05-08 08:18 AM
Hi,
I have this function to simulate a quadrature output
void TIM3_Configuration_positive_quadrature(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
int Period;
Period = 1000 / 1; // 1 Hz - 1 second on, 1 second off duty
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_OCInitStructure.TIM_Pulse = (Period * 3) / 4; // CH4 at 75%, ie half cycle later
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
}
When I set 1000 Hz as frequency (controlled by oscilloscope that is correctly set) after calling the quadrature function above the frequency becomes half.
Any idea on why of this behaviour?
2024-05-08 08:40 AM - edited 2024-05-08 09:03 AM
Hello @EmbeddedPepe,
I think this is related to the toggle match behavior, since a full square wave cycle requires two toggles (low to high and high to low) and the timer only toggles once per counter match, the frequency of the output signal will be half the rate at which the counter matches the TIM_Pulse value.
The workaround is to adjust the period or the PSC value to compensate for the toggle behavior
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.
2024-05-08 08:52 AM
Toggling will implicitly half frequency, it's a DIV2, as you're now describing the individual mark and space duty times, not the period.
Where
TIM->PSC = x - 1;
use
TIM->PSC = ((x / 2) - 1);
2024-05-08 09:25 AM - edited 2024-05-08 09:26 AM
Sorry, I didn't understand how to workaround it.
Does the function in the initial post work with mode PWM1?
2024-05-08 09:42 AM
Toggle is good for phase shifted square waves
My notation was one for halving the Prescaler (TIM->PSC) to get back to the higher (double) frequency to overcome the halving on the Period (TIM->ARR)
2024-05-08 09:49 AM - edited 2024-05-09 12:12 AM
Ohh, now I got it. Thanks! :)