cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F072 TIM1 PWM no output

licencje1
Associate II
Posted on January 03, 2018 at 15:32

Hi,

For few days I am trying to get my PWM generator works under TIM1. I cannot find what is wrong but counter seems to work but no output is available under GPIO_A8 pin.

Configuration code looks like:

static void

MX_TIM1_Init(

void

)

{

 

LL_TIM_InitTypeDef

TIM_InitStruct

;

 

LL_TIM_OC_InitTypeDef

TIM_OC_InitStruct

;

 

LL_TIM_BDTR_InitTypeDef

TIM_BDTRInitStruct

;

 

LL_GPIO_InitTypeDef

GPIO_InitStruct

;

 

/* Peripheral clock enable */

 

LL_APB1_GRP2_EnableClock(

LL_APB1_GRP2_PERIPH_TIM1

)

;

 

TIM_InitStruct.

Prescaler

=

600

;

 

TIM_InitStruct.

CounterMode

=

LL_TIM_COUNTERMODE_UP

;

 

TIM_InitStruct.

Autoreload

=

400

;

 

TIM_InitStruct.

ClockDivision

=

LL_TIM_CLOCKDIVISION_DIV4

;

 

TIM_InitStruct.

RepetitionCounter

=

1

;

 

LL_TIM_Init(

TIM1

,

&TIM_InitStruct)

;

 

LL_TIM_EnableARRPreload(

TIM1

)

;

 

LL_TIM_SetClockSource(

TIM1

,

LL_TIM_CLOCKSOURCE_INTERNAL

)

;

 

LL_TIM_OC_EnablePreload(

TIM1

,

LL_TIM_CHANNEL_CH1

)

;

 

TIM_OC_InitStruct.

OCMode

=

LL_TIM_OCMODE_PWM1

;

 

TIM_OC_InitStruct.

OCState

=

LL_TIM_OCSTATE_DISABLE

;

 

TIM_OC_InitStruct.

OCNState

=

LL_TIM_OCSTATE_DISABLE

;

 

TIM_OC_InitStruct.

CompareValue

=

200

;

 

TIM_OC_InitStruct.

OCPolarity

=

LL_TIM_OCPOLARITY_HIGH

;

 

TIM_OC_InitStruct.

OCNPolarity

=

LL_TIM_OCPOLARITY_HIGH

;

 

TIM_OC_InitStruct.

OCIdleState

=

LL_TIM_OCIDLESTATE_HIGH

;

//LL_TIM_OCIDLESTATE_LOW;

 

TIM_OC_InitStruct.

OCNIdleState

=

LL_TIM_OCIDLESTATE_LOW

;

 

LL_TIM_OC_Init(

TIM1

,

LL_TIM_CHANNEL_CH1

,

&TIM_OC_InitStruct)

;

 

LL_TIM_OC_DisableFast(

TIM1

,

LL_TIM_CHANNEL_CH1

)

;

 

LL_TIM_SetTriggerOutput(

TIM1

,

LL_TIM_TRGO_RESET

)

;

 

LL_TIM_DisableMasterSlaveMode(

TIM1

)

;

//  TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE;

//  TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE;

//  TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF;

//  TIM_BDTRInitStruct.DeadTime = 0;

//  TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE;

//  TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH;

//  TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE;

//  LL_TIM_BDTR_Init(TIM1, &TIM_BDTRInitStruct);

  /**TIM1 GPIO Configuration

  PA8   ------> TIM1_CH1

  */

//  GPIO_InitStruct.Pin = LL_GPIO_PIN_8;

//  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;

//  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;

//  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;

//  GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN; //LL_GPIO_PULL_NO;

//  GPIO_InitStruct.Alternate = LL_GPIO_AF_2;

//  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

LL_AHB1_GRP1_EnableClock(

LL_AHB1_GRP1_PERIPH_GPIOA

)

;

 

LL_GPIO_SetPinMode(

GPIOA

,

LL_GPIO_PIN_8

,

LL_GPIO_MODE_ALTERNATE

)

;

 

LL_GPIO_SetPinPull(

GPIOA

,

LL_GPIO_PIN_8

,

LL_GPIO_PULL_NO

)

;

 

LL_GPIO_SetPinSpeed(

GPIOA

,

LL_GPIO_PIN_8

,

LL_GPIO_SPEED_FREQ_HIGH

)

;

 

LL_GPIO_SetAFPin_8_15(

GPIOA

,

LL_GPIO_PIN_8

,

LL_GPIO_AF_2

)

;

 

LL_TIM_OC_SetCompareCH1(

TIM1

,

200

)

;

 

LL_TIM_CC_EnableChannel(

TIM1

,

LL_TIM_CHANNEL_CH1

)

;

 

LL_TIM_OC_SetMode(

TIM1

,

LL_TIM_CHANNEL_CH1

,

LL_TIM_OCMODE_PWM1

)

;

 

LL_TIM_GenerateEvent_UPDATE(

TIM1

)

;

 

LL_TIM_EnableCounter(

TIM1

)

;

}

