cancel
Showing results for 
Search instead for 
Did you mean: 

Setting the timer TIM1 in STM32F407

Alexandr.D
Associate II

Hello! I can not run PWM on TIM1 channel CH1N. Can you please tell me what I am doing wrong?

LL_TIM_InitTypeDef TIM_InitStruct;

LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;

LL_GPIO_InitTypeDef GPIO_InitStruct;

/* Enable the peripheral clock of GPIOs */

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOE);

/* Peripheral clock enable */

LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);

TIM_InitStruct.Prescaler = 0;

TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;

TIM_InitStruct.Autoreload = 104;

TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;

LL_TIM_Init(TIM1, &TIM_InitStruct);

LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH1N);

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

TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;

LL_TIM_OC_Init(TIM1, LL_TIM_CHANNEL_CH1N, &TIM_OC_InitStruct);

LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH1N);

LL_TIM_SetTriggerOutput(TIM1, LL_TIM_TRGO_RESET);

LL_TIM_DisableMasterSlaveMode(TIM1);

/*************************/

/* GPIO AF configuration */

/*************************/

//PE8   ------> TIM1_CH1N

/* GPIO TIM1_CH1N configuration */

  GPIO_InitStruct.Pin = LL_GPIO_PIN_8;

  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;

  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;

  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;

  GPIO_InitStruct.Alternate = LL_GPIO_AF_1;

  LL_GPIO_Init(GPIOE, &GPIO_InitStruct);

LL_TIM_OC_SetCompareCH1(TIM1, LL_TIM_GetAutoReload(TIM1) / 20 - 1); /* Set channel 1 compare register */

LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH1); /* Enable output on channel */

1 ACCEPTED SOLUTION

Accepted Solutions

You need to set TIMx_CCER.CC1NE and also TIMx_BDTR.MOE.

I don't know the Cube/LL incantations for these.

JW

PS. Looking at some F4 Cube's sources, it appears the first might be something like LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH1N); and the second LL_TIM_EnableAllOutputs(TIM1)

View solution in original post

3 REPLIES 3

You need to set TIMx_CCER.CC1NE and also TIMx_BDTR.MOE.

I don't know the Cube/LL incantations for these.

JW

PS. Looking at some F4 Cube's sources, it appears the first might be something like LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH1N); and the second LL_TIM_EnableAllOutputs(TIM1)

Alexandr.D
Associate II

Thank! You're right. After reading the documentation, I was able to find a bug.

Alexandr.D
Associate II

A working example of the initialization of PWM 50% on TIM1 CH1N:

LL_TIM_InitTypeDef TIM_InitStruct;

LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;

LL_TIM_BDTR_InitTypeDef TIM_BDTRInitStruct;

LL_GPIO_InitTypeDef GPIO_InitStruct;

/* Enable the peripheral clock of GPIOs */

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOE);

/* Peripheral clock enable */

LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);

/* TIM1 interrupt Init */

NVIC_SetPriority(TIM1_CC_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));

NVIC_EnableIRQ(TIM1_CC_IRQn);

TimOutClock = SystemCoreClock/1;

TIM_InitStruct.Prescaler = __LL_TIM_CALC_PSC(SystemCoreClock, 10000);;

TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;

TIM_InitStruct.Autoreload = __LL_TIM_CALC_ARR(TimOutClock, TIM_InitStruct.Prescaler, 100);

TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;

TIM_InitStruct.RepetitionCounter = (uint8_t)0x00;

LL_TIM_Init(TIM1, &TIM_InitStruct);

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 = ( (LL_TIM_GetAutoReload(TIM1) + 1 ) / 2);

TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;

TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;

TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW;

TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;

/* Initialize TIM instance according to parameters defined in        */

/* initialization structure.                        */

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);

/*************************/

/* GPIO AF configuration */

/*************************/

//PE8   ------> TIM1_CH1N

/* GPIO TIM1_CH1N configuration */

GPIO_InitStruct.Pin = LL_GPIO_PIN_8;

GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;

GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;

GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;

GPIO_InitStruct.Alternate = LL_GPIO_AF_1;

LL_GPIO_Init(GPIOE, &GPIO_InitStruct);

LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH1N); /* Enable output on channel */

LL_TIM_EnableCounter(TIM1);

LL_TIM_EnableAllOutputs(TIM1);