定时器采用外部触发方式计数
也就是外部时钟源模式2
此模式由 TIMx_SMCTRL .EXCEN 选择等于 1。计数器可以在外部触发输入 ETR 的每个上升沿或下降沿
计数。
- 极性选择
- 分频选择
- 过滤选择
- 选择外部时钟ETR模式
bsp_time_counter_ETR.h
#ifndef _BSP_TIME_COUNTER_ETR_H_
#define _BSP_TIME_COUNTER_ETR_H_
#include <stdint.h>
#include "n32l40x.h"
typedef enum
{
TIME2_ETR,
TIME_COUNT_ETR_NUM
}em_tim_count_etr;
//初始化函数
void bsp_timer_count_etr_all_init(void);
//获取计数值
uint32_t bsp_timer_count_etr_get_count(em_tim_count_etr id);
#endif
bsp_time_counter_ETR.c
#include "timer_counter_ETR/bsp_time_counter_ETR.h"
#include "bsp_include.h"
typedef struct
{
GPIO_Module* gpio_grp;
uint16_t pin;
uint32_t gpio_rcc;
uint8_t gpio_af;
TIM_Module* time;
uint32_t time_rcc;
uint16_t irq_x;
uint32_t period;//重载值
uint32_t prescaler;//预分频器
uint8_t it_update;
} time_counter_etr;
static time_counter_etr s_times_etr[TIME_COUNT_ETR_NUM] = {
{GPIOA,GPIO_PIN_0, RCC_APB2_PERIPH_GPIOA,GPIO_AF5_TIM2,TIM2,RCC_APB1_PERIPH_TIM2,TIM2_IRQn,0xffff,0,1},
};
/**
* 中断相关初始化.
*/
static void bsp_time_count_etr_nvic_config(time_counter_etr *ptime)
{
NVIC_InitType NVIC_InitStructure;
/* Enable the TIM1 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = ptime->irq_x;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 7;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClrIntPendingBit(ptime->time, TIM_INT_UPDATE);
TIM_ConfigInt(ptime->time, TIM_INT_UPDATE, ENABLE);
}
static void bsp_time_count_etr_rcc_config(time_counter_etr *ptime)
{
if(ptime->time==TIM1||ptime->time==TIM8)
{
RCC_EnableAPB2PeriphClk(ptime->time_rcc, ENABLE);
}
else
{
RCC_EnableAPB1PeriphClk(ptime->time_rcc, ENABLE);
}
}
static void bsp_time_count_etr_gpio_config(time_counter_etr *ptime)
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure);
RCC_EnableAPB2PeriphClk(ptime->gpio_rcc, ENABLE);
GPIO_InitStructure.Pin = ptime->pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Input;
GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
GPIO_InitStructure.GPIO_Alternate = ptime->gpio_af;
GPIO_InitPeripheral(ptime->gpio_grp, &GPIO_InitStructure);
}
/**
* 基本定时器初始化
*/
static void bsp_time_count_etr_init(time_counter_etr *ptime)
{
TIM_TimeBaseInitType TIM_TimeBaseStructure;
bsp_time_count_etr_gpio_config(ptime);
/* 1.时钟使能 */
bsp_time_count_etr_rcc_config(ptime);
/* Time 2.基本配置 */
TIM_TimeBaseStructure.Period = ptime->period;
TIM_TimeBaseStructure.Prescaler = ptime->prescaler;//预分频器
TIM_TimeBaseStructure.ClkDiv = 0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_InitTimeBase(ptime->time, &TIM_TimeBaseStructure);
//配置外部触发
TIM_ConfigExtClkMode1(ptime->time,TIM_EXT_TRG_PSC_OFF,TIM_EXT_TRIG_POLARITY_NONINVERTED,0);
//3.使能更新中断
if(ptime->it_update)
{
bsp_time_count_etr_nvic_config(ptime) ;
}
/* 4.TIM1 使能计数 */
TIM_Enable(ptime->time, ENABLE);
}
//一键初始化所有定时器
void bsp_timer_count_etr_all_init(void)
{
for(int i=0; i<TIME_COUNT_ETR_NUM; i++)
{
bsp_time_count_etr_init(s_times_etr+i);
}
}
uint32_t bsp_timer_count_etr_get_count(em_tim_count_etr id)
{
uint32_t count;
if(TIME_COUNT_ETR_NUM>id)
{
time_counter_etr *ptime = s_times_etr+id;
count = TIM_GetCnt(ptime->time);
}
return count;
}
//定时器中断集中处理函数
static void bsp_time_count_etr_irq(time_counter_etr *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_etr+TIME2_ETR);
}