专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
STM32
stm32多个超声波测距(最少四个)
2019-07-20 23:57
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
3221
17
950
有没有大侠会多个超声波测距,最少四个,怎么用?初学者,求指导,求源程序
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
17条回答
名字要起的长点
2019-07-22 09:24
正点原子 发表于 2015-5-12 22:49
先写一个出来,然后在搞多个。
原子哥我改了两个超声波,单个都能实现,但是两个一起就出问题了[mw_shl_code=c,true]#include "UltrasonicWave.h"
#include "usart.h"
#include "timer.h"
#include "delay.h"
#define TRIG_PORT GPIOC //TRIG
#define ECHO_PORT GPIOC //ECHO
#define TRIG_0_PIN GPIO_Pin_8 //TRIG
#define TRIG_1_PIN GPIO_Pin_2 //TRIG
#define ECHO_0_PIN GPIO_Pin_7 //ECHO
#define ECHO_1_PIN GPIO_Pin_1 //ECHO
float UltrasonicWave_Distance0; //¼ÆËã3öμľààë
float UltrasonicWave_Distance1;
/*
* oˉêyÃû£oUltrasonicWave_Configuration
* Ãèêö £o3¬éù2¨Ä£¿éμÄ3õê¼»ˉ
* êäèë £oÎT
* êä3ö £oÎT
*/
void UltrasonicWave_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //1رÕjtag
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = TRIG_0_PIN|TRIG_1_PIN; //PC8£¬2½óTRIG
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //éèÎaíÆíìêä3öÄ£ê½
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(TRIG_PORT, &GPIO_InitStructure); //3õê¼»ˉíaéèGPIO
GPIO_InitStructure.GPIO_Pin = ECHO_0_PIN|ECHO_1_PIN; //PC7£¬1½óECH0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //éèÎaêäèë
GPIO_Init(ECHO_PORT,&GPIO_InitStructure); //3õê¼»ˉGPIO
//GPIOC.7 ÖD¶ÏÏßòÔ¼°ÖD¶Ï3õê¼»ˉÅäÖÃ
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource7);
EXTI_InitStructure.EXTI_Line=EXTI_Line7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //¸ù¾YEXTI_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèEXTI¼Ä′æÆ÷
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line=EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //¸ù¾YEXTI_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèEXTI¼Ä′æÆ÷
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //ê1Äü°′¼üËùÔúμÄía2¿ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÇàÕ¼óÅÏ輶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×óóÅÏ輶2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1Äüía2¿ÖD¶Ïí¨μà
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //ê1Äü°′¼üËùÔúμÄía2¿ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÇàÕ¼óÅÏ輶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×óóÅÏ輶0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1Äüía2¿ÖD¶Ïí¨μà
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
}
void EXTI9_5_IRQHandler(void)
{
delay_us(10); //Ñóê±10us
if(EXTI_GetITStatus(EXTI_Line7) != RESET)
{
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2, ENABLE); //¿aÆôê±Öó
while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_0_PIN)); //μè′yμíμçƽ
TIM_Cmd(TIM2, DISABLE); //¶¨ê±Æ÷2ê§Äü
UltrasonicWave_Distance0=TIM_GetCounter(TIM2)*34/20.0; //¼ÆËã¾ààë
if((UltrasonicWave_Distance0>2)&&(UltrasonicWave_Distance0<20))
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
printf("distance0:%f cm ",UltrasonicWave_Distance0);
}else
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
}
EXTI_ClearITPendingBit(EXTI_Line7); //Çå3yEXTI7Ïß·1òÆeλ
}
}
void EXTI1_IRQHandler(void)
{
delay_us(10); //Ñóê±10us
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2, ENABLE); //¿aÆôê±Öó
while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_1_PIN)); //μè′yμíμçƽ
TIM_Cmd(TIM2, DISABLE); //¶¨ê±Æ÷2ê§Äü
UltrasonicWave_Distance1=TIM_GetCounter(TIM2)*34/20.0; //¼ÆËã¾ààë
if((UltrasonicWave_Distance1>2)&&(UltrasonicWave_Distance1<20))
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
printf("distance1:%f cm ",UltrasonicWave_Distance1);
}else
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
}
EXTI_ClearITPendingBit(EXTI_Line1); //Çå3yEXTI1Ïß·1òÆeλ
}
}
/*
* oˉêyÃû£oUltrasonicWave_StartMeasure
* Ãèêö £o¿aê¼2a¾à£¬·¢Ëíò»¸ö>10usμÄÂö3壬è»oó2aá¿·μ»Øμĸßμçƽ걼ä
* êäèë £oÎT
* êä3ö £oÎT
*/
void UltrasonicWave_StartMeasure_0(void)
{
GPIO_SetBits(TRIG_PORT,TRIG_0_PIN); //Ëí>10USμĸßμçƽ£TRIG_PORT,TRIG_PINÕaὸöÔúdefineÖDóD?
delay_us(20); //Ñóê±20US
GPIO_ResetBits(TRIG_PORT,TRIG_0_PIN);
}
void UltrasonicWave_StartMeasure_1(void)
{
GPIO_SetBits(TRIG_PORT,TRIG_1_PIN); //Ëí>10USμĸßμçƽ£TRIG_PORT,TRIG_PINÕaὸöÔúdefineÖDóD?
delay_us(20); //Ñóê±20US
GPIO_ResetBits(TRIG_PORT,TRIG_1_PIN);
}
[/mw_shl_code]
加载中...
查看其它17个回答
一周热门
更多
>
相关问题
STM32F4上I2C(在PROTEUS中模拟)调试不通的问题
6 个回答
芯片供应紧张,准备换个MCU,MM32L系列替换STM32L系列的怎么样?
7 个回答
STM32同时使用两个串口进行数据收发时数据丢包的问题
5 个回答
STM32F103串口通信死机问题
4 个回答
STM32WLE5CC连接SX1268在LoRa模式下能与 SX1278互通吗?
2 个回答
相关文章
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
×
关闭
采纳回答
向帮助了您的知道网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
STM32
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
×
付费偷看金额在0.1-10元之间
确定
×
关闭
您已邀请
0
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
原子哥我改了两个超声波,单个都能实现,但是两个一起就出问题了[mw_shl_code=c,true]#include "UltrasonicWave.h"
#include "usart.h"
#include "timer.h"
#include "delay.h"
#define TRIG_PORT GPIOC //TRIG
#define ECHO_PORT GPIOC //ECHO
#define TRIG_0_PIN GPIO_Pin_8 //TRIG
#define TRIG_1_PIN GPIO_Pin_2 //TRIG
#define ECHO_0_PIN GPIO_Pin_7 //ECHO
#define ECHO_1_PIN GPIO_Pin_1 //ECHO
float UltrasonicWave_Distance0; //¼ÆËã3öμľààë
float UltrasonicWave_Distance1;
/*
* oˉêyÃû£oUltrasonicWave_Configuration
* Ãèêö £o3¬éù2¨Ä£¿éμÄ3õê¼»ˉ
* êäèë £oÎT
* êä3ö £oÎT
*/
void UltrasonicWave_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //1رÕjtag
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = TRIG_0_PIN|TRIG_1_PIN; //PC8£¬2½óTRIG
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //éèÎaíÆíìêä3öÄ£ê½
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(TRIG_PORT, &GPIO_InitStructure); //3õê¼»ˉíaéèGPIO
GPIO_InitStructure.GPIO_Pin = ECHO_0_PIN|ECHO_1_PIN; //PC7£¬1½óECH0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //éèÎaêäèë
GPIO_Init(ECHO_PORT,&GPIO_InitStructure); //3õê¼»ˉGPIO
//GPIOC.7 ÖD¶ÏÏßòÔ¼°ÖD¶Ï3õê¼»ˉÅäÖÃ
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource7);
EXTI_InitStructure.EXTI_Line=EXTI_Line7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //¸ù¾YEXTI_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèEXTI¼Ä′æÆ÷
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line=EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //¸ù¾YEXTI_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèEXTI¼Ä′æÆ÷
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //ê1Äü°′¼üËùÔúμÄía2¿ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÇàÕ¼óÅÏ輶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×óóÅÏ輶2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1Äüía2¿ÖD¶Ïí¨μà
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //ê1Äü°′¼üËùÔúμÄía2¿ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÇàÕ¼óÅÏ輶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×óóÅÏ輶0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1Äüía2¿ÖD¶Ïí¨μà
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
}
void EXTI9_5_IRQHandler(void)
{
delay_us(10); //Ñóê±10us
if(EXTI_GetITStatus(EXTI_Line7) != RESET)
{
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2, ENABLE); //¿aÆôê±Öó
while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_0_PIN)); //μè′yμíμçƽ
TIM_Cmd(TIM2, DISABLE); //¶¨ê±Æ÷2ê§Äü
UltrasonicWave_Distance0=TIM_GetCounter(TIM2)*34/20.0; //¼ÆËã¾ààë
if((UltrasonicWave_Distance0>2)&&(UltrasonicWave_Distance0<20))
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
printf("distance0:%f cm ",UltrasonicWave_Distance0);
}else
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
}
EXTI_ClearITPendingBit(EXTI_Line7); //Çå3yEXTI7Ïß·1òÆeλ
}
}
void EXTI1_IRQHandler(void)
{
delay_us(10); //Ñóê±10us
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2, ENABLE); //¿aÆôê±Öó
while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_1_PIN)); //μè′yμíμçƽ
TIM_Cmd(TIM2, DISABLE); //¶¨ê±Æ÷2ê§Äü
UltrasonicWave_Distance1=TIM_GetCounter(TIM2)*34/20.0; //¼ÆËã¾ààë
if((UltrasonicWave_Distance1>2)&&(UltrasonicWave_Distance1<20))
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
printf("distance1:%f cm ",UltrasonicWave_Distance1);
}else
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
}
EXTI_ClearITPendingBit(EXTI_Line1); //Çå3yEXTI1Ïß·1òÆeλ
}
}
/*
* oˉêyÃû£oUltrasonicWave_StartMeasure
* Ãèêö £o¿aê¼2a¾à£¬·¢Ëíò»¸ö>10usμÄÂö3壬è»oó2aá¿·μ»Øμĸßμçƽ걼ä
* êäèë £oÎT
* êä3ö £oÎT
*/
void UltrasonicWave_StartMeasure_0(void)
{
GPIO_SetBits(TRIG_PORT,TRIG_0_PIN); //Ëí>10USμĸßμçƽ£TRIG_PORT,TRIG_PINÕaὸöÔúdefineÖDóD?
delay_us(20); //Ñóê±20US
GPIO_ResetBits(TRIG_PORT,TRIG_0_PIN);
}
void UltrasonicWave_StartMeasure_1(void)
{
GPIO_SetBits(TRIG_PORT,TRIG_1_PIN); //Ëí>10USμĸßμçƽ£TRIG_PORT,TRIG_PINÕaὸöÔúdefineÖDóD?
delay_us(20); //Ñóê±20US
GPIO_ResetBits(TRIG_PORT,TRIG_1_PIN);
}
[/mw_shl_code]
一周热门 更多>