And it is invoked inside main loop:

int

main(

void

)

{

  LL_Init()

;

 

SystemClock_Config()

;

 

MX_TIM1_Init()

;

  while(1);

}

And System Clock config:

void

SystemClock_Config(

void

)

{

  LL_FLASH_SetLatency(

LL_FLASH_LATENCY_1

)

;

 

if

(LL_FLASH_GetLatency() !=

LL_FLASH_LATENCY_1

)

  {

 

Error_Handler

()

 

}

  LL_RCC_HSI_Enable()

;

  

/* Wait till HSI is ready */

 

while

(LL_RCC_HSI_IsReady() !=

1

)

  {

   

  }

  LL_RCC_HSI_SetCalibTrimming(

16

)

;

 

LL_RCC_PLL_ConfigDomain_SYS(

LL_RCC_PLLSOURCE_HSI

,

LL_RCC_PLL_MUL_12

,

LL_RCC_PREDIV_DIV_2

)

;

 

LL_RCC_PLL_Enable()

;

  

/* Wait till PLL is ready */

 

while

(LL_RCC_PLL_IsReady() !=

1

)

  {

   

  }

  LL_RCC_SetAHBPrescaler(

LL_RCC_SYSCLK_DIV_1

)

;

 

LL_RCC_SetAPB1Prescaler(

LL_RCC_APB1_DIV_1

)

;

 

LL_RCC_SetSysClkSource(

LL_RCC_SYS_CLKSOURCE_PLL

)

;

  

/* Wait till System clock is ready */

 

while

(LL_RCC_GetSysClkSource() !=

LL_RCC_SYS_CLKSOURCE_STATUS_PLL

)

  {

 

  }

  LL_Init1msTick(

48000000

)

;

 

LL_SYSTICK_SetClkSource(

LL_SYSTICK_CLKSOURCE_HCLK

)

;

 

LL_SetSystemCoreClock(

48000000

)

;

 

LL_RCC_SetUSARTClockSource(

LL_RCC_USART2_CLKSOURCE_PCLK1

)

;

 

/* SysTick_IRQn interrupt configuration */

 

NVIC_SetPriority(

SysTick_IRQn

,

0

)

;

}

Can anyone deal with that issue?

#pwm #stm32f072 #timer1
3 REPLIES 3
Posted on January 03, 2018 at 15:45

I don't Cube, but do you set TIMx_BDTR.MOE?

JW

Posted on January 03, 2018 at 16:07

In SPL you had to enable PWM Output on TIM1/TIM8

/**

  * @brief  Enables or disables the TIM peripheral Main Outputs.

  * @param  TIMx: where x can be 1 or 8 to select the TIMx peripheral.

  * @param  NewState: new state of the TIM peripheral Main Outputs.

  *          This parameter can be: ENABLE or DISABLE.

  * @retval None

  */

void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState)

{

  /* Check the parameters */

  assert_param(IS_TIM_LIST4_PERIPH(TIMx));

  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState != DISABLE)

  {

    /* Enable the TIM Main Output */

    TIMx->BDTR |= TIM_BDTR_MOE;

  }

  else

  {

    /* Disable the TIM Main Output */

    TIMx->BDTR &= (uint16_t)~TIM_BDTR_MOE;

  }

}

In HAL you need to check

LL_TIM_EnableAllOutputs()

/**

  * @brief  Enable the outputs (set the MOE bit in TIMx_BDTR register).

  * @note The MOE bit in TIMx_BDTR register allows to enable /disable the outputs by

  *       software and is reset in case of break or break2 event

  * @note Macro @ref IS_TIM_BREAK_INSTANCE(TIMx) can be used to check whether or not

  *       a timer instance provides a break input.

  * @rmtoll BDTR         MOE           LL_TIM_EnableAllOutputs

  * @param  TIMx Timer instance

  * @retval None

  */

__STATIC_INLINE void LL_TIM_EnableAllOutputs(TIM_TypeDef *TIMx)

{

  SET_BIT(TIMx->BDTR, TIM_BDTR_MOE);

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

In main.c file, in function SystemClock_Config,insert the following line as the first code line in the function and see if is working...

 LL_FLASH_Enable64bitAccess();
�?