网上的程序,在一个超声波模块的情况下可以用,我想把它改成多个超声波,不知怎么改,大神们帮我看看问题出在哪里。[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 TRIG1_PIN GPIO_Pin_8 //TRIG1
#define ECHO1_PIN GPIO_Pin_7 //ECHO1
#define TRIG2_PIN GPIO_Pin_4 //TRIG2
#define ECHO2_PIN GPIO_Pin_3 //ECHO2
#define TRIG3_PIN GPIO_Pin_2 //TRIG3
#define ECHO3_PIN GPIO_Pin_1 //ECHO3
float UltrasonicWave1_Distance; //¼ÆËã3öμľààë
float UltrasonicWave2_Distance; //¼ÆËã3öμľààë
float UltrasonicWave3_Distance; //¼ÆËã3öμľààë
/*
* oˉêyÃû£oUltrasonicWave_Configuration
* Ãèêö £o3¬éù2¨Ä£¿éμÄ3õê¼»ˉ
* êäèë £oÎT
* êä3ö £oÎT
*/
void UltrasonicWave1_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 = TRIG1_PIN; //PC8½ó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 = ECHO1_PIN; //PC7½óECHO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //éèÎaêäèë
GPIO_Init(ECHO_PORT,&GPIO_InitStructure); //3õê¼»ˉGPIOA
//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¼Ä′æÆ÷
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¼Ä′æÆ÷
}
//
void UltrasonicWave2_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 = TRIG2_PIN; //PC8½ó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 = ECHO2_PIN; //PC7½óECHO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //éèÎaêäèë
GPIO_Init(ECHO_PORT,&GPIO_InitStructure); //3õê¼»ˉGPIOA
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource3);
EXTI_InitStructure.EXTI_Line=EXTI_Line3;
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 = EXTI3_IRQn; //ê1Äü°′¼üËùÔúμÄía2¿ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÇàÕ¼óÅÏ輶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×óóÅÏ輶2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1Äüía2¿ÖD¶Ïí¨μà
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
}
//
void UltrasonicWave3_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 = TRIG3_PIN; //PC8½ó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 = ECHO3_PIN; //PC7½óECHO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //éèÎaêäèë
GPIO_Init(ECHO_PORT,&GPIO_InitStructure); //3õê¼»ˉGPIOA
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 = EXTI1_IRQn; //ê1Äü°′¼üËùÔúμÄía2¿ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÇàÕ¼óÅÏ輶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×óóÅÏ輶2
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,ECHO1_PIN)); //μè′yμíμçƽ
TIM_Cmd(TIM2, DISABLE); //¶¨ê±Æ÷2ê§Äü
UltrasonicWave1_Distance=TIM_GetCounter(TIM2)*34/20.0; //¼ÆËã¾ààë&&UltrasonicWave_Distance<150
if((UltrasonicWave1_Distance>2)&&(UltrasonicWave1_Distance<20))
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
printf("distance1:%f cm
",UltrasonicWave1_Distance);
}else
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
}
EXTI_ClearITPendingBit(EXTI_Line7); //Çå3yEXTI7Ïß·1òÆeλ
}
}
//
void EXTI3_IRQHandler(void)
{
delay_us(10); //Ñóê±10us
if(EXTI_GetITStatus(EXTI_Line3) != RESET)
{
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2, ENABLE); //¿aÆôê±Öó
while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO2_PIN)); //μè′yμíμçƽ
TIM_Cmd(TIM2, DISABLE); //¶¨ê±Æ÷2ê§Äü
UltrasonicWave2_Distance=TIM_GetCounter(TIM2)*34/20.0; //¼ÆËã¾ààë&&UltrasonicWave_Distance<150
if((UltrasonicWave2_Distance>2)&&(UltrasonicWave2_Distance<20))
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
printf("distance2:%f cm
",UltrasonicWave2_Distance);
}else
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
}
EXTI_ClearITPendingBit(EXTI_Line3); //Çå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,ECHO3_PIN)); //μè′yμíμçƽ
TIM_Cmd(TIM2, DISABLE); //¶¨ê±Æ÷2ê§Äü
UltrasonicWave3_Distance=TIM_GetCounter(TIM2)*34/20.0; //¼ÆËã¾ààë&&UltrasonicWave_Distance<150
if((UltrasonicWave3_Distance>2)&&(UltrasonicWave3_Distance<20))
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
printf("distance3:%f cm
",UltrasonicWave3_Distance);
}else
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
}
EXTI_ClearITPendingBit(EXTI_Line1); //Çå3yEXTI7Ïß·1òÆeλ
}
}
/*
* oˉêyÃû£oUltrasonicWave_StartMeasure
* Ãèêö £o¿aê¼2a¾à£¬·¢Ëíò»¸ö>10usμÄÂö3壬è»oó2aá¿·μ»Øμĸßμçƽ걼ä
* êäèë £oÎT
* êä3ö £oÎT
*/
void UltrasonicWave1_StartMeasure(void)
{
GPIO_SetBits(TRIG_PORT,TRIG1_PIN); //Ëí>10USμĸßμçƽ£TRIG_PORT,TRIG_PINÕaὸöÔúdefineÖDóD?
delay_us(20); //Ñóê±20US
GPIO_ResetBits(TRIG_PORT,TRIG1_PIN);
}
//
void UltrasonicWave2_StartMeasure(void)
{
GPIO_SetBits(TRIG_PORT,TRIG2_PIN); //Ëí>10USμĸßμçƽ£TRIG_PORT,TRIG_PINÕaὸöÔúdefineÖDóD?
delay_us(20); //Ñóê±20US
GPIO_ResetBits(TRIG_PORT,TRIG2_PIN);
}
//
void UltrasonicWave3_StartMeasure(void)
{
GPIO_SetBits(TRIG_PORT,TRIG3_PIN); //Ëí>10USμĸßμçƽ£TRIG_PORT,TRIG_PINÕaὸöÔúdefineÖDóD?
delay_us(20); //Ñóê±20US
GPIO_ResetBits(TRIG_PORT,TRIG3_PIN);
}
[/mw_shl_code]
————————————————————————————————————————————————————————————————
分割线
————————————————————————————————————————————————————————————————
下面是网上一个超声波的情况:
[mw_shl_code=c,true]#define TRIG_PORT GPIOC //TRIG
#define ECHO_PORT GPIOC //ECHO
#define TRIG_PIN GPIO_Pin_8 //TRIG
#define ECHO_PIN GPIO_Pin_7 //ECHO
float UltrasonicWave_Distance; //¼ÆËã3öμľààë
/*
* 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_PIN; //PC8½ó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_PIN; //PC7½óECH0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //éèÎaêäèë
GPIO_Init(ECHO_PORT,&GPIO_InitStructure); //3õê¼»ˉGPIOA
//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¼Ä′æÆ÷
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¼Ä′æÆ÷
}
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_PIN)); //μè′yμíμçƽ
TIM_Cmd(TIM2, DISABLE); //¶¨ê±Æ÷2ê§Äü
UltrasonicWave_Distance=TIM_GetCounter(TIM2)*5*34/200.0; //¼ÆËã¾ààë&&UltrasonicWave_Distance<150
if(UltrasonicWave_Distance>0)
{
printf("distance:%f cm",UltrasonicWave_Distance);
}
EXTI_ClearITPendingBit(EXTI_Line7); //Çå3yEXTI7Ïß·1òÆeλ
}
}
/*
* oˉêyÃû£oUltrasonicWave_StartMeasure
* Ãèêö £o¿aê¼2a¾à£¬·¢Ëíò»¸ö>10usμÄÂö3壬è»oó2aá¿·μ»Øμĸßμçƽ걼ä
* êäèë £oÎT
* êä3ö £oÎT
*/
void UltrasonicWave_StartMeasure(void)
{
GPIO_SetBits(TRIG_PORT,TRIG_PIN); //Ëí>10USμĸßμçƽ£TRIG_PORT,TRIG_PINÕaὸöÔúdefineÖDóD?
delay_us(20); //Ñóê±20US
GPIO_ResetBits(TRIG_PORT,TRIG_PIN);
}[/mw_shl_code]
你好,给你发站内消息了
一周热门 更多>