我目前正在使用超声波接近传感器来测量水箱中的水被充满了多少。我首先尝试接收传感器和反射波的对象之间的距离测量。我已经尝试构建自己的代码(使用C语言),但是我不确定我的问题在哪里。我已编写代码以在LCD上显示。我得到的数字是669,A45,h45。下面是我为执行此任务而编写的注释函数。
void distance_sensor(void)
{
DDRC= 0x0F; //enables only one nybble to be an output
double timecnt;
double distance;
int echo;
char distb [16] = {"Container up to:"};
lcd_write(distb, 16, line_1); //displays banner on LCD
echo = (PINC & 0x10); //declares the input echo from the 2nd nybble
distance= 0; //initialization
timecnt = 0;
PORTC = 0x00;
_delay_ms(1.0);
PORTC = 0x0F;
_delay_ms(5.2); //creating a 10 us output square wave and sending it out through port c
PORTC = 0x00;
while(echo==0)
{
echo = (PINC & 0x10); //rechecking the input
timecnt = timecnt + 1; //time taken for the wave to reflect back to the sensor
_delay_us(100.0);
}
timecnt= timecnt*100;
distance = (timecnt*0.000343)/2;
hextodec(distance); //function made to split the value into msd, nsd, lsd and displays on LCD
return;
}
一旦[[TRIG]]输入上出现高电平,超声模块就会在ECHO
输出上将其设置为低电平,并发出一阵声音脉冲。如果有障碍物,声音突发会作为回声反射回去。一旦模块检测到回声,就会在ECHO