初级会员
- 积分
- 57
- 金钱
- 57
- 注册时间
- 2017-7-12
- 在线时间
- 25 小时
|
2金钱
主程序
while(1)
{
Wave_SRD_Strat();
LED1=!LED1;
delay_ms(3000);
}
下面的wave初始化程序
#define Trig GPIO_Pin_4
#define Echo GPIO_Pin_6
float Distance;
void Wave_SRD_Init(void)
{
GPIO_InitTypeDef GPIO_InitSture;
EXTI_InitTypeDef EXTI_InitSture;
NVIC_InitTypeDef NVIC_InitSture;
//èç1ûía2¿ÖD¶ÏμÄ»°Ôòò»¶¨ê1ÄüAFIO¸′óÃ1|Äü
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOE,ENABLE);
//ÅäÖÃIO¶Ë¿ú
GPIO_InitSture.GPIO_Mode=GPIO_Mode_Out_PP; //íÆíìêä3öÄ£ê½
GPIO_InitSture.GPIO_Pin=Trig; //½«E4óúTrigÏàá¬
GPIO_InitSture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitSture);
GPIO_InitSture.GPIO_Mode=GPIO_Mode_IPD; //à-êäèëÄ£ê½
GPIO_InitSture.GPIO_Pin=Echo; //½«E6óúEchoÏàá¬
GPIO_InitSture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitSture);
//ÖD¶Ïoí6¶Ë¿úó3éäò»Æe
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource6);
//ía2¿ÖD¶ÏÅäÖÃ
EXTI_InitSture.EXTI_Line=EXTI_Line6;
EXTI_InitSture.EXTI_LineCmd=ENABLE;
EXTI_InitSture.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitSture.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitSture);
//ÖD¶ÏóÅÏ輶1üàí
NVIC_InitSture.NVIC_IRQChannel=EXTI9_5_IRQn;
NVIC_InitSture.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitSture.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitSture.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitSture);
}
void EXTI9_5_IRQHandler(void)
{
delay_us(10);
if(EXTI_GetITStatus(EXTI_Line6)!=RESET)
{
TIM_SetCounter(TIM3,0);
TIM_Cmd(TIM3,ENABLE);
while(GPIO_ReadInputDataBit(GPIOE,Echo)); //μè′yμíμçƽ
TIM_Cmd(TIM3,DISABLE);
Distance=TIM_GetCounter(TIM3)*340/200.0;
if(Distance>0)
{
printf("Distance:%f cm\r\n",Distance);
}
EXTI_ClearITPendingBit(EXTI_Line6);
}
}
void Wave_SRD_Strat(void)
{
GPIO_SetBits(GPIOE,Trig); //½«TrigéèÖÃÎa¸ßμçƽ
delay_us(20); //3ÖDø′óóú10us′¥·¢£¬′¥·¢3¬éù2¨Ä£¿é1¤×÷
GPIO_ResetBits(GPIOE,Trig);
}
|
最佳答案
查看完整内容[请看2#楼]
我的理解是:超声波一直发送数据,表明程序一直进入中断,没办法执行LED控制的代码。具体原因我也不清楚,但你可以不采用外部中断的形式来读取超声波的数据,可以直接读取该引脚的电平,读到高电平开始计数,读到低电平结束计数,下面是我以前用过的代码,你可以参考一下
int GetDis(int echo,int trig)
{
int dis;
int count;
GPIO_ResetBits(GPIOF,echo); //echo端口复位
GPIO_ResetBits(GPIOF,trig); //trig端 ...
|