cancel
Showing results for 
Search instead for 
Did you mean: 

Retriggering pulse

Hello dear friends

I use the stm32f4 series. I am looking for some code to retrigger a similar pulse. To be clear, I have some circuits for zero-cross detection. Now, for each input pulse before the next in[ut pulse, I want some output pulse that would be triggered after some specific delay time, by which the output pulse is zero in delay time and active after delay, and would become zero when the next input pulse is caught. I considered using Either external interrupt or timer input capture mode for the input pulse. But for external output, my research has led me to timer one pulse mode. I couldn't find some example code with results on the internet.

The signaling is as shown in the image. T1 is the delay and T2 is the time that signal is active. For a 50hz input pulse, T1+T2 will be 20ms. What I want is a code that doesn't block MCU in the interrupt routine and T1 could be controlled.

The .ioc file is attached.

help me, please

1 ACCEPTED SOLUTION

Accepted Solutions
  • enable clock of GPIOx and TIMx
  • set up TIMx_CH1 and TIMx_CH2 as AF in GPIOx_MODER, and set proper AF number into respective GPIOx_AFR[]
  • set CH1 as Input Capture TIMx_CCMR1.CC1S = 0b01
  • set CH1 also as input into the slave-mode controller TIMx_SMCR.TS = 0b101
  • set slave-mode controller to Reset mode TIMx_SMCR.SMS=0b100
  • set CH2 as Output Compare TIMx_CCMR1.CC2S=0b00
  • set CH2 to output PWM, TIMx_CCMR.OC2M = 0b111 (PWM2 mode used to ensure low when TIMx_CNT < TIMx_CCR2)
  • enable CH2 output TIMx_CCER.CCxE=1
  • set TIMx_PSC if needed for longer periods, but if you do so, force also update by setting TIMx_EGR.UG=1, and then you may want to clear the status register TIMx_SR=0 (but leave some time between setting UG and clearing status register)
  • set TIMx_ARR to the longest anticipated period, or maybe just leave it at its maximum which is the default
  • set TIMx_CCR2 to value equivalent to time T1, you can change it later anytime you want
  • set timer to One-Pulse mode, TIMx_CR1.OPM=1
  • enable timer, TIMx_CR1.CEN=1

Drawback of this method is, that if the stream from the zero-cross detector stops, the timer overflows and then stops too, and there will be no more pulses. If this is something which happens rarely, I would enable interrupt upon Update (= overflow) and handle this situation in software in the interrup.

JW

View solution in original post

6 REPLIES 6

I don't understand what you want to accomplish. Can you please draw a timing diagram?

JW

Dear @Community member​ 

Thank you for your response

The question is edited and I hope that it has become clear.

  • enable clock of GPIOx and TIMx
  • set up TIMx_CH1 and TIMx_CH2 as AF in GPIOx_MODER, and set proper AF number into respective GPIOx_AFR[]
  • set CH1 as Input Capture TIMx_CCMR1.CC1S = 0b01
  • set CH1 also as input into the slave-mode controller TIMx_SMCR.TS = 0b101
  • set slave-mode controller to Reset mode TIMx_SMCR.SMS=0b100
  • set CH2 as Output Compare TIMx_CCMR1.CC2S=0b00
  • set CH2 to output PWM, TIMx_CCMR.OC2M = 0b111 (PWM2 mode used to ensure low when TIMx_CNT < TIMx_CCR2)
  • enable CH2 output TIMx_CCER.CCxE=1
  • set TIMx_PSC if needed for longer periods, but if you do so, force also update by setting TIMx_EGR.UG=1, and then you may want to clear the status register TIMx_SR=0 (but leave some time between setting UG and clearing status register)
  • set TIMx_ARR to the longest anticipated period, or maybe just leave it at its maximum which is the default
  • set TIMx_CCR2 to value equivalent to time T1, you can change it later anytime you want
  • set timer to One-Pulse mode, TIMx_CR1.OPM=1
  • enable timer, TIMx_CR1.CEN=1

Drawback of this method is, that if the stream from the zero-cross detector stops, the timer overflows and then stops too, and there will be no more pulses. If this is something which happens rarely, I would enable interrupt upon Update (= overflow) and handle this situation in software in the interrup.

JW

Dear @Community member​ 

Thank you for your response, would you please check the .ioc file I have attached to the question if it complies with your instructions?

I don't use Cube/CubeMX, sorry.

JW

Dear @Community member​ 

All that you said should be done. But I have one question that couldn't find in the datasheet. When I start the OPM, I expected the timer to stop on arr and be able to restart the timer anywhere else, but I couldn't do that, alternatively, I made arr a bit longer stopped the timer, and restart that.

Also I should mention that TIM.in1 and TIM.out2 share the same code in cube generated code, so I shifted to TIM.in1 and out3