cancel
Showing results for 
Search instead for 
Did you mean: 

What is correct to update PWM pulse width in STM32F0 HAL?

Junaid PV
Associate II
Posted on April 14, 2018 at 07:52

Hi,

I am trying to learn PWM generation with STM32F030F3P6. Generated code using STM32CubeMX. Basic PWM generation worked fine.

Now I am trying to change PWM pulse width dynamically in while loop of

main()

. Please see my main function. I am trying to fade-in and fade-led. But LED remains turned-off.

int main(void)
{
 uint16_t pwm_value = 0;
 uint16_t step = 10;
 HAL_Init();
 SystemClock_Config();
 MX_GPIO_Init();
 MX_TIM14_Init();
 HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1);
 while (1)
 {
 HAL_Delay(100);
 if(pwm_value = 0) {
 step = 50;
 }
 if(pwm_value >= 1000) {
 step = -50;
 }
 pwm_value += step;
 __HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, pwm_value);
 }
}
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Here is full main.c file (most of comments are stripped out).

#include 'main.h'
#include 'stm32f0xx_hal.h'
TIM_HandleTypeDef htim14;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM14_Init(void);
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
int main(void)
{
 uint16_t pwm_value = 0;
 uint16_t step = 10;
 HAL_Init();
 SystemClock_Config();
 MX_GPIO_Init();
 MX_TIM14_Init();
 HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1);
 while (1)
 {
 HAL_Delay(100);
 if(pwm_value = 0) {
 step = 50;
 }
 if(pwm_value >= 1000) {
 step = -50;
 }
 pwm_value += step;
 __HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, pwm_value);
 }
}
/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct;
 RCC_ClkInitTypeDef RCC_ClkInitStruct;
 /**Initializes the CPU, AHB and APB busses clocks
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 /**Initializes the CPU, AHB and APB busses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 /**Configure the Systick interrupt time
 */
 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
 /**Configure the Systick
 */
 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
 /* SysTick_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/* TIM14 init function */
static void MX_TIM14_Init(void)
{
 TIM_OC_InitTypeDef sConfigOC;
 htimInstance = TIM14;
 htimInit.Prescaler = 7;
 htimInit.CounterMode = TIM_COUNTERMODE_UP;
 htimInit.Period = 1000;
 htimInit.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htimInit.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 if (HAL_TIM_Base_Init(&htim14) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 if (HAL_TIM_PWM_Init(&htim14) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 sConfigOC.OCMode = TIM_OCMODE_PWM1;
 sConfigOC.Pulse = 0;
 sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
 if (HAL_TIM_PWM_ConfigChannel(&htim14, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 HAL_TIM_MspPostInit(&htim14);
}
/** Configure pins as
 * Analog
 * Input
 * Output
 * EVENT_OUT
 * EXTI
*/
static void MX_GPIO_Init(void)
{
 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOF_CLK_ENABLE();
 __HAL_RCC_GPIOA_CLK_ENABLE();
}
void _Error_Handler(char *file, int line)
{
 while(1)
 {
 }
}
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

PWM generation woks good if I remove code lines within while loop and put any desired value for line:

sConfigOC.Pulse = 0;�?

As far as I think, I am not calling __HAL_TIM_SET_COMPARE() right way or something else is required there. Could you please help me to identify that?

Thanks in advance. #stm32-f0 #timer #pwm
5 REPLIES 5
Posted on April 15, 2018 at 12:10

Fill in all fields of sConfigOC.

JW

Posted on April 15, 2018 at 18:24

Consider using a scope to check the signal.

Consider using a lower frequency so you can perceive the on/off time.

Try a fixed 50/50 duty at 1Hz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 15, 2018 at 18:49

MX_TIM14_Init() already contains setting up with all required fields filled in. Remaining parameters are OCIdleState and OCNIdleState, those are only valid for TIM1 and TIM8 (as per HAL code documentation comments), while I am using TIM14.

Posted on April 15, 2018 at 20:59

... and OCNPolarity but you are right, those you've set appear to be sufficient.

Read out and check/post the content of relevant GPIO and TIM registers.

JW

Hahnaar
Associate
 if(pwm_value = 0) {
 step = 50;
 }

There is a misstake in your main. You use a assignment operator "=" instead a comparison operator "==".

The rest should work;)