使用Tim3 ETR功能,但是不知道为什么没法计数。 大家看看我的这个定时器初始化有没有什么问题。。 完全是原子哥的那个,就是改成了TIM3

2019-10-16 02:22发布

[mw_shl_code=c,true]void TIM3_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; GPIO_InitTypeDef GPIO_InitStructure; TIM_ICInitTypeDef TIM3_ICInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /*GPIOA Configuration*/ GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); TIM_DeInit(TIM3); /***72M下定时值的计算((1+预分频EXTI_Prescaler)/72*(1+定时周期EXTI_Period))*/ TIM_TimeBaseStructure.TIM_Period=0xffff;//预分频:72M/(TIM_Period+1)=72 TIM_TimeBaseStructure.TIM_Prescaler=71; //定时时间:(1/TIM_Period)*(TIM_Prescaler+1)=0.0139Us TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //时钟分割 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//Time base configuration TIM3_ICInitStructure.TIM_Channel = TIM_Channel_1; // 选择输入端 IC1 映射到 TI1 上 TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;// 捕获下降沿 TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上 TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //捕获在捕获输入上每探测到一个边沿执行一次 TIM3_ICInitStructure.TIM_ICFilter = 0x00; //输入比较滤波器 TIM_ICInit(TIM3, &TIM3_ICInitStructure); NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3 中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级 2 级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级 0 级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道被使能 NVIC_Init(&NVIC_InitStructure); //⑤根据指定的参数初始化 NV // TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_DIV8, TIM_ExtTRGPolarity_Inverted, 4);//8分频 TIM_ITConfig( TIM3,TIM_IT_Update|TIM_IT_CC1,ENABLE);//允许更新中断和捕获中断 TIM_Cmd(TIM3,ENABLE ); //⑥使能定时器 3 } void TIM3_IRQHandler(void) { //Fre_flag=1; if((TIM3CH1_CAPTURE_STA&0X80)==0)//还未成功捕获 { // Fre_flag=2; if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) { // Fre_flag=5; if(TIM3CH1_CAPTURE_STA&0X40)//已经捕获到高电平了 { // Fre_flag=6; if((TIM3CH1_CAPTURE_STA&0X3F)==0X3F)//高电平太长了 { // Fre_flag=7; TIM3CH1_CAPTURE_STA|=0X80;//标记成功捕获了一次 TIM3CH1_CAPTURE_VAL=0XFFFF; }else TIM3CH1_CAPTURE_STA++; } } if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)//捕获 1 发生捕获事件 { // Fre_flag=3; if(TIM3CH1_CAPTURE_STA&0X40) //捕获到一个下降沿 { // Fre_flag=8; TIM3CH1_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿 TIM3CH1_CAPTURE_VAL=TIM_GetCapture1(TIM3); TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising); //设置为上升沿捕获 } else //还未开始,第一次捕获上升沿 { // Fre_flag=4; TIM3CH1_CAPTURE_STA=0; //清空 TIM3CH1_CAPTURE_VAL=0; TIM_SetCounter(TIM3,0); TIM3CH1_CAPTURE_STA|=0X40; //标记捕获到了上升沿 TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling); //设置为下降沿捕获 } } } TIM_ClearITPendingBit(TIM3, TIM_IT_CC1|TIM_IT_Update); //清除中断标志位 }[/mw_shl_code]

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。