一、仿真实验
1.实验设备:
(一)软件:Arduino IDE、proteus8.9
(二)模块:Arduino Uno(X1)、Hc-SR04超声波测距模块(X1)、液晶显示屏Lm06L(X1)、电阻10KΩ(X3)、滑动变阻器10KΩ(X1)、PCF8574(X1)
(三)实验步骤
1、新建一个文件夹,在此文件夹中新建两个文件夹一个用于放proteus仿真文件,一个用于放arduino代码。
2、在Proteus中找到并放置模块:Arduino Uno、HC-SR04超声波测距模块、液晶显示屏Lm06L、电阻10KΩ、滑动变阻器10KΩ、PCF8574元件。
(1)将HC-SR04超声波测距模块VCC引脚接入+5V电压,ECHO接在Arduino Uno 3号引脚,TR接其2号引脚,GND接地。液晶显示器SCL、SDK接Arduino Uno对应引脚上。
3、arduino中进行代码的编写。
(四)proteus接线图
2.源代码
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const int pingPin = 2; //首先定义引脚的名称,把2号引脚命名为发射引脚
const int echoPin= 3; // 此处增加了接收信号引脚,因为使用的硬件与原来例程中硬件不同
//echo是英语回声的意思,引申为接收回声,即接收信号的引脚的意思。
void setup() {
// initialize serial communication:
Serial.begin(9600); //启动串口通讯
lcd.begin(16,2);
}
void loop() {
// establish variables for duration of the ping, and the distance result
// in inches and centimeters:
long duration, inches, cm; //定义三个长整型变量,long是数据类型,位数要比int长。
// 距离 英寸 厘米
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT); // 设置pingPin引脚,也就是发射引脚为输出
digitalWrite(pingPin, LOW); //先将其设置为低电平
delayMicroseconds(2); //持续两微秒,目的是确保其后的高电平前面无随机噪音
digitalWrite(pingPin, HIGH); //再将发射引脚设置为高电平
delayMicroseconds(15); //持续15微秒
digitalWrite(pingPin, LOW); //再将其设置为低电平,也就是确保了高电平前后都是低电平
// The same pin is used to read the signal from the PING))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the ping
// to the reception of its echo off of an object.
// pinMode(pingPin, INPUT); 原来的语句,硬件的关系改写如下
//duration = pulseIn(pingPin, HIGH);原来的语句
pinMode(echoPin, INPUT); // 现在的语句:设置echo为输入模式,读取信号
duration = pulseIn(echoPin, HIGH);//测量高电平的宽度赋值给变量duration
//pulseIn是一个测量脉冲宽度的函数,这个函数的作用是测量高电平的或低电平的脉冲宽度,并用微秒来表示。原理是传感器接收到返回的
//声波信号后,随后发出一个高电平脉冲,这个脉冲的宽度与传感器从发射到接收到返回信号期间的时间相等(距离数据)。现在这个函数就用
// 来测量这个高电平的宽度,然后再换算成这段时间内声音走行的距离。该函数有两个参数,第一个参数是要测量的引脚号或名称,第二个参数是
//要测量的是高电平还是低电平,如果要测量高电平就写HIGH,低电平就是LOW,那么这个语句的意思就是测量引脚echo的高电平持续的宽度的意思。
// convert the time into a distance
inches = microsecondsToInches(duration);// 利用函数microsecondsToInches()把脉冲宽度换算成英寸
// 等效于下列语句:
//inches = duration/74/2
cm = microsecondsToCentimeters(duration);//利用函数microsecondsToCentimeters()把脉冲宽度换算成厘米
Serial.print(inches); // 在串口打印 inches 的值
Serial.print("in, "); //随后打印字母in
Serial.print(cm); //打印cm的值
Serial.print("cm"); //随后打印字母cm
Serial.println(); //换行
delay(100);
lcd.setCursor(0,0); //确定光标位置
lcd.print("duration"); //打印英语单词距离
lcd.print(":"); //打印冒号
lcd.print(cm); //打印距离的值
lcd.print("cm"); //打印字母cm
delay(1000);
lcd.clear(); //清屏
}
long microsecondsToInches(long microseconds) {
// According to Parallax's datasheet for the PING))), there are 73.746
// microseconds per inch (i.e. sound travels at 1130 feet per second).
// This gives the distance travelled by the ping, outbound and return,
// so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2;
}
3.仿真结果