2022-03-04 08:13 AM
Hello,
I have a very straightforward question but I am not able to find the answer in the user manual. I am using the L476RG nucleo board and setting the TIM5 registers to output a PWM. How can I select the output pin? Is it fixed or does it have some degree of freedom?
This is what I have write so far.
Thanks in advance.
void TIM5Init (void){
// Enable the timer clock
RCC->APB1ENR1 |= (1<<3); // Enable the tim5 clock writing 1 in bit 3
// Set the prescaler and the ARR
TIM5 -> PSC = 79; // 80 MHz/80= 1MHz clock --> 1us count
TIM5-> ARR = PWMf; // ARR value --> set the frequency: total counts before starting again the timer
// set the PWM mode
TIM5 -> CCMR1 |= (0<<0); // to set the output compare mode
TIM5 -> CCMR1 |= (1<<3); // OC1PE bit to enable the preload register
TIM5 -> CCMR1 |= (6<<4); // OC1M bits to set the PWM mode
TIM5 -> CR1 |= (1<<7); // ARPE BIT:ARR register is buffered
TIM5-> EGR |= (0<<0); // UG bit
TIM5 -> CCER |= (0<<1); // CC1P bits to set the polarity (write 0 in bit 1 to have active high)
TIM5-> CCER |= (1<<0); // output on the corresponding output pin enable ?????????
// set the duty cycle
TIM5->CCR1 = 10000; //
}
2022-03-04 08:32 AM
Hello,
The datasheet provides this information, especially tables 16 and 17:
So you need to select the right GPIO alternate function to have PWM of TIM5 available on a GPIO pin.
In your case You need to configure AF2 as shown in the table above.
You can refer to the example provided in STM32CubeL4 under the path Projects\STM32L476G_EVAL\Examples\TIM\TIM_PWMOutput : especially the file stm32l4xx_hal_msp.c in which GPIO pin of PWM output is configured.