cancel
Showing results for 
Search instead for 
Did you mean: 

PWM Generation to Blink the LED Periodicly

fatihcetin
Associate
 
I am trying LED to turn on periodicly using PWM signal but ı cannot see any blinking on LED. I think that my fault is easy but ı could not find it.
 
I am using Port D Pin 2 to blink the LED.
 
 
 
 
#include <stm32g0xx_hal.h>
#include "stm32g0xx_hal_tim.h"
#include "pwm.h"

TIM_HandleTypeDef htim1;

static void MX_TIM1_Init(void);
void Error_Handler(void);  // Declare Error_Handler function

void blink_led_pwm(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_TIM1_Init();
}

static void MX_TIM1_Init(void)
{
  TIM_MasterConfigTypeDef sMasterConfig= {0};
  TIM_OC_InitTypeDef sConfigOC= {0};
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig= {0};

  htim1.Instance= TIM1;
  htim1.Init.Prescaler= 72 - 1;
  htim1.Init.CounterMode= TIM_COUNTERMODE_UP;
  htim1.Init.Period= 100 - 1;
  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();  // Call Error_Handler on error
  }
  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();  // Call Error_Handler on error
  }
  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();  // Call Error_Handler on error
  }
  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.BreakAFMode= TIM_BREAK_AFMODE_INPUT;
  sBreakDeadTimeConfig.Break2State= TIM_BREAK2_DISABLE;
  sBreakDeadTimeConfig.Break2Polarity= TIM_BREAK2POLARITY_HIGH;
  sBreakDeadTimeConfig.Break2Filter= 0;
  sBreakDeadTimeConfig.Break2AFMode= TIM_BREAK_AFMODE_INPUT;
  sBreakDeadTimeConfig.AutomaticOutput= TIM_AUTOMATICOUTPUT_DISABLE;

  if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  {
    Error_Handler();  // Call Error_Handler on error
  }

  GPIO_InitTypeDef GPIO_InitStruct= {0};

  __HAL_RCC_GPIOD_CLK_ENABLE();

  // PD2     ------> TIM1_CH1N

  GPIO_InitStruct.Pin= GPIO_PIN_2;
  GPIO_InitStruct.Mode= GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull= GPIO_NOPULL;
  GPIO_InitStruct.Speed= GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate= GPIO_AF2_TIM1;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}

void Error_Handler(void)
{
  __disable_irq();
}
3 REPLIES 3
Andrew Neil
Evangelist III

Please use this button to properly post source code:

AndrewNeil_0-1713793582027.png

 


@fatihcetin wrote:
 
I am trying LED to turn on periodicly using PWM }

That's not really what PWM is about.

You might use PWM to vary the brightness of the LED - but not usually to give visible blinking.

https://en.wikipedia.org/wiki/Pulse-width_modulation

To get visible blinking, you'd need an extremely long period - on the order of seconds.

AScha.3
Chief

What is the LED doing then - nothing ?

+

Whats the timer clock + cpu clk ?

If you feel a post has answered your question, please click "Accept as Solution".

Check, if your connection is correct, by setting given pin as GPIO Output and "blink" the LED "manually".

Read out and check/post content of TIM and related GPIO registers.

For TIMx_CHxN, you need to have enabled in TIMx_CCER. Maybe you need to use HAL_TIMEx_PWMN_Start(). Also, TIM1 is Advanced timer so you have to set TIMx_BDTR.MOE. I don't use Cube/HAL.

JW