Skip to main content
AQuad.2
Visitor II
May 11, 2021
Question

How do I configure GPIO pin (PE8 )as PWM output to control LED Brightness.

  • May 11, 2021
  • 2 replies
  • 4099 views

I am a newbie and trying to make my hands dirty in STM32l476 Discovery board.

I am trying to control the brightness of an LED which is connected onboard at GPIO PIN PE8.

The TIMER on this pin is TIM1 CH1N.

What I have tried is till now is below. I would request someone to please go through and correct where I am wrong.

STEP1: Setting the GPIO PIN PE8 to TIM1CH1N functionality.

STEP2: Clock Configuration is set to 40MHz.

STEP3: TIMER mode and configurations as below.

Clock Source - Internal Clock Source.

Channel 1 - PWM GENERATION CH1N

STEP4: Counter Settings as below.

Prescaler Value - 40000 (To make clock as 1000 Hz)

Counter Period (ARR) - 100 -1 (Max limit in one second)

Now in programming part.

I am starting the timer with the by calling below API.

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

Now in the while loop i wanted to change brightness of an LED.

while(1)

{

htim1.Instance-> CCR1 = 50; // For 50% brightness on PE8 LED//

HAL_Dealy(1000);

htim1.Instance->CCR1 = 100; // FOR 100% brightness on PE8 LED//

HAL_Dealy(1000);

}

I would once again request someone to guide me and let me understand this topic with detailed explanation.

Thanks in Advance! and Looking forward to hear from someone.

This topic has been closed for replies.

2 replies

Javier1
Principal
May 11, 2021

why TIM1CH1N ?

the N means its going to be the exact oposite of TIM1CH1

so CCR=100 would be pwm=0%;

also try TIM1->CCR1=50 instead of htim1.instance

hit me up in https://www.linkedin.com/in/javiermuñoz/
waclawek.jan
Super User
May 11, 2021

> HAL_TIM_PWM_Start()

That does not enable the TIM1_CH1N, only TIM1_CH1. You have to use HAL_TIMEx_OCN_Start().

> let me understand this topic with detailed explanation.

If you want to understand what you are doing, read the Reference Manual. Cube/HAL is open source, you can look at what the different functions do. Or just simply don't use Cube/HAL and write programs by directly accessing the mcu registers.

> the N means its going to be the exact oposite of TIM1CH1

Only if TIM1_CH1 is enabled.

JW