/*hc-sr04超声波模块+11.0592mhz晶振+stc89c52rc单片机+四位数码管*/
#include <reg51.h>
#include<intrins.h>
unsigned char a,b,c,d;
float num=0.0;
sbit du=P2^6;
sbit we= P2^7;
sbit send=P2^1;
sbit receive=P2^2;
unsigned char duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char wei []={0xfe,0xfd,0xfb,0xf7,0x2f,0x1f};
void delay_10us()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void delay_200ms(void)
{
unsigned char i,j,k;
for(i=5;i>0;i--)
for(j=132;j>0;j--)
for(k=150;k>0;k--);
}
void main ()
{
unsigned int timeh,timel;
send=0;//先置零
while(1)
{
receive=0;//置零
EA=0;
send=1;
delay_10us();
delay_10us();//延时20us
send=0;
while(receive==0)//等待高电平
{};
TMOD=0X01;//设置定时器0的工作方式
TH0=0;//赋初值
TL0=0;
EA=1;///打开总中断开关
ET0=1;//打开定时器开关
TR0=1;
while(receive)//等待低电平
{
}
timeh=TH1;//取出定时器的值
timel=TL1;
TR0=0;
num=(TH0*255+TL0)/100.0*340/2;
a=(unsigned int )num%10;//算清各位十位百位
b=(unsigned int)num%100/10;
c=(unsigned int )num/100%10;
d=(unsigned int)num/10;
P0=0xff;//数码管显示输出
we=1;
P0=0xfe;
we=0;
P0=0x00;
du=1;
P0=duan[a];
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
P0=0x00;
du=0;
P0=0xff;
we=1;
P0=0xfd;
we=0;
P0=0x00;
du=1;
P0=duan;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
P0=0x00;
du=0;
P0=0xff;
we=1;
P0=0xfb;
we=0;
P0=0x00;
du=1;
P0=duan[c];
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
P0=0x00;
du=0;
P0=0xff;
we=1;
P0=0xf7;
we=0;
P0=0x00;
du=1;
P0=duan[d];
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
delay_200ms;
P0=0x00;
du=0;
}
}
void timer0 ()interrupt 1
{
TH0=0;
TL0=0;
}
/*四位数码管要消影+注意定时器和外部中断的使用*/
|