#include"reg52.h"
#include"intrins.h" //°üº¬_nop_()
typedef unsigned char uchar;
typedef unsigned int uint;
sbit WE1 = P2^0;
sbit WE2 = P2^1;
sbit WE3 = P2^2;
sbit WE4 = P2^3;
sbit trig = P3^7; //·¢ËÍ´¥·¢ÐͺÅÒý½Å
sbit echo = P3^6; //µÈ´ý½ÓÊܻز¨ÐźÅÒý½Å
sbit led1 = P1^0;
uchar code leddata[]=
{
0xC0, //"0"
0xF9, //"1"
0xA4, //"2"
0xB0, //"3"
0x99, //"4"
0x92, //"5"
0x82, //"6"
0xF8, //"7"
0x80, //"8"
0x90, //"9"
0x88, //"A"
0x83, //"B"
0xC6, //"C"
0xA1, //"D"
0x86, //"E"
0x8E, //"F"
0x89, //"H"
0xC7, //"L"
0xC8, //"n"
0xC1, //"u"
0x8C, //"P"
0xA3, //"o"
} ;
uint distance_time;//´æ·Å»Ø²¨Ê±¼ä
uint distance;
uchar qian = 2,bai = 3,shi = 5,ge = 9;
uint num;
bit fill_flag;
/******************************************
º¯Êý£ºstart_signal()
º¯Êý¹¦ÄÜ£º ÒýÆð³¬Éù²¨Ä£¿é·¢Ëͳ¬Éù²¨ÐźÅ
ÊäÈ룺ÎÞ
Êä³ö£ºÎÞ
*******************************************/
void start_signal()
{
trig = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
trig = 0;
}
void count_distance() //¼ÆËã¾àÀë
{
distance_time = TH0 * 256 + TL0; //µÃµ½»Ø²¨Íù·µµÄʱ¼ä
distance = (int)(1.7 * distance_time) / 100 ;//¼ÆËã¾àÀë µ¥Î»cm
qian = distance / 1000;
bai = distance % 1000 / 100;
shi = distance % 1000 % 100 / 10;
ge = distance % 1000 % 100 / 10;
TH0 = 0;
TL0 = 0;led1 = 0;
}
void timeinit() //¶¨Ê±Æ÷³õʼ»¯
{
TMOD = 0x11;//ÉèÖö¨Ê±Æ÷0Ϊ16λ¶¨Ê±Ä£Ê½£¬¶¨Ê±Æ÷1Ϊ16λ¶¨Ê±Ä£Ê½
TH0 = 0;
TL0 = 0;
TH1 = 0xf8;
TL1 = 0x30;
TR1 = 1;
EA = 1; //¿ª×ÜÖжÏ
ET0 = 1; //¿ª¶¨Ê±Æ÷ÖжÏ0
ET1 = 1; //¿ª¶¨Ê±Æ÷ÖжÏ1
}
void delay(uchar z)
{
uchar x,y;
for(x = z;x > 0;x--)
for(y = 110;y > 0;y--);
}
void display(uchar qian,uchar bai,uchar shi,uchar ge)
{
P0 = 0xff;
WE1 = 0;
P0 = leddata[qian];
delay(5);
WE1 = 1;
P0 = 0xff;
WE2 = 0;
P0 = leddata[bai];
delay(5);
WE2 = 1;
P0 = 0xff;
WE3 = 0;
P0 = leddata[shi];
delay(5);
WE3 = 1;
P0 = 0xff;
WE4 = 0;
P0 = leddata[ge];
delay(5);
WE4 = 1;
TR1 = 1;
}
void main()
{
timeinit();//¶¨Ê±Æ÷³õʼ»¯
while(1)
{
while(!echo);//µÈ´ý¸ßµçƽµÄµ½À´
TR0 = 1; //¿ª¶¨Ê±Æ÷0£¬²âÁ¿»Ø²¨µÄÂö¿í
TR1 = 0; //¹Ø±Õ¶¨Ê±Æ÷1
while(echo); //µÈ´ý¸ßµçƽ½áÊø
TR0 = 0; //¼ÆÊý½áÊø£¬¹Ø±Õ¶¨Ê±Æ÷0
}
}
void T0_time() interrupt 1 //¶¨Ê±Æ÷0ÖжϷþÎñº¯Êý
{
fill_flag = 1;
}
void T1_time() interrupt 3 //¶¨Ê±Æ÷1ÖжϷþÎñº¯Êý
{
uchar i;
TH1 = 0xf8;
TL0 = 0x30;
count_distance(); //¼ÆËã¾àÀë
display(qian,bai,shi,ge); //ÏÔʾËù²âµ½µÄ¾àÀë
num++;
if(num >= 400)
{
num = 0;
start_signal();
}
} |