超聲波測距仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
1.png (94.61 KB, 下載次數: 92)
下載附件
仿真圖
2021-7-22 23:38 上傳
51hei.gif (170.22 KB, 下載次數: 88)
下載附件
2021-7-23 00:11 上傳
單片機源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char // 以后unsigned char就可以用uchar代替
- #define uint unsigned int // 以后unsigned int 就可以用uint 代替
- sfr ISP_DATA = 0xe2; // 數據寄存器
- sfr ISP_ADDRH = 0xe3; // 地址寄存器高八位
- sfr ISP_ADDRL = 0xe4; // 地址寄存器低八位
- sfr ISP_CMD = 0xe5; // 命令寄存器
- sfr ISP_TRIG = 0xe6; // 命令觸發寄存器
- sfr ISP_CONTR = 0xe7; // 命令寄存器
- 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 KeySet_P = P3^2; // “設置”按鍵的管腳
- sbit KeyDown_P = P3^3; // “減”按鍵的管腳
- sbit KeyUp_P = P3^4; // “加”按鍵的管腳
- sbit Buzzer_P = P2^1; // 蜂鳴器的管腳
- sbit Led_P = P2^0; // LED報警燈的管腳
- sbit DQ = P1^0; // 溫度傳感器的引腳定義
- uint gAlarm; // 報警距離變量
- float gSpeed; // 保存超聲波的速度值
- /*********************************************************/
- // 擦除單片機內部EEPROM的一個扇區
- // 寫8個扇區中隨便一個的地址,便擦除該扇區,寫入前要先擦除
- /*********************************************************/
- void Sector_Erase(unsigned int add)
- {
- ISP_CONTR = 0x83;
- ISP_CMD = 0x03;
- ISP_ADDRH = (unsigned char)(add>>8);
- ISP_ADDRL = (unsigned char)(add&0xff);
- ISP_TRIG = 0x46;
- ISP_TRIG = 0xB9;
- _nop_();
- ISP_Disable();
- }
- /*********************************************************/
- // 毫秒級的延時函數,time是要延時的毫秒數
- /*********************************************************/
- void DelayMs(uint time)
- {
- uint i,j;
- for(i=0;i<time;i++)
- for(j=0;j<112;j++);
- }
- /*********************************************************/
- // 讀取溫度值
- /*********************************************************/
- int DS18B20_ReadTemp(void)
- {
- uchar j;
- int b,temp=0;
- DS18B20_ReSet(); // 產生復位脈
- DS18B20_WriteByte(0xcc); // 忽略ROM指令
- DS18B20_WriteByte(0x44); // 啟動溫度轉換指令
- DS18B20_ReSet(); // 產生復位脈
- DS18B20_WriteByte(0xcc); // 忽略ROM指令
- DS18B20_WriteByte(0xbe); // 讀取溫度指令
- for(j=0;j<16;j++) // 讀取溫度數量
- {
- DQ=0;
- _nop_();
- _nop_();
- DQ=1;
- Delay15us();
- b=DQ;
- Delay15us();
- Delay15us();
- Delay15us();
- b=b<<j;
- temp=temp|b;
- }
-
- temp=temp*0.0625*10; // 合成溫度值并放大10倍
- return (temp); // 返回檢測到的溫度值
- }
- /*********************************************************/
- // 計算測到的距離
- /*********************************************************/
- uint GetDistance(void)
- {
- uint ss; // 用于記錄測得的距離
- TH0=0;
- TL0=0;
- Trig_P=1; // 給超聲波模塊一個開始脈沖
- DelayMs(1);
- Trig_P=0;
- while(!Echo_P); // 等待超聲波模塊的返回脈沖
- TR0=1; // 啟動定時器,開始計時
- while(Echo_P); // 等待超聲波模塊的返回脈沖結束
- TR0=0; // 停止定時器,停止計時
- ss=((TH0*256+TL0)*gSpeed)/2; // 距離cm=(時間us * 速度cm/us)/2
- if(ss>999) // 把檢測結果限制999厘米內
- ss=999;
-
- return ss;
- }
- /*********************************************************/
- // 按鍵掃描
- /*********************************************************/
- void KeyScanf()
- {
- uchar i;
- uchar dat1,dat2;
- if(KeySet_P==0) // 判斷是否有按鍵按下
- {
- LcdGotoXY(1,0); // 液晶第二行刷新顯示
- LcdPrintStr(" alarm= cm ");
- LcdGotoXY(1,8); // 顯示當前的報警值
- LcdPrintNum(gAlarm);
-
- DelayMs(10); // 消除按鍵按下的抖動
- while(!KeySet_P); // 等待按鍵釋放
- DelayMs(10); // 消除按鍵松開的抖動
- i=1;
- while(i)
- {
- if(KeyDown_P==0) // 報警值減的處理
- {
- if(gAlarm>2)
- gAlarm--;
- LcdGotoXY(1,8);
- LcdPrintNum(gAlarm);
- DelayMs(300);
- }
- if(KeyUp_P==0) // 報警值加的處理
- {
- if(gAlarm<400)
- gAlarm++;
- LcdGotoXY(1,8);
- LcdPrintNum(gAlarm);
- DelayMs(300);
- }
-
- if(KeySet_P==0) // 再次按下設置鍵的判斷
- {
- LcdGotoXY(1,0); // 液晶恢復測量時的內容顯示
- LcdPrintStr(" dist= cm ");
- DelayMs(10); // 消除按鍵按下的抖動
- while(!KeySet_P); // 等待按鍵釋放
- DelayMs(10); // 消除按鍵松開的抖動
- i=0;
- }
- }
-
- dat1=gAlarm/100;
- dat2=gAlarm%100;
- Sector_Erase(0x2000);
- EEPROM_Write(0x2000,dat1);
- EEPROM_Write(0x2001,dat2);
- }
- }
- /*********************************************************/
- // 報警判斷
- /*********************************************************/
- void AlarmJudge(uint ss)
- {
- uchar i;
- float alr1,alr2,alr3,alr4;
- alr1=gAlarm/4.00*1;
- alr2=gAlarm/4.00*2;
- alr3=gAlarm/4.00*3;
- alr4=gAlarm/4.00*4;
- // 報警頻率最快
- if(ss<alr1)
- {
- for(i=0;i<10;i++)
- {
- Led_P=0;
- Buzzer_P=0;
- DelayMs(50);
- Led_P=1;
- Buzzer_P=1;
- DelayMs(50);
- KeyScanf();
- }
- }
- // 報警頻率第二快
- else if(ss<alr2)
- {
- for(i=0;i<5;i++)
- {
- Led_P=0;
- Buzzer_P=0;
- DelayMs(100);
- Led_P=1;
- Buzzer_P=1;
- DelayMs(100);
- KeyScanf();
- }
- }
- // 報警頻率第三快
- else if(ss<alr3)
- {
- for(i=0;i<2;i++)
- {
- Led_P=0;
- Buzzer_P=0;
- DelayMs(200);
- Led_P=1;
- Buzzer_P=1;
- DelayMs(200);
- KeyScanf();
- }
- }
- // 報警頻率最慢
- else if(ss<alr4)
- {
- for(i=0;i<2;i++)
- {
- Led_P=0;
- Buzzer_P=0;
- DelayMs(300);
- Led_P=1;
- Buzzer_P=1;
- DelayMs(300);
- KeyScanf();
- }
- }
- // 不報警
- else
- {
- Led_P=1;
- Buzzer_P=1;
- for(i=0;i<100;i++)
- {
- KeyScanf();
- DelayMs(10);
- }
- }
- }
- /*********************************************************/
- // 主函數
- /*********************************************************/
- void main()
- {
- uchar dat1,dat2;
- uint dist; // 保存超聲波模塊測量到的結果
- int temp; // 保存溫度傳感器測量到的結果
-
- Trig_P=0;
-
- LcdInit(); // 執行液晶初始化
- TMOD = 0x01; // 選擇定時器0,并且確定是工作方式1(為了超聲波模塊測量距離計時用的)
-
- LcdGotoXY(0,0); // 定位到第0行第0列
- LcdPrintStr(" temp= "); // 第0行顯示“ temp= ”
- LcdGotoXY(1,0); // 定位到第1行第0列
- LcdPrintStr(" dist= cm "); // 第1行顯示“ dist= cm ”
- while(DS18B20_ReadTemp()==850) // 等待溫度傳感器初始化完成
- {
- DelayMs(10);
- }
-
- dat1=EEPROM_Read(0x2000); // 從EEPROM讀取報警值
- dat2=EEPROM_Read(0x2001);
- gAlarm=dat1*100+dat2;
- if((gAlarm==0)||(gAlarm>400)) // 如果讀取到的報警值異常(等于0或大于400則認為異常)
- {
- gAlarm=25; // 重新賦值報警值為25
- }
- while(1)
- {
- temp=DS18B20_ReadTemp(); // 獲取溫度傳感器的溫度值
- LcdGotoXY(0,7); // 定位到第0行第7列
- LcdPrintTemp(temp); // 顯示當前的溫度值
-
- gSpeed=0.607*(temp/10)+331.4; // 根據公式 v=0.607T+331.4 計算出當前溫度值對應的超聲波速度,這時的單位是“米/秒”
- gSpeed=gSpeed/10000; // 將超聲波的速度從單位“m/s”轉為“cm/us”,方便后面的計算
-
- dist=GetDistance(); // 通過超聲波模塊獲取距離
- LcdGotoXY(1,7); // 光標定位
- LcdPrintNum(dist); // 將獲取到的距離在液晶上面顯示
- AlarmJudge(dist); // 判斷一下是否需要報警,是的話則報警
- }
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
仿真代碼51hei附件下載:
超聲波測距+溫度補償 proteus仿真.zip
(144.92 KB, 下載次數: 205)
2021-7-22 23:42 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|