cancel
Showing results for 
Search instead for 
Did you mean: 

PWM generation exact number of pulses

shubhamshete22
Associate
We are generating PWM using the STM32F429 TIM10 -- 
In this call-back function we counting the PWM pulses and , when counting pulse is equal to the total pulse then stop the PWM.But in this code counter are counting the properly counting 
the pulses but actually hardware send the some pulses extra for  above the 70KHz frequency.
for example, suppose my total pulse is 650000 with 100000KHz frequency,when we start the generating the PWM pulse then the  HSO_Count[0] counting the pulse, this buffer is showing 
correct pulses but actually hardware transmit the 650030 pulses. 
 
 
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
 
if(htim -> Instance == TIM10)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
HSO_Count[0]++;
 
if(HSO_Count[0]==HSO_Count_Total[0])
{
HAL_TIM_PWM_Stop_IT(&htim10, TIM_CHANNEL_1);
}
 
 
}
 
}
 
 
}
 
 
 
 
2 REPLIES 2
KnarfB
Principal III

The software handling is too slow to stop the hardware in time. You could improve a little by moving from HAL to low-level / register-level programming. 

The fastest method will be using some form of timer synchronisation. Maybe application note AN4776 "General-purpose timer cookbook for STM32 microcontroller" brings you some ideas.

hth

KnarfB

> You could improve a little by moving from HAL to low-level / register-level programming.

Avoiding the Cube/HAL interrupt handling in fact will improve things a lot, and together with proper compiler optimization and adequately high interrupt priority will have no problem with a 10us latency (i.e. will be good up to 100kHz) on a 'F4xx running at its maximum system frequency.

But it's always better to solve this kind of problems using purely hardware, if possible.

> The fastest method will be using some form of timer synchronisation.

That probably means the master-slave (TRGO-TRGI) interconnection between timers; however, there is no slave-mode controller in TIM10 in 'F4, so you'd need to choose a different timer. Starting/stopping a timer from another timer through writing to its registers using DMA may be an effective option, but it's not the simplest one.

You can also work around the problem e.g. by using SPI+DMA and outputting multiple-of-8 or -16 pulses on SCK.

JW