cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 Timer does not work

kyouma001
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions

As you set TIMx_CR1.ARPE, the first cycle until the first update takes the original ARR value, ie. 0xFFFF'FFFF timer clocks.

JW

View solution in original post

3 REPLIES 3

As you set TIMx_CR1.ARPE, the first cycle until the first update takes the original ARR value, ie. 0xFFFF'FFFF timer clocks.

JW

S.Ma
Principal

In this example, LED Dimming by PWM. (LEDs.c)

Thanks this worked for me.