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

QQ登錄

只需一步,快速開始

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

51單片機(jī)板上超聲波測(cè)距數(shù)碼管顯示

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:371793 發(fā)表于 2018-7-17 11:51 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
請(qǐng)問我用在STC89C52RC上,是在單片機(jī)板上,是4位單數(shù)碼管,數(shù)碼管一直不亮,請(qǐng)問問題出在哪里呢


#include<reg52.h>
#include<intrins.h>

void delay(unsigned int z);//延時(shí)誤差 0us                       
unsigned int time=0,timer=0;  //定義全局變量時(shí)間,距離
unsigned long S=0;
sbit trig = P2^0; //定義超聲波發(fā),收
sbit echo = P2^1;
unsigned int l_posit=0;
bit flag =0;

unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char disbuff[4]={0xff,0,0,0};

void delay(unsigned int z)
{
  unsigned int i,j;
  for(i=z;i>0;i--)
     for(j=110;j>0;j--);
}

sbit SMG_g=P0^0;
sbit SMG_s=P0^1;
sbit SMG_b=P0^2;
sbit SMG_q=P0^3;

void display()
{
  unsigned char i;
  for(i=0;i<3;i++)  
  {
    switch(l_posit)
    {
     case 0:  //選擇千位數(shù)碼管,關(guān)閉其他位
           SMG_q=0;
           SMG_b=1;
           SMG_s=1;
       SMG_g=1;
       P0=0xff;
       break;
     case 1:
           SMG_q=1;          
           SMG_b=0;
           SMG_s=1;
       SMG_g=1;
       P0=disbuff[2];
       break;
     case 2:
           SMG_q=1;      
           SMG_b=1;
           SMG_s=0;
       SMG_g=1;
       P0=disbuff[1];
           break;
     case 3:
           SMG_q=1;      
           SMG_b=1;
           SMG_s=1;
       SMG_g=0;
       P0=disbuff[0];
           break;
        }
   delay(10);
   P0=0x00;
   if(l_posit>3)
     l_posit=0;
        }
}
   void zd0() interrupt 1                  //T0中斷用來(lái)計(jì)數(shù)器溢出,超過(guò)測(cè)距范圍
  {
    flag=1;                                                         //中斷溢出標(biāo)志
  }
/********************************************************/
   void  zd3()  interrupt 3                  //T1中斷用來(lái)掃描數(shù)碼管和計(jì)800MS啟動(dòng)模塊
  {
         TH1=0xf8;
         TL1=0x30;
         display();
         timer++;
         if(timer>=400)
         {
          timer=0;
          trig=1;                                        //800MS  啟動(dòng)一次模塊
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          trig=0;
         }
  }
/*********************************************************/

void count()
        {
         time=TH0*256+TL0;
         TH0=0;
         TL0=0;
         S= (long)(time*0.17);     //算出來(lái)是CM
         if((S>=4000)||flag==1) //超出測(cè)量范圍顯示“ERR0”
         {         
          flag=0;
          disbuff[0]=10;           //“-”
          disbuff[1]=10;           //“-”
          disbuff[2]=10;           //“-”
          disbuff[3]=10;           //“-”
         }
         else
         {
          disbuff[2]=table[S%1000/100];
          disbuff[1]=table[S%100/10];
          disbuff[0]=table[S%10/1];
         }
        }

void main()
{
        TMOD=0x11;//設(shè)置工作方式
    TH0=0x00;//賦初值
        TL0=0x00;
        TH1=0xf8;  
        TL1=0x30;
    EA=1; //開總中斷
    ET0=1;         //允許T0中斷
        ET1=1;         //允許T1中斷
        TR1=1;   //開啟定時(shí)器1
       
        while(1)
        {
     while(!echo);//當(dāng)ECHO腳為1,開始記時(shí)
     TR0=1;
     while(echo);//當(dāng)ECHO腳為0,關(guān)閉計(jì)時(shí)
     TR0=0;
         count();         
         }

        }


/**************************************************************************************/



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

使用道具 舉報(bào)

沙發(fā)
ID:370265 發(fā)表于 2018-7-17 14:08 | 只看該作者
重下一份吧,反正20號(hào)就放假了
回復(fù)

使用道具 舉報(bào)

板凳
ID:303383 發(fā)表于 2018-7-17 14:23 | 只看該作者
提示: 作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽
回復(fù)

使用道具 舉報(bào)

地板
ID:373362 發(fā)表于 2018-7-17 15:53 | 只看該作者
P0 口接上拉電阻了嗎?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表