void TIM5_Configuration(unsigned short frequency)
{ //8000
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIOA0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA0
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5); //PA0复用位定时器5
TIM_DeInit(TIM5);
TIM_TimeBaseStructure.TIM_Period = frequency;
TIM_TimeBaseStructure.TIM_Prescaler = 1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //设置计数器模式为向上计数模式
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure); //将配置应用到TIM2中
TIM_ClearFlag(TIM5, TIM_FLAG_Update); //清除溢出中断标志
//初始化TIM5输入捕获参数
TIM5->CCER &= (uint16_t)~((uint16_t)TIM_CCER_CC1E); //捕获禁止
TIM5->CCMR1 |= 0x03; // IC1映射在TRC上
TIM5->SMCR |= (1 << 6); //TI1的边沿检测器
TIM5->SMCR &= ~((1 << 5) | (1 << 4));
TIM5->CCER |= ((uint16_t)TIM_CCER_CC1E); //捕获使能
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);//允许更新中断 ,允许CC1IE捕获中断
TIM_Cmd(TIM5, ENABLE);//DISABLE);
}
void TIM5_IRQHandler(void)
{
if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)//是更新中断
{
TIM_ClearITPendingBit(TIM5, TIM_IT_Update); //清除TIM3更新中断标志
}
if(TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)//捕获1发生捕获事件
{
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1); //清除TIM3更新中断标志
if(GPIOA->IDR & 0x0001) //上升沿
{
testcnt1++;
}
else
{
testcnt2++;
}
}
}