我刚刚开始学F103现在在写一个超声波测距的程序 中断测时间这块总是只计时一次。大家帮我看看

2019-07-21 01:39发布

下面是写的程序,循环计时总是只有1U也就是只能循环一次,但是在进入中断钱的计时应该远远长鱼1us始终实现不了计时  大家帮忙看看
#include "ultrasound.h"

static volatile u8 flag;
extern volatile int distance;
//超声波管脚初始化
void Ultrasound_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
//初始化时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO ,ENABLE);

//配置Trig
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//配置Echo
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//外部中断
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource12); 
EXTI_InitStructure.EXTI_Line = EXTI_Line12; //外部中断线12
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能
EXTI_Init(&EXTI_InitStructure);
//配置中断向量表
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //分组2
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;  //抢占优先级3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //响应优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_Init(&NVIC_InitStructure);  
}


void Distance_Convert()
{
int D_time = 0;
flag = 1;//计时标志位
LED_5 = 1; //计时检测灯
Ultrasound_Trig = 1; //触发超声波模块
delay_us(10); //触发时间延时
Ultrasound_Trig = 0; //关闭
while(flag)
{
delay_us(1);
D_time ++;
}
distance = D_time * 17;  //计算距离
LED_5 = 0;
}

void EXTI15_10_IRQHandler()
{
if(EXTI_GetITStatus(EXTI_Line12) != RESET)
{
flag = 0;  //检测到下降进入中断将标志位置零 跳出循环
}
EXTI_ClearITPendingBit(EXTI_Line12);  //清除中断标志
}

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