Phase shift with swapping polarity
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-12 1:58 AM
Hi
I am working on a STM32G484QET6 uC. On a timer I have configured two output channels with Output Compare Toggle Mode. So that I get a quadrature signal.
When configuring the first value, everything works perfectly. The duty cycle is 50% and the phase shift is 90 degrees to each other.
But in my application I have to change the frequency during the run, means also the CCR value to keep the 50% duty cycle. When I update the values, I stop the timer and then, after updating I restart it. Now I have the problem that the polarity is sometimes swapping after changing the values. For example, the channel 1 signal is behind channel 2 and I want it the other way around.
How do I change the values so that this doesn't happen?
Thank you!
RLD
- Labels:
-
STM32CubeMX
-
STM32G4 Series
-
TIM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-12 5:55 AM
Addition: The pulse for changing the frequency is triggered by a button via interrupt. So there is not much to do in terms of the timing of the change in frequency and timer values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-16 1:46 AM
Hello @RLD, welcome to ST Community,
I think the right sequence will be
1. Stop the timer
2. Update CCR values
3. Generate an update event (set UG bit in the TIMx_EGR register) to make sure that the new CCR values are loaded correctly
4. Restart the timer
Would you give it a try and let me know how it went?
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-16 2:56 AM - edited ‎2024-08-16 2:57 AM
Hello @Sarra.S
Sadly, it still doesn't work. The polarity of the phase shift still sometimes changes after calling up the function to change the frequency.
Here is my function, if that might help:
void UpdateTimerFrequencyPhaseShift(uint16_t frequency, double phaseShift)
{
uint32_t psc = 1;
uint32_t arr;
//================================================================
//Calculate optimal prescaler and auto reload register for given new frequency (irrelevant for my problem)
while(psc < 2 * MCU_CLK / 0x10000){
arr = MCU_CLK / psc / frequency;
if(arr <= 0x10000){
psc--;
arr--;
break;
}
psc *= 2;
}
//================================================================
HAL_TIM_OC_Stop(&htim1, TIM_CHANNEL_1);
HAL_TIM_OC_Stop(&htim1, TIM_CHANNEL_2);
TIM1->PSC = psc;
TIM1->ARR = arr/2;
TIM1->CCR2 = 1 + (arr*(phaseShift/360));
TIM1->EGR |= TIM_EGR_UG;
HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_2);
}
The frequency, the phase shift and the 50% duty cycle work perfectly, it's just this problem with the polarity swapping.
Thank You!
RLD
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-16 3:31 AM
TIM1->EGR |= TIM_EGR_UG;
Don't.
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-16 4:04 AM
Thank You!
It works now, generating the update event was not necessary.
I have now solved it with activating the register preload feature.
RLD
