- #include<reg52.h>
- #include<stdio.h>
- #include<stdlib.h>
- #define uint unsigned int
- #define uchar unsigned char
- #define ulong unsigned long
- #define t1n 100
- /*sbit L_on=P1^4;
- sbit L1=P0^1;
- sbit L2=P0^2;
- sbit L3=P0^3;
- sbit L4=P0^4;
- sbit L5=P0^5;
- sbit L6=P0^6; */
- //循迹红外对管信号反馈
- sbit Leftled = P3^2;
- sbit Rightled = P3^4;
- sbit Leftled2 = P3^3;
- sbit Rightled2 = P3^5;
- //超声波管脚定义
- sbit Trig = P2^0; //产生脉冲引脚
- sbit Echo = P2^1; //回波引脚
- //左边轮子
- sbit in1=P0^0;
- sbit in2=P0^1;
- sbit in3=P0^2 ;
- sbit in4=P0^3 ;
- //右边轮子
- sbit in11=P0^4 ;
- sbit in22=P0^5 ;
- sbit in33=P0^6 ;
- sbit in44=P0^7 ;
- sbit Beef = P3^6;
- uint ZKB = 70;
- uchar BK =0;
- uchar z=0,time=0; //计数器
- char Buffer;
- char BufferFunction = 0x63; //默认模式限定蓝牙控制
- char BufferSpeed;
- char BufferDirection;
- bit flag=0; //用于超声波
- bit flag_b=0;
- bit flag1=0;
- ulong t0,count,tc;
- ulong distance;
- ulong C;
- void Bluetooth_Move();
- void BTControl();
- void xunji();
- void bizhang();
- //超声波
- void delay(uint z);
- void delay1(void);
- void DelayMs(uint t);
- void DelayUs(uint t);
- void celiang();
- void zhuanxiang();
- /********************************************************************
- * 名称 : Delay_1ms()
- * 功能 : 延时子程序,延时时间为 1ms * x
- * 输入 : x (延时一毫秒的个数)
- * 输出 : 无
- ***********************************************************************/
- void Delay_1ms(int i)//1ms延时
- {
- int x,j;
- for(j=0;j<i;j++)
- for(x=0;x<=148;x++);
- }
- /********************************************************************
- * 名称 : Com_Int()
- * 功能 : 串口中断子函数
- * 输入 : 无
- * 输出 : 无
- ***********************************************************************/
- //当IO口有数据流入时会自动执行
- void Com_Int(void) interrupt 4
- {
- //ES = 0; //总中断关闭
- if(RI == 1) //当硬件接收到一个数据时,RI会置位串口有发送和接收两种不同的情况
- //(不论是发送还是接收,只要发送或接收完成,都会置位串口中断标志位(发送就置位TI,接收则置位RI),
- //所以=1表示的是两个标志位只要有一个为1(即是两种情况有一种发生)且此时系统允许串口中断,
- //则会转向中断服务程序,在服务程序里判断中断是TI/RI哪个为1触发的,相应的做出处理,如可以把缓冲区的数据读出来。)
- {
- Buffer=SBUF;//SBUF:51串口通信寄存器
- if(0x62<=Buffer&&Buffer<=0x69){
- BufferFunction = Buffer;
- B = 1;
- }
- if(0x30<=Buffer&&Buffer<=0x39) BufferSpeed = Buffer;
- if(0x41<=Buffer&&Buffer<=0x61) BufferDirection = Buffer;
- RI = 0; //关闭串口中断
- }
- //ES = 1;
- }
- /********************************************************************
- * 名称 : Com_Init()
- * 功能 : 串口初始化,晶振11.0592,波特率9600,使串口中断
- * 输入 : 无
- * 输出 : 无
- ***********************************************************************/
- void Com_Init(void)
- {
- TMOD = 0x21; //选择使用8位定时器1
- PCON = 0x00; //是设置波特率不加倍
- SCON = 0x50; //选择8位异步通信方式,波特率可变,由定时器控制;允许接受;发送中断标志、接收中断标志分别为0、0
- TH1 = 0xfd; //设置波特率 9600= 11.0592*1000000/12/(0x100-0xfd)/32
- TL1 = 0xfd;
- TH0 = (65536-100)/256;
- TL0 = (65536-100)/256;
- TR0 = 0; //先不启动定时器0
- TR1 = 1; //启动定时器1
- ES = 1; //开串口中断
- EA = 1; //开总中断
- //超声波管脚
- Trig=0;
- Echo=0;
- }
- //避障超声波定时器
- void timer0()interrupt 1 using 2
- {
- TH0 = (65536-100)/256; //定时器0无法溢出
- TL0 = (65536-100)/256;
- }
- void timer1()interrupt 2 using 2
- {
- TH1 = (65536-100)/256;
- TL1 = (65536-100)/256;
- z++;
- if(z>=100) z=0;
- }
- /**********速度变化*******************/
- void SpeedChoice()
- {
- switch(BufferSpeed){
- case '0': ZKB = 10;break;
- case '1': ZKB = 20;break;
- case '2': ZKB = 30;break;
- case '3': ZKB = 40;break;
- case '4': ZKB = 50;break;
- case '5': ZKB = 60;break;
- case '6': ZKB = 70;break;
- case '7': ZKB = 80;break;
- case '8': ZKB = 90;break;
- case '9': ZKB = 100;break;
- }
- }
- /*******左边轮驱动***************/
- void pwm_out_left_moto(uchar a,uchar b,uchar c,uchar d )
- {
- if(z<a) in1=1;
- else in1=0;
- if(z<b) in2=1;
- else in2=0;
- if(z<c) in3=1;
- else in3=0;
- if(z<d) in4=1;
- else in4=0;
- }
- /*********右边轮驱动*******************/
- void pwm_out_right_moto(uchar a,uchar b,uchar c,uchar d )
- {
- if(z<a) in11=1;
- else in11=0;
- if(z<b) in22=1;
- else in22=0;
- if(z<c) in33=1;
- else in33=0;
- if(z<d) in44=1;
- else in44=0;
- }
- /************超声波********************/
- //*************************启动超生波模块***************************************
- void startmodule()
- {
- count=0;
- Trig=1;
- DelayUs(18);
- Trig=0;
- while(!Echo) // 等待高电平
- {
- count++; //超时检测
- if(count>26000)
- {count=0;
- flag1=1;break;}
- }
- if(!flag1)
- {
- TR0=1; //启动定时器0
- while(Echo); //beep=0;
- TR0=0; //关闭定时器0
- }
- else
- {flag1=0;
- TH0=30000;
- TL0=30000;
- }
- }
- //*******************测距模块*********************************************
- void celiang()
- {
- t0=TH0*256+TL0;
- TH0=0;
- TL0=0;
- distance=(t0*1.7)/100; //算出来是CM
- if((distance>=700)||(flag==1)) //超出测量范围
- {
- flag=0;distance=700;
- }
- }
- void zhuanxiang()
- {
- startmodule();
- celiang();
- if(distance<25)
- {
- if(distance<15)
- {
- pwm_out_left_moto(0,100,0,100); //后退
- pwm_out_right_moto(0,100,0,100);
- delay1();
- pwm_out_left_moto(0,100,0,100); //左转
- pwm_out_right_moto(100,0,100,0);
- delay1();
- }
- else
- {
- pwm_out_left_moto(0,100,0,100); //左转
- pwm_out_right_moto(100,0,100,0);
- delay1();
- }
- }
- else
- pwm_out_left_moto(100,0,100,0);
- pwm_out_right_moto(100,0,100,0);
- }
- /*******************延时函数*********************************************/
- //***************us延时函数
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void DelayUs(uint t)
- {
- while(--t);
- }
- void DelayMs(uint t)
- {
- while(t--) //大致延时mS
- {
- DelayUs(250);
- DelayUs(250);
- }
- }
- void delay1(void)
- {
- DelayMs(60);DelayMs(60);
- }
- /********主函数********************/
- void main()
- {
- // L_on = 0;
- Com_Init();
- while(1)
- {
- SpeedChoice();
- Bluetooth_Move();
- }
- }
- /*****************************/
- void Bluetooth_Move(){
- switch(BufferFunction){
- case 0x62: //遥控
- BTControl();
- break;
- case 0x63: //循迹
- xunji();
- break;
- case 0x64: //红外
- break;
- case 0x65: //金探
- break;
- case 0x66: //测温
- break;
- case 0x67: //测湿
- break;
- case 0x68: //测速
- break;
- case 0x69: //避障
- bizhang();
- break;
- }
- }
- /*************各种小车功能***********************/
- //蓝牙遥控
- void BTControl(){
- switch(BufferDirection){
- case 0x41: pwm_out_left_moto(20,0,20,0);
- pwm_out_right_moto(20,0,20,0);
- break; //前进
- case 0x42: pwm_out_left_moto(0,ZKB,0,ZKB);
- pwm_out_right_moto(0,ZKB,0,ZKB);
- break; //后退
- case 0x43: pwm_out_left_moto(0,80,0,80);
- pwm_out_right_moto(80,0,80,0);
- break; //左上
- case 0x44: pwm_out_left_moto(80,0,80,0);
- pwm_out_right_moto(0,80,0,80);
- break; //右上
- case 0x45: pwm_out_left_moto(80,0,80,0);
- pwm_out_right_moto(0,80,0,80);
- break; //左下
- case 0x46: pwm_out_left_moto(0,80,0,80);
- pwm_out_right_moto(80,0,80,0);
- break; //右下
- case 0x61: pwm_out_left_moto(0,0,0,0);
- pwm_out_right_moto(0,0,0,0);
- break; //手离开屏幕停止
- default : pwm_out_left_moto(0,0,0,0);
- pwm_out_right_moto(0,0,0,0);
- break;
- }
- }
- //循迹
- void xunji(){
- //有信号为0 没有信号为1(碰黑)
- if(Leftled==0&&Rightled==0&&Leftled2==0&&Rightled2==0)
- {
- pwm_out_left_moto(20,0,20,0);
- pwm_out_right_moto(20,0,20,0);
- }
- else
- {
- if(Rightled==1||Rightled2==1) //右边检测到黑线
- {
- if(Leftled==0||Leftled2==0)
- {
- pwm_out_left_moto(100,0,100,0);
- pwm_out_right_moto(0,100,0,100);
- }
- }
- if(Leftled==1||Leftled2==1) //左边检测到黑线
- {
- if(Rightled==0||Rightled==0)
- {
- pwm_out_left_moto(0,100,0,100);
- pwm_out_right_moto(100,0,100,0);
- }
- }
- if(Leftled==1&&Rightled==1&&Leftled2==1&&Rightled2==1) //两边检测到黑线
- {
- pwm_out_left_moto(0,0,0,0);
- pwm_out_right_moto(0,0,0,0);
- }
- }
- }
- void bizhang() //避障
- {
- zhuanxiang();
- }
复制代码
|