|
0.png (42.15 KB, 下載次數(shù): 31)
下載附件
2018-5-14 02:55 上傳
超聲波測(cè)距IC模塊電路原理圖如下:
0.png (48 KB, 下載次數(shù): 18)
下載附件
2018-5-14 02:54 上傳
單片機(jī)源程序如下:
- /*
- * 超聲波測(cè)距模塊(CX20106+4069版本)
- *
- * 用途:超聲波測(cè)距模塊測(cè)試程序
- *
- * 作者 日期 備注
- * Huafeng Lin 20011/01/05 新增
- * Huafeng Lin 20011/01/05 修改
- *
- */
- #include<reg51.h>
- #include<INTRINS.H>
- #define uchar unsigned char
- #define uint unsigned int
- #define nop _nop_()
- #define LCM_Data P2 //數(shù)據(jù)接口
- #define Busy 0x80 //用于檢測(cè)LCM狀態(tài)字中的Busy標(biāo)識(shí)
- uint dis;
- uchar flag=0,high_time,low_time;
- sbit in=P3^3; //外部中斷1,接CX20106的7腳
- sbit csb=P1^0; //40KHz方波輸出腳
- sbit LCM_RW = P0^1; //讀寫(xiě)控制輸入端,LCD1602的第五腳
- sbit LCM_RS = P0^2; //寄存器選擇輸入端,LCD1602的第四腳
- sbit LCM_E = P0^0; //使能信號(hào)輸入端,LCD1602的第6腳
- void WriteDataLCM (uchar WDLCM); //LCD模塊寫(xiě)數(shù)據(jù)
- void WriteCommandLCM (uchar WCLCM,BuysC); //LCD模塊寫(xiě)指令
- uchar ReadDataLCM (void); //LCD模塊讀數(shù)據(jù)
- uchar ReadStatusLCM (void); //讀LCD模塊的忙標(biāo)
- void DisplayOneChar (uchar X,uchar Y,uchar ASCII); //在第X+1行的第Y+1位置顯示一個(gè)字符
- void DisplayListChar (uchar X,uchar Y,uchar delayms,uchar code *DData);
- void DisplayCursorPos (uchar X, uchar Y);
- void LCMInit (void);
- void DisplayIntData (uchar X, uchar Y,int ZhengShu,uchar Digit,uchar XiaoShu);
- void DisplayCharData (uchar X, uchar Y,uchar ZiFu);
- void DelayUs(uint us);
- void DelayMs(uint Ms);
- void init(void); //初始化
- void delay_nms(uint n); //延時(shí)nms
- void display(uint dat); //顯示函數(shù)
- void tran(void); //發(fā)射超聲波
- void delay100us(); //延時(shí)100us
- /**********************************
- 函數(shù)名稱:初始化函數(shù)
- 修改日期:
- 入口參數(shù):無(wú)
- 返回值: 無(wú)
- **********************************/
- void init(void)
- {
- TMOD=0x01;//定時(shí)器0方式1用于計(jì)時(shí),定時(shí)器1用于產(chǎn)生38K方波
- TH0=0;
- TL0=0; /* 設(shè)定T0的工作模式為2*/
- EA=1;
- IT1=1;//下降沿有效,左傳感器
- }
- /**********************************
- 函數(shù)名稱:延時(shí)函數(shù)
- 修改日期:
- 入口參數(shù):n
- 返回值: 無(wú)
- **********************************/
- void delay_nms(uint n)
- {
- uchar i;
- while(n--)
- {
- for(i = 0; i < 123; i++);
- } ;
- }
- /************************************************
- 延時(shí)100us函數(shù)
- ***********************************************/
- void delay100us()
- {
- uchar j;
- for(j=50;j>0;j--);
- }
- /**********************************
- 函數(shù)名稱:超聲波發(fā)射函數(shù)
- 修改日期:
- 入口參數(shù):無(wú)
- 返回值: 無(wú)
- **********************************/
- void tran(void)
- {
- uchar i;
- float temp;
- TH0=0;
- TL0=0;//清定時(shí)0
- TR0=1;//開(kāi)定時(shí)0
- for(i=8;i>0;i--)
- {
- csb=!csb;
- //nop;
- //nop;
- nop;
- nop;
- nop;
- nop;
- nop;
- nop;
- nop;
- nop;
- }
- csb=1;
- delay_nms(1);//延時(shí)1ms左右后再開(kāi)中斷,避免直接回來(lái)的回波
- EX1=1;
- delay_nms(50);
- if(flag==1)
- {
- temp=high_time*256+low_time;
- temp=(temp/1000)/2;
- temp*=344;
- temp=temp/10;
- //if(temp>10)
- dis=(unsigned int)temp;
- flag=0;
- }
- //else dis=0;
- }
- /**********************************
- 函數(shù)名稱:中斷函數(shù)
- 修改日期:
- 入口參數(shù):無(wú)
- 返回值: 無(wú)
- **********************************/
- void TT() interrupt 2
- {
- uint tmp;
- TR0=0;//關(guān)定時(shí)器0
- ET1=0;//關(guān)外部中斷
- //flag=1;//外部中斷標(biāo)志位
- tmp=TH0*256+TL0;
- if((tmp>0)&&(tmp<60000))
- {
- high_time=TH0;
- low_time=TL0;
- flag=1;
- }
- else
- {
- high_time=0;
- low_time=0;
- }
- }
- /*===========================================================================
- 主程序
- =============================================================================*/
- void main(void)
- {
- uchar i;
- uint tp=0;
- init();
- LCMInit();
- DisplayOneChar(1,10,'M');
- DisplayListChar(0,0,0,"LCSOFT (C) 2011");
- while(1)
- {
- tran();
- for(i=100;i>0;i--)
- {
- DisplayIntData(1,9,dis,4,2);
- }
- }
- }
- /*====================================================================
- 功 能: 在1602顯示一個(gè)整數(shù)數(shù)據(jù)
- 說(shuō) 明: 顯示一個(gè)整數(shù)數(shù)據(jù)-9999->32625. 從右至左顯示數(shù)據(jù)5位:
- ======================================================================*/
- void DisplayIntData(uchar X, uchar Y,int ZhengShu,uchar Digit,uchar XiaoShu)
- {
- uchar i=0,k=0, BCD[5]={0};
- if(Digit>5) Digit=5;
- if(ZhengShu<0)
- {
- k=1;//負(fù)數(shù)示志位
- ZhengShu=-ZhengShu;
- }
- BCD[4] = ZhengShu / 10000; //求出萬(wàn)位數(shù)據(jù)
- ZhengShu = ZhengShu % 10000;
- BCD[3] = ZhengShu / 1000; //求出千位數(shù)據(jù)
- ZhengShu = ZhengShu % 1000;
- BCD[2] = ZhengShu / 100; //求出百位數(shù)據(jù)
- ZhengShu = ZhengShu % 100;
- BCD[1] = ZhengShu / 10; //求出十位數(shù)據(jù)
- BCD[0] = ZhengShu % 10; //求出個(gè)位數(shù)據(jù)
-
- for(i=0;i<Digit;i++)//輸出顯示的數(shù)值
- {
- if((i==XiaoShu)&&(0!=XiaoShu))
- {
- DisplayOneChar(X,Y-i,'.');//輸出小數(shù)點(diǎn)
- Y= Y-1;
- }
-
- DisplayOneChar(X,Y-i,BCD[i]+0x30); //顯示一個(gè)字符
- }
- if(k==1)
- DisplayOneChar(X,Y-1,'-');//輸出負(fù)符
- }
- /*====================================================================
- 功 能:在1602顯示一個(gè)字符數(shù)據(jù)
- 說(shuō) 明:顯示一個(gè)字符數(shù)據(jù)0~256. 從左至右顯示數(shù)據(jù)3位
- ======================================================================*/
- void DisplayCharData(uchar X, uchar Y,uchar ZiFu)
- {
- uchar i=0;
- uchar ValueBCD[3];
- ValueBCD[0] = ZiFu / 100; //求出百位數(shù)據(jù)
- ZiFu = ZiFu % 100;
- ValueBCD[1] = ZiFu / 10; //求出十位數(shù)據(jù)
- ValueBCD[2] = ZiFu % 10; //求出個(gè)位數(shù)據(jù)
-
- for(i=0;i<3;i++)//輸出顯示的數(shù)值
- {
- DisplayOneChar(X,Y+i,ValueBCD[i]+0x30); //顯示一個(gè)字符
- }
- }
- /*======================================================================
- LCM初始化
- ======================================================================*/
- void LCMInit(void)
- {
- LCM_Data = 0;
- WriteCommandLCM(0x38,0); //三次顯示模式設(shè)置,不檢測(cè)忙信號(hào)
- DelayMs(5);
- WriteCommandLCM(0x38,0);
- DelayMs(5);
- WriteCommandLCM(0x38,0);
- DelayMs(5);
- WriteCommandLCM(0x38,1); //顯示模式設(shè)置,開(kāi)始要求每次檢測(cè)忙信號(hào)
- WriteCommandLCM(0x08,1); //關(guān)閉顯示
- WriteCommandLCM(0x01,1); //顯示清屏
- WriteCommandLCM(0x06,1); // 顯示光標(biāo)移動(dòng)設(shè)置
- WriteCommandLCM(0x0C,1); // 顯示開(kāi)及光標(biāo)設(shè)置
-
- DelayMs(100);
- }
- /*====================================================================
- 顯示光標(biāo)的位置
- ====================================================================*/
- void DisplayCursorPos( unsigned char X, unsigned char Y)
- {
- X &= 0x1;
- Y &= 0xF; //限制Y不能大于15,X不能大于1
- if (X) Y |= 0x40; //當(dāng)要顯示第二行時(shí)地址碼+0x40;
- Y |= 0x80; // 算出指令碼
- WriteCommandLCM(Y, 1); //這里不檢測(cè)忙信號(hào),發(fā)送地址碼
- }
- /*====================================================================
- 按指定位置顯示一串字符:第 X 行,第 y列
- 注意:字符串不能長(zhǎng)于16個(gè)字符
- ======================================================================*/
- void DisplayListChar(uchar X,uchar Y,uchar delayms, uchar code *DData)
- {
- unsigned char ListLength;
- ListLength = 0;
- X &= 0x1;
- Y &= 0xF; //限制X不能大于15,Y不能大于1
- while (DData[ListLength]!='\0') //若到達(dá)字串尾則退出
- {
- if (Y <= 0xF) //X坐標(biāo)應(yīng)小于0xF
- {
- DisplayOneChar(X, Y, DData[ListLength]); //顯示單個(gè)字符
- ListLength++;
- Y++;
- DelayMs(delayms);//延時(shí)顯示字符串
- }
- else
- break;//跳出循環(huán)體
- }
- }
- /*====================================================================
- 設(shè)定延時(shí)時(shí)間:x*1us
- ====================================================================*/
- void DelayUs(uint us)
- {
- while(us--);
- }
- /*====================================================================
- 設(shè)定延時(shí)時(shí)間:x*1ms
- ====================================================================*/
- void DelayMs(uint Ms)
- {
- uint i,TempCyc;
- for(i=0;i<Ms;i++)
- {
- TempCyc = 250;
- while(TempCyc--);
- }
- }
- /*=====================================================================
- 寫(xiě)數(shù)據(jù)函數(shù): E =高脈沖 RS=1 RW=0
- ======================================================================*/
- void WriteDataLCM(unsigned char WDLCM)
- {
- ReadStatusLCM(); //檢測(cè)忙
- LCM_Data = WDLCM;
- LCM_RS = 1;
- LCM_RW = 0;
- LCM_E = 0; //若晶振速度太高可以在這后加小的延時(shí)
- LCM_E = 0; //延時(shí)
- LCM_E = 1;
- }
- /*====================================================================
- 寫(xiě)指令函數(shù): E=高脈沖 RS=0 RW=0
- ======================================================================*/
- void WriteCommandLCM(unsigned char WCLCM,BuysC) //BuysC為0時(shí)忽略忙檢測(cè)
- {
- if (BuysC) ReadStatusLCM(); //根據(jù)需要檢測(cè)忙
- LCM_Data = WCLCM;
- LCM_RS = 0;
- LCM_RW = 0;
- LCM_E = 0;
- LCM_E = 0;
- LCM_E = 1;
- }
- /*====================================================================
- //讀數(shù)據(jù)
- ======================================================================*/
- unsigned char ReadDataLCM(void)
- {
- LCM_RS = 1;
- LCM_RW = 1;
- LCM_E = 0;
- LCM_E = 0;
- LCM_E = 1;
- return(LCM_Data);
- }
- /*====================================================================
- 正常讀寫(xiě)操作之前必須檢測(cè)LCD控制器狀態(tài):E=1 RS=0 RW=1;
- DB7: 0 LCD控制器空閑,1 LCD控制器忙。
- 讀狀態(tài)
- ======================================================================*/
- unsigned char ReadStatusLCM(void)
- {
- LCM_Data = 0xFF;
- LCM_RS = 0;
- LCM_RW = 1;
- LCM_E = 0;
- LCM_E = 0;
- LCM_E = 1;
- while (LCM_Data & Busy); //檢測(cè)忙信號(hào)
- return(LCM_Data);
- }
- /*======================================================================
- 功 能: 在1602 指定位置顯示一個(gè)字符:第一行位置0~15,第二行16~31
- 說(shuō) 明: 第 X 行,第 y 列 注意:字符串不能長(zhǎng)于16個(gè)字符
- ======================================================================*/
- void DisplayOneChar( unsigned char X, unsigned char Y, unsigned char ASCII)
- {
- X &= 0x1;
- Y &= 0xF; //限制Y不能大于15,X不能大于1
- if (X) Y |= 0x40; //當(dāng)要顯示第二行時(shí)地址碼+0x40;
- Y |= 0x80; // 算出指令碼
- WriteCommandLCM(Y, 0); //這里不檢測(cè)忙信號(hào),發(fā)送地址碼
- WriteDataLCM(ASCII);
- }
復(fù)制代碼
所有資料51hei提供下載:
超聲波測(cè)距LC模塊.rar
(317.06 KB, 下載次數(shù): 61)
2018-5-14 00:11 上傳
點(diǎn)擊文件名下載附件
超聲波模塊 下載積分: 黑幣 -5
|
|