<acronym id="xonnx"></acronym>
      <td id="xonnx"></td>
    1. <pre id="xonnx"></pre>

      1. 專注電子技術學習與研究
        當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

        超聲波測距51單片機12864液晶顯示

        作者:佚名   來源:不詳   點擊數:  更新時間:2014年07月31日   【字體:

           超聲波測距的模塊,51單片機接上12864液晶,HC-SR04超聲波模塊,就可以了,具體程序沒怎么看,超聲波模塊的硬件也不曉得,今天先貼個圖,貼個程序吧(程序是淘寶買的模塊附贈的)。

         

         



        超聲波測距51單片機12864液晶顯示源程序:
         #include <reg52.h>
         #include<intrins.h>
        //引腳定義
            sbit     RX    =  P1 ^ 1;
            sbit     TX    =  P1 ^ 2;
            sbit     CS    =  P3 ^ 3; //片選 高電平有效 單片LCD使用時可固定高電平
            sbit     SID   =  P3 ^ 4; //數據
            sbit     SCLK  =  P3 ^ 5; //時鐘

        //Function Definition 函數聲明
        void Delay(int num);
        void Init_DS18B20(void);
        unsigned char ReadOneChar(void);
        void WriteOneChar(unsigned char dat);
        unsigned int ReadTemperature(void);
        void clock_out(unsigned char dd);
        unsigned char clock_in(void);
        unsigned char read_clock(unsigned char ord);
        void write_clock(unsigned char ord, unsigned char dd);
        void Disp(void);
        void id_case1_key(void);
        void id_case2_key(void);
        void Set_time(unsigned char sel,bit sel_1);
        void Timer0_Init(void);
        void Init_1302(void);
        void Set_Bell(unsigned char sel, bit sel_1);
        void Bell(void);
        //12864
        void Write_char(bit start, unsigned char ddata);
        void Send_byte(unsigned char bbyte);
        void Delaynms(unsigned int di);
        void Lcd_init(void);
        void Disp_img(unsigned char *img);
        void LCD_Write_string(unsigned char X,unsigned char Y,unsigned char *s);
        void LCD_set_xy( unsigned char x, unsigned char y );
        unsigned char code num[]={"0123456789 :.-"};
        unsigned char code waves[]={"超聲波測距系統"};
        unsigned char code znwk[] ={"智能微控工作室"};
        unsigned char code CM[] =  {"M"};
        unsigned int  time=0;
                 long S=0;
                 bit  flag =0;
        unsigned char disbuff[4]    ={ 0,0,0,0,};
        unsigned char code logo[]={ 




        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0xE0,0x00,0x00,0x0C,0x00,0x00,0x40,0x70,0x00,0x00,0x00,0x80,0x00,
        0x00,0x00,0x00,0xE0,0x00,0x00,0x1E,0x00,0x00,0x60,0x70,0x00,0x00,0x0F,0xE0,0x00,
        0x00,0x00,0x01,0xC0,0x00,0x60,0x3C,0x00,0x00,0xE0,0x60,0x00,0x1F,0xFF,0xE0,0x00,
        0x00,0x00,0x03,0x80,0x00,0x70,0x70,0x40,0x00,0xE0,0x7E,0x00,0x1F,0xE0,0xC0,0x00,
        0x00,0x00,0x03,0x00,0x00,0x73,0xC3,0xF0,0x01,0xC3,0xFC,0x00,0x38,0xE1,0xC0,0x00,
        0x00,0x03,0xC6,0x3C,0x00,0x33,0x9F,0xF0,0x03,0x8F,0xC0,0x00,0x30,0xC1,0xC0,0x00,
        0x00,0x3F,0xEF,0xFE,0x00,0x03,0x1C,0xE0,0x07,0x01,0xCC,0x00,0x31,0xF1,0x80,0x00,
        0x00,0x79,0xDF,0xBC,0x00,0x06,0x18,0xC0,0x0E,0x01,0xFE,0x00,0x77,0xF9,0x80,0x00,
        0x00,0x01,0xB0,0x30,0x01,0xC6,0x19,0xC0,0x1E,0x7F,0xDE,0x00,0x6F,0x83,0x80,0x00,
        0x00,0x23,0xAC,0x60,0x0F,0xE6,0xF1,0x80,0x3E,0x7B,0x38,0x00,0x61,0x83,0x00,0x00,
        0x00,0x37,0x0E,0x00,0x0F,0xC7,0xB7,0x80,0xEC,0x63,0x30,0x00,0xE3,0x03,0x00,0x00,
        0x00,0x3E,0x0E,0x00,0x01,0x8F,0x37,0x01,0xCC,0x6F,0xF0,0x00,0xC7,0xF7,0x00,0x00,
        0x00,0x1E,0x0C,0x00,0x03,0x1E,0x66,0x03,0x0C,0x7F,0x00,0x01,0xFF,0xE6,0x00,0x00,
        0x00,0x1E,0x1C,0x00,0x03,0x1C,0x60,0x02,0x19,0xAC,0x00,0x01,0x96,0x06,0x00,0x00,
        0x00,0x3E,0x3E,0x00,0x03,0x18,0x60,0x00,0x19,0xFC,0x00,0x03,0x86,0x0E,0x00,0x00,
        0x00,0x7E,0x36,0x00,0x03,0x00,0xC0,0x00,0x38,0x78,0x00,0x03,0x06,0x0C,0x00,0x00,
        0x00,0xE6,0x67,0x00,0x03,0x00,0xC0,0x00,0x30,0x78,0x00,0x06,0x0C,0x0C,0x00,0x00,
        0x01,0xC6,0xC3,0x80,0x7F,0xF0,0xC0,0x00,0x30,0xFE,0x00,0x0E,0x0C,0x1C,0x00,0x00,
        0x03,0x81,0x83,0xC0,0x78,0xFF,0x80,0x00,0x61,0xCF,0x80,0x1C,0x08,0x18,0x00,0x00,
        0x06,0x07,0x03,0xE0,0x00,0x1F,0xFF,0xE0,0x67,0x07,0xE0,0x38,0x18,0xF8,0x00,0x00,
        0x08,0x0E,0x01,0xF8,0x00,0x07,0xFF,0x80,0xCE,0x03,0xF8,0x60,0x00,0x70,0x00,0x00,
        0x00,0x18,0x01,0xF8,0x00,0x01,0xFC,0x00,0xC0,0x01,0xFC,0xC0,0x00,0x70,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x06,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x07,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x06,0x00,0x00,0x00,0x0C,0xE0,0x00,0x01,0xE0,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x0E,0x00,0x00,0x00,0x3F,0xE0,0x00,0xFF,0xF0,0x00,0x38,0x00,0xE0,0x00,
        0x00,0x00,0x0C,0x00,0x00,0x0F,0xFF,0x80,0x00,0xF8,0xF0,0x00,0x38,0x00,0xE0,0x00,
        0x00,0x00,0x0C,0xF0,0x00,0x0F,0xC7,0x00,0x00,0x60,0xE0,0x00,0x38,0x00,0xE0,0x00,
        0x00,0x00,0x1F,0xF0,0x00,0x01,0x8E,0x00,0x00,0x60,0xC0,0x00,0x70,0x01,0xC0,0x00,
        0x00,0x07,0xFF,0x80,0x00,0x01,0x8C,0x00,0x00,0xE1,0x80,0x00,0x70,0x01,0xC0,0x00,
        0x00,0x07,0xF8,0x00,0x00,0x01,0x98,0x00,0x00,0xDF,0xC0,0x00,0x70,0x01,0xC0,0x00,
        0x00,0x00,0x78,0x00,0x00,0x01,0xBF,0xE0,0x00,0xFF,0x00,0x00,0x60,0x01,0x80,0x00,
        0x00,0x00,0xFC,0x00,0x00,0x7F,0xFF,0xE0,0x00,0xC0,0x00,0x00,0xE0,0x03,0x80,0x00,
        0x00,0x01,0xFE,0x00,0x00,0x7F,0xF8,0x00,0x00,0x00,0x00,0x00,0xC0,0x03,0x00,0x00,
        0x00,0x03,0xB7,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x3C,0x00,0xC0,0x03,0x00,0x00,
        0x00,0x07,0x67,0x00,0x00,0x60,0x00,0x00,0x03,0x9F,0xFE,0x00,0x80,0x02,0x00,0x00,
        0x00,0x0E,0x63,0x80,0x00,0xE0,0x00,0x01,0xFF,0xCF,0x1E,0x01,0x80,0x06,0x00,0x00,
        0x00,0x1C,0xE1,0xE0,0x00,0xC0,0x00,0x00,0xE3,0x9C,0x38,0x01,0x80,0x06,0x00,0x00,
        0x00,0x38,0xC1,0xF8,0x01,0xC0,0x00,0x00,0xC3,0x18,0x30,0x00,0x00,0x00,0x00,0x00,
        0x00,0xE0,0xFC,0xFE,0x03,0x80,0x00,0x01,0xC7,0x18,0x60,0x00,0x00,0x00,0x00,0x00,
        0x01,0xDF,0xF8,0xFE,0x03,0x00,0x00,0x01,0x8E,0x1B,0xE0,0x07,0x00,0x1C,0x00,0x00,
        0x07,0x07,0x80,0x00,0x07,0x00,0x00,0x01,0xFF,0x3F,0xE0,0x07,0x00,0x1C,0x00,0x00,
        0x0C,0x03,0x80,0x00,0x0E,0x00,0x00,0x01,0xF0,0x38,0x00,0x0F,0x00,0x3C,0x00,0x00,
        0x00,0x03,0x00,0x00,0x1C,0x00,0x00,0x01,0x00,0x20,0x00,0x06,0x00,0x18,0x00,0x00,
        0x00,0x03,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x03,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x02,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        };


        void Lcd_init(void) //初始化LCD
        {
         Delaynms(10); //啟動等待,等LCM講入工作狀態
           CS=1;
           Write_char(0,0x30);  //8 位介面,基本指令集
           Write_char(0,0x0c);  //顯示打開,光標關,反白關
           Write_char(0,0x01);  //清屏,將DDRAM的地址計數器歸零 
        }


        void Write_char(bit start, unsigned char ddata) //寫指令或數據
        {
         unsigned char start_data,Hdata,Ldata;
           if(start==0)
          start_data=0xf8;  //寫指令
            else   
             start_data=0xfa;  //寫數據
         
           Hdata=ddata&0xf0;    //取高四位
           Ldata=(ddata<<4)&0xf0;  //取低四位
           Send_byte(start_data);   //發送起始信號
           Delaynms(5); //延時是必須的
           Send_byte(Hdata);       //發送高四位
           Delaynms(1);  //延時是必須的
           Send_byte(Ldata);    //發送低四位
           Delaynms(1);  //延時是必須的
        }


        void Send_byte(unsigned char bbyte) //發送一個字節
        {
         unsigned char i;
          for(i=0;i<8;i++)
            {
             SID=bbyte&0x80; //取出最高位
           SCLK=1;
             SCLK=0;
             bbyte<<=1; //左移
            
        }


        void Delaynms(unsigned int di) //延時
        {
         unsigned int da,db;
          for(da=0;da
             for(db=0;db<10;db++);
        }


        void Disp_img(unsigned char  *img) //圖形方式12864顯示字模221 橫向取膜
        {
         unsigned char i,j;
         unsigned int k = 0;

         Write_char(0,0x36); //圖形方式
         for(i=0;i<32;i++)
           {
          Write_char(0,0x80+i);
          Write_char(0,0x80);
             for(j=0;j<16;j++)
          {
           Write_char(1,img[k++]);
          }
           }

          for(i=0;i<32;i++)
           {
          Write_char(0,0x80+i);
          Write_char(0,0x88);
             for(j=0;j<16;j++)
          {
           Write_char(1,img[k++]);
            }
         }
        }
         

        void Clr_Scr(void)//清屏函數
        {
         Write_char(0,0x01);
        }


        void LCD_set_xy( unsigned char x, unsigned char y )
        //設置LCD顯示的起始位置,X為行,Y為列
            unsigned char address;
         switch(x)
         {
          case 0: address = 0x80 + y; break;   
             case 1: address = 0x80 + y; break;
          case 2: address = 0x90 + y; break;
              case 3: address = 0x88 + y; break;
          case 4: address = 0x98 + y; break;
          default:address = 0x80 + y; break;
         }
            Write_char(0, address);
        }


        void LCD_Write_string(unsigned char X,unsigned char Y,unsigned char *s)
        // 中英文字符串顯示函數
         LCD_set_xy( X, Y );
           
            while (*s)
            {
          Write_char( 1, *s );
             s ++;
          Delaynms(1);
         }
        }


        void LCD_Write_number(unsigned char s)// 數字顯示函數

         Write_char(1,num[s]);
         Delaynms(1);
        }

        void Lcd_Mark2(void)
        {
         Clr_Scr();//清屏
         LCD_Write_string(1,0,znwk);//
         LCD_Write_string(2,0,waves);// 
         LCD_Write_string(3,7,CM);// 
        }


            void Conut(void)
         {
            time=TH0*256+TL0;
            TH0=0;
            TL0=0;
            S=time*1.87/100;       //算出來是CM   11。0592M晶振
            if(flag==1)        //超出測量
           { 
            flag=0;
            LCD_set_xy( 3, 4 );
            LCD_Write_number(13);
            LCD_Write_number(12);
            LCD_set_xy( 3, 5 );
            LCD_Write_number(13);
            LCD_Write_number(13);
          
            }
          else
           {
            disbuff[1]=S00/100;
            disbuff[2]=S000/10;
            disbuff[3]=S00 ;
            LCD_set_xy( 3, 4 );
            LCD_Write_number(disbuff[1]);
            LCD_Write_number(12);
            LCD_set_xy( 3, 5 );
            LCD_Write_number(disbuff[2]);
            LCD_Write_number(disbuff[3]);
           }
         }

        void delayms(unsigned int ms)
        {
         unsigned char i=100,j;
         for(;ms;ms--)
         {
          while(--i)
          {
           j=10;
           while(--j);
          }
         }
        }

             void zd0() interrupt 1    //T0中斷用來計數器溢出,超過測距范圍
          {
            flag=1;        //中斷溢出標志
          }

           void  StartModule()            //T1中斷用來掃描數碼管和計800MS啟動模塊
          {
           TX=1;                    //800MS  啟動一次模塊
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           _nop_();
           TX=0;
          }

        void main(void)

            TMOD=0x01;     //設T0為方式1,GATE=1
         TH0=0;
         TL0=0;
         TR0=1;

         Lcd_init();       //設置液晶顯示器
         Clr_Scr();        //清屏 
         Disp_img(logo);   //顯示歡迎使用本產品LOGO
         Delaynms(1000);
         Lcd_init();       //從圖形顯示狀態下返回
            Lcd_Mark2();


         while(1)
         {
          StartModule(); 
                     //計算
          while(!RX);  //當RX為零時等待
          TR0=1;       //開啟計數
          while(RX);   //當RX為1計數并等待
          TR0=0;    //關閉計數
          Conut();
          delayms(80);  //80MS

         }

        關閉窗口
        欧美性色欧美精品视频,99热这里只有精品mp4,日韩高清亚洲日韩精品一区二区,2020国自产拍精品高潮