简体   繁体   中英

Confused when using timer as counter on STM32F1 series

I'm implementing a project using stm32f103 microcontroller. Basically, I use Timer2 to count external pulse.

#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"  // timer library
#include "misc.h"

/* Built-in LED */
#define LEDPORT (GPIOC)
#define LEDPIN (GPIO_Pin_13)

int main(void){

    /* gpio init struct */
    GPIO_InitTypeDef gpioInit;

    /* enable clock for GPIOA thru ABP2 peripheral communication bus */
    // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC->APB2ENR |= RCC_APB2Periph_GPIOC;
    /* use LED pin */
    gpioInit.GPIO_Pin = LEDPIN;
    /* mode: output */
    gpioInit.GPIO_Mode = GPIO_Mode_Out_PP;
    gpioInit.GPIO_Speed = GPIO_Speed_2MHz;
    /* apply configuration */
    GPIO_Init(LEDPORT, &gpioInit);

    /* clear built-in led */
    GPIO_SetBits(LEDPORT, LEDPIN);

    /* Enable timer clock */
    // RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    RCC->APB1ENR |= RCC_APB1Periph_TIM2;
    /* Configure channel 2 as input, mapped on the Timer Input 1 (TI1) */
    TIM2->CCMR1 |= TIM_CCMR1_CC2S_1;
    /* Configure channel 2 detecting falling edge polarity */
    TIM2->CCER |= TIM_CCER_CC2P;
    /* Configure TIM2 in External Clock Mode 1 & select TI2 as the input source */
    TIM2->SMCR |= TIM_SMCR_SMS | TIM_SMCR_TS_2 | TIM_SMCR_TS_1;
    /* Enable the counter by writing CEN=1 in the TIMx_CR1 register */
    // TIM_Cmd(TIM2, ENABLE);    
    TIM2->CR1 |= TIM_CR1_CEN;
    /* Enable interrupt trigger. */
    // TIM_ITConfig(TIM2, TIM_IT_Trigger, ENABLE);
    TIM2->DIER |= TIM_DIER_TIE;

    for (;;){

        if (TIM_GetITStatus(TIM2, TIM_IT_Trigger) != RESET){

            TIM_ClearITPendingBit(TIM2, TIM_IT_Trigger);

            LEDPORT->ODR ^= LEDPIN;
        }
    }

    /* never reach */
    return 0;
}

The code works well. What I don't understand is

/* Configure CC2S bits = 10: CC2 channel is configured as input, IC2 is mapped on TI1 */
    TIM2->CCMR1 |= TIM_CCMR1_CC2S_1;

which supposes to use the timer input 1 but I have to configure Trigger input to Filtered Timer Input 2

/* Configure TIM2 in External Clock Mode 1 & select TI2 as the input source (110: Filtered Timer Input 2 (TI2FP2))*/
    TIM2->SMCR |= TIM_SMCR_SMS | TIM_SMCR_TS_2 | TIM_SMCR_TS_1;

Is there anyone can clarify to me?

The code works well. What I don't understand is

There are several failures in your comments, to clarify:

which supposes to use the timer input 1 but I have to configure Trigger input to Filtered Timer Input 2

TIM2->CCMR1 |= TIM_CCMR1_CC2S_1;

This configures channel 2 to detect rising edges on the TI2 input (not TI1) by writing CC2S= '01 in the TIMx_CCMR1 register.

/* Configure TIM2 in External Clock Mode 1 & select TI2 as the input source */

TIM2->SMCR |= TIM_SMCR_SMS | TIM_SMCR_TS_2 | TIM_SMCR_TS_1;

a) Configures the timer in external clock mode 1 by writing SMS=111 in the TIMx_SMCR register.

b) Selection of TI2 as the input source as specified at the beginning by writing TS=110 into the TIMx_SMCR register.

For more information, you'll find an example "TI2 external clock connection" in the latest datasheet of your uC.

BTW Did you wrote the comments? There are several failures inside your comments.

gpioInit.GPIO_Mode = GPIO_Mode_Out_PP; // Open-drain output mode for built-in LED

is not open-drain, should be commented as push-pull .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM