2024-09-15 11:16 PM
Hi,
Here is my problem, I need to output two different frequencies on one pin and I can't reconfigure the timer to new arr/psc values for a different frequency because these timers also output the frequency on other pins where the frequency/timer config must remain the same. The pins are all in separate channels from the timer. I just want to be able to switch the pin between two timer outputs. I am completely stuck and look forward to any help!
RLD
2024-09-16 12:37 AM - edited 2024-09-16 12:38 AM
Hi,
so you could switch the pin to another timer : see ds, alternate funcions on port/pins ->
if there is in alternate functions on a certain pin - maybe Tim1ch2 and Tim4ch1 - then you can just set this pin (moder bits) to the other function and you get, what you want.
If no other timer ch on this pin - you cannot.
2024-09-16 12:46 AM
Thank you!
I have already checked that in advance.
My pin has TIM1_CH3 and TIM20_CH2. So that should be possible.
But I have my struggles with reconfiguring, aka with the “moder bits”. I'm pretty new to this and have never worked with them before, I could always configure my uC with CubeMX in the past.
Could you maybe give me an example, that would help me a lot.
RLD
2024-09-16 01:44 AM - edited 2024-09-16 07:34 AM
>Could you maybe give me an example
no.
Just set in Cube your timer1 pwm output, and tim20ch2 to pwm/no output . (I never tried, but should work -> )
Then write to port x to the moder afr register, (&= and |= ) the desired value to get the other alt-function setting for this pin. And back to tim1 output same way.
2024-09-16 03:21 AM
It's not GPIOx_MODER, that should be set to AlternativeFunction all the time.
It is GPIOx_AFR[] which you have to change.
Assuming you are talking about PC2
the field for that is in GPIOC_AFR[0], each field is 4 bits wide, so you can write:
// set PC2 to TIM1_CH3
GPIOC->AFR[0] =
(GPIOC->AFR[0] & ~(0b1111 << (2 * 4))) // first mask/clear this bitfield
| (2 << (2 * 4)) // and then set it to 2 for AF2 for TIM1_CH3
;
// set PC2 to TIM20_CH2
GPIOC->AFR[0] =
(GPIOC->AFR[0] & ~(0b1111 << (2 * 4))) // first mask/clear this bitfield
| (6 << (2 * 4)) // and then set it to 6 for AF6 for TIM20_CH2
;
JW
2024-09-16 05:23 AM
Thank you!
Its correct, I am talking about PC2.
In my configuration, TIM1_CH3 runs on Output Compare Toggle and TIM20_CH2 on PWM Mode 1.
Do I have to stack these two alternate functions on this pin?
or do I have to configure the timers as non output, and use the GPIOC_AFR[0] to select which alternate function I want to access with the GPIO?
RLD
2024-09-16 05:53 AM
You haven't stated the frequencies you require but one solution would be to set the timer for some to say a 1uS interrupt then in you interrupt routine be to toggle the pin based on a target value.
i.e. target 1: toggle a 5uS would give a 10uS pulse
target 2: toggle at 10uS would give a 20uS pulse
2024-09-16 06:04 AM
I don't use Cube/CubeMX so I don't know. Try "stacking" perhaps. If you configure them as non output, you'd need to enable the outputs first "manually".
JW