超声波测距显示和避障
单片机源程序如下:
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit K1=P1^0; //P1.0到P1.4为寻线检测端
- sbit K2=P1^1;
- sbit K3=P1^2;
- sbit K4=P1^3;
- sbit K5=P1^4;
- sbit KIN1=P1^5; //P1.5 P1.6是避障检测端
- sbit KIN2=P1^6;
- sbit out1 = P2^0 ; //P2.0到P2.3是电机驱动输出控制端
- sbit out2 = P2^1 ;
- sbit out3 = P2^2 ;
- sbit out4 = P2^3 ;
- sbit Trig = P3^3; //产生脉冲引脚
- sbit Echo = P3^2; //回波引脚
- sbit beem= P3^7; //蜂鸣器控制引脚
- sbit PWM=P1^7; //舵机pwm//
- sbit SET1=P2^4 ;
- sbit SET2 =P2^5;
- sbit SET3 =P2^6;
- sbit SET4 =P2^7;
- uchar code seg7code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uint distance[4]; //测距接收缓冲区
- uint distance1;
- uchar ge,shi,bai,temp,flag,outcomeH,outcomeL,PDATA; //自定义寄存器
- bit succeed_flag; //测量成功标志
- unsigned long xdata rec_code;
- unsigned long xdata time_us;
- unsigned char xdata rec_cnt;
- unsigned char xdata kbuf;
- uchar sdata,flag; //sdata是红外遥控接收键值变量 flag是启动小车 或停止小车变量
- uint j,a;
- uchar pro;
- bit rec_b;
- bit key_save;
- bit keyp;
- void chaoshengbo();
- void conversion(uint temp_data);
- void delay_20us();
- void delay_ms(uint x);
- void display ();
- void Init() //初始化
- {
- flag=0;
- Trig=0;
-
- TMOD = 0x11; //T/C1采用16位定时器/计数器
- ET1 = 1; //定时器1开中断
- ET0 = 1;
- TH0 = 0x00;
- TL0 = 0x00;
- TH1 = 0xff;
- TL1 = 0xce;
- TR1=0;
- TR0 = 0;
- //定时计数器启动计数
- EX0 = 1; //外部中断0关中断
- PX0 = 1;
- PT1 = 1;
- EA = 1; //CPU开中断
- }
- //--------------------------------------------------
- //-------超声波测距----------------------------
- void chaoshengbo()
- {
- uint distance_data;
- display ();
- EA=0;
- Trig=1;
- delay_20us();
- Trig=0; //产生一个20us的脉冲,在Trig引脚
- while(Echo==0); //等待Echo回波引脚变高电平
- succeed_flag=0; //清测量成功标志
- TH0=0; //定时器1清零
- TL0=0; //定时器1清零
- EX0=1; //打开外部中断
- TR0=1; //启动定时器1
- EA=1;
- display ();
- display ();
- display ();
- display ();
- display ();
- EX0=0; //打开外部中断
- TR0=0;
- if(succeed_flag==1)
- {
- distance_data=outcomeH; //测量结果的高8位
- distance_data<<=8; //放入16位的高8位
- distance_data=distance_data|outcomeL;//与低8位合并成为16位结果数据
- distance_data=(distance_data/25)*43/100+1;
- }
- a=distance_data; //超声波测距 距离
- distance1=a;
- display ();
- display (); display ();
- }
- void delay_us(uint x) //演示程序
- {
- do {
- x--;
- }
- while(x>1);
- }
- void delay_ms(uint x)
- {
- while(x!=0)
- {
- delay_us(500);
- x--;
- }
- }
- void baidong() //舵机转动
- {
- TR1=1;
- pro=30; //90°
- delay_ms(100);
- pro=10; //小于10°
- delay_ms(100);
- pro=50; //大于160°
- delay_ms(100);
- pro=25; //90°
- delay_ms(100);
- TR1=0;
- }
-
- void timer0() interrupt 3//定时0.1ms
- {
- EX0=0;
- TH1=0xff;
- TL1=0xce;
- j++;
- if(j<=pro)
- {
- PWM=1;
- }
- else
- {
- PWM=0;
- }
- if(j==400) //周期20ms
- {
- j=0;
- PWM=~PWM;
- }
- }
- //左转
- void comeleft()
- {
- out1=0;
- out2=1;
- out3=1;
- out4=0;
- delay_ms(40);
- }
- //右转
- void comeright ()
- {
- out1=1;
- out2=0;
- out3=0;
- out4=1;
- delay_ms(40);
- }
- //前进加速;
- void comeon()
- { out2=1;
- delay_us(440);
- out2=0;
- out4=0;
- out1=1;
- out3=1;
- }
- //后退;
- void back()
- { out2=1;
- out4=1;
- out1=0;
- out3=0;
- delay_ms(200);
- }
- void stop() //停止
- {
- out1=0;
- out2=0;
- out3=0;
- out4=0;
- }
-
- //避障
- void shunback()
- { uint DATA1,DATA2,i;
-
- chaoshengbo();
- display ();
-
- if(distance1<12) //当超声波测距距离小于8则
- {
- stop(); //小车停止运动
- beem=0;
- for(i=0;i<200;i++){display ();}
- beem=1;
- for(i=0;i<200;i++){display ();}
- beem=0 ;
- for(i=0;i<200;i++){display ();}
- beem=1;
- EX0=0; //关闭外部中断
- TR0=0;
- TR1=1;
- pro=10; //舵机转动到0°
- delay_ms(50);
- TR1=0;
- PWM=1;
- delay_ms(10);
- chaoshengbo(); //检测0°方向 障碍物距离
- DATA1= distance1;
- for(i=0;i<200;i++){display ();}
- distance1=0;
- EX0=0; //关闭外部中断
- TR0=0;
- TR1=1;
- pro=50; //舵机转动到180°
- delay_ms(50);
- TR1=0;
- PWM=1;
- delay_ms(10);
- chaoshengbo(); //检测180°方向障碍物距离
- DATA2= distance1;
- for(i=0;i<200;i++){display ();}
- distance1=0;
-
- EX0=0; //关闭外部中断
- TR0=0;
- TR1=1;
- pro=25; //舵机返回90°方向
- delay_ms(50);
- TR1=0;
- delay_ms(10);
- if(DATA1>=12 && DATA1>DATA2){comeright ();comeon(); }//当0度方向大于 8cm 并大于180度方向 则右转 前进
- else if (DATA2>=12 && DATA2>=DATA1){comeleft(); comeon(); } //当180度方向大于 8cm 并大于0度方向 则左转 前进
- else if (DATA2<12 && DATA1<8) {back(); comeleft();comeon(); }//当180度方向和0度方向都小于8cm 则小车后退一定距离 左转 前进
- }
- else{ comeon(); }//否则小车直走
- }
- void display ()
- {
-
- P2=P2|0XF0;
- P0=~seg7code[0];
- SET1=0;
- delay_us(20);
- P2=P2|0XF0;
-
- P0=~seg7code[distance1/100];
- SET2=0;
- delay_us(20);
- P2=P2|0XF0;
- P0=~seg7code[(distance1%100)/10];
- SET3=0;
- delay_us(20);
- P2=P2|0XF0;
- P0=~seg7code[distance1%10];
- SET4=0;
- delay_us(20);
- P2=P2|0XF0;
-
- }
- void main(void)
- {
- P1=0XFF;
- P2=0XFF;
- P3=0XFF;
- P0=0XFF;
- Init(); //初始化
- baidong();
- while(1)
- {
- shunback();
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
超声波测距显示并避障.rar
(33.77 KB, 下载次数: 10)
|