谁能告诉我我自己编的51单片机程序为什么就是点不亮数码管呢,就那数码管部分感觉没效果似的
是超声波测距电路,超声波模块用的是US-100,,电路是我自己连的,其他部分都有反应,,就数码管部分没反应,数码管是三个共阳极数码管,用P0(段选)和P2(位选)口控制,...
是超声波测距电路,超声波模块用的是US-100,,电路是我自己连的,其他部分都有反应,,就数码管部分没反应,数码管是三个共阳极数码管,用P0(段选)和P2(位选)口控制,没加锁存器的那种。求大神指导,,感觉没作用的地方已经在程序中标出了
#include<reg52.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
void zhishideng(uint N);
void Delayms(uint xms);
void delay_20us(void );
unsigned char code smg[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
/*unsigned char code smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00};*/
uint distance,time,tl,th, M,a1;
sbit tr=P1^0;
sbit ec=P1^1;
sbit LED=P1^7;
sbit buzzer=P1^6;
void delay_20us(void )
{
uchar a ;
for(a=0;a<100;a++);
}
void delay(uint a2 )
{
while(a2--);
}
//......................................................
void zhishideng(uint N)
{
LED=0;
Delayms(N*2); //调用延时程序,修改括号里面的值可以调整延时时间
LED=1;
Delayms(N*2); // 调用延时程序
}
void Delayms(uint xms)
{
uint i, j;
for (i=xms;i>0;i--) //xms即延时约xms毫秒
for (j=110;j>0;j--);
}
//...................................................
/*void display(void )
{
P2=0x00;
P0=smg[distance/100];
P2=0x01; //位选
P0=0xff;
Delayms(20);
P2=0x00;
P0=smg[distance%100/10];
P2=0x02;
P0=0xff;
Delayms(20);
P2=0x00;
P0=smg[distance%10];
P2=0x04;
P0=0xff;
Delayms(20);
}*/
void main(void )
{
TMOD=0X10;
while(1)
{
tr=1; //超声波输入端
delay_20us(); //延时20us
tr=0; //产生一个20us的脉冲
while(ec==0); //等待Echo回波引脚变高电平,回高电平则下边计时器T1开始计时
TH1=0; //定时器1清零
TL1=0; //定时器1清零
TF1=0; //计数溢出标志
TR1=1; //启动定时器1
while(ec==1);//高电平时一直在这卡住,低电平时结束计时立马计算距离
th=TH1;
tl=TL1;
TL1=0;
TH1=0;
TR1=0; //关闭定时器1
time=th*256+tl; //us
distance=time*0.017; //厘米 */还是用0.034/2=0.017
//.......从这开始感觉没作用.............从这开始感觉没作用.................................
if(distance>=10000)
{P0=0xff;
buzzer=0;
Delayms(1000);}
else if(distance<10000){
P2=0x00;
P0=smg[distance/1000];
P2=0x01; //位选
delay(50 );
P0=0xff;
delay(50 );
P2=0x00;
P0=smg[distance%1000/100];
P2=0x02; //位选
delay(50 );
P0=0xff;
delay(50 );
P2=0x00;
P0=smg[distance%1000%100/10];
P2=0x04;
delay(50 );
P0=0xff;
delay(50 );
P2=0x00;
P0=smg[distance%1000%100%10];
P2=0x08;
delay(50 );
P0=0xff;
delay(50 );
//....感觉没作用的部分到这结束...........................................
if(distance<30)//蜂鸣器警报
{
buzzer=0;
Delayms(100);
buzzer=1;
Delayms(100);
buzzer=0;
Delayms(100);
buzzer=1;
Delayms(100);
}
else buzzer=1;
zhishideng( distance);
}
}
} 展开
#include<reg52.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
void zhishideng(uint N);
void Delayms(uint xms);
void delay_20us(void );
unsigned char code smg[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
/*unsigned char code smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00};*/
uint distance,time,tl,th, M,a1;
sbit tr=P1^0;
sbit ec=P1^1;
sbit LED=P1^7;
sbit buzzer=P1^6;
void delay_20us(void )
{
uchar a ;
for(a=0;a<100;a++);
}
void delay(uint a2 )
{
while(a2--);
}
//......................................................
void zhishideng(uint N)
{
LED=0;
Delayms(N*2); //调用延时程序,修改括号里面的值可以调整延时时间
LED=1;
Delayms(N*2); // 调用延时程序
}
void Delayms(uint xms)
{
uint i, j;
for (i=xms;i>0;i--) //xms即延时约xms毫秒
for (j=110;j>0;j--);
}
//...................................................
/*void display(void )
{
P2=0x00;
P0=smg[distance/100];
P2=0x01; //位选
P0=0xff;
Delayms(20);
P2=0x00;
P0=smg[distance%100/10];
P2=0x02;
P0=0xff;
Delayms(20);
P2=0x00;
P0=smg[distance%10];
P2=0x04;
P0=0xff;
Delayms(20);
}*/
void main(void )
{
TMOD=0X10;
while(1)
{
tr=1; //超声波输入端
delay_20us(); //延时20us
tr=0; //产生一个20us的脉冲
while(ec==0); //等待Echo回波引脚变高电平,回高电平则下边计时器T1开始计时
TH1=0; //定时器1清零
TL1=0; //定时器1清零
TF1=0; //计数溢出标志
TR1=1; //启动定时器1
while(ec==1);//高电平时一直在这卡住,低电平时结束计时立马计算距离
th=TH1;
tl=TL1;
TL1=0;
TH1=0;
TR1=0; //关闭定时器1
time=th*256+tl; //us
distance=time*0.017; //厘米 */还是用0.034/2=0.017
//.......从这开始感觉没作用.............从这开始感觉没作用.................................
if(distance>=10000)
{P0=0xff;
buzzer=0;
Delayms(1000);}
else if(distance<10000){
P2=0x00;
P0=smg[distance/1000];
P2=0x01; //位选
delay(50 );
P0=0xff;
delay(50 );
P2=0x00;
P0=smg[distance%1000/100];
P2=0x02; //位选
delay(50 );
P0=0xff;
delay(50 );
P2=0x00;
P0=smg[distance%1000%100/10];
P2=0x04;
delay(50 );
P0=0xff;
delay(50 );
P2=0x00;
P0=smg[distance%1000%100%10];
P2=0x08;
delay(50 );
P0=0xff;
delay(50 );
//....感觉没作用的部分到这结束...........................................
if(distance<30)//蜂鸣器警报
{
buzzer=0;
Delayms(100);
buzzer=1;
Delayms(100);
buzzer=0;
Delayms(100);
buzzer=1;
Delayms(100);
}
else buzzer=1;
zhishideng( distance);
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |