OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2875|回复: 2

超声波测距 怎样改写为两个模块

[复制链接]

3

主题

6

帖子

0

精华

新手上路

积分
43
金钱
43
注册时间
2015-9-28
在线时间
4 小时
发表于 2016-5-13 01:18:10 | 显示全部楼层 |阅读模式
1金钱
现在是只能连接一个
应该怎样修改才能实现多个超声波传感器的同时控制
小白请教.[mw_shl_code=applescript,true]#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "usart2.h"
#include "TIM5_INT.h"
#include "stm32f10x_it.h"
#include "NVIC_CONFIG.h"
#include "math.h"
#include "dog.h"




int main()
{
           u16 time_ms100=0,time_ms150=0,time_ms200=0,time_ms250=0,time_ms300=0,time_ms400=0;
           int temp;
       
                delay_init();
          Init_Tim5_Int();
                Init_Nvic();
                uart_init(115200);
                uart3_init(9600);
                IWDG_Init(4,625);           //óë·ÖÆμêyÎa64,ÖØÔØÖμÎa625,òç3öê±¼äÎa1s          
            Ctr_Tim5(1);

        while(1)
        {

               
                if(time_flag==1)
                {
                        time_ms100++;
                        time_flag=0;
                       
                        if(time_ms100==100)
                        {
                                time_ms100=0;
                                IWDG_Feed();        //Î11·
                                USART_SendData(USART3, 0xe8);
                                while((USART3->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
                                delay_us(40);
                                USART_SendData(USART3, 0x02);
                                while((USART3->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
                                delay_us(40);
                                USART_SendData(USART3, 0xbc);
                                while((USART3->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
                                delay_us(200);
                                printf("%d\n",distance);

               
                       
                        }
                       
                       

                       
                }



        }       
       
}


[/mw_shl_code]
[mw_shl_code=applescript,true]#include "sys.h"
#include "usart2.h"
#include "usart.h"
#include "stm32f10x_rcc.h"

//′®¿ú3ÖD¶Ï·tÎñ3ìDò
//×¢òa,¶áè¡USARTx->SRÄü±üÃaÄaÃûÆäÃîμÄ′íÎó          
u8 USART3_RX_BUF[USART3_REC_LEN];     //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
//½óêÕ×′ì¬
//bit15£¬        ½óêÕíê3é±êÖ¾
//bit14£¬        ½óêÕμ½0x0d
//bit13~0£¬        ½óêÕμ½μÄóDD§×Ö½úêyÄ¿
u16 USART3_RX_STA=0;       //½óêÕ×′쬱ê¼Ç       


//3õê¼»ˉIO ′®¿ú1
//bound:2¨ìØÂê
void uart3_init(u32 bound){
    //GPIO¶Ë¿úéèÖÃ
    GPIO_InitTypeDef GPIO_InitStructure;
        USART_ClockInitTypeDef USART_ClockInitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
         
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);        //ê1ÄüUSART1£¬GPIOAê±Öó
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
        USART_DeInit(USART3);  //¸′λ′®¿ú1
         //USART3_TX   PB.10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;        //¸′óÃíÆíìêä3ö
    GPIO_Init(GPIOB, &GPIO_InitStructure);
   
    //USART3_RX          PB.11
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
    GPIO_Init(GPIOB, &GPIO_InitStructure);  //3õê¼»ˉPB.11

   //Usart3 NVIC ÅäÖÃ

    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇàÕ¼óÅÏè¼¶3
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;                //×óóÅÏè¼¶3
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQí¨μàê1Äü
        NVIC_Init(&NVIC_InitStructure);        //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
  
   //USART 3õê¼»ˉéèÖÃ

        USART_InitStructure.USART_BaudRate = bound;//ò»°ãéèÖÃÎa9600;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
        USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
        USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæÅ¼D£Ñéλ
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;        //êÕ·¢Ä£ê½
       
         USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
         USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
         USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
         USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

    USART_Init(USART3, &USART_InitStructure); //3õê¼»ˉ′®¿ú
        USART_ClockInit(USART3, &USART_ClockInitStructure);
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//¿aÆôÖD¶Ï
    USART_Cmd(USART3, ENABLE);                    //ê1Äü′®¿ú

}


[/mw_shl_code]

超声波ks103-stm32串口代码.zip

5.91 MB, 下载次数: 62

代码

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

12

主题

336

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1576
金钱
1576
注册时间
2015-8-9
在线时间
625 小时
发表于 2016-5-13 09:00:04 | 显示全部楼层
回复

使用道具 举报

9

主题

92

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1134
金钱
1134
注册时间
2014-7-25
在线时间
176 小时
发表于 2016-5-13 09:11:00 | 显示全部楼层
如果不想浪费外设资源,可以通过硬件电路的选通来实现
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-7 22:35

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表