智能小车红外避障c语言程序 #include <AT89X52.H> bit RandomFactor = 0 ; bit RandomFactorBuf = 0 ; #include <Timer.H> #define FLeftMotor P0_2 = 1 ; P0_3 = 0 #define BLeftMotor P0_2 = 0 ; P0_3 = 1 #define FRightMotor P0_5 = 1 ; P0_4 = 0 #define BRightMotor P0_5 = 0 ; P0_4 = 1 #define LeftStop P0_2 = 0 ; P0_3 = 0 #define RightStop P0_4 = 0 ; P0_5 = 0 #define CarStop P0_2 = 0 ; P0_3 = 0 ; P0_4 = 0 ; P0_5 = 0 #define RightSenser P0_0 #define LeftSenser P0_1 //******************************************************** // 左转 //******************************************************** void TurnLeft_1() { LeftStop ; FRightMotor ; } //******************************************************** // 快速左转 //******************************************************** void TurnLeft_Fast() { BLeftMotor ; FRightMotor ; } //******************************************************** // 右转 //******************************************************** void TurnRight_1() { RightStop ; FLeftMotor ; } //******************************************************** // 快速右转 //******************************************************** void TurnRight_Fast() { BRightMotor ; FLeftMotor ; } //******************************************************** // 用**的方式进行车头右转 //******************************************************** void BTurnRight_1() { LeftStop ; BRightMotor ; } //******************************************************** // 用**的方式进行车头左转 //******************************************************** void BTurnLeft_1() { RightStop ; BLeftMotor ; } //******************************************************** // 前进 //******************************************************** void FCar() { FLeftMotor ; FRightMotor ; } //******************************************************** // 后退 //******************************************************** void BCar() { BLeftMotor ; BRightMotor ; } //******************************************************** // 壁障程序 //******************************************************** void Obstacle_Avoid(void) { if(RightSenser && LeftSenser&&PWM) { FCar() ; } if((!RightSenser) && LeftSenser&&PWM) { BTurnLeft_1() ; } if(RightSenser && (!LeftSenser)&&PWM) { BTurnRight_1() ; } if(!(RightSenser || LeftSenser)) { RandomFactorBuf = RandomFactor ; while (!(RightSenser || LeftSenser)) { if(RandomFactorBuf) { BTurnLeft_1() ; delay (300) ; } if(!RandomFactorBuf) { BTurnRight_1() ; delay (300) ; } } } if(!PWM) { LeftStop ; RightStop ; }
} //******************************************************** // 跟随程序 //******************************************************** void Follow(void) { if(RightSenser && LeftSenser&&PWM) { LeftStop ; RightStop; } if((!RightSenser) && LeftSenser&&PWM) { TurnRight_1() ; } if(RightSenser && (!LeftSenser)&&PWM) { TurnLeft_1() ; } if(!(RightSenser || LeftSenser)) { FCar() ; } } //******************************************************** //******************************************************** main() { Timer0Setup(500) ; Timer1Setup(50) ; Obstacle_Avoid() ; // Follow() ; }
智能避障小车C51程序 (2010-07-21 13:00:00) 标签: 电子 教育 #include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int #define left_infrare 0 #define right_infrare 1 #define dj_state1 0X5F //前进 #define dj_state2 0X4F //右转 #define dj_state3 0X1F //左转 #define dj_state4 0X0F //后退 #define dj_state5 0XfF //停车 #define light_off 0x0f //关转向灯 #define left_light 0X5F //左转向灯 两个是5f #define right_light 0XaF //右转向灯0xaf,两个是0xbf #define back_light 0XcF //刹车灯即后灯 #define front_light 0x3f //前灯 #define light_on 0xff //开所有灯 #define true 1 #define false 0 #define LCD_Data P0 #define Busy 0x80 //用于检测LCD状态字中的Busy标识 sbit c=P1^2; //转向灯使能端 uchar code talk1[]={"backward"}; uchar code talk2[]={"forward"}; uchar code talk3[]={"Turnleft"}; uchar code talk4[]={"Turn right"}; uchar flage =0x00; sbit ledcs=P1^2; //74H573的片选信号 //sbit left_led=P0^2; //左红外发射管 //sbit right_led=P0^3; //右红外发射管 sbit LCD_RS = P1^5; //LCD定义引脚 sbit LCD_RW = P1^6; // sbit LCD_E = P1^7 ; void Delay5Ms(void) { uint TempCyc = 5552; while(TempCyc--); } //400ms延时 void Delay400Ms(void) {uchar TempCycA = 5; uint TempCycB; while(TempCycA--) { TempCycB=7269; while(TempCycB--); } } //LCD读状态 unsigned char ReadStatusLCD(void) { LCD_Data = 0xFF; LCD_RS = 0; LCD_RW = 1; LCD_E = 0; LCD_E = 0; LCD_E = 1; while (LCD_Data & Busy); //检测忙信号 return(LCD_Data); } //LCD写数据 void WriteDataLCD(unsigned char WDLCD ) { ReadStatusLCD(); //检测忙 LCD_Data = WDLCD; LCD_RS=1; LCD_RW =0; LCD_E = 0; //若晶振速度太高可以在这后加小的延时 LCD_E = 0; //延时 ,为了安全 LCD_E = 0; //延时 LCD_E = 1; } //LCD写指令 void WriteCommandLCD(unsigned char WCLCD,BuysC) { if (BuysC) ReadStatusLCD(); //根据需要检测忙,BuysC为0时忽略忙检测 LCD_Data = WCLCD; LCD_RS= 0; LCD_RW= 0; LCD_E = 0; //延时 ,为了安全 LCD_E = 0; LCD_E = 0; //延时 LCD_E = 1; } void LCDInit(void) //LCD初始化 { Delay400Ms(); LCD_Data = 0; WriteCommandLCD(0x38,0); //三次显示模式设置,不检测忙信号 Delay5Ms(); WriteCommandLCD(0x38,0); Delay5Ms(); WriteCommandLCD(0x38,0); Delay5Ms(); WriteCommandLCD(0x38,1); //显示模式设置,开始要求每次检测忙信号 WriteCommandLCD(0x08,1); //关闭显示 WriteCommandLCD(0x01,1); //显示清屏 WriteCommandLCD(0x06,1); // 显示光标移动设置 WriteCommandLCD(0x0C,1); // 显示开及光标设置 } //按指定位置显示一个字符 void DisplayOneChar(uchar X, uchar Y, uchar DData) { Y &= 0x1; X &= 0xF; //限制X不能大于15,Y不能大于1 if (Y) X |= 0x40; //当要显示第二行时地址码+0x40; X |= 0x80; // 算出LCD的指令码 WriteCommandLCD(X, 0); //这里不检测忙信号,发送地址码 WriteDataLCD(DData); } //按指定位置显示一串字符(只能写一行); void DisplayListChar(uchar X, uchar Y,uchar ListLength, uchar *DData,uchar n) { uchar i; Y &= 0x01; X &= 0x0F; //限制X不能大于15,Y不能大于1 for(i=0;i<ListLength;i++) { if (X <= 0x0F) //X坐标应小于0xF { DisplayOneChar(X, Y, DData); //显示单个字符 if(n==true)Delay400Ms(); X++; } } }
void infrared_ray()interrupt 0 using 3 { uchar i=90; flage=0x01; //接受标志位 while(i--); //减小灵敏度 EX0=0; //关掉中断,等到发射方波后才开启,处于别动 } // 延时子程序 void delay(uint n) { while(--n); } //中断初始化 void Init0(void) { EA=1; IT0=1; }
void seng_wave(uchar timer,bit n)//timer通过载波发射信号的时间,n->左右发射管的选择 { uchar i; P1 |= 0X04; //ledcs=1为74ls573为11脚为高电平时数据直接输出,为低时把数据锁存住,即保持 IE |= 0X01; P0 |=0x0c; //04 for(i=timer;i>0;i--) { if(n)P0^=0x08; // 右发射管通过载波发射信号//00 else P0^=0x04; // 左发射管通过载波发射信号//0c delay(100); //这里控制着灵敏度(控制38khz的方波的多少)和距离 } //timer*delay(x)即为发射管得到的平均电流 P1 &= 0Xfb; IE &= 0Xfe; } //led转向灯指示子程序 void light_control(uchar deng) { ledcs=1; P0 =deng; ledcs=0; //11111011 } //电机和灯光的控制部分 void control(uchar n,uchar dj_state,uchar light) { uchar i; // P1|=0x04; light_control(light); //led转向指示灯 delay(100); P2 =dj_state; //电机的方向控制 WriteCommandLCD(0x01,1); //LCD显示清屏 switch(dj_state) { case dj_state2 :{ DisplayListChar(3,1,10,talk4,false);}break; case dj_state3: { DisplayListChar(3,1,8,talk3,false);}break; case dj_state4: { DisplayListChar(3,1,7,talk1,false); }break; default :break; } for(i=n;i>0;i--) {delay(2000);} P2=dj_state5; //停车 light_control(light_off); //led关闭 WriteCommandLCD(0x01,1); //LCD显示清屏 P2=dj_state1; //前进 if(dj_state1) { P1|=0X04; //ledcs=1; P0=0x0f; P1&=0XFB; delay(100); DisplayListChar(0,0,7,talk2,false); } } void move_car(void) { uchar temp =0x00; //左边红外管发射 seng_wave(1,left_infrare); //向下为中断开启有关闭后,要执行的语句 if(flage==0x01){temp|=0x01;flage=0x00;} //右边红外管发射 delay(30); seng_wave(1,right_infrare); //向下为中断开启有关闭后,要执行的语句 if(flage==0x01){temp|=0x02;flage=0x00;} //左边有障碍物,右转 if(temp==0x01){control(2,dj_state2,left_light); temp =0x00;} //右边有障碍物,左转 else if(temp==0x02) {control(2,dj_state3,right_light ); temp =0x00;} //两个方向都有障碍物,后退,右转 else if(temp==0x03) {control(10,dj_state4,back_light ); control(5,dj_state2,right_light ); temp =0x00;} } void main(void) { Init0(); //中断初始化 P1 |= 0X04; //开锁存器的控制位 P0 = 0xFf; //数据口的清零 P1&=0XFB; //关锁存器的控制位 LCDInit(); //LCD初始化 WriteCommandLCD(0x01,1); //显示清屏 delay(100); P2=dj_state1; DisplayListChar(0,0,8,talk2,false); while(1) { move_car(); //主要控制部分 delay(200000);//延时 } }
|