原创 at89c2051制作的超声波测距源程序

2009-9-23 10:51 2391 4 4 分类: MCU/ 嵌入式

#include <REG2051.H>

#define k1 P3_4
#define csbout    P3_5                  //超声波发送
#define csbint    P3_7                  //超声波接收
#define csbc="0".034
#define bg  P3_3
unsigned char csbds,opto,digit,buffer[3],xm1,xm2,xm0,key,jpjs;//显示标识
unsigned char convert[10]={0x3F,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//0~9段码
unsigned int s,t,i, xx,j,sj1,sj2,sj3,mqs,sx1;
bit cl;     
                            
void csbcj();
void delay(j);                              //延时函数
void scanLED();                              //显示函数
void timeToBuffer();                  //显示转换函数
void keyscan();
void k1cl();
void k2cl();
void k3cl();
void k4cl();
void offmsd();


void main()                              //主函数
{
     EA="1";                                //开中断
       TMOD="0x11";                        //设定时器0为计数,设定时器1定时
     ET0=1;                              //定时器0中断允许
     ET1=1;                              //定时器1中断允许
     TH0=0x00;
     TL0=0x00;
     TH1=0x9E;
     TL1=0x57;
     csbds="0";
     csbint="1";
     csbout="1";
     cl="0";
     ōpto=0xff;
     jpjs="0";
     sj1=45;
     sj2=200;
     sj3=400;
     k4cl();
     TR1=1;                       
     while(1)
       {
           keyscan();
           if(jpjs<1)
           {
           csbcj();
           if(s>sj3)
           {
           buffer[2]=0x76;     
           buffer[1]=0x76;     
           buffer[0]=0x76;     
           }
           else if(s<sj1)
           {
           buffer[2]=0x40;     
           buffer[1]=0x40;     
           buffer[0]=0x40;
           }
           else timeToBuffer();     
           }
           else timeToBuffer();            //将值转换成LED段码
           offmsd();
             scanLED();                  //显示函数
           if(s<sj2)
           bg="0";
           bg="1";
     }
}


void scanLED()                       //显示功能模块
{
    digit="0x04";
    for( i="0"; i<3; i++)        //3位数显示
    {
        P3=~digit&opto;        //依次显示各位数
        P1=~buffer;        //显示数据送P1口
        delay(20);              //延时处理
        P1=0xff;             //P1口置高电平(关闭)
        if((P3&0x10)==0)      //判断3位是否显示完
           key="0";
        digit>>=1;             //循环右移1位
    }
}

void timeToBuffer()                //转换段码功能模块
{
     xm0=s/100;     
     xm1=(s-100*xm0)/10;
     xm2=s-100*xm0-10*xm1;
     buffer[2]=convert[xm2];     
     buffer[1]=convert[xm1];
     buffer[0]=convert[xm0];
}

void delay(i)                             
{
    while(--i);
}

void timer1int (void)  interrupt 3  using 2
{
     TH1=0x9E;
     TL1=0x57;
     csbds++;
     if(csbds>=40)
     {
           csbds="0";
           cl="1";
     }           
}

void csbcj()
{
     if(cl==1)
     {
               TR1=0;
           TH0=0x00;
           TL0=0x00;
           i="10";
           while(i--)
           {
                 csbout=!csbout;
           }
           TR0=1;                 
               i="mqs";                              //盲区
           while(i--)
           {
           }
           i="0";
           while(csbint)
           {
                 i++;
                 if(i>=2450)                  //上限值
                 csbint="0";
           }
           TR0=0;
           TH1=0x9E;
           TL1=0x57;
           t="TH0";
           t="t"*256+TL0;
           s="t"*csbc/2;
           TR1=1;
           cl="0";
     }
}

void keyscan()                        //健盘处理函数
{
     xx="0";
     if(k1!=1)                              // 判断开关是否按下
     {
           delay(400);                        //延时去抖动
           if(k1!=1)                              // 判断开关是否按下     
           {           
           while(!k1)
           {
                 delay(30);     
                 xx++;
           }
           if(xx>2000)           
           {
                 jpjs++;
                 if(jpjs>4)
                 jpjs="0";
           }
           xx="0";
           switch(jpjs)
           {
           case 1: k1cl();break;           
           case 2: k2cl();break;
           case 3: k3cl();break;
           case 4: k4cl();break;
           }
           }
     }
}
    


void k1cl()
{
sj1=sj1+5;
if(sj1>100)
sj1=30;
s=sj1;
}
void k2cl()
{
sj2=sj2+5;
if(sj2>500)
sj2=40;
s=sj2;
}

void k3cl()
{
sj3=sj3+10;
if(sj3>500)
sj3=100;
s=sj3;
}

void k4cl()
{
sx1=sj1-1;
sx1=sx1/csbc;
mqs=sx1/4.5;
}

void offmsd()                                             
{
    if (buffer[0] == 0x3f)                           
    buffer[0] = 0x00;
}
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
4
关闭 站长推荐上一条 /3 下一条