cancel
Showing results for 
Search instead for 
Did you mean: 

Generating N-pulse with stm32f107

JJohn.3
Associate II

Hello,

I need to have N pulse wave form with specific frequency at PA0 and PD12 pins. I will be grateful if you could help me.

8 REPLIES 8

What have you accomplished so far? Where is the problem?

JW

JJohn.3
Associate II

Thank you very much,

I need to know the proper the ways to accomplish this purpose before doing anything in order to read and find out the procedures.

There are many ways, none of the is "the proper one", depending on the exact requirements.

Maybe the most straightforward one would be a timer set to PWM, with enabled interrupt upon Update; in the interrupt count to N and disable/stop the PWM.

JW

JJohn.3
Associate II

Dear waclawek.jan,

I appreciate your explanation and your suggestion.

I follow this website:https://stm32f4-discovery.net/2014/05/stm32f4-stm32f429-discovery-pwm-tutorial/

to generate a PWM signal

		TimeStruct.TIM_Prescaler=Prescale;
		TimeStruct.TIM_CounterMode=TIM_CounterMode_Up;
		TimeStruct.TIM_Period=Period;
		TimeStruct.TIM_ClockDivision=TIM_CKD_DIV1;
		TimeStruct.TIM_RepetitionCounter=0;
		TIM_TimeBaseInit(TIM5, &TimeStruct);	
		TIM_Cmd(TIM5, ENABLE);		
	
		TIMOCStruct.TIM_OCMode=TIM_OCMode_PWM2;
		TIMOCStruct.TIM_OutputState=TIM_OutputState_Enable;
		TIMOCStruct.TIM_OCPolarity=TIM_OCPolarity_Low;
		TIMOCStruct.TIM_Pulse=Pulse;
		TIM_OC1Init(TIM5, &TIMOCStruct);
		TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable);	

it works fine. But I need to know

1)how to enable an interrupt after N pulse? How to change this code to generate N pulse and an interrupt?

2)Based on my research, theoretically, PWM generates an interrupt after N+1 Pulse, Does it correct?

I don't use Cube, I use terminology given by RM.

My suggestion was to enable interrupt upon Update, i.e. setting. You would enable that interrupt before you enable the timer itself. In the interrupt routine, you would increment some variable, and if that variable reaches N, you would disable the timer.

Some timers have Repetition Counter (TIMx_RCR), which enable to count the number of pulses automatically in hardware. Using One-Pulse mode in those timers would lead them to automatically stop after a given number of pulses, with no software intervention needed; they would then also invoke the Update interrupt only after those N pulses. In STM32F4xx, only TIM1 and TIM8 have the TIMx_RCR register.

JW

JJohn.3
Associate II

I really appreciate your response.

Yes, but for counting the variable(for example for F=1Mhz duty cycle=50%) our timer should be enabled each 500nS which is not acceptable for my MCU.

My MCU (Stm32f107) has two advanced timers (Tim1 and TIM8) which my output pins are connected to TIM4 and TIM5 channel. Any other ideas?

This may be a bit more complicated. Maybe the easiest is have two timers set as master-slave, the slave being the timer which outputs PWM, set to Gated mode, and the master timer enabling the slave for the time needed to output the N pulses.

Another option is to use same mechanism you'd use for variable duty PWM using DMA, which would load respective CCRx N times the required duty and for the N+1th pulse it would load 0% duty.

JW

JJohn.3
Associate II

Thank you very much, could you provide me a reference about the first approach (master-slave) mode?