TIM2, using TI2 external clock problem (STM32F303CCT6)
Hi all!,
I want to use TIM2 to count tghe times a button is pressed.
I connected the button to PA1, with a pulldown resistor. When the button is pressed, a hign pulse enters to PA1.
I run this program, and read the TIM2->CNT, but it doesnt move.
This is the program I'm testing. Please, any advices?
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
//PA1 Alternate Function.
GPIOA->MODER &= ~GPIO_MODER_MODER1_0;
GPIOA->MODER |= GPIO_MODER_MODER1_1;
//Alternate Function 3 = TI2
GPIOA->AFR[0] |= 0x00000030;
GPIOA->AFR[0] &= 0xFFFFFF3F;
TIM2->CR1 = RESET;
TIM2->CCMR1 = RESET;
TIM2->CCER = RESET; //CC2E MUST BE 0 IN ORDER TO CHANGE CC2S
TIM2->SMCR = RESET;
//CC2 input, mapped on TI2
TIM2->CCMR1 |= TIM_CCMR1_CC2S_1;
//Select external clock mode 1
TIM2->SMCR |= TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1 | TIM_SMCR_SMS_2;
//TI2FP2
TIM2->SMCR |= TIM_SMCR_TS_1 | TIM_SMCR_TS_2;
TIM2->CCER |= TIM_CCER_CC2E; //Capture compare enable register
//TIM2->PSC = (SystemCoreClock / 1000) - 1; //Milisegundos
TIM2->ARR = 30;
TIM2->EGR = RESET;
TIM2->EGR |= TIM_EGR_UG;
TIM2->CR1 |= TIM_CR1_CEN;