2

I'm new here and I have some questions about generating a PWM signal for dimming an led using STM32 boards. I'm trying to generate the PWM only by writing the nessasarry bits with bit manipulation. But the LED does not light. I don't know if I'm missing something for a correct PWM generation, because I'm new to this microcontroller.

I'm using a NUCLEO-L432KC with a click-shield (PA8 is connected to a green led)

  • The GPIOA clock is enabled
  • The PA8 pin is set to AFR[1] to access the correct register (register for pin 8 to 17)
  • I'm using TIM1 because it's the only timer available for PA8.

The following code is inside the main function.

// Enable GPIOA and TIM1 clock
    RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
    RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;

    HAL_Delay(1);

    // Configure PA8 for alternate function (AF1: TIM1_CH1)
    GPIOA->MODER &= ~GPIO_MODER_MODE8;   // Clear bits
    GPIOA->MODER |= GPIO_MODER_MODE8_1;  // Set PA8 to AF mode
    GPIOA->AFR[1] |= (0x1 << ((8 - 8) * 4)); // Set AF1 for PA8

    // Set timer properties
    TIM1->PSC = 0;                    // Prescaler
    TIM1->ARR = 4999;                 // Auto-reload (5kHz)
    TIM1->CCR1 = 2499;                // 50% duty cycle
    TIM1->CCMR1 &= ~TIM_CCMR1_OC1M;   // Clear output compare mode
    TIM1->CCMR1 |= TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2;  // Set PWM mode 1
    TIM1->CCER |= TIM_CCER_CC1E;      // Enable output

    // Generate update event and start the timer
    TIM1->EGR |= TIM_EGR_UG;
    TIM1->CR1 |= TIM_CR1_CEN;         // Enable Timer

Am I missing something?

5
  • ~0x3 << (8*2) has a different value to ~(0x3 << (8*2)), which is probably what you intend. Commented Sep 20, 2024 at 19:47
  • Do you have a resistor in series with the LED? If not you may burn out (or may have already burned out) the LED and/or the GPIO pin. Commented Sep 20, 2024 at 19:58
  • @pmacfarlane the LED is still working fine. The LED is on a click shield and probably has a resistor in series. Commented Sep 20, 2024 at 20:23
  • Ooof -- always throw a current limiting resistor in series with the LED to avoid the audible sizzle, magic puff of smoke and characteristic smell of burning electronics. 200 - 5K Ohms is fine. Commented Sep 21, 2024 at 1:03
  • The LED is on the Click-shield and has resistors in series Commented Sep 21, 2024 at 15:34

2 Answers 2

0
  1. Do not use magic numbers. Use human readable bitfield names.
  2. You do not have any delay between enabling the clock and first access to hardware register. Carefully read Reference Manual
  3. I do not see that you enable the timer clock.

I am not going to check timer registers until you use proper human readable CMSIS definitions.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh sorry I used these magical numbers for me to learn about them. I updated the code according to your points
-1

I know LEDs work off direct current and if it's not getting the correct direct current it will not light or dim so that's why certain controllers manipulate the DC electrical current to dim but not destroy the circuit. You might have to engineer a microcontroller that turns on-off in milliseconds and a custom power signal to have the LED dim. So basically you will have to design a controller that works like an alternating current (ac) at a certain rate and standard AC current flickers on and off at 60 HZ 60 times per second. So basically design a controller that turns DC into a AC type 60 HZ guessing to have a dim or check out Alibaba for a LED driver controller that might work.

1 Comment

I am turning the LED on with a logic output, so won't the concern about the AC supply be irrelevant?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.