0
\$\begingroup\$

I am trying to use this code to control brightness of a LED using channel 3 of Timer 1 (PA10 pin) on the STM32F411:

int main()
{
 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // Enable GPIOA clock
 RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // Enable Timer 1 clock
 // Configure PA10 as alternate function mode
 GPIOA->MODER |= (2 << 20); // Set alternate function mode for PA10
 GPIOA->OTYPER &= ~(1 << 10); // Output push-pull
 GPIOA->AFR[1] |= (1 << 8); // Set AF1 for PA10 (TIM1_CH3)
 // Configure Timer 1
 TIM1->PSC = 16 - 1; // Prescaler value
 TIM1->ARR = 1000; // Auto-reload register value (period)
 TIM1->CCMR2 |= (6 << 4); // PWM mode 1 for Channel 3
 TIM1->CCER |= TIM_CCER_CC3E; // Enable capture/compare 3 output
 TIM1->CR1 |= TIM_CR1_CEN; // Enable the timer
 // Set initial duty cycle (50%)
 TIM1->CCR3 = 500;
 while(1)
 {
 
 }
}

As you can see I've configure the GPIO and TIMER1 registers, trying to generate a 50% duty cycle PWM signal, but for some reason it is not working, measured output is always 2.0 v no matter the duty cycle configured on the CCR3 register.

asked Apr 2, 2024 at 22:34
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I have run into a similar issue very recently, with an H7, but ST likes to reuse their peripherals.

Advanced timers (TIM1 and TIM8) have a built-in break option, for cases where you'd want an external input to be able to step the PWM output without interacting with the firmware. You must disable that break for TIM1 to output anything.

In general, if all you need is a basic PWM, I advise against using TIM1 or TIM8, they are quite complicated for timers and will bring you only pain. Use TIM2 through TIM7, or anything above TIM9, assuming they have PWM outputs.

answered Apr 3, 2024 at 4:25
\$\endgroup\$
1
  • 1
    \$\begingroup\$ In other words, TIM1->BDTR = TIM_BDTR_MOE. \$\endgroup\$ Commented Apr 3, 2024 at 7:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.