本帖最后由 3955793141 于 2017-1-8 18:35 编辑
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- sbit Trig1 = P2^0;
- sbit Trig2 = P2^1;
- sbit Echo1 = P3^2;
- sbit Echo2 = P3^3;
- sbit DULA = P2^6;
- sbit WELA = P2^7;
- uchar code SEG7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//êyÂë1ü0-9
- uchar ge1,shi1,bai1,ge2,shi2,bai2,temp,flag,outcomeH,outcomeL,i,uselection,distance_data,time,time1;
- bit succeed_flag;
- void conversion1(uint temp_data);
- void delay_20us();
- void display(uint o);
- void ultrasonic_detect1();
- void ultrasonic_detect2();
- void delay_10us();
- void main(void)
- {
- double s;
- time=0;
- i=0;
- flag=0;
- Trig1=0;
- Trig2=0;
- TMOD=0x12;
- TH0=0;
- TL0=0;
- TH1=0;
- TL1=0;
- TR0=1;
- ET0=1;
- EA=1;
- while(1)
- {
- if(time>=40)
- {
- ultrasonic_detect1();
- time=0;
- }
- if(succeed_flag==1)
- {
- time1=outcomeH;
- time1<<=8;
- time1=time1|outcomeL;
- s=(time1*0.9216)/5.8;
- s=(uint)s;
- conversion1(s);
- succeed_flag=0;
- }
- display(1);
- }
- }
- INTO_() interrupt 0
- {
- TR1=0;
- TR0=1;
- EX0=0;
- outcomeH =TH1;
- outcomeL =TL1;
- succeed_flag=1;
- }
- timer0() interrupt 1
- {
- time++;
- }
- //ía2¿ÖD¶Ï1 P3.3
- INT1_() interrupt 2
- {
- TR1=0;
- TR0=1;
- EX0=0;
- outcomeH =TH1;
- outcomeL =TL1;
- succeed_flag=1;
- }
- void ultrasonic_detect1()
- {
- EA=0;
- TR0=0;
- uselection = 2;
- Trig1=1;
- delay_20us();
- Trig1=0;
- TH1=0;
- TL1=0;
- while(Echo1==0);
- TR1=1;
- succeed_flag=0;
- EX0=1;
- TF1=0;
- EA=1;
- }
- void ultrasonic_detect2()
- {
- EA=0;
- TR0=0;
- Trig2=1;
- uselection = 1;
- delay_20us();
- delay_20us();
- Trig2=0;
- while(Echo2==0);
- succeed_flag=0;
- EX0=1;
- TH1=0;
- TL1=0;
- TL0=0;
- TF1=0;
- TR1=1; //Æô¶ˉ¶¨ê±Æ÷1
- EA=1;
- }
- void conversion1(uint temp_data)
- {
- uchar ge_data,shi_data,bai_data ;
- bai_data=temp_data/100 ;
- shi_data=temp_data/10%10;
- ge_data=temp_data%10;
- bai1=SEG7[bai_data];
- shi1=SEG7[shi_data];
- ge1 =SEG7[ge_data];
- }
- void display(uint o)
- {
- switch(1)
- {
- case 1 :
- DULA = 1 ;P0= bai1 ;DULA = 0 ;P0=0xff;
- WELA = 1 ;P0= 0xFE ;WELA = 0 ;delay_10us();
- DULA = 1 ;P0= shi1 ;DULA = 0 ;P0=0xff;
- WELA = 1 ;P0= 0xFD ;WELA = 0 ;delay_10us();
-
- DULA = 1 ;P0= ge1 ;DULA = 0 ;P0=0xff;
- WELA = 1 ;P0= 0xFB ;WELA = 0 ;delay_10us();
- break;
- default: break;
- }
- }
- void delay_20us()
- { uchar bt ;
- for(bt=0;bt<100;bt++);
- }
- void delay_10us()
- { uchar bt ;
- for(bt=0;bt<25;bt++);
- }
复制代码我写了一个双通道的超声波测距电路,现在只写了单通道的,我在调试的时候发现,只有定时器1的低八位TL1有值,可是TH1没有值,导致只能计算一部分的距离然后又重新机选了,请问这是怎么回事呢? 还有就是为什么LED上会有残影= =。
啊可以了,原来是我定义错了,犯了低级错误~
此帖出自
小平头技术问答
一周热门 更多>