找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3611|回复: 0
打印 上一主题 下一主题
收起左侧

51单片机超声波测距+数码管显示

[复制链接]
跳转到指定楼层
楼主
ID:438586 发表于 2018-12-12 21:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

                     #include <AT89x51.H>                //器件配置文件
                         #include <intrins.h>
                         #define  RX  P0_1
                         #define  TX  P0_2
                         unsigned int  time=0;
                         unsigned int  timer=0;
                         unsigned char posit=0;
                         unsigned long S=0;
                         bit      flag =0;
                         unsigned char const discode[] ={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF,0xff/*-*/};
                         unsigned char const positon[3]={ 0xdf,0xef,0xf7};
                         unsigned char disbuff[4]           ={ 0,0,0,0,};
/********************************************************/
    void Display(void)                                 //扫描数码管
        {
         if(posit==0)
         {P1=(discode[disbuff[posit]])&0x7f;}
         else
         {P1=discode[disbuff[posit]];}
          P2=positon[posit];
          if(++posit>=3)
          posit=0;
        }
/********************************************************/
    void Conut(void)
        {
         time=TH0*256+TL0;
         TH0=0;
         TL0=0;
       
         S=(time*1.7)/100;     //算出来是CM
         if((S>=700)||flag==1) //超出测量范围显示“-”
         {         
          flag=0;
          disbuff[0]=10;           //“-”
          disbuff[1]=10;           //“-”
          disbuff[2]=10;           //“-”
         }
         else
         {
          disbuff[0]=S%1000/100;
          disbuff[1]=S%1000%100/10;
          disbuff[2]=S%1000%10 %10;
         }
        }
/********************************************************/
     void zd0() interrupt 1                  //T0中断用来计数器溢出,超过测距范围
  {
    flag=1;                                                         //中断溢出标志
  }
/********************************************************/
   void  zd3()  interrupt 3                  //T1中断用来扫描数码管和计800MS启动模块
  {
         TH1=0xf8;
         TL1=0x30;
         Display();
         timer++;
         if(timer>=400)
         {
          timer=0;
          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=0x11;                   //设T0为方式1,GATE=1;
        TH0=0;
        TL0=0;         
        TH1=0xf8;                   //2MS定时
        TL1=0x30;
        ET0=1;             //允许T0中断
        ET1=1;                           //允许T1中断
        TR1=1;                           //开启定时器
        EA=1;                           //开启总中断

        while(1)
        {
         while(!RX);                //当RX为零时等待
         TR0=1;                            //开启计数
         while(RX);                        //当RX为1计数并等待
         TR0=0;                                //关闭计数
     Conut();                        //计算
        }

  }


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表