Skip to main content
Visitor II
September 16, 2024
Question

two timer signals on one pin

  • September 16, 2024
  • 3 replies
  • 1433 views

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

    This topic has been closed for replies.

    3 replies

    Super User
    September 16, 2024

    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.

    RLDAuthor
    Visitor II
    September 16, 2024

    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

    Super User
    September 16, 2024

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

     

    Super User
    September 16, 2024

    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

    RLDAuthor
    Visitor II
    September 16, 2024

    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

    Super User
    September 16, 2024

    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

     

    Visitor II
    September 16, 2024

    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