【N32L40X】学习笔记09-使用TIM2通道1或TIM5的通道2进行计数

定时器外部计数模式

外部计数模式只可以使用通道1和通道2,通道3通道4不可以用作外部计数模式

硬件引脚

【N32L40X】学习笔记09-使用TIM2通道1或TIM5的通道2进行计数_第1张图片

【N32L40X】学习笔记09-使用TIM2通道1或TIM5的通道2进行计数_第2张图片

【N32L40X】学习笔记09-使用TIM2通道1或TIM5的通道2进行计数_第3张图片

【N32L40X】学习笔记09-使用TIM2通道1或TIM5的通道2进行计数_第4张图片

【N32L40X】学习笔记09-使用TIM2通道1或TIM5的通道2进行计数_第5张图片

外部触发计数

【N32L40X】学习笔记09-使用TIM2通道1或TIM5的通道2进行计数_第6张图片

使用实例

使用TIM2通道1或TIM5的通道2进行计数实例

time_ext_count.h

#ifndef _BSP_TIME_COUNT_TIX_H_
#define _BSP_TIME_COUNT_TIX_H_

#include 
#include "n32l40x.h"
typedef enum
{

    TIME2_COUNT_CH1,
    TIME5_COUNT_CH2,
    TIME_COUNT_TIX_NUM
} em_tim_count_tix;
//外部计数功能的初始化
void bsp_timer_count_tix_all_init(void);
//获取定时器的计数值
uint32_t bsp_timer_count_tix_get_count(em_tim_count_tix id);
//定时器外部计数使能控制
void bsp_time_count_gather_ctrl(int on_off);


#endif


time_ext_count.c


#include "timer_count_tix/bsp_time_count_tix.h"
#include "bsp_include.h"


typedef struct
{
    char *name;//定时器名字-自定义
    GPIO_Module* gpio_grp; //定时器外部时钟输入的gpio分组
    uint16_t pin;//定时器外部时钟输入的gpio pin
    uint32_t gpio_rcc;//定时器外部时钟输入的gpio 时钟
    uint8_t gpio_af;//作为定时器外部时钟输入的gpio复用模式

    TIM_Module* time;//定时器
    uint16_t ch;//定时器通道
    uint32_t time_rcc;//定时器时钟
    IRQn_Type irq_x;//定时器中断编号
    uint16_t trig;//定时器外部时钟输入信号源

} time_count_tix_t;


static time_count_tix_t s_times_tix[TIME_COUNT_TIX_NUM] = {


    {"time2 count",GPIOA,GPIO_PIN_0, RCC_APB2_PERIPH_GPIOA,GPIO_AF2_TIM2,TIM2,TIM_CH_1,RCC_APB1_PERIPH_TIM2,TIM2_IRQn,TIM_TRIG_SEL_TI1FP1},
    {"time5 count",GPIOA,GPIO_PIN_1, RCC_APB2_PERIPH_GPIOA,GPIO_AF7_TIM5,TIM5,TIM_CH_2,RCC_APB1_PERIPH_TIM5,TIM5_IRQn,TIM_TRIG_SEL_TI2FP2},
};


/**
 * 基本定时器初始化
 */
static void bsp_time_count_tix_init(time_count_tix_t *ptime)
{

    RCC_EnableAPB1PeriphClk(ptime->time_rcc, ENABLE);
    RCC_EnableAPB2PeriphClk(ptime->gpio_rcc, ENABLE);//| RCC_APB2_PERIPH_AFIO


    GPIO_InitType GPIO_InitStructure;
    GPIO_InitStruct(&GPIO_InitStructure);
    GPIO_InitStructure.Pin 	   = ptime->pin;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Input;///GPIO_Mode_Input;
    GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
    GPIO_InitStructure.GPIO_Alternate = ptime->gpio_af;
    GPIO_InitPeripheral(ptime->gpio_grp, &GPIO_InitStructure);



    /* Time 2.基本配置 */
    TIM_TimeBaseInitType TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.Period	= 0xffff;
    TIM_TimeBaseStructure.Prescaler = 0;//预分频器
    TIM_TimeBaseStructure.ClkDiv	= 0;
    TIM_TimeBaseStructure.CntMode	= TIM_CNT_MODE_UP;
    TIM_InitTimeBase(ptime->time, &TIM_TimeBaseStructure);

    TIM_ICInitType TIM_ICInitStructure;

    TIM_ICInitStructure.Channel	  = ptime->ch;
    TIM_ICInitStructure.IcPolarity	= TIM_IC_POLARITY_FALLING;
    TIM_ICInitStructure.IcSelection = TIM_IC_SELECTION_DIRECTTI;
    TIM_ICInitStructure.IcPrescaler = TIM_IC_PSC_DIV1;
    TIM_ICInitStructure.IcFilter	= 0x0;
    TIM_ICInit(ptime->time, &TIM_ICInitStructure);

    TIM_SelectInputTrig(ptime->time, ptime->trig);
    TIM_SelectSlaveMode(ptime->time, TIM_SLAVE_MODE_EXT1);

    TIM_Enable(ptime->time, ENABLE);

    printf("name %s \r\n",ptime->name);

}

void bsp_time_count_gather_ctrl(int on_off)
{

    if(on_off)
    {

        TIM_SetCnt(TIM2,0);
        TIM_SetCnt(TIM5,0);
        TIM_Enable(TIM5, ENABLE);
        TIM_Enable(TIM2, ENABLE);
        //TIM_Enable(TIM3, ENABLE);
    }
    else
    {
        TIM_Enable(TIM5, DISABLE);
        TIM_Enable(TIM2, DISABLE);
        //TIM_Enable(TIM3, DISABLE);
    }

}




//一键初始化所有定时器
void bsp_timer_count_tix_all_init(void)
{


    //init_time2_count_tix();
    // init_time5_count_tix();
    for(int i=0; i<TIME_COUNT_TIX_NUM; i++)
    {
        bsp_time_count_tix_init(s_times_tix+i);

    }



}



uint32_t bsp_timer_count_tix_get_count(em_tim_count_tix id)
{
    uint32_t count;

    if(TIME_COUNT_TIX_NUM>id)
    {
        time_count_tix_t *ptime = s_times_tix+id;
        count = TIM_GetCnt(ptime->time);
    }
    return count;
}


//定时器中断集中处理函数
static void bsp_time_count_etr_irq(time_count_tix_t *ptime)
{
    if (TIM_GetIntStatus(ptime->time, TIM_INT_UPDATE) != RESET)
    {
        TIM_ClrIntPendingBit(ptime->time, TIM_INT_UPDATE);
    }

}
void TIM2_IRQHandler(void)
{

    bsp_time_count_etr_irq(s_times_tix+TIME2_COUNT_CH1);

}
void TIM5_IRQHandler(void)
{

    bsp_time_count_etr_irq(s_times_tix+TIME5_COUNT_CH2);

}







你可能感兴趣的:(N32L40X,定时器,外部计数1,N32L40X)