Skip to main content
Visitor II
July 3, 2024
Question

Stat and stop toggle of output compare mode precisely

  • July 3, 2024
  • 2 replies
  • 1209 views

Hi all, 

 

for a projet I need to create a signal at 500khz, and every 60 µs I want to stop the toggle (or not) 

I generate the signal with the output compare of tim2 (on stm32L073)

then, 

in 

TIM2_IRQHandler(void)

I try to count the number of call, then stop the toggle. but the in fact, I have more than 5 period.

the command to stop and start the toggle is correct, but the bit duration not. where can I count the exact number of the toggling of tim2 output compare please?

 

bit_duration = bit_duration +1;

 

if (bit_duration == 5) // 5 is an exemple to test.

{

TIM2 -> CCMR1 &= ~TIM_CCMR1_OC1M; //set to 0 = keep level

TIM2 -> CCMR1 |= (3 << TIM_CCMR1_OC1M_Pos); // restart the toogle

bit_duration = 0;

}

    This topic has been closed for replies.

    2 replies

    Super User
    July 3, 2024

    500kHz toggle means 1MHz interrupt rate. That's a bad idea.

    Try using timers in master-slave arrangement, slave in gated mode.

    JW

    Seth1erAuthor
    Visitor II
    July 3, 2024

    thanks to your reply.

     

    I read the document to give, that's why I use the output compare mode of the timer to generate de 1MHz interrupt and toggle.

     

    I will try to use an other timer interrupt base on the tim2 1MHZ interrupt but I need to read more about that I never do this.

    thank you 

    Super User
    July 4, 2024

    No, I mean, don't use interrupts at all.

    Run one timer so that it overflows at 1MHz rate, set one of its outputs to toggle, that will generate you the 500kHz waveform.

    Use that timer as slave, i.e. you set its slave-mode controller (in TIMx_SMCR) to Gated mode, and select an another , master timer as TRGI source.

    In that master timer, set it to overflow at 100kHz rate, set one of its channels to generate PWM, and select that channel (in TIMx_CR2) to be source of TRGO, which goes towards the slave timer.

    That will result in modulated 500kHz waveform; by changing the master timer's channel (which generates the TRGO signal) you can change the modulation.

    JW