cancel
Showing results for 
Search instead for 
Did you mean: 

changing output compare value while code is running

shivam1
Associate II

i am using stm32f446re. i am using timer in synchronization mode one timer triggers another timer so my query is master timers triggers slave timer to generate another timer pwm so that triggers value  i want to control so what hal function i use to control this triggers value while code is running then i want to change this trigger value.

 

here is an example what i want-

- lets say master generate 50% duty cycle pwm so when master is 25% of its pwm then slave gets trigger to generate its own pwm.so i want to control this 25% value. so what HAL function i use to control this value while code is running.  

1 ACCEPTED SOLUTION

Accepted Solutions

If you use Trigger mode of slave-mode controller in slave timer, that only sets TIMx_CR1.CEN, so it does nothing if TIMx_CR1.CEN is already set (i.e. if the slave timer is running continuously). If that's the case, you have to clear TIMx_CR1.CEN in slave when you change the compare value in master.

JW

View solution in original post

5 REPLIES 5

If you use Trigger mode of slave-mode controller in slave timer, that only sets TIMx_CR1.CEN, so it does nothing if TIMx_CR1.CEN is already set (i.e. if the slave timer is running continuously). If that's the case, you have to clear TIMx_CR1.CEN in slave when you change the compare value in master.

JW

Sarra.S
ST Employee

Hello @shivam1

check this HAL macro: 

/**
  * @brief  Sets the TIM Capture Compare Register value on runtime without
  *         calling another time ConfigChannel function.
  * @param  __HANDLE__: TIM handle.
  * @param  __CHANNEL__ : TIM Channels to be configured.
  *          This parameter can be one of the following values:
  *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
  *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
  *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
  *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
  * @param  __COMPARE__: specifies the Capture Compare register new value.
  * @retval None
  */
#define __HAL_TIM_SET_COMPARE(__HANDLE__, __CHANNEL__, __COMPARE__) \
(*(__IO uint32_t *)(&((__HANDLE__)->Instance->CCR1) + ((__CHANNEL__) >> 2)) = (__COMPARE__))

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

shivam1
Associate II

thanks for your reply....

i know this function already but this not what i am asking i want to change that trigger value not width of any pwm.that function you provided is used for changing the width of pwm. i want to change that trriger value which activate second slave timer to turn on. please read my given example in my question.... 

see in this photo i want to change this arrow mark value.. tell which hal macro i use to change it  

Screenshot 2024-04-19 115943.png

I don't use Cube/HAL.

You can look at the HAL_TIM_OC_ConfigChannel() function's source, what does it do with that value, but it probably simply stores it into TIMx_CCRx.

The macro @Sarra.S posted looks very much like it does exactly this.

In your original question, TIMx_CCRx of master provides the point, where it generates TRGO which is then input as TRGI to slave. However, as I've said, if you use Trigger mode of the Slave-mode controller of the slave timer, it sets its TIMx_CR1.CEN only once, so subsequent TRGI from master are ignored.You have to clear slave's TIMx_CR1.CEN (stop the slave counter) to have it triggered from the master (with already adjusted TIMx_CCRx which provides the moment of trigger) again.

JW