在proteus中通過3個超聲波檢測距離實現對車位剩余數量的檢測
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
ETEHGRHT4(Z{]`C[D)HS%(U.png (41.47 KB, 下載次數: 44)
下載附件
2020-3-1 16:23 上傳
單片機源程序如下:
- #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)
2020-3-1 16:24 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|