最近在做超声波测距,写完代码后,把买来的模块插到
单片机发现固定一个值不变,求各位大神解答!
#include<reg52.h>
unsigned int i=0;
sbit tring=P2^0;
sbit echo=P2^1;
sbit beep=P1^5;
unsigned int qian,bai,shi,ge,
time,s,time1,time2,flag=0;
unsigned char code DIG_CODE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
bit succeed_flag;
void delay_20us()
{
unsigned int bt ;
for(bt=0;bt<100;bt++);
}
void delay(unsigned int x)
{
unsigned int y;
for(;x>0;x--)
for(y=110;y>0;y--);
}
void INT0_time() interrupt 0 //外部中断
{
time1=TH1;
time2=TL1;
time=time1*256+time2;
succeed_flag=1;
EX0=0;
}
void T1_time() interrupt 3 //计时
{
TH1=0;
TL1=0;
}
void display() //显示
{
P2=0x00;
P0=DIG_CODE[ge];
delay(3);
P2=0x04;
P0=DIG_CODE[shi];
delay(3);
P2=0x08;
P0=DIG_CODE[bai];
delay(3);
P2=0x0c;
P0=DIG_CODE[qian];
delay(3);
}
void main() //主函数
{
flag=0;
tring=0;
TMOD=0x11;
TR0=1;
IT0=0;
ET0=0;
EX0=0;
EA=1;
while(1)
{
EA=0;
tring=1;
delay_20us();
tring=0; //发送一个20us的高电平
while(echo==0); //检测回波信号是否为高
succeed_flag=0;
TH1=0;
TL1=0;
TF1=0;
TR1=1;
while(echo==1);
EA=1;
EX0=1;
IE0=1;
EX0=0;
TR1=0;
if(succeed_flag==1)
{
s=(long)(time*0.17);
qian=s/1000;
bai=s/100%10;
shi=s%100/10;
ge=s%10;
}
display();
}
}
#include"reg51.h"
#include <intrins.h>
bit flag =0;
sbit TX=P2^0;
sbit RX=P2^1;
unsigned int time=0;
unsigned int timer=0;
unsigned char posit=0;
unsigned long S=0;
#define GPIO_DIG P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
unsigned char code DIG_CODE[17]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned int num;
unsigned char disbuff[4]={ 0,0,0,0,};
/*******************************************************************************
*******************************************************************************/
void DigDisplay()
{
unsigned char i;
unsigned int j;
for(i=0;i<8;i++)
{
switch(i)
{
case(0):
LSA=0;LSB=0;LSC=0; break;
case(1):
LSA=1;LSB=0;LSC=0; break;
case(2):
LSA=0;LSB=1;LSC=0; break;
case(3):
LSA=1;LSB=1;LSC=0; break;
case(4):
LSA=0;LSB=0;LSC=1; break;
case(5):
LSA=1;LSB=0;LSC=1; break;
case(6):
LSA=0;LSB=1;LSC=1; break;
case(7):
LSA=1;LSB=1;LSC=1; break;
}
GPIO_DIG=disbuff[i];ë
j=10;
while(j--);
GPIO_DIG=0x00;//ÏûÒþ
}
}
/********************************************************/
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S= (long)(time*0.17);
if((S>=4000)||flag==1)
{
flag=0;
disbuff[0]=0x3f;
disbuff[1]=0x50;
disbuff[2]=0x50;
disbuff[3]=0x79;
}
else
{
disbuff[3]=DIG_CODE[S/1000];
disbuff[2]=DIG_CODE[S%1000/100];
disbuff[1]=DIG_CODE[S%100/10];
disbuff[0]=DIG_CODE[S%10];
}
}
/********************************************************/
void zd0() interrupt 1
{
flag=1;
}
/********************************************************/
void zd3() interrupt 3
{
TH1=0xf8;
TL1=0x30;
DigDisplay();
timer++;
if(timer>=500)
{
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;
TH0=0;
TL0=0;
TH1=0xf8;
TL1=0x30;
ET0=1;
ET1=1;
TR1=1;
EA=1;
TR0=1;
while(1)
{
while(!RX);
TR0=1; //¿ªÆô¼ÆÊý
while(RX);
TR0=0;
Conut();
}
}
一周热门 更多>