stm32指南者+超声波

VCC 供 5V电源,

GND 为地线,

TRIG 触发控制信号输入,

ECHO 回响信号输出。

//ECHO PA4
//TRIG PA5

1.了解超声波的原理

(1)采用 IO 口 TRIG 触发测距,给最少 10us 的高电平信号。

(2)模块自动发送 8 个 40khz 的方波,自动检测是否有信号返回;

(3)有信号返回,通过 IO 口 ECHO 输出一个高电平,高电平持续的时间就是超声波从发射到返回的时间。测试距离=(高电平时间*声速(340M/S))/2;

在这里插入图片描述

int main(void)
{
    
    
	  unsigned int count=0;
	  float distance=0;
	  float distancebuf[]={
    
    0};
		/* USART1 config 115200 8-N-1 */
		USART1_Config();
    Distance_Config();  //测距模块对应的引脚初始化
    CLI() ;//关闭总中断
	  SEI(); //开总中断
	  Tim3_Config();  //定时器的初始化
	
	  
	  GPIO_ResetBits(GPIOA,GPIO_Pin_5);  //先拉低电平
	
	  while(1)
		{
    
    
      //  printf("测距开始\n");
			  GPIO_SetBits(GPIOA,GPIO_Pin_5);  //拉高电平
			  Delay(2000000000);  //延时20个微秒
			//  Delay(20);
			//  Delay(20);
			 GPIO_ResetBits(GPIOA,GPIO_Pin_5); 

       TIM3->CNT=0;  //TIM3的计数器清0

       while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 0); //等待ECHO的高电平
		

       TIM_Cmd(TIM3,ENABLE);  //运行TIM3进行计时
       
			while((GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 1) && (TIM3->CNT < TIM3->ARR-10));
			 
			  TIM_Cmd(TIM3,DISABLE);  
       		
        count=TIM3->CNT;

        printf("count=%d",count);			
			  distance=ChangeDistance(count) -1;
			  printf(" 当前距离为:%f\n",distance);
			  Delay(2000000000); 
				//Delay(50); 
			  //while(1);
				
     }
}

void Delay(unsigned short int time)  //粗略的延时函数(自带的延迟)
{
    
    
    unsigned char i=0;
	  while(time--)
		{
    
    
        i=100;   // i改的越大,延迟越高
			  while(i--);
    }
}

float ChangeDistance(unsigned int cout1)
{
    
    
    float distance=0;
	  printf("cou1=%d\n",cout1);
	  distance=cout1/58.0;
	
	  return distance;
}

usart文件内容

void USART1_Config(void)
{
		GPIO_InitTypeDef GPIO_InitStructure;
		USART_InitTypeDef USART_InitStructure;
		
		/* config USART1 clock */
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
		
		/* USART1 GPIO config */
		/* Configure USART1 Tx (PA.09) as alternate function push-pull */
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
	
		/* Configure USART1 Rx (PA.10) as input floating */
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
			
		/* USART1 mode config */
		USART_InitStructure.USART_BaudRate = 115200;
		USART_InitStructure.USART_WordLength = USART_WordLength_8b;
		USART_InitStructure.USART_StopBits = USART_StopBits_1;
		USART_InitStructure.USART_Parity = USART_Parity_No ;
		USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
		USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
		USART_Init(USART1, &USART_InitStructure); 
		USART_Cmd(USART1, ENABLE);
}

///重定向c库函数printf到USART1
int fputc(int ch, FILE *f)
{
		/* 发送一个字节数据到USART1 */
		USART_SendData(USART1, (uint8_t) ch);
		
		/* 等待发送完毕 */
		while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);		
	
		return (ch);
}

///重定向c库函数scanf到USART1
int fgetc(FILE *f)
{
		/* 等待串口1输入数据 */
		while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

		return (int)USART_ReceiveData(USART1);
}

猜你喜欢

转载自blog.csdn.net/weixin_44868057/article/details/108526809