超聲波測距模塊
單片機源程序如下:
- /*******************************************************************************
- * 日期 : 2015.7.29
- * 實 驗 名 : 1602 顯示 超聲波 返回距離值
- * 實驗說明 : 1.基于22.1184MHz晶振
- * 注 意 : 用本程序掃描得出最大距離為 60 厘米 最小為 2 厘米
- 掃描周期60ms以上 并將 掃描標志位 設置在 u_speed()函數 后面
- -----------------------------------------------------------------------------------
- * 修改內容 :
- *******************************************************************************/
- #include <reg52.h>
- #include "Ultrasonic.h"
- #include "1602.h"
- #define LED P0
- #define LED_SEL P2
- static char flag = 0;
- unsigned char code LED_num []={ //共陰
- 0x3f,0x06,0x5b,0x4f,0x66,
- 0x6d,0x7d,0x07,0x7f,0x6f} ;
- unsigned char code place[]={0x00,0x01,0x02,0x03} ;
- void t_init(); //中斷初始化
- int main()
- {
- unsigned int re_1 = 0, re_2 = 0;
- unsigned char time = 0;
- unsigned char index = 0;
- unsigned int length = 0;
- t_init();
- // Lcd1602_Int ();
- // writecom(0x80 + 3);
- // writedata('0');
- // writedata('0');
- // writedata('0');
- // writedata('0');
- // writedata(' ');
- // writedata('M');
- // writedata('M');
-
- while(1)
- {
- if(flag == 1) //80ms 為一次超聲波掃描周期
- {
- time++;
- flag = 0;
-
- // writecom(0x80 + 3);
- // writedata('0' + re_1%10000/1000);
- // writedata('0' + re_1%1000/100);
- // writedata('0' + re_1%100/10);
- // writedata('0' + re_1%10/1);
-
-
- switch(index)
- {
- case 0:LED_SEL = place[0] ; LED = LED_num[length%10000/1000];break;
- case 1:LED_SEL = place[1] ; LED = LED_num[length%1000/100];break;
- case 2:LED_SEL = place[2] ; LED = LED_num[length%100/10];break;
- case 3:LED_SEL = place[3] ; LED = LED_num[length%10/1];break;
- default : ;
- }
- if(index<3)
- {
- index++;
- }
- else
- {
- index = 0;
- }
-
- }
-
- if(time == 7)
- {
- time = 0;
- TR1 = 0;
- //length = u_range(); //掃描標志位要放在返回值后面, u_range()函數 執行有點長 影響測量周期
- re_1 = length;
- TR1 = 1;
- }
- }
- return 0;
- }
- void t_init()
- {
- TMOD = 0X11;
-
- TH1 = 0x0B8; //定時10ms
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
超聲波測距.rar
(54.72 KB, 下載次數: 6)
2018-4-15 14:39 上傳
點擊文件名下載附件
|