cancel
Showing results for 
Search instead for 
Did you mean: 

Difficulty with speed of updating PWM duty cycle

ethanbraun
Associate

Hi, I'm a relatively new programmer, attempting to program a "Bluepill" module, which has the STM32F103C8T6 microcontroller. I'd like to be able to quickly change the duty cycle on a PWM output, so that I can adjust the position of a servo motor as I change the position of an attached joystick. The trouble is that every time I change the value of the CCR register for the desired timer and channel, there is an enormous delay (usually about 5 seconds or so) until the PWM duty cycle actually changes to reflect the new value, and subsequently, when the servo changes position.

I used this tutorial as a starting point before modifying it a bit to include a handle for the servo motor that I attach. I've attached my main.c file as well as the source and header files I made for the servo itself. You can find the call to SERVO_MoveTo on line 270 of main.c.

Should I be using DMA to transfer the data over to the CCR register quicker? I'm not sure if that would be appropriate or not.

 

Thank you in advance.

1 REPLY 1
KnarfB
Principal III

I won't go through all your code, but there is no hardware feature that significantly delays CCRx register updates even without DMA. Check all your calculations and/or add debug output.

I would set the prescaler such that the timer increments per microsecond like

PSC = (HAL_RCC_GetPCLK1Freq()/1000000)-1 Note the -1.

Then, for a 50ms period

ARR=50000-1

which makes it easy to set CCR to a value in the range you want.

Since you are using the HAL functions, update CCR with

__HAL_TIM_SetCompare(&htimX, TIM_CHANNEL_Y, Z-1);

(your struct member for CCR is missing a volatile which may be crucial)

hth

KnarfB