- sbit p30=P3^0;
- sbit p20=P2^0;
- sbit p32=P3^2;
- uint n;
- //红外波
- void delay(uint t, uchar i)
- { for(;t>0;t--)
- { for(;i>0;i--); }
- }
- void time0() interrupt 1
- {n++;
- if(n>=308)
- { p30=0;
- if(n==616)
- n=0;
- }
- else p30=~p30;
- }
- void inter1() interrupt 2
- {p20=0;
- delay(8000,125);//0.5S
- }
- void main()
- {
- TMOD=0X02;TH0=0XF3;TL0=0XF3;
- ET0=1;TR0=1;
- EX1=1;IT1=1;
- PX1=1;
- EA=1;
- while(1)
- {p20=1;p30=0;
- }
- }
- P31=~P31;
- }
- void Delay_1ms(uint j)
- { uchar i=0;
- for(;j>0;j--)
- for(i=0;i<125;i++);
- }
- void fashong()
- { uchar k;
- ET0=1;TR0=1;
- Delay_1ms(3);
- for(k=0;k<jianhao;k++)
- {ET0=0;TR0=0;P31=0;
- Delay_1ms(1);
- ET0=1;TR0=1;
- Delay_1ms(1);
- }
- ET0=0;TR0=0;P31=0;
- Delay_1ms(3);
- }
- void key()
- {if(p32==0)
- {jianhao=1;flag=1;}
- if(p33==0)
- {jianhao=2;flag=1;}
- if(p34==0)
- {jianhao=3;flag=1;}
- if(p35==0)
- {jianhao=4;flag=1;}
- }
- void main()
- {
- TMOD=0X02;TH0=0XF3;TL0=0XF3;EA=1;P31=0;jianhao=0;
- while(1)
- {//p20=1;p21=1;p22=1;p23=1;
- // p24=0;p25=0;p26=0;p27=0;
- p32=1;p33=1;p34=1;p35=1;
- // P1=a[jianhao];
- key();
- if(flag)
- { p27=0;
- fashong();
- flag=0;}
- p27=1;
- }
- }
- //角度测量
- #include"reg51.h"
- #define uchar unsigned char
- #define uint unsigned int
- uchar code seg[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar code segnode[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
- #define digport P0
- #define dataport P1
- #define wordport P2
- #define r 45
- sbit eoc= P3^3;
- sbit clk= P3^4;
- sbit start= P3^6;
- sbit oe= P3^7;
- bit flag;
- uchar a[4]={0xff,0xff,0xff,0xff};//黑屏
- uchar dig=0x01;
- void delay(uint t)
- {for(;t<0;t--);}
- /*void display()
- {
- uchar i;
- wordport=0xff;
- for(i=0;i<4;i++)
- {
-
- wordport=a[i];
- digport=dig<<1;
- delay(300);
- }
- }*/
- void Display(void)
- {
- uchar i,k,select=0x01;
- for(i=0;i<=5;i++)
- { digport=select;
- wordport=a[i];
- select<<=1;
- for(k=0;k<=100;k++)//改
- {;}
- }
- }
- void time0() interrupt 1
- { clk=~clk; }
- void chuli()
- {
- unsigned long int da;
- //eoc=0;
- start=1;
- oe=1;
- da=dataport;
- delay(10);
- oe=0;
- // da=da*196;
- if(da<127)a[3]=0xbf;//128改127//127.5=127
- if(da>=127)a[3]=0xff;
- // if(da<5000||da>45000){da=0;}
-
- if(a[3]==0xff) {da=da-127; da=da*1961;} //}if(da>49000)da=50000;}
- if(a[3]==0xbf) {da=128-da; da=da*1961;}//128改127
-
- da=da*r;
- if(da>9000000)da=9000000;
- a[0]=seg[da/10000%10];
- if((da/1000%10)>5)
- a[0]=seg[da/10000%10+1];
- a[1]=segnode[da/100000%10];
- a[2]=seg[da/1000000];
- start=0;
- delay(10);
- }
- void main(void)
- { uchar k;
- delay(30);
- // IT1=1;EX1=1;
- TMOD=0x02;TH0=253;TL0=253;
- TR0=1;ET0=1;PT0=1;
- EA=1;
- //eoc=0;
- start=0;
- start=1;
- start=0;
- for(;;)
- {
- if(eoc) chuli();
- for(k=0;k<100;k++)
- { Display();}
- }
-
- }
复制代码
可以测距,测角度
|