|
在proteus中通過(guò)3個(gè)超聲波檢測(cè)距離實(shí)現(xiàn)對(duì)車位剩余數(shù)量的檢測(cè)
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
ETEHGRHT4(Z{]`C[D)HS%(U.png (41.47 KB, 下載次數(shù): 44)
下載附件
2020-3-1 16:23 上傳
單片機(jī)源程序如下:
- #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管腳
- /*********************************************************/
- // 毫秒級(jí)的延時(shí)函數(shù),time是要延時(shí)的毫秒數(shù)
- /*********************************************************/
- void DelayMs(uint time)
- {
- uint i,j;
- for(i=0;i<time;i++)
- for(j=0;j<112;j++);
- }
- /*********************************************************/
- // 1602液晶寫命令函數(shù),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液晶寫數(shù)據(jù)函數(shù),dat就是要寫入的數(shù)據(jù)
- /*********************************************************/
- 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液晶初始化函數(shù)
- /*********************************************************/
- void LcdInit()
- {
- LcdWriteCmd(0x38); // 16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)口
- LcdWriteCmd(0x0C); // 開(kāi)顯示,不顯示光標(biāo)
- LcdWriteCmd(0x06); // 地址加1,當(dāng)寫入數(shù)據(jù)后光標(biāo)右移
- LcdWriteCmd(0x01); // 清屏
- }
- /*********************************************************/
- // 液晶光標(biāo)定位函數(shù)
- /*********************************************************/
- void LcdGotoXY(uchar line,uchar column)
- {
- // 第一行
- if(line==0)
- LcdWriteCmd(0x80+column);
- // 第二行
- if(line==1)
- LcdWriteCmd(0x80+0x40+column);
- }
- /*********************************************************/
- // 液晶輸出字符串函數(shù)
- /*********************************************************/
- void LcdPrintStr(uchar *str)
- {
- while(*str!='\0')
- LcdWriteData(*str++);
- }
- /*********************************************************/
- // 液晶輸出數(shù)字
- /*********************************************************/
- void LcdPrintNum(uint num)
- {
- LcdWriteData(num/100+0x30); // 百位
- LcdWriteData(num%100/10+0x30); // 十位
- LcdWriteData(num%10+0x30); // 個(gè)位
- }
- /*********************************************************/
- // 計(jì)算測(cè)到的距離
- /*********************************************************/
- uint GetDistanceP(void)
- {
- uint sp; // 用于記錄測(cè)得的距離
- TH0=0;
- TL0=0;
- Trig_P=1; // 給超聲波模塊一個(gè)開(kāi)始脈沖
- DelayMs(1);
- Trig_P=0;
- while(!Echo_P); // 等待超聲波模塊的返回脈沖
- TR0=1; // 啟動(dòng)定時(shí)器,開(kāi)始計(jì)時(shí)
- while(Echo_P); // 等待超聲波模塊的返回脈沖結(jié)束
- TR0=0; // 停止定時(shí)器,停止計(jì)時(shí)
- sp=((TH0*256+TL0)*0.034)/2; // 距離cm=(時(shí)間us * 速度cm/us)/2
- return sp;
- }
- uint GetDistanceS(void)
- {
- uint ss; // 用于記錄測(cè)得的距離
- TH0=0;
- TL0=0;
- Trig_S=1; // 給超聲波模塊一個(gè)開(kāi)始脈沖
- DelayMs(1);
- Trig_S=0;
- while(!Echo_S); // 等待超聲波模塊的返回脈沖
- TR0=1; // 啟動(dòng)定時(shí)器,開(kāi)始計(jì)時(shí)
- while(Echo_S); // 等待超聲波模塊的返回脈沖結(jié)束
- TR0=0; // 停止定時(shí)器,停止計(jì)時(shí)
- ss=((TH0*256+TL0)*0.034)/2; // 距離cm=(時(shí)間us * 速度cm/us)/2
- return ss;
- }
- uint GetDistanceA(void)
- {
- uint sa; // 用于記錄測(cè)得的距離
- TH0=0;
- TL0=0;
- Trig_A=1; // 給超聲波模塊一個(gè)開(kāi)始脈沖
- DelayMs(1);
- Trig_A=0;
- while(!Echo_A); // 等待超聲波模塊的返回脈沖
- TR0=1; // 啟動(dòng)定時(shí)器,開(kāi)始計(jì)時(shí)
- while(Echo_A); // 等待超聲波模塊的返回脈沖結(jié)束
- TR0=0; // 停止定時(shí)器,停止計(jì)時(shí)
- sa=((TH0*256+TL0)*0.034)/2; // 距離cm=(時(shí)間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); // 顯示車位余量
- }
- /*********************************************************/
- // 主函數(shù)
- /*********************************************************/
- void main()
- {
- // uchar dat1,dat2;
- uint distP,distA,distS;
-
- Trig_P=0;
- Trig_A=0;
- Trig_S=0;
-
- LcdInit(); // 執(zhí)行液晶初始化
- TMOD = 0x01; // 選擇定時(shí)器0,并且確定是工作方式1(為了超聲波模塊測(cè)量距離計(jì)時(shí)用的)
- 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(); // 通過(guò)超聲波模塊獲取距離
- LcdGotoXY(0,2); // 定位到第1行第2列
- LcdPrintNum(distS); // 將獲取到的距離在液晶上面顯示
- detection(distS,'S');
-
- distP=GetDistanceP(); // 通過(guò)超聲波模塊獲取距離
- LcdGotoXY(0,11); // 定位到第1行第11列
- LcdPrintNum(distP); // 將獲取到的距離在液晶上面顯示
- detection(distP,'P');
-
- distA=GetDistanceA();
- LcdGotoXY(1,2); // 定位到第2行第11列
- LcdPrintNum(distA);
- detection(distA,'A');
-
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
超聲波車位檢測(cè)Y.zip
(82.03 KB, 下載次數(shù): 73)
2020-3-1 16:24 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|