1
\$\begingroup\$

The goal is to use the PWM feature of stm32 HAL TIM libraries to light up 4 leds on pins 0, 1, 4 and 5

I have generated the following code using CubeMX:

void MX_TIM3_Init(void)
{
 TIM_ClockConfigTypeDef sClockSourceConfig;
 TIM_MasterConfigTypeDef sMasterConfig;
 TIM_OC_InitTypeDef sConfigOC;
 htim3.Instance = TIM3;
 htim3.Init.Prescaler = 32;
 htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim3.Init.Period = 1000;
 htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 HAL_TIM_Base_Init(&htim3);
 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
 HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
 HAL_TIM_PWM_Init(&htim3);
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
 sConfigOC.OCMode = TIM_OCMODE_PWM1;
 sConfigOC.Pulse = 500;
 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
 HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);
 HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2);
 HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3);
 HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4);
 HAL_TIM_MspPostInit(&htim3); 
}

This initiates the timer.

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{ 
 GPIO_InitTypeDef GPIO_InitStruct;
 if(htim->Instance==TIM3)
 {
 /* USER CODE BEGIN TIM3_MspPostInit 0 */
 /* USER CODE END TIM3_MspPostInit 0 */
 /**TIM3 GPIO Configuration 
 PB0 ------> TIM3_CH3
 PB1 ------> TIM3_CH4
 PB4 ------> TIM3_CH1
 PB5 ------> TIM3_CH2 
 */
 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
 GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 /* USER CODE BEGIN TIM3_MspPostInit 1 */
 /* USER CODE END TIM3_MspPostInit 1 */
 }
}

This initiates the GPIO pins for PWM.

Also the clock is enabled in the HAL_TIM_Base_MspInit callback function using __TIM3_CLK_ENABLE().

In the main.c file I have added the following code after all initializations, to start the base timer and PWM:

HAL_TIM_Base_Start(&htim3);
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_ALL);

Now after running this code on the stm32f070cb the LEDs do not light up at least slightly.

I have tried changing the compare values CCRx. Also the GPIO pins work since I can use HAL_GPIO_WritePin to turn on the LEDs. Besides that I found out that the counter is actually running between 0 and 1000 using the __HAL_TIM_GetCounter macro. It just seems that the LEDs are not reacting to the compare values.

Did someone have a similar problem or have some hints to go from here?

Bence Kaulics
6,48312 gold badges35 silver badges61 bronze badges
asked Apr 13, 2016 at 10:46
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

Thanks to a kind suggestion I was able to get the PWM working using the TIM_PWMOutput example under the STM32F0 CubeMX firmware:

STM32Cube_FW_F0_V1.5.0\Projects\STM32F0308Discovery\Examples\TIM\TIM_PWMOutput

I have now solved my problem using the code from the example. Though I am not sure where it is different from the code generated by CubeMX.

answered Apr 14, 2016 at 14:24
\$\endgroup\$
0
\$\begingroup\$

I recently experienced the same problem with you. Modify the prototype of function HAL_TIM_MspPostInit(void) as below :

/**/
static void HAL_TIM_MspPostInit(void)
{ 
 GPIO_InitTypeDef GPIO_InitStruct;
 /**TIM3 GPIO Configuration 
 PB0 ------> TIM3_CH3
 PB1 ------> TIM3_CH4
 PB4 ------> TIM3_CH1
 PB5 ------> TIM3_CH2 
 */
 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
 GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

and use the function as HAL_TIM_MspPostInit(); in the main function. Basically, this is same with MX_GPIO_Init function you may find in the HAL libraries. I cannot understand why they added another functions to initiate GPIOs for PWM in particular.

ThreePhaseEel
9,1284 gold badges29 silver badges43 bronze badges
answered Dec 30, 2017 at 14:42
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.