久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3298|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

CX20106+4069超聲波測(cè)距電路與單片機(jī)源碼(帶詳細(xì)代碼注釋)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主

超聲波測(cè)距IC模塊電路原理圖如下:


單片機(jī)源程序如下:
  1. /*
  2. * 超聲波測(cè)距模塊(CX20106+4069版本)
  3. *
  4. * 用途:超聲波測(cè)距模塊測(cè)試程序
  5. *
  6. * 作者                                        日期                                備注
  7. * Huafeng Lin                        20011/01/05                        新增
  8. * Huafeng Lin                        20011/01/05                        修改
  9. *
  10. */

  11. #include<reg51.h>
  12. #include<INTRINS.H>

  13. #define uchar unsigned char
  14. #define uint  unsigned int

  15. #define        nop                        _nop_()

  16. #define LCM_Data     P2    //數(shù)據(jù)接口
  17. #define Busy         0x80   //用于檢測(cè)LCM狀態(tài)字中的Busy標(biāo)識(shí)

  18. uint        dis;
  19. uchar        flag=0,high_time,low_time;

  20. sbit         in=P3^3;        //外部中斷1,接CX20106的7腳
  21. sbit         csb=P1^0;        //40KHz方波輸出腳

  22. sbit    LCM_RW     = P0^1;  //讀寫(xiě)控制輸入端,LCD1602的第五腳
  23. sbit    LCM_RS     = P0^2;  //寄存器選擇輸入端,LCD1602的第四腳
  24. sbit    LCM_E      = P0^0;  //使能信號(hào)輸入端,LCD1602的第6腳

  25. void    WriteDataLCM                (uchar WDLCM);                //LCD模塊寫(xiě)數(shù)據(jù)
  26. void    WriteCommandLCM        (uchar WCLCM,BuysC);         //LCD模塊寫(xiě)指令
  27. uchar   ReadDataLCM                (void);                //LCD模塊讀數(shù)據(jù)
  28. uchar   ReadStatusLCM        (void);                //讀LCD模塊的忙標(biāo)
  29. void    DisplayOneChar   (uchar X,uchar Y,uchar ASCII);                //在第X+1行的第Y+1位置顯示一個(gè)字符
  30. void    DisplayListChar  (uchar X,uchar Y,uchar delayms,uchar code *DData);
  31. void    DisplayCursorPos (uchar X, uchar Y);
  32. void    LCMInit          (void);
  33. void    DisplayIntData   (uchar X, uchar Y,int ZhengShu,uchar Digit,uchar XiaoShu);
  34. void    DisplayCharData  (uchar X, uchar Y,uchar ZiFu);

  35. void DelayUs(uint us);
  36. void DelayMs(uint Ms);

  37. void         init(void);                        //初始化
  38. void         delay_nms(uint n);        //延時(shí)nms
  39. void         display(uint dat);        //顯示函數(shù)
  40. void         tran(void);                        //發(fā)射超聲波
  41. void         delay100us();                //延時(shí)100us

  42. /**********************************
  43. 函數(shù)名稱:初始化函數(shù)
  44. 修改日期:
  45. 入口參數(shù):無(wú)
  46. 返回值:  無(wú)
  47. **********************************/
  48. void init(void)
  49. {
  50.            TMOD=0x01;//定時(shí)器0方式1用于計(jì)時(shí),定時(shí)器1用于產(chǎn)生38K方波
  51.         TH0=0;
  52.         TL0=0;                /* 設(shè)定T0的工作模式為2*/
  53.         EA=1;
  54.         IT1=1;//下降沿有效,左傳感器
  55. }
  56. /**********************************
  57. 函數(shù)名稱:延時(shí)函數(shù)
  58. 修改日期:
  59. 入口參數(shù):n
  60. 返回值:  無(wú)
  61. **********************************/
  62. void delay_nms(uint n)
  63. {
  64.          uchar i;
  65.       while(n--)
  66.       {
  67.           for(i = 0; i < 123; i++);
  68.        }                                ;
  69. }
  70. /************************************************
  71. 延時(shí)100us函數(shù)
  72. ***********************************************/
  73. void delay100us()
  74. {
  75.         uchar j;
  76.         for(j=50;j>0;j--);
  77. }
  78. /**********************************
  79. 函數(shù)名稱:超聲波發(fā)射函數(shù)
  80. 修改日期:
  81. 入口參數(shù):無(wú)
  82. 返回值:  無(wú)
  83. **********************************/
  84. void tran(void)
  85. {
  86.         uchar i;
  87.         float temp;
  88.         TH0=0;
  89.         TL0=0;//清定時(shí)0
  90.         TR0=1;//開(kāi)定時(shí)0
  91.         for(i=8;i>0;i--)
  92.         {
  93.                 csb=!csb;
  94.                 //nop;
  95.                 //nop;
  96.                 nop;
  97.                 nop;
  98.                 nop;
  99.                 nop;
  100.                 nop;
  101.                 nop;
  102.                 nop;
  103.                 nop;
  104.         }
  105.         csb=1;
  106.         delay_nms(1);//延時(shí)1ms左右后再開(kāi)中斷,避免直接回來(lái)的回波
  107.         EX1=1;
  108.         delay_nms(50);
  109.         if(flag==1)
  110.         {
  111.                 temp=high_time*256+low_time;
  112.                 temp=(temp/1000)/2;
  113.                 temp*=344;
  114.                 temp=temp/10;
  115.                 //if(temp>10)
  116.                 dis=(unsigned int)temp;
  117.                 flag=0;
  118.         }
  119.         //else dis=0;
  120. }
  121. /**********************************
  122. 函數(shù)名稱:中斷函數(shù)
  123. 修改日期:
  124. 入口參數(shù):無(wú)
  125. 返回值:  無(wú)
  126. **********************************/
  127. void TT() interrupt 2
  128. {
  129.         uint tmp;
  130.         TR0=0;//關(guān)定時(shí)器0
  131.         ET1=0;//關(guān)外部中斷
  132.         //flag=1;//外部中斷標(biāo)志位
  133.         tmp=TH0*256+TL0;
  134.         if((tmp>0)&&(tmp<60000))        
  135.         {
  136.                 high_time=TH0;
  137.                 low_time=TL0;
  138.                 flag=1;        
  139.         }
  140.         else
  141.         {
  142.                 high_time=0;
  143.                 low_time=0;        
  144.         }
  145. }

  146. /*===========================================================================
  147. 主程序
  148. =============================================================================*/
  149. void main(void)
  150. {
  151.         uchar i;
  152.         uint tp=0;
  153.         init();
  154.         LCMInit();
  155.         DisplayOneChar(1,10,'M');
  156.         DisplayListChar(0,0,0,"LCSOFT (C) 2011");
  157.         while(1)
  158.         {
  159.                 tran();
  160.                 for(i=100;i>0;i--)
  161.                 {
  162.                         DisplayIntData(1,9,dis,4,2);
  163.                 }         
  164.         }
  165. }         
  166. /*====================================================================  
  167. 功 能:    在1602顯示一個(gè)整數(shù)數(shù)據(jù)
  168. 說(shuō) 明:    顯示一個(gè)整數(shù)數(shù)據(jù)-9999->32625.  從右至左顯示數(shù)據(jù)5位:   
  169. ======================================================================*/
  170. void DisplayIntData(uchar X, uchar Y,int ZhengShu,uchar Digit,uchar XiaoShu)
  171. {
  172.       uchar i=0,k=0, BCD[5]={0};
  173.           if(Digit>5)  Digit=5;
  174.       if(ZhengShu<0)
  175.           {
  176.          k=1;//負(fù)數(shù)示志位
  177.          ZhengShu=-ZhengShu;
  178.           }
  179.       BCD[4]  =  ZhengShu / 10000;         //求出萬(wàn)位數(shù)據(jù)
  180.       ZhengShu = ZhengShu % 10000;
  181.       BCD[3]  =  ZhengShu / 1000;          //求出千位數(shù)據(jù)
  182.       ZhengShu = ZhengShu % 1000;
  183.       BCD[2]  =  ZhengShu / 100;           //求出百位數(shù)據(jù)
  184.       ZhengShu = ZhengShu % 100;
  185.       BCD[1]  =  ZhengShu / 10;            //求出十位數(shù)據(jù)
  186.       BCD[0]  =  ZhengShu % 10;            //求出個(gè)位數(shù)據(jù)
  187.          
  188.       for(i=0;i<Digit;i++)//輸出顯示的數(shù)值
  189.       {   
  190.                if((i==XiaoShu)&&(0!=XiaoShu))
  191.                    {  
  192.                       DisplayOneChar(X,Y-i,'.');//輸出小數(shù)點(diǎn)
  193.                           Y= Y-1;
  194.                     }
  195.                         
  196.                DisplayOneChar(X,Y-i,BCD[i]+0x30); //顯示一個(gè)字符
  197.           }
  198.       if(k==1)
  199.           DisplayOneChar(X,Y-1,'-');//輸出負(fù)符
  200. }
  201. /*====================================================================
  202. 功 能:在1602顯示一個(gè)字符數(shù)據(jù)
  203. 說(shuō) 明:顯示一個(gè)字符數(shù)據(jù)0~256.  從左至右顯示數(shù)據(jù)3位
  204. ======================================================================*/
  205. void DisplayCharData(uchar X, uchar Y,uchar ZiFu)
  206. {
  207.       uchar i=0;
  208.       uchar ValueBCD[3];
  209.       ValueBCD[0]  = ZiFu / 100;           //求出百位數(shù)據(jù)
  210.               ZiFu = ZiFu % 100;
  211.       ValueBCD[1]  = ZiFu / 10;            //求出十位數(shù)據(jù)
  212.       ValueBCD[2]  = ZiFu % 10;            //求出個(gè)位數(shù)據(jù)
  213.          
  214.       for(i=0;i<3;i++)//輸出顯示的數(shù)值
  215.       {         
  216.                DisplayOneChar(X,Y+i,ValueBCD[i]+0x30); //顯示一個(gè)字符
  217.           }
  218. }
  219. /*======================================================================
  220. LCM初始化
  221. ======================================================================*/
  222. void LCMInit(void)
  223. {
  224. LCM_Data = 0;
  225. WriteCommandLCM(0x38,0); //三次顯示模式設(shè)置,不檢測(cè)忙信號(hào)
  226. DelayMs(5);
  227. WriteCommandLCM(0x38,0);
  228. DelayMs(5);
  229. WriteCommandLCM(0x38,0);
  230. DelayMs(5);
  231. WriteCommandLCM(0x38,1); //顯示模式設(shè)置,開(kāi)始要求每次檢測(cè)忙信號(hào)
  232. WriteCommandLCM(0x08,1); //關(guān)閉顯示
  233. WriteCommandLCM(0x01,1); //顯示清屏
  234. WriteCommandLCM(0x06,1); // 顯示光標(biāo)移動(dòng)設(shè)置
  235. WriteCommandLCM(0x0C,1); // 顯示開(kāi)及光標(biāo)設(shè)置

  236. DelayMs(100);
  237. }
  238. /*====================================================================
  239.    顯示光標(biāo)的位置
  240. ====================================================================*/
  241. void DisplayCursorPos( unsigned char X, unsigned char Y)
  242. {
  243. X &= 0x1;
  244. Y &= 0xF; //限制Y不能大于15,X不能大于1
  245. if (X) Y |= 0x40; //當(dāng)要顯示第二行時(shí)地址碼+0x40;
  246. Y |= 0x80; // 算出指令碼
  247. WriteCommandLCM(Y, 1); //這里不檢測(cè)忙信號(hào),發(fā)送地址碼
  248. }
  249. /*====================================================================  
  250.   按指定位置顯示一串字符:第 X 行,第 y列
  251.   注意:字符串不能長(zhǎng)于16個(gè)字符
  252. ======================================================================*/
  253. void DisplayListChar(uchar X,uchar Y,uchar delayms, uchar code *DData)
  254. {
  255. unsigned char ListLength;

  256. ListLength = 0;

  257. X &= 0x1;
  258. Y &= 0xF; //限制X不能大于15,Y不能大于1
  259. while (DData[ListLength]!='\0') //若到達(dá)字串尾則退出
  260.   {
  261.      if (Y <= 0xF) //X坐標(biāo)應(yīng)小于0xF
  262.      {
  263.         DisplayOneChar(X, Y, DData[ListLength]); //顯示單個(gè)字符
  264.         ListLength++;
  265.         Y++;
  266.             DelayMs(delayms);//延時(shí)顯示字符串
  267.      }
  268.      else
  269.             break;//跳出循環(huán)體
  270.   }
  271. }
  272. /*====================================================================
  273.   設(shè)定延時(shí)時(shí)間:x*1us
  274. ====================================================================*/
  275. void DelayUs(uint us)
  276. {
  277.     while(us--);
  278. }
  279. /*====================================================================
  280.   設(shè)定延時(shí)時(shí)間:x*1ms
  281. ====================================================================*/
  282. void DelayMs(uint Ms)
  283. {
  284.   uint i,TempCyc;
  285.   for(i=0;i<Ms;i++)
  286.   {
  287.     TempCyc = 250;
  288.     while(TempCyc--);
  289.   }
  290. }                                 
  291. /*=====================================================================
  292. 寫(xiě)數(shù)據(jù)函數(shù): E =高脈沖 RS=1 RW=0
  293. ======================================================================*/
  294. void WriteDataLCM(unsigned char WDLCM)
  295. {
  296. ReadStatusLCM(); //檢測(cè)忙
  297. LCM_Data = WDLCM;
  298. LCM_RS = 1;
  299. LCM_RW = 0;
  300. LCM_E = 0; //若晶振速度太高可以在這后加小的延時(shí)
  301. LCM_E = 0; //延時(shí)
  302. LCM_E = 1;
  303. }
  304. /*====================================================================
  305.   寫(xiě)指令函數(shù): E=高脈沖 RS=0 RW=0
  306. ======================================================================*/
  307. void WriteCommandLCM(unsigned char WCLCM,BuysC) //BuysC為0時(shí)忽略忙檢測(cè)
  308. {
  309. if (BuysC) ReadStatusLCM(); //根據(jù)需要檢測(cè)忙
  310. LCM_Data = WCLCM;
  311. LCM_RS = 0;
  312. LCM_RW = 0;
  313. LCM_E = 0;
  314. LCM_E = 0;
  315. LCM_E = 1;
  316. }
  317. /*====================================================================
  318. //讀數(shù)據(jù)
  319. ======================================================================*/
  320. unsigned char ReadDataLCM(void)
  321. {
  322. LCM_RS = 1;
  323. LCM_RW = 1;
  324. LCM_E = 0;
  325. LCM_E = 0;
  326. LCM_E = 1;
  327. return(LCM_Data);
  328. }
  329. /*====================================================================
  330.   正常讀寫(xiě)操作之前必須檢測(cè)LCD控制器狀態(tài):E=1 RS=0 RW=1;
  331.   DB7: 0 LCD控制器空閑,1 LCD控制器忙。
  332.   讀狀態(tài)
  333. ======================================================================*/
  334. unsigned char ReadStatusLCM(void)
  335. {
  336. LCM_Data = 0xFF;
  337. LCM_RS = 0;
  338. LCM_RW = 1;
  339. LCM_E = 0;
  340. LCM_E = 0;
  341. LCM_E = 1;
  342. while (LCM_Data & Busy); //檢測(cè)忙信號(hào)  
  343. return(LCM_Data);
  344. }
  345. /*======================================================================
  346. 功 能:     在1602 指定位置顯示一個(gè)字符:第一行位置0~15,第二行16~31
  347. 說(shuō) 明:     第 X 行,第 y 列  注意:字符串不能長(zhǎng)于16個(gè)字符
  348. ======================================================================*/
  349. void DisplayOneChar( unsigned char X, unsigned char Y, unsigned char ASCII)
  350. {
  351. X &= 0x1;
  352. Y &= 0xF; //限制Y不能大于15,X不能大于1
  353. if (X) Y |= 0x40; //當(dāng)要顯示第二行時(shí)地址碼+0x40;
  354. Y |= 0x80; // 算出指令碼
  355. WriteCommandLCM(Y, 0); //這里不檢測(cè)忙信號(hào),發(fā)送地址碼
  356. WriteDataLCM(ASCII);
  357. }
