In this program, we configured the TIM1 timer to generate a frequency of 1kHz. In the while loop, you can see how we use __HAL_TIM_SET_COMPARE to modify the duty cycle of the signal when the PWM is already active. Note how these values relate to the percentage of the signal and the value loaded in the Period field when configuring the timer frequency.

#include "bsp.h"

TIM_HandleTypeDef TimHandle;    /*TIM control structure*/
TIM_OC_InitTypeDef sConfig;     /*PWM channel structure*/

int main( void )
{
    HAL_Init();  /*Init library*/

    /* Config TIM1 to generate a signal with a 1ms/1KHz period*/
    TimHandle.Instance           = TIM1;
    TimHandle.Init.Prescaler     = 10;
    TimHandle.Init.Period        = 1600;
    TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;
    HAL_TIM_PWM_Init(&TimHandle);

    /* Config PWM channel with high polarity at 10% duty cycle */
    sConfig.OCMode     = TIM_OCMODE_PWM1;
    sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfig.OCFastMode = TIM_OCFAST_DISABLE;
    sConfig.Pulse = 160;
    HAL_TIM_PWM_ConfigChannel( &TimHandle, &sConfig, TIM_CHANNEL_1 );
    HAL_TIM_PWM_Start( &TimHandle, TIM_CHANNEL_1 );

    while(1)
    {
        /* reset the duty cycle to 25% */
        __HAL_TIM_SET_COMPARE( &TimHandle, TIM_CHANNEL_1, 400 );
        HAL_Delay( 500 );

       /* reset the duty cycle to 50% */
        __HAL_TIM_SET_COMPARE( &TimHandle, TIM_CHANNEL_1, 800 );
        HAL_Delay( 500 );

        /* reset the duty cycle to 75% */
        __HAL_TIM_SET_COMPARE( &TimHandle, TIM_CHANNEL_1, 1200 );
        HAL_Delay( 500 );
    }
}

msps.c

/*function called by HAL_TIM_PWM_Init to configure the slected pins as PWM outputs*/
void HAL_TIM_PWM_MspInit( TIM_HandleTypeDef *htim )
{
    GPIO_InitTypeDef   GPIO_InitStruct;

    __TIM1_CLK_ENABLE();  /*eneable clock in TIM1*/
    __GPIOC_CLK_ENABLE(); /*eneable clock on port C*/

    /*config pin C8 in altern mode 2 as TIM1_CH1*/
    GPIO_InitStruct.Pin = GPIO_PIN_8; 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF2_TIM1;
    HAL_GPIO_Init( GPIOC, &GPIO_InitStruct );
}