cancel
Showing results for 
Search instead for 
Did you mean: 

Generate code for STM32CubeIDE (V1.13.1) from STM32CubeMX (6.9.1)

cr_qc
Associate II

Hello,

I am newbie to use of these tools. I want to initialize all the I/O and some modules on a STM32F030CC micro-controller.  I defined all the I/O from STM32CubeMX.  I want to use the 2 PWM output on PB0 (TM3_CH3) and PB1 (TM3_CH4). I transfer the project on IDE, compile and run it.  All GPIO work fine but the I do not have the PWM outputs. What is wrong?

static void MX_TIM3_Init(void)
{

/* USER CODE BEGIN TIM3_Init 0 */

/* USER CODE END TIM3_Init 0 */

TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};

/* USER CODE BEGIN TIM3_Init 1 */

/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 65535;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 1234;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
{
Error_Handler();
}
sConfigOC.Pulse = 4321;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */

/* USER CODE END TIM3_Init 2 */
HAL_TIM_MspPostInit(&htim3);

}

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

You need to start the PWM by doing a HAL_TIM_PWM_Start() call.

HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);

 See an example PWM application here:

https://github.com/STMicroelectronics/STM32CubeF0/blob/b13360984a1f68fed5bf7a58f8d81f919778ed38/Projects/STM32072B_EVAL/Examples/TIM/TIM_PWMOutput/Src/main.c#L179

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

3 REPLIES 3
cr_qc
Associate II

Last section:

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(htim->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspPostInit 0 */

/* USER CODE END TIM3_MspPostInit 0 */

__HAL_RCC_GPIOB_CLK_ENABLE();
/**TIM3 GPIO Configuration
PB0 ------> TIM3_CH3
PB1 ------> TIM3_CH4
*/
GPIO_InitStruct.Pin = POS_PWM_Pin|MOT_ENABLE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* USER CODE BEGIN TIM3_MspPostInit 1 */

/* USER CODE END TIM3_MspPostInit 1 */
}

}

TDK
Guru

You need to start the PWM by doing a HAL_TIM_PWM_Start() call.

HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);

 See an example PWM application here:

https://github.com/STMicroelectronics/STM32CubeF0/blob/b13360984a1f68fed5bf7a58f8d81f919778ed38/Projects/STM32072B_EVAL/Examples/TIM/TIM_PWMOutput/Src/main.c#L179

If you feel a post has answered your question, please click "Accept as Solution".
cr_qc
Associate II

Thank you! Work!