標(biāo)題: 智能小車 超聲波測(cè)距 [打印本頁]

作者: demoliu    時(shí)間: 2015-10-30 16:41
標(biāo)題: 智能小車 超聲波測(cè)距
  1. #include<reg52.h>

  2. #include <intrins.h>

  3. #define uint unsigned int

  4. #define uchar unsigned char


  5. sbit in=P3^3;          //超聲波接收口
  6. sbit out=P3^4;          //超聲波發(fā)射口


  7. //LCD1602端口定義

  8. sbit RS=P0^6;          //寄存器選擇位,將RS位定義為P2.0引腳           //1602引腳定義
  9. sbit RW=P0^5;          //讀寫選擇位,將RW位定義為P2.1引腳
  10. sbit E=P0^4;           //使能信號(hào)位,將E位定義為P2.2引腳
  11. sbit BF=P1^7;          //忙碌標(biāo)志位,,將BF位定義為P0.7引腳
  12. #define LCD1602data P1 //1602數(shù)據(jù)口接單片機(jī)P1口


  13. uint time;
  14. unsigned long s=0;
  15. bit flag; //超聲波超出測(cè)量范圍標(biāo)志位
  16. uint s1,s2,s3;



  17. /*****************************************************
  18. 以下為1602模塊語句
  19. ***************************************************/


  20. /*****************************************************
  21. 函數(shù)功能:延時(shí)1ms
  22. ***************************************************/
  23. void delay1ms()
  24. {
  25.            unsigned char i,j;       
  26.         for(i=0;i<10;i++)
  27.         for(j=0;j<33;j++);                 
  28. }
  29. /*****************************************************
  30. 函數(shù)功能:延時(shí)若干毫秒
  31. 入口參數(shù):n
  32. ***************************************************/
  33. void delay(unsigned int n)
  34. {
  35.            unsigned int i;
  36.         for(i=0;i<n;i++)
  37.         delay1ms();
  38. }
  39. /*****************************************************
  40. 函數(shù)功能:判斷液晶模塊的忙碌狀態(tài)
  41. 返回值:result。result=1,忙碌;result=0,不忙
  42. ***************************************************/
  43. bit BusyTest(void)
  44.   {
  45.             bit result;
  46.         RS=0;//根據(jù)規(guī)定,RS為低電平,RW為高電平時(shí),可以讀狀態(tài)
  47.             RW=1;
  48.             E=1;        //E=1,才允許讀寫
  49.             _nop_();   //空操作
  50.             _nop_();
  51.             _nop_();
  52.            _nop_();   //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間       
  53.             result=BF;  //將忙碌標(biāo)志電平賦給result
  54.         E=0;
  55.             return result;
  56.   }
  57. /*****************************************************
  58. 函數(shù)功能:將模式設(shè)置指令或顯示地址寫入液晶模塊
  59. 入口參數(shù):dictate
  60. ***************************************************/
  61. void Write_com (unsigned char dictate)
  62. {   
  63.     while(BusyTest()==1); //如果忙就等待
  64.          RS=0;            //根據(jù)規(guī)定,RS和R/W同時(shí)為低電平時(shí),可以寫入指令
  65.          RW=0;   
  66.          E=0;             //E置低電平(寫指令時(shí),
  67.                           // 就是讓E從0到1發(fā)生正跳變,所以應(yīng)先置"0"
  68.          _nop_();
  69.          _nop_();         //空操作兩個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  70.          LCD1602data=dictate;//將數(shù)據(jù)送入P0口,即寫入指令或地址
  71.          _nop_();
  72.          _nop_();
  73.          _nop_();
  74.          _nop_();         //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  75.          E=1;             //E置高電平
  76.          _nop_();
  77.          _nop_();
  78.          _nop_();
  79.          _nop_();         //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  80.           E=0;            //當(dāng)E由高電平跳變成低電平時(shí),液晶模塊開始執(zhí)行命令
  81. }
  82. /*****************************************************
  83. 函數(shù)功能:指定字符顯示的實(shí)際地址
  84. 入口參數(shù):x
  85. ***************************************************/
  86. void WriteAddress(unsigned char x)
  87. {
  88.      Write_com(x|0x80); //顯示位置的確定方法規(guī)定為"80H+地址碼x"       
  89. }
  90. /*****************************************************
  91. 函數(shù)功能:將數(shù)據(jù)(字符的標(biāo)準(zhǔn)ASCII碼)寫入液晶模塊
  92. 入口參數(shù):y(為字符常量)
  93. ***************************************************/
  94. void WriteData(unsigned char y)
  95. {
  96.     while(BusyTest()==1);  
  97.           RS=1;           //RS為高電平,RW為低電平時(shí),可以寫入數(shù)據(jù)
  98.           RW=0;
  99.           E=0;            //E置低電平(寫指令時(shí),,
  100.                        // 就是讓E從0到1發(fā)生正跳變,所以應(yīng)先置"0"
  101.           LCD1602data=y;  //將數(shù)據(jù)送入P0口,即將數(shù)據(jù)寫入液晶模塊
  102.           _nop_();
  103.           _nop_();
  104.           _nop_();
  105.               _nop_();       //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  106.           E=1;          //E置高電平
  107.           _nop_();
  108.           _nop_();
  109.           _nop_();
  110.           _nop_();        //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  111.           E=0;            //當(dāng)E由高電平跳變成低電平時(shí),液晶模塊開始執(zhí)行命令
  112. }
  113. /*****************************************************
  114. 函數(shù)功能:對(duì)LCD的顯示模式進(jìn)行初始化設(shè)置
  115. ***************************************************/
  116. void LcdInt(void)
  117. {
  118.            delay(15);      //延時(shí)15ms,首次寫指令時(shí)應(yīng)給LCD一段較長(zhǎng)的反應(yīng)時(shí)間
  119.            Write_com(0x38);  //顯示模式設(shè)置:16×2顯示,5×7點(diǎn)陣,8位數(shù)據(jù)接口
  120.         delay(5);   //延時(shí)5ms 
  121.         Write_com(0x38);
  122.         delay(5);
  123.         Write_com(0x38); //3次寫 設(shè)置模式
  124.         delay(5);
  125. //        Write_com(0x0F);  //顯示模式設(shè)置:顯示開,有光標(biāo),光標(biāo)閃爍
  126. //        delay(5);

  127.         Write_com(0x0c);  //顯示模式設(shè)置:顯示開,不顯示光標(biāo)
  128.         delay(5);

  129.         Write_com(0x06);  //顯示模式設(shè)置:光標(biāo)右移,字符不移
  130.         delay(5);                                                                                          
  131.         Write_com(0x01);  //清屏幕指令,將以前的顯示內(nèi)容清除
  132.         delay(5);
  133. }

  134. //add為顯示字符的位置(在第二行顯示)//x為顯示的字符
  135. //調(diào)用此函數(shù)請(qǐng)注意:此函數(shù)是為顯示數(shù)字的,display(10,9);display(10,a);
  136. //分別表示第二行第十位顯示數(shù)字9和a代表的數(shù)字
  137. //如果想把此函數(shù)改寫成顯示字母的,
  138. //只需把最后WriteData(0x30+x);中的
  139. // 0x30+ 去掉即可,調(diào)用時(shí)注意格式  display(10,'a');
  140. display(unsigned char add,unsigned char x)            
  141. {                                         
  142.                        

  143.                  WriteAddress(0x00);
  144.                                                                                                                                                                                
  145.                  delay(5);

  146.                  Write_com(0x80+0x40+add);

  147.                  WriteData(0x30+x);
  148. }



  149. LCD1602()//1602初顯示
  150. {
  151.        

  152.               LcdInt();         //調(diào)用LCD初始化函數(shù)  
  153.           delay(10);
  154.               Write_com(0x01);                //清屏

  155.                   delay(5);

  156.                   WriteAddress(0x00);
  157. //設(shè)置顯示位置為第一行第一個(gè)字,這條語句可以不用寫,因?yàn)槟J(rèn)是從第一行第一個(gè)字顯示                                                                                                                                                                               
  158.                   delay(5);
  159. //第一行顯示 DISTANCE:   
  160.                   WriteData('D');
  161.               WriteData('I');
  162.                   WriteData('S');
  163.                   WriteData('T');
  164.                   WriteData('A');       
  165.                   WriteData('N');       
  166.                   WriteData('C');       
  167.                   WriteData('E');       
  168.                   WriteData(':');
  169. }



  170. /*****************************************************
  171. 以上為1602模塊語句
  172. ***************************************************/


  173. void  FS()                          //發(fā)射超聲波
  174.   {
  175.           out=1;                                      
  176.           _nop_();
  177.           _nop_();
  178.           _nop_();
  179.           _nop_();
  180.           _nop_();
  181.           _nop_();
  182.           _nop_();
  183.           _nop_();
  184.           _nop_();
  185.           _nop_();
  186.           _nop_();
  187.           _nop_();
  188.           _nop_();
  189.           _nop_();
  190.           _nop_();
  191.           _nop_();
  192.           _nop_();
  193.           _nop_();
  194.           _nop_();
  195.           _nop_();
  196.           _nop_();
  197.           out=0;
  198.   }

  199. void conut(void) //計(jì)算并顯示超聲波測(cè)到的距離
  200. {
  201.          time=TH0*256+TL0;
  202.          TH0=0;
  203.          TL0=0;
  204.        
  205.          s=(time*1.7)/100;     //算出來單位是CM

  206.          if((s>=700)||flag==1) //超出測(cè)量范圍顯示"ERROR"
  207.          {         
  208.           flag=0;
  209.          
  210.           Write_com(0x80+0x40+11);
  211.           WriteData('E');        delay(5);
  212.       WriteData('R');        delay(5);
  213.           WriteData('R');        delay(5);
  214.           WriteData('O');        delay(5);
  215.           WriteData('R');        delay(5);
  216.          }
  217.          else
  218.          {
  219.           s1=s%1000/100;
  220.           s2=s%1000%100/10;
  221.           s3=s%1000%10 %10;
  222.           display(11,s1);
  223.           WriteData('.');
  224.           display(13,s2);       
  225.           display(14,s3);
  226.           WriteData('m');
  227.          }


  228. }
  229. /********************************************************/
  230. void zd0() interrupt 1                  //T0中斷用來計(jì)數(shù)器溢出,超過測(cè)距范圍
  231. {
  232.     flag=1;                                                         //中斷溢出標(biāo)志
  233. }

  234. main()
  235. {
  236.          LCD1602();
  237.          while(1)
  238.          {
  239.                     TMOD=0x01;                   //設(shè)T0為方式1,GATE=0;
  240.                TH0=0;
  241.                TL0=0;         
  242.                ET0=1;             //允許T0中斷
  243.                EA=1;
  244.                                               //開啟總中斷       
  245.                     while(1)
  246.                    {
  247.                               FS();
  248.                            while(!in);
  249.                            TR0=1;
  250.                            while(in);
  251.                            TR0=0;
  252.                            conut();
  253.                                     
  254.                    }
  255.                   
  256.          }

  257. }
復(fù)制代碼







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1