新手上路
- 积分
- 25
- 金钱
- 25
- 注册时间
- 2015-2-10
- 在线时间
- 0 小时
|
发表于 2014-11-19 19:08:03
|
显示全部楼层
给你参考一下
void TIM1_Cap_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/*RCC Configuration*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //开启了复用时钟
/*GPIO Configuration*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_8|GPIO_Pin_11);
/*Timebase Configuration*/
TIM_InternalClockConfig(TIM1); //时钟源选择内部时钟用于计时
TIM_TimeBaseInitStructure.TIM_Prescaler = TIM1_PSC; //预分频计数器
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //上升沿计数
TIM_TimeBaseInitStructure.TIM_Period = TIM1_ARR; //计数周期
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频,不分频
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
/*PWM_Input Configuration*/
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //输入通道1
TIM_ICInitStructure.TIM_ICPolarity= TIM_ICPolarity_Rising; //上升沿捕获
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //输入映射选择为对应的Tl1
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //预分频1
TIM_ICInitStructure.TIM_ICFilter = 0x00; //滤波器:不滤波
TIM_ICInit(TIM1,&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; //输入通道4
TIM_ICInit(TIM1,&TIM_ICInitStructure);
/*NVIC Configuration*/
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_Init(&NVIC_InitStructure);
/*Interrupt Configuration*/
TIM_ClearITPendingBit(TIM1,TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC4);
TIM_ITConfig(TIM1,TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC4,ENABLE);
TIM_Cmd(TIM1,ENABLE);
}
void TIM1_CC_IRQHandler(void)
{
//
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1|TIM_IT_CC4);
}
void TIM1_UP_IRQHandler(void)
{
//
TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
} |
|