2023-01-27 02:52 AM
Hi I am trying to initialize TIM2 to get a PWM outpt from PA15 of my STM32F407 development board. Here is my TIM init function:
void TIMConfig (void) {
/*************>>>>>>> STEPS FOLLOWED <<<<<<<<************
1. Enable the TIM2 clock
2. Set clock division, enable auto-reload preload
3.
********************************************************/
// 1. Enable the TIM2 clock
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
// 2. Set clock coeff. as 2, enable auto-reload preload
TIM2->CR1 = TIM_CR1_CKD_0 | TIM_CR1_ARPE | TIM_CR1_CMS_0;
//TIM2->DIER |= TIM_DIER_UIE;
TIM2->CNT = 0;
TIM2->PSC = (uint32_t) 0;
TIM2->ARR = (uint32_t) 8400;
TIM2->CCR1 = (uint32_t) 840;
TIM2->RCR = (uint32_t) 0;
TIM2->CCMR1 = TIM_CCMR1_OC1PE | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_0;
TIM2->CCER = TIM_CCER_CC1E | TIM_CCER_CC1P;
TIM2->CR1 |= TIM_CR1_CEN;
}
And GPIO config function:
void GPIOConfig (void) {
/*************>>>>>>> STEPS FOLLOWED <<<<<<<<************
1. Enable the GPIO clock
2. Set the pin modes
3. Configure the output mode
********************************************************/
// 1. Enable the GPIO clock
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN | RCC_AHB1ENR_GPIOAEN;
// 2. Set the pin modes
// D13 as output
GPIOD->MODER |= GPIO_MODER_MODE13_0;
// A15 as alternate function
GPIOA->MODER |= GPIO_MODER_MODE15_1;
GPIOA->AFR[1] |= GPIO_AFRH_AFSEL15_0;
// 3. Configure the output mode
GPIOD->OTYPER |= 0;
GPIOD->OSPEEDR |= 0;
}
I call these 2 function in main but I can not see the PWM signal on PA15 pin. What is my fault? Thanks in advance.
Solved! Go to Solution.
2023-01-27 03:51 AM
As you set TIMx_CR1.ARPE, the first cycle until the first update takes the original ARR value, ie. 0xFFFF'FFFF timer clocks.
JW
2023-01-27 03:51 AM
As you set TIMx_CR1.ARPE, the first cycle until the first update takes the original ARR value, ie. 0xFFFF'FFFF timer clocks.
JW
2023-01-27 07:45 PM
In this example, LED Dimming by PWM. (LEDs.c)
2023-01-29 07:46 AM
Thanks this worked for me.