cancel
Showing results for 
Search instead for 
Did you mean: 

pwm pin out on nucleo stm32f303k8

Walter Enzo Re
Associate II
Posted on July 04, 2018 at 12:51

Hi EveryOne.

Hi have a problem with PWM on my Stm32f303k8, nucleo board.

Hi searched on this forum, but I not find an answer that solve my problem.

So: I need to have a pwm output on PB_1 Pin, and, to do this, I've used cubeMX to generate the code:

This is Tim.c

#include 'tim.h'

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

TIM_HandleTypeDef htim1;

TIM_HandleTypeDef htim2;

/* TIM1 init function */

void MX_TIM1_Init(void)

{

  TIM_MasterConfigTypeDef sMasterConfig;

  TIM_OC_InitTypeDef sConfigOC;

  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;

  htim1.Instance = TIM1;

  htim1.Init.Prescaler = 7900;

  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim1.Init.Period = 999;

  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  htim1.Init.RepetitionCounter = 0;

  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

  if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

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

  {

    _Error_Handler(__FILE__, __LINE__);

  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;

  sConfigOC.Pulse = 499;

  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_3) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

  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.BreakFilter = 0;

  sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;

  sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;

  sBreakDeadTimeConfig.Break2Filter = 0;

  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;

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

  {

    _Error_Handler(__FILE__, __LINE__);

  }

  HAL_TIM_MspPostInit(&htim1);

}

void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)

{

  if(tim_pwmHandle->Instance==TIM1)

  {

  /* USER CODE BEGIN TIM1_MspInit 0 */

  /* USER CODE END TIM1_MspInit 0 */

    /* TIM1 clock enable */

    __HAL_RCC_TIM1_CLK_ENABLE();

  /* USER CODE BEGIN TIM1_MspInit 1 */

  /* USER CODE END TIM1_MspInit 1 */

  }

}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)

{

  if(tim_baseHandle->Instance==TIM2)

  {

  /* USER CODE BEGIN TIM2_MspInit 0 */

  /* USER CODE END TIM2_MspInit 0 */

    /* TIM2 clock enable */

    __HAL_RCC_TIM2_CLK_ENABLE();

  /* USER CODE BEGIN TIM2_MspInit 1 */

  /* USER CODE END TIM2_MspInit 1 */

  }

}

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(timHandle->Instance==TIM1)

  {

  /* USER CODE BEGIN TIM1_MspPostInit 0 */

  /* USER CODE END TIM1_MspPostInit 0 */

 

    /**TIM1 GPIO Configuration    

    PB1     ------> TIM1_CH3N

    */

    GPIO_InitStruct.Pin = PWM_PIN_Pin;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

    GPIO_InitStruct.Alternate = GPIO_AF6_TIM1;

    HAL_GPIO_Init(PWM_PIN_GPIO_Port, &GPIO_InitStruct);

  /* USER CODE BEGIN TIM1_MspPostInit 1 */

  /* USER CODE END TIM1_MspPostInit 1 */

  }

}

void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* tim_pwmHandle)

{

  if(tim_pwmHandle->Instance==TIM1)

  {

  /* USER CODE BEGIN TIM1_MspDeInit 0 */

  /* USER CODE END TIM1_MspDeInit 0 */

    /* Peripheral clock disable */

    __HAL_RCC_TIM1_CLK_DISABLE();

  /* USER CODE BEGIN TIM1_MspDeInit 1 */

  /* USER CODE END TIM1_MspDeInit 1 */

  }

}

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)

{

  if(tim_baseHandle->Instance==TIM2)

  {

  /* USER CODE BEGIN TIM2_MspDeInit 0 */

  /* USER CODE END TIM2_MspDeInit 0 */

    /* Peripheral clock disable */

    __HAL_RCC_TIM2_CLK_DISABLE();

  /* USER CODE BEGIN TIM2_MspDeInit 1 */

  /* USER CODE END TIM2_MspDeInit 1 */

  }

}

This is the code I have.

I tryed to connect by jumper PB_1 to a external led, but it always off.

I've tryed also to use a pwm example on stmcubef3, but the result is the same.

Can anyone help me, please?

Many thanks,

Walter.

1 ACCEPTED SOLUTION

Accepted Solutions
John Craven
Senior
Posted on July 04, 2018 at 15:21

you have setup the timer for PWM signal generation on the complementary output.

you need to use;

HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);

I tested it here on a nuc-303k8. It works.

Also, there is no need to call BASE_Start function before PWM.

it only call the enable macro, __HAL_TIM_ENABLE(htim);

PWM_Start functions also call this timer enable macro.

View solution in original post

9 REPLIES 9
Posted on July 04, 2018 at 12:59

I don't understand the Cube gobbledygook, but if this is TIM1, is TIM1_BDTR.MOE set to 1?

JW

John Craven
Senior
Posted on July 04, 2018 at 13:31

Might help if you show the code where you start the timer also!

Walter Enzo Re
Associate II
Posted on July 04, 2018 at 13:33

Hi JW,

many thanks for your reply.

I don't know if that register is set to 1, so i added this line in my code:

htim1.Instance->BDTR = 1;

And i tryed to connect PB_1 (That shoud be D6) to Led, but it still off.

Walter Enzo Re
Associate II
Posted on July 04, 2018 at 13:35

Hi Jhon,

many thanks for your reply.

In main function I use:

HAL_TIM_Base_Start(&htim1);

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);

To start pwm.

John Craven
Senior
Posted on July 04, 2018 at 15:21

you have setup the timer for PWM signal generation on the complementary output.

you need to use;

HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);

I tested it here on a nuc-303k8. It works.

Also, there is no need to call BASE_Start function before PWM.

it only call the enable macro, __HAL_TIM_ENABLE(htim);

PWM_Start functions also call this timer enable macro.

John Craven
Senior
Posted on July 04, 2018 at 15:50

if you want the CH3_CH3N dual output mode, you call both starts

0690X0000060LbUQAU.png

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);

HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);

Posted on July 04, 2018 at 16:23

don't know if that register is set to 1

Don't have a debugger? Or some means to do debug printouts?

htim1.Instance->BDTR = 1

MOE is bit 15 in BDTR.

Read the TIM1 chapter in reference manual.

JW

[EDIT] Looking at Cube/HAL, it appears that MOE gets set in HAL_TIM_PWM_Start()

Posted on July 05, 2018 at 11:22

Hi Jhon,

Many thanks for your reply.

Now works, many thanks!

Ali Yazdanifar
Associate II
Posted on July 05, 2018 at 16:26

Hi, you can do this work in this way: in the stm32cubeMX you set this settings :

0690X0000060Le9QAE.png

you can set your duty cycle and other settings in this way :

0690X0000060LeOQAU.png

 with this settings , we have 50% duty cycle with 1Hz frequency . after generating your project :

you can start your pwm in this way :

HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);

<write this on top of while(1) >

you can change your duty cycle with

__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4,3999);

its all thing that you should do .