復(fù)制代碼

所有資料51hei提供下載:
超聲波測(cè)距LC模塊.rar (317.06 KB, 下載次數(shù): 61)




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:354404 發(fā)表于 2018-8-13 21:40 | 只看該作者
為什么我做出來(lái)測(cè)出來(lái)沒(méi)有用啊
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产精品久久久久久久久久久久午夜片 | 日韩av在线免费 | 欧洲一区二区三区 | 国产免费人成xvideos视频 | 国产丝袜一区二区三区免费视频 | 国产美女网站 | 精品福利在线 | 四虎海外 | 91精品国产一区二区三区 | 黄色一级免费 | 日韩高清成人 | 日本黄视频在线观看 | 成人国产网站 | 狠狠综合久久av一区二区小说 | 欧美三级久久久 | 免费一区二区三区 | 国产 日韩 欧美 中文 在线播放 | 亚洲天堂一区 | 久草资源在线视频 | 国产精品视频免费观看 | 日韩在线观看 | 欧美xxxx色视频在线观看免费 | 日韩免费视频一区二区 | 羞羞视频在线观看免费观看 | 亚洲网在线 | 欧美日韩亚洲视频 | 欧美精品一二三区 | 99热视| 亚洲成人三级 | 精品中文在线 | www.xxxx欧美 | 亚洲欧美日韩在线 | 久久com | 国产高清久久久 | 亚洲精品66 | 欧美寡妇偷汉性猛交 | 日韩一及片 | 日韩三级在线观看 | 久久久久国产一区二区三区 | 欧美午夜一区 | 日韩中文字幕网 |