程序

  • 格式:docx
  • 大小:12.97 KB
  • 文档页数:6

#include //器件配置文件

#include

//按键声明

sbit RX=P3^2;

sbit TX=P3^3;

sbit S1=P1^4;

sbit S2=P1^5;

sbit S3=P1^6;

//蜂鸣器

sbit Feng=P2^0;

unsigned int time=0;

unsigned int timer=0;

unsigned char posit=0;

unsigned char pinlv=5;

unsigned long S=0;

unsigned long BJS=50;//报警距离80CM

//模式 0正常模式 1调整

char Mode=0; bit flag=0,flag_BJ;

unsigned char const discode[]

={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0xff/*-*/};

unsigned char const positon[4]={0xfd,0xfb,0xf7,0xfe};

unsigned char disbuff[4] ={0,0,0,0};

unsigned char disbuff_BJ[4] ={0,0,0,0};//报警信息

//延时100ms

void delay(void) //误差 0us

{

unsigned char a,b,c;

for(c=10;c>0;c--)

for(b=38;b>0;b--)

for(a=130;a>0;a--);

}

//按键扫描

void Key_()

{

//+

if(S1==0)

{

delay();

while(S1==0)

{

P1=P1|0x0f;

}

BJS++;

if(BJS==151) {

BJS=0;

}

}

//-

else if(S2==0)

{

delay();

while(S2==0)

{

P1=P1|0x0f;

}

BJS--;

if(BJS==0)

{

BJS=150;

}

}

//功能

else if(S3==0)

{

delay();

while(S3==0)

{

P1=P1|0x0f;

}

Mode++;

if(Mode==2)

{

Mode=0;

}

}

}

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

//扫描数码管

void Display(void)

{

//正常显示

if(Mode==0)

{

P0=0x00;

if(posit==0)//数码管的米标志

{ P0=(discode[disbuff[posit]])|0x80;

}

else

{

P0=discode[disbuff[posit]];

}

P1=positon[posit];

if(++posit>=3)

posit=0;

}

//报警显示

else

{

P0=0x00;

if(posit==0)//数码管的米标志

{

P0=(discode[disbuff_BJ[posit]])|0x80;

}

else if(posit==3)

{

P0=0x76;

}

else

{

P0=discode[disbuff_BJ[posit]];

}

P1=positon[posit];

if(++posit>=4)

posit=0;

}

}

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

//计算

void Conut(void)

{

time=TH0*256+TL0;

TH0=0;

TL0=0;

S=(time*1.7)/100; //算出来是CM

if(Mode==0)

{

if((S>=700)||flag==1) //超出测量范围显示“-”

{ Feng=0;

flag=0;

disbuff[0]=10; //“-”

disbuff[1]=10; //“-”

disbuff[2]=10; //“-”

}

else

{

//距离大于报警距

if(S<=BJS)

{

flag_BJ=1;

}

else

{

flag_BJ=0;

Feng=1;

}

disbuff[0]=S%1000/100;

disbuff[1]=S%1000%100/10;

disbuff[2]=S%1000%10 %10;

}

}

else

{

Feng=1;

flag_BJ=0;

disbuff_BJ[0]=BJS%1000/100;

disbuff_BJ[1]=BJS%1000%100/10;

disbuff_BJ[2]=BJS%1000%10 %10;

}

}

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

//定时器0

void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围

{

flag=1; //中断溢出标志

}

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

//定时器1 void zd3() interrupt 3 //T1中断用来扫描数码管和计800MS启动 模块

{

unsigned int m;

TH1=0xf8;

TL1=0x30;

Key_();

Display();

timer++;

if(flag_BJ==1)

{

m++;

if(m>=(S+10))

{

m=0;

Feng=!Feng;

}

}

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(); //计算

}

}