cancel
Showing results for 
Search instead for 
Did you mean: 

One Pulse Generation after N interrupts have occured.

NAnand1
Associate III

I have an application implemented with the NUCLEO STM32L552 board, in which I need to generate a sinewave of a known frequency (Period=T) and a single short pulse per cycle at a known phase angle say A.

I have divided the time period of the signal say T into 360 steps, with each step representing a degree,  and run a timer in Interrupt mode with the ARR value equal to T/360. In the Interrupt SR, I output the sine value from a pre-calculated source at a given degree, increment it, and reset the counter to 0 as it equals 360. This way the DAC successively outputs values from 0-360 and continues to output the sinewave which is working perfectly fine.

Now I have to generate a single pulse of a short fixed duration at a given degree in each cycle. I am not able to find the right way to do it. Could anyone guide me please.

4 REPLIES 4
nouirakh
ST Employee

Hello @NAnand1 ,

You need to configure a Second Timer or channel for Pulse Generation if your main timer has a second channel, you can use it otherwise, initialize the second timer and configure it in output compare mode( Set the compare value to the count corresponding to the phase angle 'A'). Then we need to Implement the Pulse Generation in the ISR. 
Fix ISR configurations (output compare event, delay...)
Finlay, After generating the pulse, reset the compare value to the count for the next phase angle 'A' in the next cycle.

NAnand1
Associate III

Thanks for the suggestion. Actually I do have a flag which goes TRUE when the angle match occurs with the set value. (I count up a variable in the interrupts) So I can start a pulse generator there which instantly goes high on starting, and goes low after the pulse width period..However I am unable to find the correct HAL command to start the pulse from the programme. (All documentation explains starting from internal or external triggers)

NAnand1
Associate III

Then we need to Implement the Pulse Generation in the ISR

Pl let me know how to do this part

> However I am unable to find the correct HAL command to start the pulse from the programme.

There may be no straighforward Cube/HAL API call for this - Cube/HAL inevitably implements only a fraction of what the hardware allows, whatever demed Cube authors to be "typical".

Generating a single pulse differs from generating PWM only one single detail, i.e. that you set the TIMx_CR1.OPM bit. Then you start pulse generation by starting the timer's counter, i.e. setting TIMx_CR1.CNT. So the preliminary action is:

- set TIMx_CCMRx.OCxM to the appropriate PWM mode (1 or 2)

- set TIMx_ARR to the total period, you may want to set it to (pulse-length + 1) so that the pulse starts only one timer clock cycle after enable

- set TIMx_CCRx to generate the pulse with required length (if TIMx_ARR is set to pulse_length + 1, set TIMx_CCRx to 1)

- set TIMx_CCER.CCxE to enable given channel; set also TIMx_BDTR.MOE, if needed

- set TIMx_CR1.OPM to 1

and then, if you want the pulse to be generated, just set TIMx_CR1.CEN to 1.

 

If you want to stick to Cube, as an experiment, try to set up PWM in CubeMX, set TIMx_CR1.OPM bit, and then enable the timer, while observing the given timer channel's output.

JW