//定时器3通道2输入捕获配置
//arr:自动重装值(TIM3是16位的!!)
//psc:时钟预分频数
void TIM3_CH2_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //TIM3时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //GPIOA7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA7
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM3); //PA7复用位定时器
TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
//初始化TIM2输入捕获参数
TIM2_ICInitStructure.TIM_Channel = TIM_Channel_2; //映射到通道2!!!!!!!!!!!!!!!
TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI2上
TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM2_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM3, &TIM3_ICInitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC2,ENABLE);//允许更新中断 ,允许CC2IE捕获中断
TIM_Cmd(TIM3,ENABLE ); //使能定时器2
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
}
//捕获状态
//[7]:0,没有成功的捕获;1,成功捕获到一次.
//[6]:0,还没捕获到低电平;1,已经捕获到低电平了.
//[5:0]:捕获低电平后溢出的次数(对于16位定时器来说,1us计数器加1,溢出时间:0.065536秒)
u8 TIM3CH2_CAPTURE_STA=0; //输入捕获状态
u16 TIM3CH2_CAPTURE_VAL; //输入捕获值(TIM3是16位)
//定时器3中断服务程序
void TIM3_IRQHandler(void)
{
if((TIM3CH2_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)//溢出
{
if(TIM3CH2_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM3CH2_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM3CH2_CAPTURE_STA|=0X80; //标记成功捕获了一次
TIM3CH2_CAPTURE_VAL=0XFFFF;
}else TIM3CH2_CAPTURE_STA++;
}
}
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); //CC2P=0 设置为上升沿捕获
}else //还未开始,第一次捕获上升沿
{
TIM3CH2_CAPTURE_STA=0; //清空
TIM3CH2_CAPTURE_VAL=0;
TIM3CH2_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_Cmd(TIM3,DISABLE ); //关闭定时器3
TIM_SetCounter(TIM3,0);
TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC2P=1 设置为下降沿捕获
TIM_Cmd(TIM3,ENABLE ); //使能定时器3
}
}
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
}
main
TIM3_CH2_Cap_Init(0XFFFF,84-1);
SR04_START_3();
if(TIM3CH2_CAPTURE_STA&0X80) //成功捕获到了一次高电平
{
temp_3=TIM3CH2_CAPTURE_STA&0X3F;
temp_3*=0XFFFF; //溢出时间总和
temp_3+=TIM3CH2_CAPTURE_VAL; //得到总的高电平时间
//printf("HIGH:%lld us
",temp_3); //打印总的高点平时间
TIM3CH2_CAPTURE_STA=0; //开启下一次捕获
}
Distance_3 = ((temp_3/1000000.0)*Sound_Speed*100.0)/2.0; //测量距离单位cm
printf("DISTANCE3:%lld cm
",Distance_3); //打印距离
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>