在proteus中通过3个超声波检测距离实现对车位剩余数量的检测
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char // 以后unsigned char就可以用uchar代替
- #define uint unsigned int
- uint dis_temp=50;
- uint Margin;
- uint cwA,cwP,cwS;
- sbit LcdRs_P = P2^7; // 1602液晶的RS管脚
- sbit LcdRw_P = P2^6; // 1602液晶的RW管脚
- sbit LcdEn_P = P2^5; // 1602液晶的EN管脚
- sbit Trig_P = P2^2; // 超声波模块的Trig管脚
- sbit Echo_P = P2^3; // 超声波模块的Echo管脚
- sbit Trig_A = P2^0; // 超声波模块的Trig管脚
- sbit Echo_A = P2^1; // 超声波模块的Echo管脚
- sbit Trig_S = P1^6; // 超声波模块的Trig管脚
- sbit Echo_S = P1^7; // 超声波模块的Echo管脚
- /*********************************************************/
- // 毫秒级的延时函数,time是要延时的毫秒数
- /*********************************************************/
- void DelayMs(uint time)
- {
- uint i,j;
- for(i=0;i<time;i++)
- for(j=0;j<112;j++);
- }
- /*********************************************************/
- // 1602液晶写命令函数,cmd就是要写入的命令
- /*********************************************************/
- void LcdWriteCmd(uchar cmd)
- {
- LcdRs_P = 0;
- LcdRw_P = 0;
- LcdEn_P = 0;
- P0=cmd;
- DelayMs(2);
- LcdEn_P = 1;
- DelayMs(2);
- LcdEn_P = 0;
- }
- /*********************************************************/
- // 1602液晶写数据函数,dat就是要写入的数据
- /*********************************************************/
- void LcdWriteData(uchar dat)
- {
- LcdRs_P = 1;
- LcdRw_P = 0;
- LcdEn_P = 0;
- P0=dat;
- DelayMs(2);
- LcdEn_P = 1;
- DelayMs(2);
- LcdEn_P = 0;
- }
- /*********************************************************/
- // 1602液晶初始化函数
- /*********************************************************/
- void LcdInit()
- {
- LcdWriteCmd(0x38); // 16*2显示,5*7点阵,8位数据口
- LcdWriteCmd(0x0C); // 开显示,不显示光标
- LcdWriteCmd(0x06); // 地址加1,当写入数据后光标右移
- LcdWriteCmd(0x01); // 清屏
- }
- /*********************************************************/
- // 液晶光标定位函数
- /*********************************************************/
- void LcdGotoXY(uchar line,uchar column)
- {
- // 第一行
- if(line==0)
- LcdWriteCmd(0x80+column);
- // 第二行
- if(line==1)
- LcdWriteCmd(0x80+0x40+column);
- }
- /*********************************************************/
- // 液晶输出字符串函数
- /*********************************************************/
- void LcdPrintStr(uchar *str)
- {
- while(*str!='\0')
- LcdWriteData(*str++);
- }
- /*********************************************************/
- // 液晶输出数字
- /*********************************************************/
- void LcdPrintNum(uint num)
- {
- LcdWriteData(num/100+0x30); // 百位
- LcdWriteData(num%100/10+0x30); // 十位
- LcdWriteData(num%10+0x30); // 个位
- }
- /*********************************************************/
- // 计算测到的距离
- /*********************************************************/
- uint GetDistanceP(void)
- {
- uint sp; // 用于记录测得的距离
- TH0=0;
- TL0=0;
- Trig_P=1; // 给超声波模块一个开始脉冲
- DelayMs(1);
- Trig_P=0;
- while(!Echo_P); // 等待超声波模块的返回脉冲
- TR0=1; // 启动定时器,开始计时
- while(Echo_P); // 等待超声波模块的返回脉冲结束
- TR0=0; // 停止定时器,停止计时
- sp=((TH0*256+TL0)*0.034)/2; // 距离cm=(时间us * 速度cm/us)/2
- return sp;
- }
- uint GetDistanceS(void)
- {
- uint ss; // 用于记录测得的距离
- TH0=0;
- TL0=0;
- Trig_S=1; // 给超声波模块一个开始脉冲
- DelayMs(1);
- Trig_S=0;
- while(!Echo_S); // 等待超声波模块的返回脉冲
- TR0=1; // 启动定时器,开始计时
- while(Echo_S); // 等待超声波模块的返回脉冲结束
- TR0=0; // 停止定时器,停止计时
- ss=((TH0*256+TL0)*0.034)/2; // 距离cm=(时间us * 速度cm/us)/2
- return ss;
- }
- uint GetDistanceA(void)
- {
- uint sa; // 用于记录测得的距离
- TH0=0;
- TL0=0;
- Trig_A=1; // 给超声波模块一个开始脉冲
- DelayMs(1);
- Trig_A=0;
- while(!Echo_A); // 等待超声波模块的返回脉冲
- TR0=1; // 启动定时器,开始计时
- while(Echo_A); // 等待超声波模块的返回脉冲结束
- TR0=0; // 停止定时器,停止计时
- sa=((TH0*256+TL0)*0.034)/2; // 距离cm=(时间us * 速度cm/us)/2
- return sa;
- }
- void detection(uint dis,uchar buf)
- {
- if(dis>=dis_temp)
- {
- switch(buf)
- {
- case'S':RD = 0;cwS=1;break;
- case'P':WR = 0;cwP=1;break;
- case'A':T1 = 0;cwA=1;break;
-
- }
- }
- if(dis<dis_temp)
- {
- switch(buf)
- {
- case'S':RD = 1;cwS=0;break;
- case'P':WR = 1;cwP=0;break;
- case'A':T1 = 1;cwA=0;break;
-
- }
- }
- Margin=cwS+cwP+cwA;
- LcdGotoXY(1,13); // 定位到第1行第11列
- LcdWriteData(Margin%10+0x30); // 显示车位余量
- }
- /*********************************************************/
- // 主函数
- /*********************************************************/
- void main()
- {
- // uchar dat1,dat2;
- uint distP,distA,distS;
-
- Trig_P=0;
- Trig_A=0;
- Trig_S=0;
-
- LcdInit(); // 执行液晶初始化
- TMOD = 0x01; // 选择定时器0,并且确定是工作方式1(为了超声波模块测量距离计时用的)
- LcdGotoXY(0,0); // 定位到第0行第0列
- LcdPrintStr("S= cm"); // 第0行显示“HC-SR04 System”
- LcdGotoXY(0,9); // 定位到第0行第0列
- LcdPrintStr("P= cm"); // 第0行显示“HC-SR04 System”
- LcdGotoXY(1,0); // 定位到第1行第0列
- LcdPrintStr("A= cm"); // 第1行显示“S= cm”
- LcdGotoXY(1,9); // 定位到第1行第0列
- LcdPrintStr("KY: /3"); // 第1行显示“S= cm”
- while(1)
- {
- distS=GetDistanceS(); // 通过超声波模块获取距离
- LcdGotoXY(0,2); // 定位到第1行第2列
- LcdPrintNum(distS); // 将获取到的距离在液晶上面显示
- detection(distS,'S');
-
- distP=GetDistanceP(); // 通过超声波模块获取距离
- LcdGotoXY(0,11); // 定位到第1行第11列
- LcdPrintNum(distP); // 将获取到的距离在液晶上面显示
- detection(distP,'P');
-
- distA=GetDistanceA();
- LcdGotoXY(1,2); // 定位到第2行第11列
- LcdPrintNum(distA);
- detection(distA,'A');
-
- }
- }
复制代码
所有资料51hei提供下载:
超声波车位检测Y.zip
(82.03 KB, 下载次数: 73)
|