单片机源程序如下:- #include "timer.h"
- TIM_ICInitTypeDef TIM2_ICInitStructure;
- TIM_ICInitTypeDef TIM4_ICInitStructure;
- void TIM2_Cap_Init(u16 arr,u16 psc)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能TIM2时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟
-
- //TIM2的2个通道
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 输入 下拉
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
-
-
- TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频器
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
-
- //初始化TIM2通道1输入捕获参数
- TIM2_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 选择输入端 IC1映射到TI1上
- TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
- TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
- TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
- TIM2_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
- TIM_ICInit(TIM2, &TIM2_ICInitStructure);
- //初始化TIM2通道1输入捕获参数
- TIM2_ICInitStructure.TIM_Channel = TIM_Channel_2; //CC1S=01 选择输入端 IC1映射到TI1上
- TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
- TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
- TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
- TIM2_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
- TIM_ICInit(TIM2, &TIM2_ICInitStructure);
-
- TIM2_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 选择输入端 IC1映射到TI1上
- TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
- TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
- TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
- TIM2_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
- TIM_ICInit(TIM2, &TIM2_ICInitStructure);
-
- TIM2_ICInitStructure.TIM_Channel = TIM_Channel_4; //CC1S=01 选择输入端 IC1映射到TI1上
- TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
- TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
- TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
- TIM2_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
- TIM_ICInit(TIM2, &TIM2_ICInitStructure);
-
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级0级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
-
- TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);//允许更新中断 ,允许CC2IE捕获中断
-
- TIM_Cmd(TIM2,ENABLE );
- }
- //*****************************************************************
- //************************************************************
- void TIM4_Cap_Init(u16 arr,u16 psc)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能TIM4时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOA时钟
-
- //TIM4的2个通道
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 输入 下拉
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9);
-
-
- TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频器
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
-
- //初始化TIM4通道1输入捕获参数
- TIM4_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 选择输入端 IC1映射到TI1上
- TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
- TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
- TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
- TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
- TIM_ICInit(TIM4, &TIM4_ICInitStructure);
- TIM4_ICInitStructure.TIM_Channel = TIM_Channel_2; //CC1S=01 选择输入端 IC1映射到TI1上
- TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
- TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
- TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
- TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
- TIM_ICInit(TIM4, &TIM4_ICInitStructure);
-
- TIM4_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 选择输入端 IC1映射到TI1上
- TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
- TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
- TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
- TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
- TIM_ICInit(TIM4, &TIM4_ICInitStructure);
-
- TIM4_ICInitStructure.TIM_Channel = TIM_Channel_4; //CC1S=01 选择输入端 IC1映射到TI1上
- TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
- TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
- TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
- TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
- TIM_ICInit(TIM4, &TIM4_ICInitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级0级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
-
- TIM_ITConfig(TIM4,TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);//允许更新中断 ,允许CC2IE捕获中断
-
- TIM_Cmd(TIM4,ENABLE );
- }
- u8 TIM2CH1_CAPTURE_STA=0; //输入捕获状态
- u16 TIM2CH1_CAPTURE_VAL; //输入捕获值
- u8 TIM2CH2_CAPTURE_STA=0; //输入捕获状态
- u16 TIM2CH2_CAPTURE_VAL; //输入捕获值
- u16 TIM2CH3_CAPTURE_VAL; //输入捕获值
- u8 TIM2CH3_CAPTURE_STA=0; //输入捕获状态
- u16 TIM2CH4_CAPTURE_VAL; //输入捕获值
- u8 TIM2CH4_CAPTURE_STA=0; //输入捕获状态
- u8 TIM4CH1_CAPTURE_STA=0; //输入捕获状态
- u16 TIM4CH1_CAPTURE_VAL; //输入捕获值
- u8 TIM4CH2_CAPTURE_STA=0; //输入捕获状态
- u16 TIM4CH2_CAPTURE_VAL; //输入捕获值
- u16 TIM4CH3_CAPTURE_VAL; //输入捕获值
- u8 TIM4CH3_CAPTURE_STA=0; //输入捕获状态
- u16 TIM4CH4_CAPTURE_VAL; //输入捕获值
- u8 TIM4CH4_CAPTURE_STA=0; //输入捕获状态
- extern u32 distan1,distan2;
- extern u32 distan3,distan4;
- extern u32 distan5,distan6;
- extern u32 distan7,distan8;
- void TIM2_IRQHandler(void)
- {
- if((TIM2CH1_CAPTURE_STA&0X80)==0)//还未成功捕获
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
- {
- if(TIM2CH1_CAPTURE_STA&0X40)//已经捕获到高电平了
- {
- if((TIM2CH1_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
- {
- TIM2CH1_CAPTURE_STA|=0X80;//标记成功捕获了一次
- TIM2CH1_CAPTURE_VAL=0XFFFF;
- }else TIM2CH1_CAPTURE_STA++;
- }
- }
- if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)//捕获1发生捕获事件
- {
- if(TIM2CH1_CAPTURE_STA&0X40) //捕获到一个下降沿
- {
- TIM2CH1_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
- TIM2CH1_CAPTURE_VAL=TIM_GetCapture1(TIM2);
-
- distan1=TIM2CH1_CAPTURE_STA&0X3F;
- distan1*=65536;//溢出时间总和
- distan1+=TIM2CH1_CAPTURE_VAL;//得到总的高电平时间
- distan1=340*distan1/2/10000;
-
- TIM2CH1_CAPTURE_STA=0;
- TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
- }else //还未开始,第一次捕获上升沿
- {
- TIM2CH1_CAPTURE_STA=0; //清空
- TIM2CH1_CAPTURE_VAL=0;
- TIM_SetCounter(TIM2,0);
- TIM2CH1_CAPTURE_STA|=0X40; //标记捕获到了上升沿
- TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
- }
- }
- }
-
- if((TIM2CH2_CAPTURE_STA&0X80)==0)//还未成功捕获
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
-
- {
- if(TIM2CH2_CAPTURE_STA&0X40)//已经捕获到高电平了
- {
- if((TIM2CH2_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
- {
- TIM2CH2_CAPTURE_STA|=0X80;//标记成功捕获了一次
- TIM2CH2_CAPTURE_VAL=0XFFFF;
- }else TIM2CH2_CAPTURE_STA++;
- }
- }
- if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)//捕获1发生捕获事件
- {
- if(TIM2CH2_CAPTURE_STA&0X40) //捕获到一个下降沿
- {
- TIM2CH2_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
- TIM2CH2_CAPTURE_VAL=TIM_GetCapture2(TIM2);
- distan2=TIM2CH2_CAPTURE_STA&0X3F;
- distan2*=65536;//溢出时间总和
- distan2+=TIM2CH2_CAPTURE_VAL;//得到总的高电平时间
- distan2=340*distan2/2/10000;
-
- TIM2CH2_CAPTURE_STA=0;
- TIM_OC2PolarityConfig(TIM2,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
- }else //还未开始,第一次捕获上升沿
- {
- TIM2CH2_CAPTURE_STA=0; //清空
- TIM2CH2_CAPTURE_VAL=0;
- TIM_SetCounter(TIM2,0);
- TIM2CH2_CAPTURE_STA|=0X40; //标记捕获到了上升沿
- TIM_OC2PolarityConfig(TIM2,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
- }
- }
- }
-
-
- if((TIM2CH3_CAPTURE_STA&0X80)==0)//还未成功捕获
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
- {
- if(TIM2CH3_CAPTURE_STA&0X40)//已经捕获到高电平了
- {
- if((TIM2CH3_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
- {
- TIM2CH3_CAPTURE_STA|=0X80;//标记成功捕获了一次
- TIM2CH3_CAPTURE_VAL=0XFFFF;
- }else TIM2CH3_CAPTURE_STA++;
- }
- }
- if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)//捕获1发生捕获事件
- {
- if(TIM2CH3_CAPTURE_STA&0X40) //捕获到一个下降沿
- {
- TIM2CH3_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
- TIM2CH3_CAPTURE_VAL=TIM_GetCapture3(TIM2);
-
- distan3=TIM2CH3_CAPTURE_STA&0X3F;
- distan3*=65536;//溢出时间总和
- distan3+=TIM2CH3_CAPTURE_VAL;//得到总的高电平时间
- distan3=340*distan3/2/10000;
-
- TIM2CH3_CAPTURE_STA=0;
- TIM_OC3PolarityConfig(TIM2,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
- }else //还未开始,第一次捕获上升沿
- {
- TIM2CH3_CAPTURE_STA=0; //清空
- TIM2CH3_CAPTURE_VAL=0;
- TIM_SetCounter(TIM2,0);
- TIM2CH3_CAPTURE_STA|=0X40; //标记捕获到了上升沿
- TIM_OC3PolarityConfig(TIM2,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
- }
- }
- }
-
- if((TIM2CH4_CAPTURE_STA&0X80)==0)//还未成功捕获
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
- {
- if(TIM2CH4_CAPTURE_STA&0X40)//已经捕获到高电平了
- {
- if((TIM2CH4_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
- {
- TIM2CH4_CAPTURE_STA|=0X80;//标记成功捕获了一次
- TIM2CH4_CAPTURE_VAL=0XFFFF;
- }else TIM2CH4_CAPTURE_STA++;
- }
- }
- if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)//捕获1发生捕获事件
- {
- if(TIM2CH4_CAPTURE_STA&0X40) //捕获到一个下降沿
- {
- TIM2CH4_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
- TIM2CH4_CAPTURE_VAL=TIM_GetCapture4(TIM2);
-
- distan4=TIM2CH4_CAPTURE_STA&0X3F;
- distan4*=65536;//溢出时间总和
- distan4+=TIM2CH4_CAPTURE_VAL;//得到总的高电平时间
- distan4=340*distan4/2/10000;
-
- TIM2CH4_CAPTURE_STA=0;
- TIM_OC4PolarityConfig(TIM2,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
- }else //还未开始,第一次捕获上升沿
- {
- TIM2CH4_CAPTURE_STA=0; //清空
- TIM2CH4_CAPTURE_VAL=0;
- TIM_SetCounter(TIM2,0);
- TIM2CH4_CAPTURE_STA|=0X40; //标记捕获到了上升沿
- TIM_OC4PolarityConfig(TIM2,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
- }
- }
- }
-
- TIM_ClearITPendingBit(TIM2, TIM_IT_CC1| TIM_IT_CC2| TIM_IT_CC3| TIM_IT_CC4|TIM_IT_Update); //清除中断标志位
- }
- //***********************************************************************************
- //***********************************************************************************
- void TIM4_IRQHandler(void)
- {
-
-
-
- if((TIM4CH1_CAPTURE_STA&0X80)==0)//还未成功捕获
- {
- if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
- {
- if(TIM4CH1_CAPTURE_STA&0X40)//已经捕获到高电平了
- {
- if((TIM4CH1_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
- {
- TIM4CH1_CAPTURE_STA|=0X80;//标记成功捕获了一次
- TIM4CH1_CAPTURE_VAL=0XFFFF;
- }else TIM4CH1_CAPTURE_STA++;
- }
- }
- if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET)//捕获1发生捕获事件
- {
- if(TIM4CH1_CAPTURE_STA&0X40) //捕获到一个下降沿
- {
- TIM4CH1_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
- TIM4CH1_CAPTURE_VAL=TIM_GetCapture1(TIM4);
-
- distan5=TIM4CH1_CAPTURE_STA&0X3F;
- distan5*=65536;//溢出时间总和
- distan5+=TIM4CH1_CAPTURE_VAL;//得到总的高电平时间
- distan5=340*distan5/2/10000;
-
- TIM4CH1_CAPTURE_STA=0;
- TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
- }else //还未开始,第一次捕获上升沿
- {
- TIM4CH1_CAPTURE_STA=0; //清空
- TIM4CH1_CAPTURE_VAL=0;
- TIM_SetCounter(TIM4,0);
- TIM4CH1_CAPTURE_STA|=0X40; //标记捕获到了上升沿
- TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
- }
- }
- }
-
-
-
- if((TIM4CH2_CAPTURE_STA&0X80)==0)//还未成功捕获
- {
- if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
-
- {
- if(TIM4CH2_CAPTURE_STA&0X40)//已经捕获到高电平了
- {
- if((TIM4CH2_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
- {
- TIM4CH2_CAPTURE_STA|=0X80;//标记成功捕获了一次
- TIM4CH2_CAPTURE_VAL=0XFFFF;
- }else TIM4CH2_CAPTURE_STA++;
- }
- }
- if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)//捕获1发生捕获事件
- {
- if(TIM4CH2_CAPTURE_STA&0X40) //捕获到一个下降沿
- {
- TIM4CH2_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
- TIM4CH2_CAPTURE_VAL=TIM_GetCapture2(TIM4);
- distan6=TIM4CH2_CAPTURE_STA&0X3F;
- distan6*=65536;//溢出时间总和
- distan6+=TIM4CH2_CAPTURE_VAL;//得到总的高电平时间
- distan6=340*distan6/2/10000;
-
- TIM4CH2_CAPTURE_STA=0;
- TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
- }else //还未开始,第一次捕获上升沿
- {
- TIM4CH2_CAPTURE_STA=0; //清空
- TIM4CH2_CAPTURE_VAL=0;
- TIM_SetCounter(TIM4,0);
- TIM4CH2_CAPTURE_STA|=0X40; //标记捕获到了上升沿
- TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
- }
- }
- }
-
-
- if((TIM4CH3_CAPTURE_STA&0X80)==0)//还未成功捕获
- {
- if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
-
- {
- if(TIM4CH3_CAPTURE_STA&0X40)//已经捕获到高电平了
- {
- if((TIM4CH3_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
- {
- TIM4CH3_CAPTURE_STA|=0X80;//标记成功捕获了一次
- TIM4CH3_CAPTURE_VAL=0XFFFF;
- }else TIM4CH3_CAPTURE_STA++;
- }
- }
- if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET)//捕获1发生捕获事件
- {
- if(TIM4CH3_CAPTURE_STA&0X40) //捕获到一个下降沿
- {
- TIM4CH3_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
- TIM4CH3_CAPTURE_VAL=TIM_GetCapture3(TIM4);
- distan7=TIM4CH3_CAPTURE_STA&0X3F;
- distan7*=65536;//溢出时间总和
- distan7+=TIM4CH3_CAPTURE_VAL;//得到总的高电平时间
- distan7=340*distan7/2/10000;
-
- TIM4CH3_CAPTURE_STA=0;
- TIM_OC3PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
- }else //还未开始,第一次捕获上升沿
- {
- TIM4CH3_CAPTURE_STA=0; //清空
- TIM4CH3_CAPTURE_VAL=0;
- TIM_SetCounter(TIM4,0);
- TIM4CH3_CAPTURE_STA|=0X40; //标记捕获到了上升沿
- TIM_OC3PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
- }
- }
- }
-
-
- if((TIM4CH4_CAPTURE_STA&0X80)==0)//还未成功捕获
- {
- if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
-
- {
- if(TIM4CH4_CAPTURE_STA&0X40)//已经捕获到高电平了
- {
- if((TIM4CH4_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
- {
- TIM4CH4_CAPTURE_STA|=0X80;//标记成功捕获了一次
- TIM4CH4_CAPTURE_VAL=0XFFFF;
- }else TIM4CH4_CAPTURE_STA++;
- }
- }
- if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET)//捕获1发生捕获事件
- {
- if(TIM4CH4_CAPTURE_STA&0X40) //捕获到一个下降沿
- {
- TIM4CH4_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
- TIM4CH4_CAPTURE_VAL=TIM_GetCapture4(TIM4);
- distan8=TIM4CH4_CAPTURE_STA&0X3F;
- distan8*=65536;//溢出时间总和
- distan8+=TIM4CH4_CAPTURE_VAL;//得到总的高电平时间
- distan8=340*distan8/2/10000;
-
- TIM4CH4_CAPTURE_STA=0;
- TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
- }else //还未开始,第一次捕获上升沿
- {
- TIM4CH4_CAPTURE_STA=0; //清空
- TIM4CH4_CAPTURE_VAL=0;
- TIM_SetCounter(TIM4,0);
- TIM4CH4_CAPTURE_STA|=0X40; //标记捕获到了上升沿
- TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
- }
- }
- }
-
-
- TIM_ClearITPendingBit(TIM4, TIM_IT_CC1| TIM_IT_CC2| TIM_IT_CC3| TIM_IT_CC4|TIM_IT_Update); //清除中断标志位
- }
复制代码
全部资料51hei下载地址:
TIM2和TIM4八超声波.7z
(183.58 KB, 下载次数: 23)
|