2019-07-14 16:24发布
wangyuzi87 发表于 2018-11-1 13:28 本帖最后由 刚声打火机 于 2015-10-19 09:11 编辑 两路都是可以捕获的哈
最多设置5个标签!
两路都是可以捕获的哈
我公司产品中有实际应用
给你贴上源码 可以参考一下
#define TIM1_PRESCALER 2
typedef void (*tim1_cc_irq_t)(void);
/* 使能TIM1 */
#define START_TIM1()
do {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
} while (0);
/* 停止TIM1 */
#define STOP_TIM1()
do {
TIM_Cmd(TIM1, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, DISABLE);
} while (0);
void bsp_tim1_init(void);
void tim1_ch3_set_irq_callback(tim1_cc_irq_t func);
void tim1_ch4_set_irq_callback(tim1_cc_irq_t func);
static tim1_cc_irq_t __tim1_cc3_irq = NULL;
static tim1_cc_irq_t __tim1_cc4_irq = NULL;
/* +------------------------------------------+ *
* | TIM1初始化 | *
* +------------------------------------------+ */
void bsp_tim1_init(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* TIM1 CC CH3 */
bsp_gpio_init(GPIOA,
GPIO_Pin_10,
GPIO_Mode_AF,
GPIO_Speed_50MHz,
GPIO_OType_PP,
GPIO_PuPd_UP);
/* TIM1 CC CH4 */
bsp_gpio_init(GPIOA,
GPIO_Pin_11,
GPIO_Mode_AF,
GPIO_Speed_50MHz,
GPIO_OType_PP,
GPIO_PuPd_UP);
/* Connect TIM pins to AF2 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_2);
TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF;
/* 累计 TIM_Period个频率后产生一个更新或者中断 */
TIM_TimeBaseStructure.TIM_Prescaler = TIM1_PRESCALER - 1; // 24M
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 采样分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
TIM_Cmd(TIM1, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_CC3, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_CC4, ENABLE);
nvic_config(TIM1_CC_IRQn, TIM1_CC_PRIOR, ENABLE);
}
/* +------------------------------------------+ *
* | TIM1 CH3 初始化回调函数 | *
* +------------------------------------------+ */
void tim1_ch3_set_irq_callback(tim1_cc_irq_t func)
{
__tim1_cc3_irq = func;
}
/* +------------------------------------------+ *
* | TIM1 CH4 初始化回调函数 | *
* +------------------------------------------+ */
void tim1_ch4_set_irq_callback(tim1_cc_irq_t func)
{
__tim1_cc4_irq = func;
}
/* +------------------------------------------+ *
* | TIM1 CC 中断 | *
* +------------------------------------------+ */
void TIM1_CC_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_CC3) == SET) {
TIM_ClearITPendingBit(TIM1, TIM_IT_CC3);
if (NULL != __tim1_cc3_irq) {
(__tim1_cc3_irq)();
}
}
if (TIM_GetITStatus(TIM1, TIM_IT_CC4) == SET) {
TIM_ClearITPendingBit(TIM1, TIM_IT_CC4);
if (NULL != __tim1_cc4_irq) {
(__tim1_cc4_irq)();
}
}
}
补充一下:
bsp_gpio_init() 这个函数我自己封装过,定义是这样的:
/* +------------------------------------------+ *
* | 封装了一层GPIO初始化函数 | *
* +------------------------------------------+ */
/* GPIO Group Periph Manager*/
typedef struct ggm_s {
GPIO_TypeDef *gpio_x;
u32 periph;
vu32 bit_map;
} ggpm_t;
static ggpm_t ggpm[MAX_GPIO_GROUP_NUM] = {
{GPIOA, RCC_AHBPeriph_GPIOA, 0x0},
{GPIOB, RCC_AHBPeriph_GPIOB, 0x0},
{GPIOC, RCC_AHBPeriph_GPIOC, 0x0},
{GPIOD, RCC_AHBPeriph_GPIOD, 0x0},
{GPIOE, RCC_AHBPeriph_GPIOE, 0x0},
{GPIOF, RCC_AHBPeriph_GPIOF, 0x0},
};
void bsp_gpio_init(GPIO_TypeDef *gpio_x, u32 gpio_pin, GPIOMode_TypeDef mode, GPIOSpeed_TypeDef speed, GPIOOType_TypeDef otype, GPIOPuPd_TypeDef pupd)
{
GPIO_InitTypeDef gpio_init_struct;
int i = 0;
for (i = 0; i < (sizeof(ggpm) / sizeof(ggpm_t)); i++) {
if (ggpm.gpio_x == gpio_x) {
if ( ! (ggpm.bit_map & GPIO_PIN_NUM_MASK) ) {
RCC_AHBPeriphClockCmd(ggpm.periph, ENABLE);
}
ggpm.bit_map |= gpio_pin;
gpio_init_struct.GPIO_Pin = gpio_pin;
gpio_init_struct.GPIO_Mode = mode;
gpio_init_struct.GPIO_Speed = speed;
gpio_init_struct.GPIO_OType = otype;
gpio_init_struct.GPIO_PuPd = pupd;
GPIO_Init(gpio_x, &gpio_init_struct);
break;
}
}
}
你这个代码封装的还是不错的
一周热门 更多>