#include "STC15F2K60S2.h"
#include "INTRINS.H"
unsigned char code table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
sbit WEI1=P1^0;
sbit WEI2=P1^1;
sbit WEI3=P1^2;
sbit WEI4=P1^3;
sbit trig=P3^2;
sbit echo=P3^3;
int haomi=0,i,j,k;
void Delay12us() //@11.0592MHz
{
unsigned char i;
i = 28;
while (--i);
}
void Timer0Init(void) //6微秒@18.432MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xBE; //设置定时初值
// TH0 = 0xFF; //设置定时初值
// TF0 = 0; //清除TF0标志
//TR0 = 1; //定时器0开始计时
}
void Delay3us() //@18.432MHz
{
unsigned char i;
_nop_();
i = 11;
while (--i);
}
/*void INT1_init()
{
EX1=1;
IT1=1;
EA=1;
}
void xiajiangyan() interrupt 2;
{
TR0=0;
i=haomi/1000;
j=haomi%1000/10;
k=haomi%10;
WEI1=1;
WEI2=0;
WEI3=1;
WEI4=1;
P0=table[i];
Delay3us();
WEI1=1;
WEI2=1;
WEI3=0;
WEI4=1;
P0=table[j];
Delay3us();
WEI1=1;
WEI2=1;
WEI3=1;
WEI4=0;
P0=table[k];
Delay3us();
}*/
void Delay3ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 33;
j = 66;
do
{
while (--j);
} while (--i);
}
void Delay50us() //@18.432MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 1;
j = 226;
do
{
while (--j);
} while (--i);
}
void main()
{
Timer0Init();
//INT1_init();
TMOD=0X01;
//TR0=1;
ET0=1;
EA=1;
P0M1=0x00;
P0M0=0xFF;
while(1)
{
trig=0;
//echo=0;
trig=1;
Delay12us();
trig=0;
while(!(echo==1));
TR0 = 1;
while(!(echo==0));
TR0 = 0;
haomi=haomi/10;
i=haomi/100;
j=haomi%100/10;
k=haomi%10;
WEI1=1;
WEI2=1;
WEI3=0;
WEI4=1;
P0=table[i];
Delay3ms();
Delay3ms();
WEI1=1;
WEI2=0;
WEI3=1;
WEI4=1;
P0=table[j];
Delay3ms();
Delay3ms();
WEI1=1;
WEI2=1;
WEI3=1;
WEI4=0;
P0=table[k];
//Delay50us();
haomi=0;
}
}
void Timer0()interrupt 1
{
TL0 = 0x91; //设置定时初值
TH0 = 0xFF; //设置定时初值
haomi++;
} |