51单片机控制HC-SR04超声波测距离用数码管显示,电路很简单,程序下载后数码管显示一下四个1就再也不亮了
#include<reg51.h>#include<intrins.h>#defineucharunsignedchar#defineuintunsignedint#de...
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uchar time1; uchar time3; bit flag;
sbit TR=P1^0; sbit EC=P1^1;
sbit DA=P2^2; sbit DB=P2^3; sbit DC=P2^4;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void rupt1() interrupt 1
{
TH0=0xd8;TL0=0xf0;
time1++;
if(time1==100)
{
time1=0;flag=1;
}
}
void rupt3() interrupt 3
{
TH1=0x00;TL1=0x00;
time3++;
}
void init()
{
TR=0;EC=0;
TMOD=0x11;
TH0=0xd8;TL0=0xf0;
TH1=0x00;TL1=0x00;
EA=1;ET0=1;ET1=1;
TR0=1;TR1=0;
}
uint change()
{
ulong va; float val; uint value;
va=(time3*65536+TH1*256+TL1);
val=va*0.034;
value=(uint)val;
return value;
}
void disp(uint num)
{
uchar a,i,j;
for(i=0;i<4;i++)
{
switch(i)
{
case 0:DA=0;DB=0;DC=0;a=num/1000; break;
case 1:DA=1;DB=0;DC=0;a=num/100%10;break;
case 2:DA=0;DB=1;DC=0;a=num/10%10; break;
case 3:DA=1;DB=1;DC=0;a=num%10; break;
}
P0=table[1];
j=50;while(j--);
P0=0x00;
}
}
void main()
{
uint value;
init();
while(1)
{
if(flag==1)
{
TR=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
TR=0;
while(EC==0);
TR1=1;
while(EC==1);
TR1=0;
value=change();
time3=0;TH1=0;TL1=0;flag=0;
}
disp(value);
}
} 展开
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uchar time1; uchar time3; bit flag;
sbit TR=P1^0; sbit EC=P1^1;
sbit DA=P2^2; sbit DB=P2^3; sbit DC=P2^4;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void rupt1() interrupt 1
{
TH0=0xd8;TL0=0xf0;
time1++;
if(time1==100)
{
time1=0;flag=1;
}
}
void rupt3() interrupt 3
{
TH1=0x00;TL1=0x00;
time3++;
}
void init()
{
TR=0;EC=0;
TMOD=0x11;
TH0=0xd8;TL0=0xf0;
TH1=0x00;TL1=0x00;
EA=1;ET0=1;ET1=1;
TR0=1;TR1=0;
}
uint change()
{
ulong va; float val; uint value;
va=(time3*65536+TH1*256+TL1);
val=va*0.034;
value=(uint)val;
return value;
}
void disp(uint num)
{
uchar a,i,j;
for(i=0;i<4;i++)
{
switch(i)
{
case 0:DA=0;DB=0;DC=0;a=num/1000; break;
case 1:DA=1;DB=0;DC=0;a=num/100%10;break;
case 2:DA=0;DB=1;DC=0;a=num/10%10; break;
case 3:DA=1;DB=1;DC=0;a=num%10; break;
}
P0=table[1];
j=50;while(j--);
P0=0x00;
}
}
void main()
{
uint value;
init();
while(1)
{
if(flag==1)
{
TR=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
TR=0;
while(EC==0);
TR1=1;
while(EC==1);
TR1=0;
value=change();
time3=0;TH1=0;TL1=0;flag=0;
}
disp(value);
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询