我想通过TIM5_CH2来捕获,引脚是PA1,用的是将IC1.IC2都映射到TI2,IC1上升沿触发.IC2下降沿触发。可是程序就是不仅让中断。代码如下
void TIM5_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); /*使能TIM2时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); /*使能GPIOA时钟*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; /**/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /*PA0 输入*/
GPIO_Init(GPIOA,&GPIO_InitStructure);
// GPIO_ResetBits(GPIOA,GPIO_Pin_3); /*PA0 下拉*/
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM5,&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
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 = 0;
TIM_ICInit(TIM5,&TIM_ICInitStructure);
/*中断分组初始化*/
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; /*TIM2中断*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; /*先占优先级2级*/
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; /*从优先级0级*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /*IRQ通道被使能*/
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2,ENABLE);
// TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC3,ENABLE);
TIM_Cmd(TIM5,ENABLE);
}
u16 Current1 = 0;
u16 Current2 = 0;
u16 Last1 = 0;
u16 Last2 = 0;
/**
* 定时器2中断服务程序
*/
void TIM5_IRQHandler(void)
{
// Current1 = TIM_GetCounter(TIM5);
// printf("Current1=%d
", Current1);
// printf("Current2=%d
", Current2);
// printf("TIM2_IRQHandler
");
if(TIM_GetITStatus(TIM5, TIM_IT_CC1)!=RESET)
{
// printf("falling
");
TIM_ClearITPendingBit(TIM5,TIM_IT_CC1); /*清除中断标志位*/
Current1 = TIM_GetCounter(TIM5);
// Current1 = TIM_GetCapture1(TIM5);
falling = Current1 - Last2;
Last1 = Current1;
printf("Current1=%d
", Current1);
}
if(TIM_GetITStatus(TIM5, TIM_IT_CC2)!=RESET)
{
TIM_ClearITPendingBit(TIM5,TIM_IT_CC2); /*清除中断标志位*/
printf("raising
");
Current2 = TIM_GetCounter(TIM5);
// Current2 = TIM_GetCapture2(TIM5);
falling = Current2 - Last1;
Last2 = Current2;
printf("Current2=%d
", Current2);
}
}
一周热门 更多>