cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U083C-DK TIM2 based PWM for built in LED

Turbo
Associate

Trying to run timer2 to set pwm signal for the BLUE LED (PA5).

The MCU is STM32U083MCT6 and I used rm for STM32U083MC series (https://www.st.com/resource/en/reference_manual/rm0503-stm32u0-series-advanced-armbased-32bit-mcus-stmicroelectronics.pdf)

I set each bit and register separately to be sure that everything is set correctly, but still miss something related to TIM2.

What settings to check related to TIM2 and what is the best approach to configure timers of this MCU?

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Super User

Generate an update event to update ARR.

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

View solution in original post

3 REPLIES 3
TDK
Super User

Generate an update event to update ARR.

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

It works! Thank you!

Gyessine
ST Employee

Hello @Turbo 
Did you enable the GPIOA clock?
I'am not seeing 

  RCC->IOPENR  |= RCC_IOPENR_GPIOAEN;    

in your code.
However, can you try this code sequence, it should make you able to toggle PA5 pin using TIM2 

  /* USER CODE BEGIN 2 */
  RCC->IOPENR  |= RCC_IOPENR_GPIOAEN;    
  RCC->APBENR1 |= RCC_APBENR1_TIM2EN;    
  (void)RCC->IOPENR;                 
  GPIOA->MODER &= ~(0x3U << (5 * 2));    
  GPIOA->MODER |=  (0x2U << (5 * 2));    
  GPIOA->OTYPER &= ~(1U << 5);

  GPIOA->OSPEEDR &= ~(0x3U << (5 * 2));
  GPIOA->OSPEEDR |=  (0x3U << (5 * 2));

  GPIOA->PUPDR &= ~(0x3U << (5 * 2));

  GPIOA->AFR[0] &= ~(0xFU << (5 * 4));   
  GPIOA->AFR[0] |=  (0x1U << (5 * 4));   


  TIM2->CR1 = 0x0000U;    

  TIM2->PSC = 15;           
  TIM2->ARR = 999;           

  TIM2->CCR1 = 500;     

 
  TIM2->CCMR1 &= ~(TIM_CCMR1_OC1M | TIM_CCMR1_CC1S);
  TIM2->CCMR1 |=  (6U << TIM_CCMR1_OC1M_Pos); 
  TIM2->CCMR1 |=  TIM_CCMR1_OC1PE;           
 
  TIM2->CCER &= ~TIM_CCER_CC1P;
  TIM2->CCER |=  TIM_CCER_CC1E;

  TIM2->CR1 |= TIM_CR1_ARPE;


  TIM2->EGR = TIM_EGR_UG;

 
  TIM2->CR1 |= TIM_CR1_CEN;
  /* USER CODE END 2 */

Capture d'écran 2026-03-09 130355.png
BR
Gyessine

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.