1 //????=8M //MCU=STC10F04XE //P0.0-P0.6????????????? //Trig = P1^0 //Echo = P3^2 #include //????һ??52??׼?ں˵?ͷ?ļ? #define uchar unsigned char //????һ?·???ʹ?? #define uint unsigned int #define ulong unsigned long //*********************************************** /*sfr CLK_DIV = 0x97; //ΪSTC??Ƭ??????,ϵͳʱ?ӷ?Ƶ //ΪSTC??Ƭ????IO?????õ?ַ???? sfr P0M1 = 0X93; sfr P0M0 = 0X94; sfr P1M1 = 0X91; sfr P1M0 = 0X92; sfr P2M1 = 0X95; sfr P2M0 = 0X96;*/ //*********************************************** sbit Trig = P1^0; //???????????? sbit Echo = P3^2; //?ز????? sbit test = P1^1; //?????????? uchar code SEG7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//?????0-9 uint distance[4]; //?????ջ????? uchar ge,shi,bai,temp,flag,outcomeH,outcomeL,i; //?Զ???Ĵ??? bit succeed_flag; //?????ɹ???־ //********???????? void conversion(uint temp_data); void delay_20us(); //void pai_xu(); void main(void) // ?????? { uint distance_data,a,b; uchar CONT_1; /* CLK_DIV=0X03; //ϵͳʱ??Ϊ1/8????pdf-45ҳ?? P0M1 = 0; //??io??????Ϊ??????? P1M1 = 0; P2M1 = 0; P0M0 = 0XFF; P1M0 = 0XFF; P2M0 = 0XFF;*/ i=0; flag=0; test =0; Trig=0; //???????????????????? TMOD=0x11; //??ʱ??0????ʱ??1??16λ??????ʽ TR0=1; //??????ʱ??0 IT0=0; //?ɸߵ?ƽ??͵?ƽ???????ⲿ?ж? ET0=1; //?򿪶?ʱ??0?ж? //ET1=1; //?򿪶?ʱ??1?ж? EX0=0; //?ر??ⲿ?ж? EA=1; //?????ж?0 while(1) //????ѭ?? { EA=0; Trig=1; delay_20us(); Trig=0; //????һ??20us?????壬??Trig???? while(Echo==0); //?ȴ?Echo?ز????ű?ߵ?ƽ succeed_flag=0; //??????ɹ???־ EX0=1; //???ⲿ?ж? TH1=0; //??ʱ??1???? TL1=0; //??ʱ??1???? TF1=0; // TR1=1; //??????ʱ??1 EA=1; while(TH1 < 30);//?ȴ??????Ľ????????65.535???루?????ж?ʵ?֣? TR1=0; //?رն?ʱ??1 EX0=0; //?ر??ⲿ?ж? if(succeed_flag==1) { distance_data=outcomeH; //????????ĸ?8λ distance_data<<=8; //????16λ?ĸ?8λ distance_data=distance_data|outcomeL;//???8λ?ϲ???Ϊ16λ??????? distance_data*=12; //??Ϊ??ʱ??Ĭ??Ϊ12??Ƶ distance_data/=58; //΢??ĵ?λ????58???????? } //Ϊʲô????58???????ף? Y??=??X??*344??/2 // X??=?? 2*Y?ף?/344 ==??X??=0.0058*Y?? ==??????=΢??/58 if(succeed_flag==0) { distance_data=0; //û?лز??????? test = !test; //???ԵƱ仯 } /// distance[i]=distance_data; //??????????????ݷ??뻺???? /// i++; /// if(i==3) /// { /// distance_data=(distance[0]+distance[1]+distance[2]+distance[3])/4; /// pai_xu(); /// distance_data=distance[1]; a=distance_data; if(b==a) CONT_1=0; if(b!=a) CONT_1++; if(CONT_1>=3) { CONT_1=0; b=a; conversion(b); } /// i=0; /// } } } //*************************************************************** //?ⲿ?ж?0???????жϻز???ƽ INTO_() interrupt 0 // ?ⲿ?ж???0?? { outcomeH =TH1; //ȡ????ʱ????ֵ outcomeL =TL1; //ȡ????ʱ????ֵ succeed_flag=1; //???ɹ??????ı?־ EX0=0; //?ر??ⲿ?ж? } //**************************************************************** //??ʱ??0?ж?,??????ʾ timer0() interrupt 1 // ??ʱ??0?ж???1?? { TH0=0xfd; //д?붨ʱ??0??ʼֵ TL0=0x77; switch(flag) {case 0x00:P0=ge; P2=0xfd;flag++;break; case 0x01:P0=shi;P2=0xfe;flag++;break; case 0x02:P0=bai;P2=0xfb;flag=0;break; } } //***************************************************************** /* //??ʱ??1?ж?,??????????????ʱ timer1() interrupt 3 // ??ʱ??0?ж???1?? { TH1=0; TL1=0; } */ //****************************************************************** //??ʾ????ת?????? void conversion(uint temp_data) { uchar ge_data,shi_data,bai_data ; bai_data=temp_data/100 ; temp_data=temp_data%100; //ȡ?????? shi_data=temp_data/10 ; temp_data=temp_data%10; //ȡ?????? ge_data=temp_data; bai_data=SEG7[bai_data]; shi_data=SEG7[shi_data]; ge_data =SEG7[ge_data]; EA=0; bai = bai_data; shi = shi_data; ge = ge_data ; EA=1; } //****************************************************************** void delay_20us() { uchar bt ; for(bt=0;bt<100;bt++); } /* void pai_xu() { uint t; if (distance[0]>distance[1]) {t=distance[0];distance[0]=distance[1];distance[1]=t;} /*????ֵ if(distance[0]>distance[2]) {t=distance[2];distance[2]=distance[0];distance[0]=t;} /*????ֵ if(distance[1]>distance[2]) {t=distance[1];distance[1]=distance[2];distance[2]=t;} /*????ֵ } */