找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3399|回復: 2
收起左側(cè)

單片機脈寬測量超聲波數(shù)碼管顯示程序

[復制鏈接]
ID:114240 發(fā)表于 2017-8-3 10:36 | 顯示全部樓層 |閱讀模式
超聲波數(shù)碼管顯示
0.png

0.png

單片機脈寬測量源程序如下:
  1. /***********************************************************************************************************/
  2. //hc-sr04 超聲波測距模塊 DEMO 程序
  3. //晶振:11。0592
  4. //程序          QQ: 517535000
  5. //接線:模塊TRIG接 P0.2  ECH0 接P0.1
  6. //數(shù)碼管:共陽數(shù)碼管P1接數(shù)據(jù)口,P2.5 P2.4 P2.3接選通數(shù)碼管
  7. /***********************************************************************************************************/            
  8.                      #include <AT89x51.H>                //器件配置文件
  9.                          #include <intrins.h>
  10.                          #define  RX  P0_1
  11.                          #define  TX  P0_2
  12.                          unsigned int  time=0;
  13.                          unsigned int  timer=0;
  14.                          unsigned char posit=0;
  15.                          unsigned long S=0;
  16.                          bit      flag =0;
  17.                          unsigned char const discode[] ={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF,0xff/*-*/};
  18.                          unsigned char const positon[3]={ 0xdf,0xef,0xf7};
  19.                          unsigned char disbuff[4]           ={ 0,0,0,0,};
  20. /********************************************************/
  21.     void Display(void)                                 //掃描數(shù)碼管
  22.         {
  23.          if(posit==0)
  24.          {P1=(discode[disbuff[posit]])&0x7f;}
  25.          else
  26.          {P1=discode[disbuff[posit]];}
  27.           P2=positon[posit];
  28.           if(++posit>=3)
  29.           posit=0;
  30.         }
  31. /********************************************************/
  32.     void Conut(void)
  33.         {
  34.          time=TH0*256+TL0;
  35.          TH0=0;
  36.          TL0=0;
  37.         
  38.          S=(time*1.7)/100;     //算出來是CM
  39.          if((S>=700)||flag==1) //超出測量范圍顯示“-”
  40.          {         
  41.           flag=0;
  42.           disbuff[0]=10;           //“-”
  43.           disbuff[1]=10;           //“-”
  44.           disbuff[2]=10;           //“-”
  45.          }
  46.          else
  47.          {
  48.           disbuff[0]=S%1000/100;
  49.           disbuff[1]=S%1000%100/10;
  50.           disbuff[2]=S%1000%10 %10;
  51.          }
  52.         }
  53. /********************************************************/
  54.      void zd0() interrupt 1                  //T0中斷用來計數(shù)器溢出,超過測距范圍
  55.   {
  56.     flag=1;                                                         //中斷溢出標志
  57.   }
  58. /********************************************************/
  59.    void  zd3()  interrupt 3                  //T1中斷用來掃描數(shù)碼管和計800MS啟動模塊
  60.   {
  61.          TH1=0xf8;
  62.          TL1=0x30;
  63.          Display();
  64.          timer++;
  65.          if(timer>=400)
  66.          {
  67.           timer=0;
  68.           TX=1;                                        //800MS  啟動一次模塊
  69.           _nop_();
  70.           _nop_();
  71.           _nop_();
  72.           _nop_();
  73.           _nop_();
  74.           _nop_();
  75.           _nop_();
  76.           _nop_();
  77.           _nop_();
  78.           _nop_();
  79.           _nop_();
  80.           _nop_();
  81.           _nop_();
  82.           _nop_();
  83.           _nop_();
  84.           _nop_();
  85.           _nop_();
  86.           _nop_();
  87.           _nop_();
  88.           _nop_();
  89.           _nop_();
  90.           TX=0;
  91.          }
  92.   }
  93. /*********************************************************/

  94.         void  main(  void  )

  95.   {  
  96.     TMOD=0x11;                   //設T0為方式1,GATE=1;
  97.         TH0=0;
  98.         TL0=0;         
  99.         TH1=0xf8;                   //2MS定時
  100.         TL1=0x30;
  101.         ET0=1;             //允許T0中斷
  102. ……………………

  103. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:
超聲波測距數(shù)碼管顯示.rar (74.8 KB, 下載次數(shù): 34)



回復

使用道具 舉報

ID:243894 發(fā)表于 2018-6-5 14:48 | 顯示全部樓層
收下這份大禮
回復

使用道具 舉報

ID:407566 發(fā)表于 2018-11-6 16:12 | 顯示全部樓層
P2.5 P2.4 P2.3接選通數(shù)碼管        這個怎么接?
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

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

快速回復 返回頂部 返回列表