#include "include.h"
float Current_Height=0;
/*******************************************************************************
*oˉêyÔ-Dí: void Ultrasonic_Init(void)
*1|¡¡¡¡Äü: 3¬éù2¨òy½Å3õê¼»ˉ
*******************************************************************************/
void Ultrasonic_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD ;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_ResetBits(GPIOD, GPIO_Pin_9);
}
/*******************************************************************************
*oˉêyÔ-Dí: void Ultrasonic_Ranging(void)
*1|¡¡¡¡Äü: 3¬éù2¨2a¾à,·μ»Ø¸ßμçƽ걼ä
*******************************************************************************/
void Ultrasonic_Ranging(void)
{
static u8 count=0;
u16 error=0;
static float Last_Height=0;
u16 Ultrasonic_count=0;
if(count==10||count==0)
{
GPIO_SetBits(GPIOD, GPIO_Pin_9);
Delay_us(15);
GPIO_ResetBits(GPIOD, GPIO_Pin_9);
while(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_8)==0)
{
if(error>4000)
{
break;
}
else
{
error++;
}
}
if(error<3500)
{
while(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_8)==1)
{
if(Ultrasonic_count>28950)
{
break;
}
else
{
Ultrasonic_count++;
}
}
}
Current_Height=Ultrasonic_count;
Current_Height=Current_Height/96.5;
if(fabs(Current_Height-Last_Height)<20)
{
Last_Height=Current_Height;
}
count=1;
}
else
{
Current_Height=Last_Height;
count++;
}
}
|