cancel
Showing results for 
Search instead for 
Did you mean: 

two timer signals on one pin

RLD
Associate III

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

7 REPLIES 7
AScha.3
Chief II

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.

If you feel a post has answered your question, please click "Accept as Solution".
RLD
Associate III

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

>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.

 

If you feel a post has answered your question, please click "Accept as Solution".

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

waclawekjan_0-1726481798596.png

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

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

MHank.1
Associate III

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

 

 

PartsBin - An Electronic Parts Organizer for Windows

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