新手上路
- 积分
- 43
- 金钱
- 43
- 注册时间
- 2015-9-28
- 在线时间
- 4 小时
|
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]
|
|