2021-09-11 08:22 PM
I am using the Bluepill board. I want to generate PWM signal on Timer2 and set up a timer interrupt at 20ms interval on Timer3. I realized that the PWM signal cannot be generated if I set up the timer interrupt on Timer3. How do I solve this problem?
Solved! Go to Solution.
2021-09-12 07:08 AM
> left_pwm / 1000000
> right_pwm / 1000000
These always evaluate to 0. If you want to use float, cast them to float first instead.
TIM2->CCR1 = (int) ((((float) left_pwm / 1000000) / 0.02) * 65535);
TIM2->CCR2 = (int) ((((float) right_pwm / 1000000) / 0.02) * 65535);
2021-09-29 04:40 PM
The constant 0.02 is of a double type and because of the brackets there will be double operations, including the division, at runtime. One can write 0.02f to use a float type, but in this case the whole expression can be reordered into much more efficient.
TIM2->CCR1 = (float)left_pwm * (float)(65535 / 0.02 / 1000000);
No double operations at runtime, no divisions at all - just a single multiplication at runtime.