硬件电路没有问题,程序出现了问题,数码管显示为0 天使了一下午也没有解决 望大神们解答!!!!
- #include<reg51.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- u8 time,distance;
- bit flag=0; //中断溢出标志
- u8 pros[3];
- void delay(u16 i)
- {
- while(i--);
- }
- void delay20us(void) //误差 -0.46875us
- {
- unsigned char a,b;
- for(b=3;b>0;b--)
- for(a=1;a>0;a--);
- }
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- sbit TRIG=P2^5;
- sbit ECHO=P2^6;
- void Usair() //中断
- {
- TMOD=0X01; //定时器0
- TH0=0;
- TL0=1;
- EA=1;
- TR0=1;
- }
- void Count() //计算距离
- {
- time=TH0*256+TL0;
- TH0=0;
- TL0=0;
- distance=(time*1.87)/100;
- }
- void Digplay() //数码管动态扫描
- {
- u8 i;
- 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;
-
- }
- P1=pros[i];
- delay(100);
- P1=0X00;
- }
- }
- void KeyDown() //数码管显示
- {
- if(flag==1)
- {
- flag=0;
- pros[0]=0x40;
- pros[1]=0x40;
- }
- pros[0]=smgduan[distance/10];
- pros[1]=smgduan[distance%10];
- }
- void star_Ch() //打开超声波测距模块
- {
- TRIG=1;
- delay20us();
- TRIG=0;
- }
- void main()
- {
- Usair();
- while(1)
- {
- star_Ch();
- while(!ECHO);
- TR0=1;
- while(ECHO)
- TR0=0;
- delay(1000);
- Digplay();
- KeyDown();
- }
- }
- void Init() interrupt 1
- {
- TH0=0;
- TL0=0;
- flag=1;
- }
复制代码
|