cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F030R8T6 TIM1 PWM NO WORK FOR STM32CUBEMX

JACK TAN
Associate II
Posted on March 27, 2017 at 19:20

HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);

HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);

HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);

HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);

__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,40);

__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,80);

__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,120);

__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_4,190);

//TIM1SETUP(cubeMX CODE):

/* TIM1 init function */

static void MX_TIM1_Init(void)

{

TIM_ClockConfigTypeDef sClockSourceConfig;

TIM_MasterConfigTypeDef sMasterConfig;

TIM_OC_InitTypeDef sConfigOC;

TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;

htim1.Instance = TIM1;

htim1.Init.Prescaler = 24;

htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

htim1.Init.Period = 200;

htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim1.Init.RepetitionCounter = 0;

htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

if (HAL_TIM_Base_Init(&htim1) != HAL_OK)

{

Error_Handler();

}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)

{

Error_Handler();

}

sConfigOC.OCMode = TIM_OCMODE_PWM1;

sConfigOC.Pulse = 0;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;

sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;

sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;

if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)

{

Error_Handler();

}

sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;

sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;

sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;

sBreakDeadTimeConfig.DeadTime = 0;

sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;

sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;

sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;

if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)

{

Error_Handler();

}

HAL_TIM_MspPostInit(&htim1);

}

1 ACCEPTED SOLUTION

Accepted Solutions
Khouloud GARSI
Lead II
Posted on March 28, 2017 at 13:00

Hi

‌,

In 'stm32f0xx_hal_msp.c' file, the alternate function should be correctly configured.

This line should be integrated:

GPIO_InitStruct.Alternate = GPIO_AF2_TIM1;�?

This should solve your problem.

Sorry for any inconvenience it may have brought.

Khouloud

View solution in original post

6 REPLIES 6
JACK TAN
Associate II
Posted on March 27, 2017 at 19:22

STM32CUBEMX V4.20

Khouloud GARSI
Lead II
Posted on March 28, 2017 at 13:00

Hi

‌,

In 'stm32f0xx_hal_msp.c' file, the alternate function should be correctly configured.

This line should be integrated:

GPIO_InitStruct.Alternate = GPIO_AF2_TIM1;�?

This should solve your problem.

Sorry for any inconvenience it may have brought.

Khouloud

Khouloud GARSI
Lead II
Posted on March 28, 2017 at 17:37

Hi

‌,

For the PWM to be generated correctly, even for TIM3, you have toadd the line below:

GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;�?

Khouloud.

Posted on March 28, 2017 at 17:03

Why TIM3 doesn't need to be set Alternate function?

else if(htim->Instance==TIM3)

{

/* USER CODE BEGIN TIM3_MspPostInit 0 */

/* USER CODE END TIM3_MspPostInit 0 */

/**TIM3 GPIO Configuration

PC6 ------> TIM3_CH1

PC7 ------> TIM3_CH2

PC8 ------> TIM3_CH3

PC9 ------> TIM3_CH4

*/

GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

}

Posted on March 29, 2017 at 16:27

HI

Posted on March 29, 2017 at 16:34

HI.I use tim1 pwm,tim3 pwm and tim15 pwm.And addAlternate set,tim1 pwm and tim3 pwm work,but tim15 no work.

________________

Attachments :

SIGN_K.ioc.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HykE&d=%2Fa%2F0X0000000bBp%2F06rZCKvCb2e6r_Pw88IlcpEmHX9E9RZwy.4qMA7Kyfk&asPdf=false