cancel
Showing results for 
Search instead for 
Did you mean: 

Seeking Guidance on Reading Multiple Values from RC using PWM Input with STM Timer

Matyáš
Associate II

Hello Community,

I've been grappling with a challenge in my project involving the STM series, and despite my efforts, I haven't been able to find a comprehensive tutorial addressing my specific issue. Here's a snippet of what I have so far:

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef * htim2)
{
capture_value = HAL_TIM_ReadCapturedValue(htim2, TIM_CHANNEL_1);
capture_value2 = HAL_TIM_ReadCapturedValue(htim2, TIM_CHANNEL_2);
capture_value3 = HAL_TIM_ReadCapturedValue(htim2, TIM_CHANNEL_3);
capture_value4 = HAL_TIM_ReadCapturedValue(htim2, TIM_CHANNEL_4);
}

HAL_TIM_IC_Start(htim2, TIM_CHANNEL_1);
HAL_TIM_IC_Start(htim2, TIM_CHANNEL_2);
HAL_TIM_IC_Start(htim2, TIM_CHANNEL_3);
HAL_TIM_IC_Start(htim2, TIM_CHANNEL_4);

I've defined the channels and variables accordingly.

The whole point of my problem is that when I set the timer to combined channels and PMW input on channel 1, It shows there is only one gpio input for it. So Im basically forced to connect all 4 channels of RC via one pin and so then connecting them all toghether and during testing via "Live expressions" the value is controlled by all the channels.

So I tried to do the same thing, but istead of Combined channles, i tried to set all of the 4 channels of TIM to PMW generation.And during debuging, Im suddenly presented with all of the values are zero and not changing.

i think i tried everything. I love the STM series, but Im just beginner and from my persepction, there arent too much of tutorials sadly, so Im trying the power of this community, please help me. I welcome all posible solutions. Thanks

 

5 REPLIES 5
TDK
Guru

Can you clarify what the problem is specifically? TIM2 does indeed have one timer.

> So I tried to do the same thing, but istead of Combined channles, i tried to set all of the 4 channels of TIM to PMW generation.And during debuging, Im suddenly presented with all of the values are zero and not changing.

The code you posted is for input capture, not PWM generation. Which one do you want and what are you trying to achieve?

There are PWM generation examples in CubeMX. Here is one of them that generates PWM on all 4 channels of a timer:

https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM32F429ZI-Nucleo/Examples/TIM/TIM_PWMOutput

 

If you feel a post has answered your question, please click "Accept as Solution".

Im trying to read the values from my RC using stm32f407g-discovery for this project. Do you also have an example for this board by any chance? 

This code works as I want to, but only for one channel, can I somehow rework this code so the pmw input will be at least for four main channels?

uint32_t frequency, duty_cycle;
uint32_t capture_value;
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef*htim)
{
if(htim ->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
capture_value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
if (capture_value)
{
frequency = SystemCoreClock / (capture_value);
duty_cycle = 10000 * HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2) / capture_value;

}

}

HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);


}

>>stm32f407g-discovery for this project. Do you also have an example for this board by any chance? 

I posted one back in the day using the SPL, not sure I've had cause to rework it.

But yes, probably want ONE TIM per RC channel because PWM Input mode uses 2 TIM Channels (CH1/CH2) to measure Period and Duty, and resets the CNT. Assuming your RC receiver device is synchronous, you might be able to use CH3 and CH4 to simply latch the count.

Personally, I'd probably have the thing clocking at 1 MHz, giving micro-second readings of the phase directly. The 20 ms / 50 Hz (20000 us) fitting within the capabilities of the 16-bit TIM's too.

You might be able to Input 4 channels if you can use one channel's rising edge to synchronize / reset the counter and then measure the falling edges in the capture registers

Generation of RC Servo signals, each TIM can generate FOUR PWM Output signals, programming the signal width in micro-seconds into each channel's compare register, and a TIM period of 20 ms / 50 Hz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Always start with stating, which STM32 are you using, perhaps also what hardware and software.

Also, "RC" may not be an universally recognized abbreviation so you should have explained that too; I presume it's some sort of a remote control, and you want to capture output of the remote unit, which through PWM controls servos.

What ST calls "PWM input" is a combination of capture on two channels utilizing the possibility of redirect one channel to the neighbouring one and invert it; in conjunction wiht Reset mode of the Slave-mode Controller, (see description of TIMx_SMCR.SMS in TIM chapter of Reference Manual (RM). So, if you want to utilize this mode, which resets the counter upon one of the edges (and there's only one counter per timer), you can capture only one PWM signal per timer.

If the PWM is guaranteed to have both phases long enough (i.e. there's a guaranteed minimum and maximum duty cycle and the frequency is fixed), and the shortest time is long enough to evaluate interrupt even in the worst case (*), you can simply leave the timer free running, and use plain capture on individual channels.

 (*) - this may or may not be possible with given RC and your given hardware, also depends on your programming skills, and possibly also the ability of get rid of Cube/HAL which is not written with speed in mind.

Another option would be to use DMA, but that's a long story and I don't think it is

So, your best option now, is to split your 4 input signals to 4 timers.

JW