2
\$\begingroup\$

I have a STM32F429 Discovery and wants set a PWM to toggle led with period of 1 second using Timer 1, channel 1.

So, I follow this equation:

enter image description here

My clock APB1 is set to 1MHz.

So by my calculations I got a value for the Prescaler = 19.

PWM_Frequency = 1Hz;
Timer_Clock = 1MHz;
Timer_Count = 49999;
RCR = 0;

And for the pulse 50% I use:

pulse_length = ((TIM_Period + 1) * DutyCycle) / 100 - 1;
pulse_lenght = 24999;

enter image description here

But not works, the Led blinks very faster.

Follow my code:

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 = 19;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 49999;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_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 = 24999;
  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(__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.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  HAL_TIM_MspPostInit(&htim1);

}
\$\endgroup\$
  • \$\begingroup\$ if you want led to toggle every 1s the PWM frequency should be 0.5Hz an ratio 50% \$\endgroup\$ – P__J__ Aug 20 '17 at 22:39
  • \$\begingroup\$ What is Ratio? I'am brazilian. So I calculate with 0.5Hz and find Prescaler = 39. I don't have oscilloscopie and the led not appers toggling with 1 sec. \$\endgroup\$ – Augusto Sanson Cadini Aug 20 '17 at 23:10
  • \$\begingroup\$ ????? PWM RATIO.Google it. If the frequency is different that means your clock does not have 1MHz frequency \$\endgroup\$ – P__J__ Aug 20 '17 at 23:17
  • \$\begingroup\$ Ok, I unterstand more my problem, my question is: The clock source of TIMER1 is APB1 or APB2? \$\endgroup\$ – Augusto Sanson Cadini Aug 21 '17 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.