Reputation: 147
Goal: implementing a phase shift between PWM TIM1 and TIM8 (STM32F303CBT6).
How will I do this:
I will configure TIM1/TIM8 as MASTER/SLAVE and change the phase shift between them.
TIM_SetCounter(TIM1, 0); // 0° phase shift
TIM_SetCounter(TIM1, 72); // 180° phase shift
What I encountered:
I want to convert the phase shift from degrees to the number of ticks, which I will write to TIM_SetCounter. (0..72 ticks = 0..180°)
Where f = 100 kHz is the PWM frequency. Period = 10 µs, half-cycle 5 µs.
72 ticks is half a period (100 kHz), the entire period is 144 ticks. (based on TIM1/TIM8 timer settings). It turns out that the time delay will be in the range from 0 to 5 µs.
I have compiled the following formulas to calculate the phase shift:
int degPhaseShiftCalc(int tdelay) {
int phShift = (360 * 100 * tdelay) / 1000;
return phShift;
}
int tDelayCalc(int phShift) {
float tdelay = (phShift * 1000) / (360 * 100);
int result = round(tdelay / 0.07);
return result;
}
Where 0.07 is the number microseconds per 1 tick. (5us/72ticks) I used rounding because the system only accepts whole numbers of ticks.
Calculation results:
With a small phase shift, I have an output of 0 ticks in the range of 0..35 degrees. I can assume that this is due to the small number of ticks per half period and you need to change the timer settings so that the number is > 72.
Question: What is the best way to implement the function of converting degrees to ticks?
SOLVED Thank you @Simon Goater, @Chris_B
float tdelay = ((float)phShift * 1000.0) / (360.0 * 100.0);
Converting to float solved the problem.
Upvotes: 2
Views: 212