超声波手势识别在市场上已经有见实现,但研究其传感器发现并不是市场上随意可见的,如果暂且考虑成本,该如何入门实现简单的手势识别呢。聊天中老师给出一个很好的提议,就是固定四个超声波,分别为上下左右,然后进行程序上的对应编号,用单片机实现四路超声波的距离数据读取,然后程序分析读取的数据进而判断手势。STM32单片机有多个定时器,每个定时器接入一个超声波,分别接入四个,定时器分别开始工作以计数,将得到的距离信息一次性发送四个方向的值到串口,串口连接到PC机,PC机获取到四组值,然后进行分析解释。下面将实现第一步,STM32 实现四路超声波获取。
实现效果:
Python的串口程序上每次从串口获取到上下左右四组值,并输出在控制台,相关的串口实现可以参考:Python的串口通信(pyserial)
超声波模块:HC-SR04
单片机:stm32f103c8t6
注意:程序正常工作的前提是必须按复位键
获取值【上,下,左,右】
对应echo的IO【PA0 , PA11 , PA7 , PB6】
对应trig 的IO【PB12 , PB13 , PB14 , PB15】
对应定时器【定时器2通道1,高级定时器1通道4,定时器3通道2,定时器4通道1】
串口1:A9为TX A10为RX
超声波使用基本介绍:
(1)采用IO口TRIG触发测距,给至少10us的高电平信号;
(2)模块自动发送8个40khz的方波,自动检测是否有信号返回;
(3)有信号返回,通过IO口ECHO输出一个高电平,高电平持续的时间就是超声波从发射到返回的时间。测试距离=(高电平时间*声速(340M/S)) (未实现)
超声波时序:
主函数代码:
#include "stm32f10x.h" //STM32头文件
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "timex.h"
#include "trig.h" extern u8 TIM3CH1_CAPTURE_STA; //输入捕获状态
extern u16 TIM3CH1_CAPTURE_VAL; //输入捕获值
extern u8 TIM3CH2_CAPTURE_STA; //输入捕获状态
extern u16 TIM3CH2_CAPTURE_VAL; //输入捕获值
extern u8 TIM2CH1_CAPTURE_STA; //输入捕获状态
extern u16 TIM2CH1_CAPTURE_VAL; //输入捕获值
extern u8 TIM4CH1_CAPTURE_STA; //输入捕获状态
extern u16 TIM4CH1_CAPTURE_VAL; //输入捕获值
extern u8 TIM1CH4_CAPTURE_STA; //输入捕获状态
extern u16 TIM1CH4_CAPTURE_VAL; //输入捕获值
int main (void){//主程序
u32 temp1=;
//u32 temp2=0;
u32 temp3=;
u32 temp4=;
u32 temp24=;
RCC_Configuration(); //时钟设置
TR_Init(); //输出初始化 USART1_Init(); TIM3_Cap_Init(0XFFFF,-); //以1Mhz的频率计数 //打印总的高点平时间
TIM2_Cap_Init(0XFFFF,-); //以1Mhz的频率计数 //打印总的高点平时间
TIM4_Cap_Init(0XFFFF,-); //以1Mhz的频率计数 //打印总的高点平时间
TIM1_Cap_Init(0XFFFF,-); //以1Mhz的频率计数 //打印总的高点平时间
delay_s();
printf("********** INIT ALL 11*********\r\n");
PBout()=;
delay_us();
PBout()=; PBout()=;
delay_us();
PBout()=; PBout()=;
delay_us();
PBout()=; PBout()=;
delay_us();
PBout()=; while(){
//定时器2通道1
if(TIM2CH1_CAPTURE_STA&0X80)//成功捕获到了一次上升沿
{
temp1=TIM2CH1_CAPTURE_STA&0X3F;
temp1*=;//溢出时间总和
temp1+=TIM2CH1_CAPTURE_VAL;//得到总的高电平时间
printf("time2-----:%d \r\n",temp1);//打印总的高点平时间
TIM2CH1_CAPTURE_STA=;//开启下一次捕获
PBout()=;
delay_us();
PBout()=;
} //定时器1通道4
if(TIM1CH4_CAPTURE_STA&0X80)//成功捕获到了一次上升沿
{
temp24=TIM1CH4_CAPTURE_STA&0X3F;
temp24*=;//溢出时间总和
temp24+=TIM1CH4_CAPTURE_VAL;//得到总的高电平时间
printf("time14-----:%d \r\n",temp24);//打印总的高点平时间
TIM1CH4_CAPTURE_STA=;//开启下一次捕获
PBout()=;
delay_us();
PBout()=;
}
//定时器3通道2
if(TIM3CH2_CAPTURE_STA&0X80)//成功捕获到了一次上升沿
{
temp3=TIM3CH2_CAPTURE_STA&0X3F;
temp3*=;//溢出时间总和
temp3+=TIM3CH2_CAPTURE_VAL;//得到总的高电平时间
printf("time32----:%d \r\n",temp3);//打印总的高点平时间
TIM3CH2_CAPTURE_STA=;//开启下一次捕获
PBout()=;
delay_us();
PBout()=;
}
//定时器4通道1
if(TIM4CH1_CAPTURE_STA&0X80)//成功捕获到了一次上升沿
{
temp4=TIM4CH1_CAPTURE_STA&0X3F;
temp4*=;//溢出时间总和
temp4+=TIM4CH1_CAPTURE_VAL;//得到总的高电平时间
printf("time4-----:%d \r\n",temp4);//打印总的高点平时间
TIM4CH1_CAPTURE_STA=;//开启下一次捕获
PBout()=;
delay_us();
PBout()=;
} //发送最终结果------------------------------------
if((temp1 >)&&(temp24 >)&&(temp3 >)&&(temp4 >)){ printf("[%d,%d,%d,%d]",temp1,temp24,temp3,temp4);
temp1=temp24=temp3=temp4=;
PBout()=;
} }
}
定时器2通道1代码实现:
#include"timex.h"
#include "usart.h" //定时器3通道1输入捕获配置 TIM_ICInitTypeDef TIM2_ICInitStructure; void TIM2_Cap_Init(u16 arr,u16 psc)
{ GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
printf("---time 22222 1111 ---\r\n");
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能TIM2时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PA0清除之前设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_0); //PA0下拉 //初始化定时器5 TIM5
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的时间基数单位 //初始化TIM5输入捕获参数
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); //中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
//printf("***********************************\r\n");
TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC1,ENABLE);//允许更新中断 ,允许CC1IE捕获中断 TIM_Cmd(TIM2,ENABLE ); //使能定时器5 } u8 TIM2CH1_CAPTURE_STA=; //输入捕获状态
u16 TIM2CH1_CAPTURE_VAL; //输入捕获值 //定时器5中断服务程序
void TIM2_IRQHandler(void)
{
//printf("********************222222***************\r\n");
if((TIM2CH1_CAPTURE_STA&0X80)==)//还未成功捕获
{
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);
TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
}else //还未开始,第一次捕获上升沿
{
TIM2CH1_CAPTURE_STA=; //清空
TIM2CH1_CAPTURE_VAL=;
TIM_SetCounter(TIM2,);
TIM2CH1_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
} TIM_ClearITPendingBit(TIM2, TIM_IT_CC1|TIM_IT_Update); //清除中断标志位 }
定时器1通道4实现:
#include"timex.h"
#include "usart.h" TIM_ICInitTypeDef TIM1_ICInitStructure; void TIM1_Cap_Init(u16 arr,u16 psc)
{ GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
printf("---time 11111 4444 ---\r\n");
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_11; //清除之前设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // 输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_8|GPIO_Pin_11); //下拉 //初始化定时器1
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(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 //初始化TIM1
TIM1_ICInitStructure.TIM_Channel = TIM_Channel_4; //CC1S=01 选择输入端 IC1映射到TI1上
TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM1_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM1, &TIM1_ICInitStructure); //中断分组初始化
// Device header
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; //中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 TIM_ITConfig(TIM1,TIM_IT_CC4|TIM_IT_Update,ENABLE);//允许更新中断 ,允许CC1IE捕获中断 TIM_Cmd(TIM1,ENABLE ); //使能定时器1 } u8 TIM1CH4_CAPTURE_STA=; //输入捕获状态
int TIM1CH4_CAPTURE_VAL_0,TIM1CH4_CAPTURE_VAL; //输入捕获值
//定时器5中断服务程序
void TIM1_UP_IRQHandler(void)
{ if((TIM1CH4_CAPTURE_STA&0X80)==)//还未成功捕获
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) {
if(TIM1CH4_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM1CH4_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM1CH4_CAPTURE_STA|=0X80;//标记成功捕获了一次
TIM1CH4_CAPTURE_VAL=0XFFFF;
}else TIM1CH4_CAPTURE_STA++;
}
}
} TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //清除中断标志位 }
void TIM1_CC_IRQHandler(void)
{
if((TIM1CH4_CAPTURE_STA&0X80)==)//还未成功捕获
{
if (TIM_GetITStatus(TIM1, TIM_IT_CC4) != RESET)//捕获1发生捕获事件
{
if(TIM1CH4_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM1CH4_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
TIM1CH4_CAPTURE_VAL=TIM_GetCapture4(TIM1)-TIM1CH4_CAPTURE_VAL_0;
TIM_OC4PolarityConfig(TIM1,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
}else //还未开始,第一次捕获上升沿
{
TIM1CH4_CAPTURE_STA=; //清空
TIM1CH4_CAPTURE_VAL_0=TIM_GetCapture4(TIM1);//!!!!!!!!!!!!!!!!!!!!!!!
//TIM_SetCounter(TIM1,0);
TIM1CH4_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC4PolarityConfig(TIM1,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
} TIM_ClearITPendingBit(TIM1, TIM_IT_CC1); //清除中断标志位 }
定时器3通道2代码实现:
#include"timex.h"
#include "usart.h" //定时器3通道1输入捕获配置 TIM_ICInitTypeDef TIM3_ICInitStructure;
TIM_ICInitTypeDef TIM3_ICInitStructure1;
void TIM3_Cap_Init(u16 arr,u16 psc)
{ GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure; printf("---time 33333 2222 ---\r\n");
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //清除之前设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_6 | GPIO_Pin_7); //下拉 //初始化定时器5 TIM5
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(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 //初始化TIM5输入捕获参数
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 选择输入端 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;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM3, &TIM3_ICInitStructure); //初始化TIM5输入捕获参数
TIM3_ICInitStructure1.TIM_Channel = TIM_Channel_2; //CC1S=01 选择输入端 IC1映射到TI1上
TIM3_ICInitStructure1.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM3_ICInitStructure1.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI2上!!!!!!!!!!!!!! !!!!!
TIM3_ICInitStructure1.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM3_ICInitStructure1.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM3, &TIM3_ICInitStructure1); //中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
//printf("***********************************\r\n");
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2,ENABLE);//允许更新中断 ,允许CC1IE捕获中断 TIM_Cmd(TIM3,ENABLE ); //使能定时器5 } u8 TIM3CH1_CAPTURE_STA=; //输入捕获状态
u16 TIM3CH1_CAPTURE_VAL; //输入捕获值 u8 TIM3CH2_CAPTURE_STA=; //输入捕获状态
u16 TIM3CH2_CAPTURE_VAL; //输入捕获值 void TIM3_IRQHandler(void)
{
//printf("********************222222***************\r\n"); if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) {
if((TIM3CH1_CAPTURE_STA&0X80)==)//还未成功捕获
{
if(TIM3CH1_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM3CH1_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{ TIM3CH1_CAPTURE_VAL=0XFFFF;
TIM3CH1_CAPTURE_STA|=0X80;//标记成功捕获了一次
}else TIM3CH1_CAPTURE_STA++;
}
} if((TIM3CH2_CAPTURE_STA&0X80)==)//还未成功捕获
{
if(TIM3CH2_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM3CH2_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{ TIM3CH2_CAPTURE_VAL=0XFFFF;
TIM3CH2_CAPTURE_STA|=0X80;//标记成功捕获了一次
}else TIM3CH2_CAPTURE_STA++;
} }
}else{ if((TIM3CH1_CAPTURE_STA&0X80)==)//还未成功捕获
{
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)//捕获1发生捕获事件
{ if(TIM3CH1_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM3CH1_CAPTURE_STA|=0X80; //标记成功捕获到一次高电平脉宽
TIM3CH1_CAPTURE_VAL=TIM_GetCapture1(TIM3);
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
}else //还未开始,第一次捕获上升沿
{
TIM3CH1_CAPTURE_STA=; //清空
TIM3CH1_CAPTURE_VAL=;
TIM_SetCounter(TIM3,); TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
TIM3CH1_CAPTURE_STA|=0X40; //标记捕获到了上升沿
}
}
} if((TIM3CH2_CAPTURE_STA&0X80)==)//还未成功捕获
{
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)//捕获1发生捕获事件
{
if(TIM3CH2_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM3CH2_CAPTURE_STA|=0X80; //标记成功捕获到一次高电平脉宽
TIM3CH2_CAPTURE_VAL=TIM_GetCapture2(TIM3);
TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
}else //还未开始,第一次捕获上升沿
{
TIM3CH2_CAPTURE_STA=; //清空
TIM3CH2_CAPTURE_VAL=;
TIM_SetCounter(TIM3,); TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
TIM3CH2_CAPTURE_STA|=0X40; //标记捕获到了上升沿
}
} }}
// TIM_SetCounter(TIM3,0);
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1|TIM_IT_Update|TIM_IT_CC2); //清除中断标志位 }
定时器4通道1代码实现:
#include"timex.h"
#include "usart.h" //定时器4通道1输入捕获配置 TIM_ICInitTypeDef TIM4_ICInitStructure; void TIM4_Cap_Init(u16 arr,u16 psc)
{ GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
printf("---time 44444 1111 ---\r\n");
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能TIM4时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOB时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //清除之前设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_6); // 下拉 //初始化定时器5 TIM5
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的时间基数单位 //初始化TIM5输入捕获参数
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); //中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
//printf("***********************************\r\n");
TIM_ITConfig(TIM4,TIM_IT_Update|TIM_IT_CC1,ENABLE);//允许更新中断 ,允许CC1IE捕获中断 TIM_Cmd(TIM4,ENABLE ); //使能定时器5 } u8 TIM4CH1_CAPTURE_STA=; //输入捕获状态
u16 TIM4CH1_CAPTURE_VAL; //输入捕获值 //定时器5中断服务程序
void TIM4_IRQHandler(void)
{
//printf("********************222222***************\r\n");
if((TIM4CH1_CAPTURE_STA&0X80)==)//还未成功捕获
{
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);
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
}else //还未开始,第一次捕获上升沿
{
TIM4CH1_CAPTURE_STA=; //清空
TIM4CH1_CAPTURE_VAL=;
TIM_SetCounter(TIM4,);
TIM4CH1_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
} TIM_ClearITPendingBit(TIM4, TIM_IT_CC1|TIM_IT_Update); //清除中断标志位 }