红外避障小车c语言程序

合集下载

嵌入式课程设计--红外遥控小车程序

嵌入式课程设计--红外遥控小车程序

#include <reg52.h>#include <intrins.h>#define uchar unsigned char#define uint unsigned intsbit IRIN = P3^3;sbit P1_0=P1^0; //数码管动态扫描接口sbit P1_1=P1^1;sbit P1_5=P1^5; //蜂鸣器报警接口sbit P2_0=P2^0; //控制两个直流电机的控制端口sbit P2_1=P2^1;sbit P2_2=P2^2;sbit P2_3=P2^3;sbit P2_4=P2^4;sbit P2_5=P2^5;bit flag; //红外解码判断标志位,0有效uchar IR_buf[4]={0x00,0x00,0x00,0x00};//IR_buf[0]、IR_buf[1]为用户码低位、用户码高位接收缓冲区// IR_buf[2]、IR_buf[3]为键数据码和键数据码反码接收缓冲区uchar disp_buf[4]={0x00,0x00,0x40,0x00}; //显示缓冲单元ucharseg_data[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x 8e};//0~F符的显示码(字形码)void delay(uchar x); //延时函数void Delay_ms(uint xms); //延时void init();void car_forward(); //小车前进函数void car_back(); //小车后退函数void car_Leftturn(); //小车左转弯函数void car_Rightturn(); //小车右转弯函数void car_stop(); //小车停止函数void Display(uint m); //数码管显示函数void aralm();void car_jiaspeed();void car_jianspeed();/********以下是主函数********/void main(){init();while(1){Display(disp_buf[2]);switch(disp_buf[2]){case 0x46: car_forward();break;case 0x15: car_back();break;case 0x44: car_Leftturn();break;case 0x43: car_Rightturn();break;case 0x40: car_stop();break;case 0x47:car_jiaspeed();break;case 0x45:car_jianspeed();break;default: aralm();break;}}}void init(){EA=1;EX1=1; //允许总中断中断,使能INT1 外部中断IT1 = 1; //触发方式为脉冲负边沿触发IRIN=1; //遥控输入脚置1P2_0=0;P2_1=0;P2_2=0;P2_3=0;P2_4=1;P2_5=1;}void car_jiaspeed(){P2_4=1;P2_5=1;}void car_jianspeed(){while(1){P2_4=1;P2_5=0;Delay_ms(200);P2_4=0;P2_5=1;Delay_ms(200);if(disp_buf[2]!=0x45){break;}}}/********以下是0.14ms的x倍延时函数********/void delay(uchar x) //延时x*0.14ms{uchar i;while(x--)for (i = 0; i<13; i++);}/********以下是延时函数********/void Delay_ms(uint xms){uint i,j;for(i=xms;i>0;i--) //i=xms即延时约xms毫秒for(j=110;j>0;j--);}/********以下是显示函数********/void Display(uint m){uint a,b;a=m;b=m;a=a>>4;b=b&0x0f;P1_0=0;P1_1=1;P0=seg_data[a];Delay_ms(5);P1_0=1;P1_1=0;P0=seg_data[b];Delay_ms(5);}void car_forward() {P2_4=1;P2_5=1;P2_0=1;P2_1=0;P2_2=1;P2_3=0;}void car_back() {P2_4=1;P2_5=1;P2_0=0;P2_1=1;P2_2=0;P2_3=1;}void car_Leftturn() {P2_0=1;P2_1=0;P2_2=0;P2_3=0;}void car_Rightturn(){P2_0=0;P2_1=0;P2_2=1;P2_3=0;}void car_stop(){P2_0=0;P2_1=0;P2_2=0;P2_3=0;}void aralm(){P1_5=0;Delay_ms(500);P1_5=1;Delay_ms(500);}/********以下是外中断1函数********/void IR_decode() interrupt 2{uchar i,j,k,count=0;EX1 = 0; //暂时关闭外中断1中断请求delay(20); //延时20*0.14=2.8msif (IRIN==1) //等待IRIN低电平出现{EX1 =1; //开外中断0return; //中断返回}while (!IRIN) delay(1); //等待IRIN变为高电平,跳过9ms的低电平引导码for (j=0;j<4;j++) //收集四组数据,即用户码低位、用户码高位、键值数据码和键值数码反码{for (k=0;k<8;k++) //每组数据有8位{while (IRIN) //等待IRIN变为低电平,跳过4.5ms的高电平引导码信号。

避障小车程序

避障小车程序
go();
else
{
back();
delay_1ms(400);
right_s();
delay_1ms(300);
}
}
void main()
{
send_wave(); //测距离函数
smg_display(); //处理距离显示函数
{
TR0 = 0;
flag_csb_juli = 2;
distance = 888;
flag_hc_value = 0;
break ;
}
else
{
flag_csb_juli = 1;
}
}
if(flag_csb_juli == 1)
for(i=0;i<q;i++)
for(j=0;j<120;j++);
}
/***********************处理距离函数****************************/
void smg_display()
{
dis_smg[0] = smg_du[distance % 10];
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
//数码管段选定义 0 1 2 3 4 5 6 7 8 9
smg_w dis_smg[i]; //段选
}
void delay()
{
_nop_(); //执行一条_nop_()指令就是1us

智能小车_走黑线_避障_物体跟随_红外遥控_C程序

智能小车_走黑线_避障_物体跟随_红外遥控_C程序
bit flag_bizhang=0;
bit flag_gensui=0;
bit flag_yaokong=0;
void delay(unsigned int n)
{
unsigned char i, j,k;
for(k=0;k<=n;k++)
{
_nop_();
_nop_();
i = 5;
j = 6 ;
do
{
while (--j);
} while (--i);
}
}
void beep(void)
{
unsigned char i;
for(i=0;i<3;i++)
{
BUZZ=~BUZZ;
delay(10);
}
BUZZ=1;
}
void gogogo(void)
{
IN1=1;
IN2=0;
IN3=1;
IN4=0;
}
/*遥控器按键设置*/
if(k5==0)
{
while(!k5);
flag_heixian=0;
flag_bizhang=0;
flag_gensui=0;
flag_yaokong=1;
}
}
void main(void)
{
flag_heixian=0;
flag_bizhang=0;
flag_gensui=0;
sbit d=P0^6;//对应D1D
sbit a=P0^5;//对应D2A
sbit c=P0^4;//对应D3C
/*按键功能定义*/
sbit k2=P3^0;//走黑线

基于51单片机的红外遥控智能小车源程序(C语言)

基于51单片机的红外遥控智能小车源程序(C语言)

/*预处理命令*/#include<reg52.h> //包含单片机寄存器的头文件#include<intrins.h> //包含_nop_()函数定义的头文件#define uchar unsigned char#define uint unsigned int#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};sbit IRIN=P3^2; //红外接收器数据线sbit LCD_RS = P0^7;sbit LCD_RW = P0^6;sbit LCD_EN = P0^5;uchar begin[]={"My car!"};uchar cdis1[]={"jiansu!"};uchar cdis2[]={"qianjin!"};uchar cdis3[]={"jiasu!"};uchar cdis4[]={"zuozhuang!"};uchar cdis5[]={"STOP!"};uchar cdis6[]={"youzhuan!"};uchar cdis8[]={"daoche!"};sbit M1 = P1^0;sbit M2 = P1^1;sbit M3 = P1^2;sbit M4 = P1^3;sbit EN12 = P1^4;sbit EN34 = P1^5;uchar IRCOM[7];uchar m,n;uchar t=2;uchar g;uchar code digit[]={"0123456789"};uint v;uchar count;bit flag;void delayxms(uchar t);void delay(unsigned char x) ;void delay1(int ms);void motor();void lcd_display();/*检查LCD忙状态lcd_busy为1时,忙,等待。

自动避障红外电动小车C51程序

自动避障红外电动小车C51程序
#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"};
****************************/
void infrared_ray()interrupt 0 using 3
{ uchar i=90;
flage=0x01; //接受标志位
while(i--); //减小灵敏度
EX0=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); //三次显示模式设置,不检测忙信号
//右边有障碍物,左转
else if(temp==0x02) {control(2,dj_state3,right_light ); temp =0x00;}
//两个方向都有障碍物,后退,右转
else if(temp==0x03) {control(10,dj_state4,back_light );

智能小车循迹避障红外遥控C语言代码

智能小车循迹避障红外遥控C语言代码

.//智能小车避障、循迹、红外遥控C语言代码//实现功能有超声波避障,红外遥控智能小车,红外传感器实现小车自动循迹,1602显示小车的工作状态,另有三个独立按键分别控制三种状态的转换//注:每个小车的引脚配置都不一样,要注意引脚的配置,但是我的代码注释比较多,看起来比较容易一点#include <reg52.h>#include <math.h>#includelcd.h#include <intrins.h>#define uchar unsigned char#define uint unsigned intuchar ENCHAR_PuZh1[8]= run ;//1602显示数组uchar ENCHAR_PuZh2[8]= back ;uchar ENCHAR_PuZh3[8]= stop ;uchar ENCHAR_PuZh4[8]= left ;uchar ENCHAR_PuZh5[8]= right ;uchar ENCHAR_PuZh6[8]= xunji ;uchar ENCHAR_PuZh7[8]= bizhang;uchar ENCHAR_PuZh8[8]= yaokong;#define HW P2 //红外传感器引脚配置P2k口#define PWM P1 /* L298N管脚定义*//******************************超声波引脚控制******************************/sbit ECHO=P3^2; //超声波接收引脚定义兼红外遥控按键state_total =2sbit TRIG=P3^3; //超声波发送引脚定义/////红外控制引脚配置sbit KEY2=P3^7; //红外接收器数据线兼循迹按键state_total= 0sbit KEY1=P3^4; //独立按键p3.4控制自动避障state_total=1uchar state_total=3,state_2=0;//总状态控制全局变量0为自动循迹模块1为自动避障模块2为红外遥控uchar state_1,DAT; //红外扫描标志位uchar time_1=0,time_2=0;//定时器1中断全局变量time_ 2控制PWM脉冲计数time_1控制转弯延时计数也做延时一次0.005suchar time,timeH,timeL,state=0;//超声波测量缓冲变量state为超声波状态检测控制全局变量uint count=0; //1602显示计数/**************************/unsigned char IRCOM[7]; //红外接收头接收数据缓存IRCOM[2]存放的为数据unsigned char Number,distance[4],date_data[8]={0,0,0,0,0,0,0,0}; //红外接收缓存变量/***********/void IRdelay(char x); //x*0.14MS 红外头专用delayvoid run();void back();'..void stop();void left_90();void left_180();void right_90();void delay(uint dat); //void init_test();void delay_100ms(uint ms) ;void display(uchar temp); //超声波显示驱动void bizhang_test();void xunji_test();void hongwai_test();void Delay10ms(void);void init_test()//定时器0 1 外部中断0 1 延时初始化{TMOD=0x11; //设置定时器0 1 工作方式1 16位初值定时器TH1=0Xfe; //装入初值定时一次为0.0005s 2000hzTL1=0x0c;TF0=0; //定时器0方式1计数溢出标志TF1=0; //定时器1方式1计数溢出标志ET0=1; //允许定时器0中断溢出ET1=1; //允许定时器1中断溢出EA=1; //开总中断if(state_total==1)//为超声波模块时初始化{TRIG=0; //发射引脚低电平ECHO=0; // 接收引脚低电平EX0=0; //关闭外部中断IT0=1; //由高电平变低电平,触发外部中断0 }if(state_total==2) //红外遥控初始化{ IT1=1; //外部中断1为负跳变触发EX1=1; //允许外部中断1TRIG=1; // 3.3为高电平I/O口初始化}delay(60); //等待硬件操作}void main(){ uint i;delay(50);init_test();TR1=1; //开启定时器1LCD1602_Init() ;delay(50);while(state_2==0)'..{if(KEY1==0) //检测按键s1是否按下{Delay10ms(); //消除抖动if(KEY1==0){state_total=0; //总状态定义0为自动循迹模块1为自动避障模块2为红外遥控while((i<30)&&(KEY1==0)) //检测按键是否松开{Delay10ms();i++;}i=0;}}if(TRIG==0) //检测按键s2是否按下{Delay10ms(); //消除抖动if(TRIG==0){state_total=1; //总状态定义0为自动循迹模块为自动避 1 2为红外遥控障模块while((i<30)&&(TRIG==0)) //检测按键是否松开{Delay10ms();i++;}i=0;}}s3是否按下if(KEY2==0) //检测按键{消除抖动Delay10ms(); //if(KEY2==0){1 总状态定义// 0为自动循迹模块为自动避state_total=2;2 为红外遥控障模块检测按键是否松开while((i<30)&&(KEY2==0)) //{Delay10ms();i++;}i=0;'..}}}init_test();delay(50); //等待硬件操作50usTR1=0; //关闭定时器1if(state_total==1){//SPEED=90; //自动循迹速度控制高电平持续次数占空比为10的低电平bizhang_test();}if(state_total==0){// SPEED=98; //自动循迹速度控制高电平持续次数占空比为40的低电平xunji_test();}if(state_total==2){//SPEED=98; //自动循迹速度控制高电平持续次数占空比为40的低电平hongwai_test();}}void init0_suspend(void) interrupt 0 //3 为定时器1的中断号 1 定时器0的中断号0 外部中断1 2 外部中断0 4 串口中断{timeH=TH0; //记录高电平次数timeL=TL0; //state=1; //标志状态为1,表示已接收到返回信号EX0=0; //关闭外部中断0}void time0_suspend0(void) interrupt 1 //3 为定时器1的中断号 1 定时器0的中断号0 外部中断1 2 外部中断0 4 串口中断{if(state_total==1) // 自动避障初值装入{ TH0=0X00; //装入初值TL0=0x00;}if(state_total==0) //自动循迹初值装入{ TH0=0Xec; //装入初值定时一次0.005s 200hzTL0=0x78;time_1++; //控制转弯延时计数'..}}void IR_IN(void) interrupt 2{unsigned char j,k,N=0;EX1 = 0;IRdelay(5);if (TRIG==1){ EX1 =1;return;}//确认IR信号出现while (!TRIG) //等IR变为高电平,跳过9ms的前导低电平信号。

红外遥控小车程序

红外遥控小车程序

附录一程序代码红外遥控编码程序#include<reg51.h>#define uchar unsigned char#define uint unsigned int#define uintlong unsigned longsbit p3_0=P3^0;sbit p2_1=P2^1;sbit p1_0=P1^0;sbit p1_1=P1^1;sbit p1_2=P1^2;sbit p1_3=P1^3;bit out;uint keyvalue=0x00,flag_key=0,value1,value2,keycount=0,i,j,flag_set=0, flag_press=0;uchar code keycode[4]={0x7f,0xbf,0xdf,0xef};uchar codeportvalue[12]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x00, 0x0b};uchar code wy[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};//**********红外发送部分**********//uchar user1=0x00,user2=0x00;uint count=0,endcount=0;uint irdata=0;void deltime(void);void key_scan(void);void sendirdata(void);main(void){EA = 1;TMOD = 0x11;ET0 = 1;p3_0=1;P1=0xff;TH0 = 0xFF;TL0 = 0xE4;TR0 = 0;while(1){key_scan();if(flag_press==1){flag_press=0;TR0=1;sendirdata();}}}//**********按键扫描***********// void key_scan(void){for(i=0;i<=3;i++){P1=keycode[i];if(p1_3==0){keycount=i*3+0;flag_key=1; break;}if(p1_2==0){keycount=i*3+1;flag_key=1;break;}if(p1_1==0){keycount=i*3+2;flag_key=1;break;}}if(flag_key==1){flag_key=0;value1=P1;deltime();value2=P1;if(value1==value2){keyvalue=portvalue[keycount];flag_set=1;flag_press=1;}while(flag_set){value2=P1;if(value1!=value2)flag_set=0;}}}//**********延时**********//void deltime(void){uint k;for(k=0;k<=20;k++){ }}//**********定时中断**********// void time0int(void) interrupt 1{TH0=0xFF;TL0=0xE4;count++;}//**********发送数据**********// void sendirdata(){uchar s=0,datapd=0;endcount=320;p3_0=0;count=0;do{}while(count<endcount); endcount=180;count=0;p3_0=1;do{}while(count<endcount);for(s=0;s<=11;s++){endcount=20;count=0;p3_0=0;do{}while(count<endcount);endcount=20;count=0;p3_0=1;do{}while(count<endcount);}irdata=keyvalue;for(s=0;s<=7;s++){datapd=irdata & wy[s];if (datapd==0){endcount=20;count=0;} else{endcount=60;count=0;} p3_0=0;do{}while(count<endcount);endcount=20;count=0;p3_0=1;do{}while(count<endcount);}irdata=keyvalue;for(s=0;s<=7;s++){datapd=irdata & wy[s];if (datapd==0){endcount=60;count=0;} else{endcount=20;count=0;}p3_0=0;do{}while(count<endcount);endcount=20;count=0;p3_0=1;do{}while(count<endcount);}TR0=0;}红外遥控解码及驱动程序COUNT EQU 30HFLAG_USER1 EQU 45HFLAG_USER2 EQU 46HSA VEDATA EQU 47HCS1 EQU P3.0CS2 EQU P3.1RS EQU P3.3RW EQU P3.4E EQU P3.5PAGES EQU 56HCOLUMN EQU 57HNUM EQU 58HORG 0000HLJMP MAINORG 0003HLJMP EXTER0INTORG 000BHLJMP TIMER0INTORG 1000HMAIN:MOV P1,#0C5HSETB P3.6SETB P3.7MOV TMOD,#01H ;中断初始化MOV TH0,#0FFHMOV TL0,#9CHSETB EASETB IT0SETB ET0SETB EX0MOV R0,52H ;数据接收初始化MOV 52H,#00HMOV 53H,#00HMOV 54H,#00HMOV 55H,#00HMOV 51H,#00HMOV COUNT,#00HMOV R1,#08HMOV R2,#02HCLR PSW.5 ;数据接收标志CLR PSW.1 ;数据处理标志MOV FLAG_USER1,#00HLCALL CLEARMOV DPTR,#TAB0 ;显示初始化MOV PAGES,#1MOV COLUMN,#0LCALL LCD_DISPMOV DPTR,#TAB2MOV PAGES,#1MOV COLUMN,#16LCALL LCD_DISPMOV DPTR,#TAB1MOV PAGES,#1MOV COLUMN,#32LCALL LCD_DISPMOV DPTR,#TAB3MOV PAGES,#1MOV COLUMN,#48LCALL LCD_DISPMOV DPTR,#TAB4MOV PAGES,#1MOV COLUMN,#64LCALL LCD_DISPMOV DPTR,#TAB5MOV PAGES,#1MOV COLUMN,#80 ;显示红外遥控模式LCALL LCD_DISPMOV DPTR,#TAB6MOV PAGES,#3MOV COLUMN,#0LCALL LCD_DISPMOV DPTR,#TAB7MOV PAGES,#3MOV COLUMN,#16LCALL LCD_DISPMOV DPTR,#TAB8MOV PAGES,#3MOV COLUMN,#32LCALL LCD_DISPMOV DPTR,#TAB10MOV PAGES,#3MOV COLUMN,#64LCALL LCD_DISPMOV DPTR,#TAB9MOV PAGES,#3MOV COLUMN,#48LCALL LCD_DISP ;显示状态:初始LOOP: ;是否进行数据处理JNB PSW.1,$LCALL DATACHULICLR PSW.1MOV A,SA VEDATALCALL DATASHOWENDLOOP:LJMP LOOPEXTER0INT: ;外部中断SETB TR0MOV 30H,#00HRETITIMER0INT: ;定时中断,数据的验证和接收.MOV TH0,#0FFHMOV TL0,#9CHINC 30HSETB P3.2MOV C,P3.2JB PSW.5,DATARECEIVEPDJNC ENDTIMER0INTMOV A,30HCLR CSUBB A,#90JC ENDTIMER0INTSETB PSW.5CLR TR0MOV R1,#08HMOV 51H,#00HMOV R0,#52HMOV R2,#04H ;共接受两组数据LJMP ENDTIMER0INTDATARECEIVEPD: ;数据接收判断JNC ENDTIMER0INTCLR TR0MOV A,30HCLR TR0SUBB A,#10JC ORECEIVESETB CMOV A,51HRRC AMOV 51H,ALJMP WENDPDORECEIVE:CLR CMOV A,51HRRC AMOV 51H,AWENDPD:DJNZ R1,ENDTIMER0INT ;重复两次接收,确保存储.MOV R1,#08HMOV @R0,51HMOV @R0,51HINC R0MOV 51H,#00HDJNZ R2,ENDTIMER0INTCLR PSW.5SETB PSW.1SETB P0.0ENDTIMER0INT: ;定时中断结束RETIDATACHULI: ;数据处理MOV A,52HCLR CSUBB A,FLAG_USER1JNZ ENDDATACHULIMOV A,54HANL A,55HJNZ ENDDATACHULIMOV A,54HMOV SA VEDATA,AMOV 52H,#00HMOV 53H,#00HMOV 54H,#00HMOV 55H,#00HENDDATACHULI: ;处理完成,返回主程序RETDATASHOW: ;判断接收信号,进行相应处理CJNE A,#00H,CON1LJMP AOTO ;状态判断CON1:CJNE A,#02H,CON2MOV P1,#0FAHMOV DPTR,#TAB11MOV PAGES,#3LJMP ENDDATASHOW ;后转并显示CON2:CJNE A,#04H,CON3MOV P1,#0E5HMOV DPTR,#TAB12MOV PAGES,#3LJMP ENDDATASHOW ;左转并显示CON3:CJNE A,#05H,CON4MOV P1,#0C5HMOV DPTR,#TAB13MOV PAGES,#3LJMP ENDDATASHOW ;停转并显示CON4:CJNE A,#06H,CON5MOV P1,#0D5HMOV DPTR,#TAB14MOV PAGES,#3LJMP ENDDATASHOW ;右转并显示CON5:CJNE A,#08H,CON6MOV P1,#0F5HMOV DPTR,#TAB15MOV PAGES,#3LJMP ENDDATASHOW ;前转并显示CON6:JMP ENDDATASHOW1ENDDATASHOW:MOV COLUMN,#48LCALL LCD_DISPMOV DPTR,#TAB16MOV PAGES,#3MOV COLUMN,#64LCALL LcD_DISPENDDATASHOW1:RETAOTO:MOV P1,#0C0H ;寻迹壁障程序MOV DPTR,#TAB17MOV PAGES,#1MOV COLUMN,#0LCALL LCD_DISPMOV DPTR,#TAB18MOV PAGES,#1MOV COLUMN,#16LCALL LCD_DISPMOV DPTR,#TAB19MOV PAGES,#1MOV COLUMN,#32LCALL LCD_DISPMOV DPTR,#TAB20MOV PAGES,#1MOV COLUMN,#48LCALL LCD_DISP ;显示寻迹避障MOV DPTR,#TAB10MOV PAGES,#3MOV COLUMN,#64LCALL LCD_DISPMOV DPTR,#TAB9MOV PAGES,#3MOV COLUMN,#48LCALL LCD_DISP ;显示初始LCALL DLY ;模式工作前等待3秒AOTO1: ;自动寻迹避障程序MOV DPTR,#TAB15MOV PAGES,#3MOV COLUMN,#48LCALL LCD_DISPMOV DPTR,#TAB16MOV PAGES,#3MOV COLUMN,#64LCALL LCD_DISP ;显示前转MOV P1,#0F5HJNB PSW.5,AOTO2 ;检测红外信号LCALL CLEARMOV P1,#0C5HLCALL DLY1LJMP MAINAOTO2:JNB P1.6,ZZ ;状态判断JNB P1.7,YZJNB P3.6,HTJNB P3.7,HTSJMP AOTO1ZZ:MOV P1,#0E5H ;右寻迹灯亮MOV DPTR,#TAB12MOV PAGES,#3MOV COLUMN,#48LCALL LCD_DISPLCALL DLYSJMP DDYZ:MOV P1, #0D5H ;左寻迹灯亮MOV DPTR,#TAB14MOV PAGES,#3MOV COLUMN,#48LCALL LCD_DISPLCALL DLYHT:MOV P1,#0FAH ;发现障碍物MOV DPTR,#TAB11MOV PAGES,#3MOV COLUMN,#48LCALL LCD_DISPLCALL DLYSJMP ZZDD:LJMP AOTO1DL Y:MOV R7,#0BDH ;3秒延时DL1:MOV R6,#0E6HDL0:MOV R5,#21HDJNZ R5,$DJNZ R6,DL0DJNZ R7,DL1RETDLY1:MOV R7,#0A7H ;1秒延时DL3:MOV R6,#0ABHDL2:MOV R5,#10HDJNZ R5,$DJNZ R6,DL2DJNZ R7,DL3NOPRETCLEAR: MOV R4,#00H ;清屏程序CLEAR1:MOV A ,R4ORL A,#0B8HLCALL WCL0LCALL WCR0MOV B,#40HLCALL WCL0LCALL WCR0MOV R3,#40HCLEAR2:MOV B,#00HLCALL WCL1LCALL WCR1DJNZ R3,CLEAR2INC R4CJNE R4, #08H, CLEAR1RETLCD_DISP: ;汉字显示程序MOV R1,#02HMOV R0,#00HMOV NUM,#10HDISP: MOV R4,NUMDISPWORD:MOV A,COLUMNCLR CSUBB A,#63JNC YXPMOV A,COLUMNORL A,#40HMOV B,ALCALL WCL0ORL A,#0B8HMOV B,ALCALL WCL0 DISPDATA1:INC COLUMNMOV A,R0MOVC A,@A+DPTRMOV B,ALCALL WCL1LJMP NEXTDISPYXP:MOV A,COLUMNCLR CSUBB A,#64ORL A,#40HMOV B,ALCALL WCR0MOV A,PAGESORL A,#0B8HMOV B,ALCALL WCR0 DISPDATA2:INC COLUMNMOV A,R0MOVC A,@A+DPTRMOV B,ALCALL WCR1 NEXTDISP:INC R0DJNZ R4,DISPWORDCLR CSUBB A,#10HMOV COLUMN, AINC PAGESDJNZ R1,DISPRETWCL0: CLR CS1SETB CS2CLR RSSETB RWWCL00: MOV P2,#0FFHSETB EMOV A,P2CLR EJB ACC.7,WCL00CLR RWMOV P2, BSETB ENOPCLR ERETWCL1: CLR CS1SETB CS2CLR RSSETB RWWCL11: MOV P2,#0FFHSETB EMOV A,P2CLR EJB ACC.7,WCL11CLR RWSETB RSMOV P2, BSETB ECLR ERETWCR0: SETB CS1CLR CS2CLR RSSETB RWWCR00: MOV P2,#0FFHSETB EMOV A,P2CLR EJB ACC.7,WCR00CLR RWMOV P2, BSETB ECLR ERETWCR1: SETB CS1CLR CS2CLR RSSETB RWWCR11: MOV P2,#0FFHSETB EMOV A,P2CLR EJB ACC.7,WCR11CLR RWSETB RSMOV P2, BSETB ECLR ERETTAB0:DB20H,30H,0ACH,63H,20H,10H,04H,04H,04H,04H,0FCH,04H,06H,04H,00H,00H,22H,67H,22H,12H,12H,52H,40H,40H,40H,40H,7FH,40H,40H,60H,40H,00H ; 红TAB1:DB40H,42H,44H,0CCH,00H,42H,36H,2AH,22H,0FEH,21H,29H,25H,80H,00H,00H,00H,40H,20H,1FH,21H,41H,5DH,49H,49H,4FH,49H,49H,5DH,61H,21H,00H ; 外TAB2:DB00H,80H,0E0H,18H,17H,10H,90H,70H,00H,0FFH,20H,40H,80H,00H,00H,00H,01H,40H,20H,11H,0AH,04H,03H,00H,00H,0FFH,00H,00H,00H,01H,03H,00H ; 遥TAB3:DB10H,10H,10H,0FFH,90H,50H,0CH,44H,24H,15H,06H,14H,24H,54H,0CH,00H,02H,42H,81H,7FH,00H,40H,42H,42H,42H,42H,7EH,42H,43H,62H,40H,00H ; 控TAB4:DB10H,10H,0D0H,0FFH,94H,14H,0E4H,0BFH,0A4H,0A4H,0A4H,0BFH,0F4H,26H,04H,00H,04H,03H,00H,0FFH,08H,89H,8BH,4AH,2AH,1EH,2AH,2AH,4BH,0C8H,48H,00H ; 模TAB5:DB10H,10H,90H,90H,90H,90H,90H,10H,0FFH,10H,12H,14H,10H,18H,10H,00H,00H,20H,60H,20H,1FH,10H,10H,08H,01H,06H,18H,20H,40H,80H,70H,00H ; 式TAB6:DB00H,08H,30H,00H,0FFH,20H,20H,20H,20H,0FFH,20H,22H,24H,30H,20H,00H,08H,0CH,02H,01H,0FFH,40H,20H,1CH,03H,00H,03H,0CH,30H,60H,20H,00H; 状TAB7:DB04H,04H,84H,84H,44H,24H,54H,8FH,14H,24H,44H,44H,84H,86H,84H,00H,01H,21H,1CH,00H,3CH,40H,42H,4CH,40H,40H,70H,04H,08H,31H,00H,00H;态TAB8:DB000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,036H,036H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H ;:TAB9:DB10H,10H,91H,0D6H,30H,98H,00H,08H,08H,0F8H,08H,08H,08H,0FCH,08H,00H,02H,01H,00H,0FFH,01H,82H,40H,20H,18H,07H,40H,80H,40H,3FH,00H,00H ; 初TAB10:DB10H,10H,0F0H,1FH,10H,0F0H,40H,60H,58H,47H,40H,40H,50H,60H,0C0H,00H,40H,22H,15H,08H,16H,61H,00H,0FEH,42H,42H,42H,42H,42H,0FFH,02H,00H;始TAB11:DB00H,00H,0FCH,24H,24H,24H,24H,24H,24H,22H,22H,23H,0A2H,30H,20H,00H,40H,30H,0FH,00H,00H,0FFH,41H,41H,41H,41H,41H,41H,0FFH,01H,00H,00H;后TAB12:DB08H,08H,08H,08H,88H,78H,0FH,08H,08H,08H,08H,88H,0CH,08H,00H,00H,20H,50H,48H,46H,41H,41H,41H,41H,7FH,41H,41H,41H,41H,60H,40H,00H;左TAB13:DB40H,20H,0F8H,07H,00H,04H,74H,54H,55H,56H,54H,54H,76H,04H,00H,00H,00H,00H,0FFH,04H,03H,01H,05H,45H,85H,7DH,05H,05H,01H,05H,03H,00H ;停TAB14:DB08H,08H,08H,08H,0C8H,38H,0FH,08H,08H,08H,08H,08H,88H,0CH,08H,00H,08H,04H,02H,01H,0FFH,41H,41H,41H,41H,41H,41H,41H,0FFH,01H,00H,00H ;右TAB15:DB08H,08H,0E8H,29H,2AH,2EH,0E8H,08H,08H,0CCH,0AH,0BH,0E8H,0CH,08H,00H,00H,00H,0FFH,09H,49H,89H,7FH,00H,00H,0FH,40H,80H,7FH,00H,00H,00H;前TAB16:DB08H,0C8H,0B8H,8FH,0E8H,88H,40H,48H,0C8H,7FH,48H,48H,4CH,68H,40H,00H,08H,18H,08H,04H,0FFH,04H,00H,02H,13H,22H,62H,9AH,06H,02H,00H,00H;转TAB17:DB00H,00H,82H,92H,92H,92H,92H,92H,92H,92H,92H,0FFH,02H,00H,00H,00H,04H,04H,04H,04H,0CH,34H,04H,04H,44H,84H,7FH,04H,04H,06H,04H,00H ;寻TAB18:DB40H,42H,0CCH,00H,08H,0C8H,08H,0F9H,0AH,0EH,0F8H,08H,4CH,88H,00H,00H,40H,20H,1FH,20H,42H,41H,50H,4FH,40H,50H,5FH,40H,40H,63H,20H,00H ;迹TAB19:DB40H,42H,0C4H,00H,0FEH,22H,22H,3FH,8AH,0A8H,0C9H,8EH,0C8H,0ACH,88H,00H,40H,20H,1FH,22H,5FH,51H,51H,5FH,40H,44H,44H,7FH,44H,64H,20H,00H ;避TAB20:DB00H,0FEH,22H,5AH,86H,10H,0D2H,56H,5AH,53H,5AH,56H,0F2H,58H,10H,00H,00H,0FFH,04H,08H,17H,10H,17H,15H,15H,0FDH,15H,15H,17H,10H,10H,00H ;障END。

无线遥控循迹避障小车代码

无线遥控循迹避障小车代码

#include< reg51.h >#define uchar unsigned char#define uint unsigned int#define MOTOR_C P1 //P1口作为电机的控制口。

//#define SIGNAL P3 //P3口的低两位为循迹传感器输入口。

#define SHELVES 10 //速度总档数。

#define BACK 0xfa //后退。

#define FORWARD 0xf5 //前进。

#define WXYK P2 //无线遥控sbit senserr = P3^2; //(右)循迹。

sbit senserl = P3^3; //(左)循迹。

sbit hwr = P3^0; //(前)红外壁障传感器入口。

sbit hwl = P3^1; //(后)红外壁障传感器入口。

sbit PWM_R = P1^0; //右电机PWM输入口。

sbit PWM_L = P1^2; //左电机PWM输入口。

sbit PWM_HR = P1^1; //(后退)右电机。

sbit PWM_HL = P1^3; //(后退)左电机。

sbit wxr_a = P2^4; //无线遥控接收端D0sbit wxb_b = P2^5; //无线遥控接收端D1sbit wxl_c = P2^6; //无线遥控接收端D2sbit wxs_d = P2^7; //无线遥控接收端D3void timer0_init( void ); //定时器0初始化函数。

void timer1_init( void ); //定时器1初始化函数。

void right( void ); //前进右转弯函数。

void left( void ); //前进左转弯函数。

void forward( void ); //前进函数。

void hright(void); //后退右转函数。

void hleft(void); //后退左转函数。

寻迹避障小车51程序(绝版模块化程序)

寻迹避障小车51程序(绝版模块化程序)

M a i n.c #include "common.h"#include "motor.h"#include "timer.h"uchar speed_l;uchar speed_r;sbit out = P3^7;void main(){P1 = 0xff;flag_l = 0;flag_r = 0;sensor_ldata = 0;sensor_rdata = 0;avoid_keyfunc = 0;findline_keyfunc = 0;motor_stop();timerinit();while(1){/*********************************************************循迹功能*********************************************************/while(findline_keyfunc){/**************************************************************** 正常情况下前进****************************************************************/ if(sensor == 0x27){motor_go();speed_l = 50;speed_r = 47;}/****************************************************************如果小车偏左****************************************************************/if((sensor == 0x37) || (sensor == 0x17) || (sensor == 0x0f) || (sensor == 0x1f) || (sensor == 0x3f)){motor_r();speed_l = 45;speed_r--;if(speed_r == 30){speed_r = 31;}}/**************************************************************** 如果小车偏右****************************************************************/if((sensor == 0x67) || (sensor == 0x47) || (sensor == 0x87) || (sensor == 0xc7) ||(sensor == 0xe7)){motor_l();speed_r = 45;speed_l--;if(speed_l == 30){speed_l = 31;}}}/////////////////////////////////////////////////////////////////// /////****************************************************************** * 避障功能******************************************************************* / while(avoid_keyfunc){/*********************************************************** 如果没有检测到有障碍物***********************************************************/ if((sensor_ldata == 1) && (sensor_rdata == 1)){motor_go();speed_l = 48;speed_r = 50;}/*********************************************************** 如果检测到右边有障碍物***********************************************************/ if((sensor_ldata == 1) && (sensor_rdata == 0)){motor_go();speed_l--;speed_r = 100;if(speed_l == 10){speed_l = 11;}}/****************************************************** 如果左边检测到有障碍物******************************************************/ if((sensor_ldata == 0) && (sensor_rdata == 1)){motor_go();speed_r--;speed_l = 100;if(speed_r == 10){speed_r = 11;}}/****************************************************** 如果都检测到有障碍物******************************************************/ if((sensor_ldata == 0) && (sensor_rdata == 0)){motor_go();speed_r = 100;speed_l = 10;}//////////////////////////////////////}/////////////////////////////////////motor_stop();}}MAIN.H#ifndef MAIN_H#define MAIN_Hextern uchar speed_l;extern uchar speed_r;#endifMotor.c#include "common.h"#include "timer.h"sbit in1 = P2^0;sbit in2 = P2^1; //左电机sbit in3 = P2^2;sbit in4 = P2^3;//右电机sbit ENA = P2^4; //只有当ENA=1时左电机才能转sbit ENB = P2^5; // 只有。

红外避障小车c语言程序

红外避障小车c语言程序
e 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 ; } //******************************************************** // 快速左转
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() ; } } //********************************************************

智能红外循迹小车程序

智能红外循迹小车程序

#include<reg52.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longsbit you_2=P2^0;//红外探测端口定义sbit you_1=P2^1;sbit zhong=P2^2;sbit zuo_1=P2^3;sbit zuo_2=P2^4;sbit CG=P0^1;sbit DC=P0^0;uchar code L_F[8]= {0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90};//左电机正转uchar code L_B[8]= {0x90,0x80,0xc0,0x40,0x60,0x20,0x30,0x10};//左电机反转uchar code R_F[8]= {0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};//右电机正转uchar code R_B[8]= {0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};//右电机反转uchar code B_F[8]= {0x91,0x83,0xc2,0x46,0x64,0x2c,0x38,0x19};//左反右正uchar code F_B[8]= {0x19,0x38,0x2c,0x64,0x46,0xc2,0x83,0x91}; //左正右反uchar code duandian[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//断电unsigned char code qianjin[]={0x11,0x33,0x22,0x66,0x44,0xcc,0x88,0x99};//前进unsigned char h[]={0x11,0x33,0x22,0x66,0x44,0xcc,0x88,0x99};//uchar i;void delay(uint z){uint k ;uint j;for(k=0; k<z; k++)for(j=0; j<110; j++);}void QJ(unsigned int i){for(i=0;i<8;i++){P1=h[i]=qianjin[i];delay(13);}}void DD(){P1=0x00;delay(300);}void wtj(){while(1){if(P2==0xff){DD();delay(1000);break;}else{QJ(8);}}}void YG_1(){unsigned char i;for(i=0;i<8;i++){P1=h[i]=F_B[i];delay(10);}}void ZG_1(){ unsigned char i;for(i=0;i<8;i++){P1=h[i]=B_F[i];delay(10);}}void byg(){while(1){if(P2==0xfb)break;else if(P2==0xf9)break;else if(P2==0xfd)break;/*if(P2^0==0)break;else if(P2^1==0)break;/*else if(P2^2==0)break;else if(P2^3==0)break;else if(P2^4==0)break;*/elseYG_1();}}void bzg(){while(1){if(P2==0xfb)break;else if(P2==0xf3)break;else if(P2==0xf7)break;/*if(P2^0==0)break;else if(P2^1==0)break;else if(P2^2==0)break;if(P2^3==0)break;else if(P2^4==0)break;*/elseZG_1();}}void YG_2(){//unsigned char i; //unsigned char g;if(P2==0xfa){//delay(4000); wtj();byg();/*for(g=0;g<40;g++){for(i=0;i<8;i++){P1=F_B[i];delay(20);}} */}else if(P2==0xf2){//delay(4000); wtj();byg();/*for(g=0;g<40;g++){for(i=0;i<8;i++){P1=F_B[i];delay(20);}}*/}else if(P2==0xf6){//delay(4000);byg();/*for(g=0;g<40;g++){for(i=0;i<8;i++){P1=F_B[i];delay(20);}}*/}else if(P2==0xf4){//delay(4000); wtj();byg();/*for(g=0;g<40;g++){for(i=0;i<8;i++){P1=F_B[i];delay(20);}}*/}else ;}void ZG_2(){//unsigned char i; //unsigned char g;if(P2==0xeb){//delay(4000); wtj();bzg();/*for(g=0;g<40;g++){for(i=0;i<8;i++){P1=B_F[i];delay(20);}}*/else if(P2==0xe9){//delay(4000); wtj();bzg();/*for(g=0;g<40;g++) {for(i=0;i<8;i++){P1=B_F[i];delay(20);}}*/}else if(P2==0xed){//delay(4000); wtj();bzg();/*for(g=0;g<40;g++) {for(i=0;i<8;i++){P1=B_F[i];delay(20);}}*/}else if(P2==0xe5){//delay(4000); wtj();bzg();/*for(g=0;g<40;g++) {for(i=0;i<8;i++){P1=B_F[i];delay(20);}}*/}else ;}void ZG(){unsigned char i;for(i=0;i<8;i++){P1=h[i]=R_F[i];delay(11);}}void YG(){unsigned char i;for(i=0;i<8;i++){P1=h[i]=L_F[i];delay(11);}}/*void HT(){unsigned char i;for(i=0;i<8;i++){P1=houtui[i];delay(55);}}*/void zd(){while(1){if(P2==0xe0)QJ(8);else if(P2==0xff){while(1){DD();}}else{QJ(80);if(P2==0xff){while(1){DD();}}else{DC=0;break;break;}}}}void zd1(){while(1){if(P2==0xe0)QJ(8);else if(P2==0xff){while(1){DD();}}else{QJ(80);if(P2==0xff)while(1){DD();}}else{DC=0;break;}}}}void hy(){unsigned char i;for(i=0;i<8;i++){P1=h[i];delay(10);}}void main(){DC=1;while(1){QJ(8);if(P2!=0xff){delay(9000);break;}}while(1){DC=1;if(CG==1){delay(9000);DC=1;while(1){switch(P2){case 0xfb:QJ(8);break;//11011case 0xf1:QJ(8);break;//10001case 0xf3:QJ(8);break;//10011case 0xf7:ZG();break;//10111case 0xe7:ZG_1();break;//00111case 0xef:ZG_1();break;//01111case 0xe3:ZG_1();break; //00011case 0xe1:ZG_1();break; //00001case 0xf9:QJ(8);break;//11001case 0xfd:YG();break;//11101case 0xfc:YG_1();break;//11100case 0xfe:YG_1();break;//11110case 0xf8:YG_1();break; //11000case 0xf0:YG_1();break;//10000case 0xfa://11010QJ(16);// delay(4000);YG_2();//f7 break;case 0xf2://10010QJ(16);// delay(4000);YG_2();break;case 0xf6://10110QJ(16);// delay(4000);YG_2();break;case 0xf4://10100QJ(16);// delay(4000);YG_2();break;case 0xeb://01011QJ(16);//delay(4000);ZG_2();//fd break;case 0xe9://01001QJ(16);//delay(4000);ZG_2();break;case 0xed://01101QJ(16);//delay(4000);ZG_2();break;case 0xe5://00101QJ(16);//delay(4000);ZG_2();break;case 0xe0:zd1();break;//00000case 0xff:hy();break;//11111default:QJ(8); break;}}}else{switch(P2){case 0xfb:QJ(8);break;//11011 case 0xf1:QJ(8);break;//10001case 0xf3:QJ(8);break;//10011 case 0xf7:ZG();break;//10111 case 0xe7:ZG_1();break;//00111 case 0xef:ZG_1();break;//01111 case 0xe3:ZG_1();break; //00011 case 0xe1:ZG_1();break; //00001case 0xf9:QJ(8);break;//11001 case 0xfd:YG();break;//11101 case 0xfc:YG_1();break;//11100 case 0xfe:YG_1();break;//11110 case 0xf8:YG_1();break; //11000 case 0xf0:YG_1();break;//10000case 0xfa://11010QJ(16);// delay(4000);YG_2();//f7 break;case 0xf2://10010QJ(16);// delay(4000);YG_2();break;case 0xf6://10110QJ(16);// delay(4000);YG_2();break;case 0xf4://10100QJ(16);// delay(4000);YG_2();break;case 0xeb://01011QJ(16);//delay(4000);ZG_2();//fd break;case 0xe9://01001QJ(16);//delay(4000);ZG_2();break;case 0xed://01101QJ(16);//delay(4000);ZG_2();break;case 0xe5://00101QJ(16);//delay(4000);ZG_2();break;case 0xe0:zd();break;//00000case 0xff:hy();break;//11111default:QJ(8); break;}}}}。

红外遥控控制小车前进,后退,左右转程序

红外遥控控制小车前进,后退,左右转程序
{delay(1);}
while (!IRIN) //等 IR 变为高电平
{delay(1);}
while (IRIN) //计算IR高电平时长
{
delay(1);
N++;
{delay(1);}
for (j=0;j<4;j++) //收集四组数据
{
for (k=0;k<8;k++) //每组数据有8位
{
while (IRIN) //等 IR 变为低电平,跳过4.5ms的前导高电平信号。
{
uchar j,k,N=0;
EX0 = 0;
delay(15);
if (IRIN==1)
{ EX0 =1;
return;
}
//确认IR信号出现
while (!IRIN) //等IR变为高电平,跳过9ms的前导低电平信号。
beep();
EX0 = 1;
}
void delay(uchar x) //x*0.14MS红外用判断时间
{
uchar i;
while(x--)
{
for (i = 0; i<13; i++) {;}
}
}
sbit ENA = P3^0;//L298的使能端
sbit ENB = P3^1;//L298的使能端
sbit IRIN=P3^2;//声明红外接口
uchar IRCOM[4],m,zuozhuan,youzhuan,zhuanwa,shache,qj,dc,j=20;
uchar K,a,b;

红外遥控小车的c程序

红外遥控小车的c程序

#include<reg52.h>#include<math.h>#include<intrins.h>#define uchar unsigned char //数据类型#define uint unsigned intsbit hwx=P3^3;uchar key;uchar l_lhj[66];//定义66位数组变量来存储接收的时间参数uchar ledmap[10]={0x3f, 0x06 , 0x5b, 0x4f, 0x66, 0x6d,0x7d};//*********************************************sbit ENa=P2^4; //* L298的Enable Asbit ENb=P2^7; //* L298的Enable Bsbit IN1=P2^2; //* L298的Input 1sbit IN2=P2^3; //* L298的Input 2sbit IN3=P2^5; //* L298的Input 3sbit IN4=P2^6; //* L298的Input 4uchar t=0; //* 中断计数器uchar m1=0; //* 电机1速度值uchar m2=0; //* 电机2速度值uchar tmp1,tmp2; //* 电机当前速度值//*************************************************//函数定义void Init8952( ) ; //初始化AT89C52单片机void Move_Fo( ) ; // 小车向前运动void Move_Ba( ) ; // 小车向后运动void Move_Le( ) ; // 小车向左运动void Move_Ri( ) ; // 小车向右运动void Stop( );// 小车停止void Move_Qi(); //小车加速运动void Move_Sl();//小车减速运动void Delay(uchar n) ;// 延时n 毫秒void Delay1(void);//空5个指令void Motor(uchar index , char speed);//控制小车速度void Car_Action();//*****************************************************//延时//*****************************************************void Delay(uchar n){uchar i;while(n--){for (i=0;i<250;i++){}}}void Delay1(void)//空5个指令{unsigned char i=13;while(i)i--;}//******************************************************* //单片机初始化//******************************************************* void Init8952(){TMOD = 0x02; //定时器0工作与方式2EX1=1; //开启外部中断1IT1=1; // 设置成下降沿触发方式TH0=0x9B; // 装入定时器0的初值TL0=0x9B;ET0=1; // 定时器0允许中断TR0=1; // 启动定时器0EA = 1 ;// 开总中断}//******************************************************** //电机控制函数index-电机号(1,2); speed-电机速度(-100到+100) //******************************************************** void motor(uchar index, char speed){if(speed>=-100 && speed<=100){if(index==1) // 电机1的处理{m1=abs(speed); // 取速度的绝对值if(speed<0) // 速度值为负则反转{IN1=0;IN2=1;}else // 不为负数则正转{IN1=1;IN2=0;}}if(index==2) // 电机2的处理{m2 = abs(speed); // 电机2的速度控制if(speed<0) // 电机2的方向控制{IN3 = 0;IN4 = 1;}else{IN3 = 1;IN4 = 0;}}}}//***************************************void Move_Fo( ) // 小车向前运动{P1 = ledmap[1];Motor(1,40);Delay(250) ;}void Move_Ba( ) // 小车向后运动{P1 = ledmap[2];Motor(1,-40) ;Delay(250) ;}void Move_Le( ) // 小车向左运动{P1 = ledmap[3];//Motor(1,40);Motor(2,90);Delay(30) ;Motor(1,80);Delay(50) ;}void Move_Ri( ) // 小车向右运动{P1 = ledmap[4];Motor(2,-90);Delay(30) ;//Delay(250) ;Motor(1,80);Delay(50) ;}void Move_Qi( )//加速{uchar i;P1 = ledmap[5];for(i=40;i<100;i+=15){Motor(1,i);// Motor(2,i-3);Delay(250) ;Delay(250) ;}}void Move_Sl()//减速{uchar j;P1 = ledmap[6];for(j=80;j>0;j-=8){Motor(1,j);Delay(250) ;Delay(250) ;}}void Stop( ) //小车停止{P1 = ledmap[0];IN1 = IN2;IN3 = IN4;}void Car_Action() //判断红外线信号小车响应运动{switch( key ){case 0x03://暂停Stop( ) ; break ;case 0x01: //前进Move_Fo() ;break;case 0x02: // 后退Move_Ba() ;break;case 0x04: // 左转Move_Le() ;break;case 0x05: // 右转Move_Ri() ;break;case 0x06://快速运行Move_Qi( ) ; break ;case 0x07:Move_Sl( ) ;break ;//减速}}//************************************************* //主函数void main( ){//Delay(100) ;Init8952( );while(1){Car_Action();//Delay(10) ;}//*****************************************************//定时器0 中断函数void timer0( ) interrupt 1 // T0中断服务程序{if(t==0) // 1个PWM周期完成后才会接受新数值{tmp1 = m1 ;tmp2 = m2 ;}if(t<tmp1)ENa = 1 ;elseENa = 0 ; // 产生电机1的PWM信号if(t<tmp2)ENb = 1 ;elseENb = 0 ; // 产生电机2的PWM信号t++;if(t>=100) // 1个PWM信号由100次中断产生t = 0 ;}//********************************************************void hongwai(void) interrupt 2 //外部中断1 ,INT1(P3^3)连接红外线接收IC数据脚{uchar i,j,tmp;EX1=0;j=33;//传送一组数包括引导码1位,地址码8位加反码8位,指令码8位加反码8位,总共33位i=0;//从第一维数组开始tmp=100;//加八延时,while(tmp){tmp--;}if(hwx){//然后再检测红线接收脚是有数据招收,有继续,没有则退出EX1=1;return;}{//循环接收33位数据,为何我们用到66位数组,我们可以不接收高电平时间常数,只接低电平常数就//可以判断1或0了,在这里我们都接收,为串口调试软件接收计算波形//还有一点要知道,接收波形是反向,在没有接收时端口为高电平tmp=0;l_lhj[i]=1;//时间量从1开始while(!hwx){//检测高低电平的变化l_lhj[i]++;//没变继续加1Delay1();//加入延时,是因为我们采用8位二进来存储,如果不加延时,时间量将计满tmp++;//此变量为防止干扰带来的死循环if(tmp==250)break;}i++;tmp=0;l_lhj[i]=1;//时间量从1开始while(hwx){//检测高低电平的变化l_lhj[i]++;//没变继续加1Delay1();//同上tmp++;if(tmp==250)break;}i++;j--;}i=255;//加入循环延时,抗干扰while(i){tmp=255;while(tmp){tmp--;}i--;}tmp=0;for(i=3;i<19;i++,i++){//处理地址位,对低电平时间数据的数理,这里我们只处理地址码和指令码,引导码和反码我们就忽略tmp>>=1;//右移一位,接收低电平在前if(l_lhj[i]>30)//检测低电平时间超过30就确认为1tmp+=0x80;}for(i=35;i<51;i++,i++){//同上,这里处理指令位tmp>>=1;if(l_lhj[i]>30)tmp+=0x80;}key=tmp;EX1=1;}。

红外遥控小车程序

红外遥控小车程序

程序已调试,能实现前进、后退、左前转、右前转、左后转、右后转、左圆转、右圆转等,并且有LED灯显示。

//**********************************************//***************红外小车***********************//**********************************************#include<reg52.h>#define uint unsigned int#define uchar unsigned charsbit M11=P1^0; // 电机1接口sbit M12=P1^1;sbit M21=P1^2; // 电机2接口sbit M22=P1^3;sbit LED0=P0^0; // 紧急灯sbit LED1=P0^1; // 左前灯sbit LED2=P0^2; // 右前灯sbit LED3=P0^3; // 左后灯sbit LED4=P0^4; // 左后灯sbit F=P1^5; //蜂鸣器接口sbit Int0 = P3^3; // 红外接收采用外部中断1uchar TEMP1=0; //全局变量用于存放红外返回值-用于PWM占空比的控制uchar TEMP2=10; // 用于方向的控制uchar databus[7];//用于红外接收存放数据//************************************************************//延时函数void delay1(uint xms) //delay 0.1ms{uint i,j;for(i=xms;i>0;i--)for(j=13;j>0;j--);}//************************************************************//********************接受解码函数uchar Rec(){uchar temp=0; //函数内部临时变量用于返回函数值uint i=0; // 用于存放数据的临时变量uint j=0;uint m=0; // 局部计数临时变量uint n=0; // 局部计数临时变量uint k=0; // 局部计数临时变量EX1=0; // 关外部中断1 以防再次外部1中断的介入while(!Int0) //用于计时前9Ms低电平的引导码{delay1(1);m++; // 计数引导码的长度}if(m<=60)return 0; //while(Int0) //用于计时4.5ms高电平的引导码{delay1(1);n++; //计时高电平}if(n<=30)return 0;M11=M12=M21=M22=0; //进入中断停止电机防止电机颤抖for(i=0;i<4;i++)for(j=0;j<8;j++){while(!Int0) //过滤数据的低位0.56ms低电平{delay1(1);}while(Int0) // 进入高电平计时{delay1(1);k++; // k计时每次的高电平}databus[i]=databus[i]>>1; // 数据接收数组首先默认接收为0信号即databus 【i】//第7位移入0if(k>=10){databus[i]=databus[i]|0x80; // 若为1把第8位置1}k=0; //把k的值重新置零};if(databus[2]!=~databus[3])return 0;temp=databus[2]; // 返回赋值return temp; //}//****************************************************//**********红外接受中断服务函数+接受编码TEMP1TEMP2void int0() interrupt 2{uchar temp; // 临时存放红外接收函数返回值uint i=0; // 计数临时变量LED0=1; //因紧急灯是闪烁的每次中断时结果不一样为保证效果先灭紧急指示灯temp=Rec(); // 红外函数返回赋值switch (temp) // 对返回的数据进行编码方便后面的操作{case 0x40:{TEMP2=0;//紧急状态暂停break;}case 0x46:{TEMP2=1;//前进LED2=LED1=0; //指示灯设置LED4=LED3=1;break;}case 0x44:{TEMP2=2;//左前转LED1=0; //指示灯设置LED2=LED4=LED3=1;break;}case 0x43 :{TEMP2=3;//右前转LED2=0; //指示灯设置LED1=LED4=LED3=1;break;}case 0x15:{TEMP2=4; //倒车状态LED2=LED1=1; //指示灯设置LED4=LED3=0;break;}case 0x07:{TEMP2=5;//左后转LED3=0; //指示灯设置LED1=LED4=LED2=1;break;}case 0x09:{TEMP2=6;//右后转LED4=0; //指示灯设置LED1=LED2=LED3=1;break;}case 0x45:{TEMP2=7; //左园转break;}case 0x47:{TEMP2=8;//右园转break;}} //每次接受红外蜂鸣器蜂鸣一次F=1;delay1(300);F=0;EA=1; // 开总中断EX1=1; // 开外1中断IT1=1; // 下降沿触发}//*****************************************//**********电机驱动函数void PWM_RUN(){uchar n=0; //n=TEMP2;if(n==0) //紧急状态{M11=M12=M21=M22=0;LED0=~LED0; //设置紧急灯动作delay1(1000);}if(n==1) //m1和m2都前转前进{ M11=1; M12=0; M21=0; M22=1; }if(n==2) //m1停止m2前转实现左前转弯{ M11=0; M12=0; M21=0; M22=1; }if(n==3) // m1前转m2停止实现右前转{ M11=1; M12=0; M21=0; M22=0; }if(n==4)//m1m2倒转实现倒车{ M11=0; M12=1; M21=1; M22=0; }if(n==5) //m1停止m2倒转实现左后转{ M11=0; M12=0; M21=1; M22=0; }if(n==6)// m1倒转m2停止实现右后转{ M11=0; M12=1; M21=0; M22=0; }if(n==7)//m1倒传m2前转实现左方向原地打转{ M11=0; M12=1; M21=0; M22=1; }if(n==8)//m1前转m2倒转实现右方向原地打转{ M11=1; M12=0; M21=1; M22=0; }}//***************************THE main()void main(){EA=1; // 开总中断EX1=1; // 开外1中断IT1=1; // 下降沿触发P1=0; // P1口为电机控制口初始化为0即开机时电机是停止的while(1) {PWM_RUN();}}。

红外线遥控小车c程序

红外线遥控小车c程序

#include <reg52.h>sbit servo=P2^5; //PWM信号输出sbit zuo11=P1^0; //为0为左边正转sbit zuo12=P1^1; //为1为左边正转sbit you21=P1^2; //为1为右边正转sbit you22=P1^3; //为0为右边正转uchar redmessage[4]={0x00,0x00,0x00,0x00};uchar qianjin[]={0xf6,0xf0,0xf0}; // 左右行驶变量uchar houtui[]={0xf9,0xf0,0xf0}; ////uint qianX=0,houX=0; //uint haha; //uint mark; //uint mark1,mark2;uchar control=12; //角度标识uchar jishu; //0.125ms次数标识unsigned long m,Tc;void delay(uint a){uint b,c;for(b=a;b>0;b--)for(c=115;c>0;c--);}void T1_out() interrupt 2{Tc=TH0*256+TL0; //提取中断时间间隔时长TH0=0;TL0=0; //定时中断重新置零if((Tc>8000)&&(Tc<14000)){m=0;mark1=1;return;} //找到启始码if(mark1==1){if(Tc>1450&&Tc<3000){redmessage[m/8]=redmessage[m/8]>>1|0x80;m++;}if(Tc>700&&Tc<1450){redmessage[m/8]=redmessage[m/8]>>1;m++; //取码}if(m==32){m=0;mark1=0;if(redmessage[2]==~redmessage[3]){mark2=1 ;}else{mark2=0; //取码完成后判断读码是否正确}}}}void red_check(){if(mark2==1){switch(redmessage[2]){case 0x16: //遥控器+键按下角度加45control++; //角度标识加1jishu=0; //按键按下则20ms周期从新开始if(control==21){control=20; //已经是180度,则保持}break;case 0x0d: //遥控器-键按下角度减45control--; //角度标识减1jishu=0;if(control==0){control=1; //已经是0度,则保持}break;case 0x15:houX=0;qianX=1; //前进数据位break;case 0x18:qianX=0;houX=1; //后退数据位break;case 0x19:TR1=~TR1;P1=0xf0; //启动数据位break;default:break;}mark2 = 0;}}void initial_T01(){TMOD=0x21;TH1=0xfc;TL1=0x66;TH0=0xff;TL0=0x8c;EA=1;ET0=1;TR0=1;ET1=1;TR1=0;}void main(){m=0;mark1=0;EA=1;IT1=1;EX1=1;//// TMOD=0x11;// TH0=0;// TL0=0;// TR0=1;control=1;jishu=0;servo=0;initial_T01();servo=1;while(1){red_check(); //红外接收处理}}void T0_waytwo() interrupt 1{TH0=0xff;TL0=0x8c;if(jishu<control)servo=1;elseservo=0;jishu++;jishu=jishu%160;}void T1_waytwo() interrupt 3{TH1=0xfc; //1ms一中断TL1=0x66;mark++;if(mark==5){mark=0;if(qianX==1)P1=qianjin[haha];if(houX==1)P1=houtui[haha];//// haha++;// if(haha==2) haha=0;}}。

智能小车避障C程序

智能小车避障C程序

智能小车的四路避障C程序前,左,后避障传感器现在对应中,右,左三路寻线传感器.#include <stdio.h>#include <intrins.h>#include "STC12C5410AD.H"#include "sio.h"#define MIN9MS 0x0120 //9ms中心是0x0159#define MAX9MS 0x01a0#define MIN45MS 0x0090 //4.5ms中心是0x00ac #define MAX45MS 0x00d0#define MIN225MS 0x0040 //2.25ms中心是0x0056#define MAX225MS 0x0080#define MIN056MS 0x000e //0.56ms中心是0x0015#define MAX056MS 0x001b#define MIN168MS 0x0020 //1.68ms中心是0x0040#define MAX168MS 0x0060sfr ISP_CUNTR = 0xE7;sbit LED1 = P1^1;sbit LED2 = P1^2;sbit IR_FRONT = P3^3;sbit IR_LEFT = P3^4;sbit IR_RIGHT = P3^5;sbit IR_BACK = P1^3;sbit IR_OUT = P1^0;sbit PWM0 = P3^7;sbit MOTO_IN_A1 = P1^7;sbit MOTO_IN_A2 = P1^6;sbit MOTO_IN_B1 = P1^5;sbit MOTO_IN_B2 = P1^4;bit power_stat;static unsigned char car_stat; //小车状态:0,停止;1,前进;2,后退;3,左转;4,右转;ff,自控寻线模式static unsigned char code led_mod_table[3][20] = {{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0},{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0} };unsigned char idata led_mod = 0;static unsigned char idata led_tick = 0; static unsigned char idata led_ptr = 0;#define IR_SIGNAL_TOTAL 21#define IR_SIGNAL_VALID 18static unsigned char codeir_table[IR_SIGNAL_TOTAL] = {1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0 };static unsigned char codeir_check_table[IR_SIGNAL_TOTAL] = { 0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0 };static unsigned char idata ir_ptr = 0;static unsigned char idata front_signal = 0; static unsigned char idata back_signal = 0;static unsigned char idata left_signal = 0;static unsigned char idata right_signal = 0;static bit front_obj = 0, back_obj = 0, left_obj = 0, right_obj = 0;static void delay(unsigned long v) {while (v--) {}}/** PCA中断计数,根据位置判断信号区域和定义,位置0表示初始,1代表引导码信号,2表示引导码间隔,* 3表示第一个bit的信号,4表示第一个bit的间隔,以次类推...* 更具体见对应的红外线协议.*/static unsigned char idata pca_tick;static unsigned char idata pca_int_count;static unsigned char data pca_int_total; /* 根据引导头确定总长度*/static unsigned int idata period; /* 红外信号占或空周期计数*/static unsigned char idata data_buf[6]; /* 红外线协议数据缓冲区*/static unsigned int idata ccap1; //PCA0上一次的的计数static unsigned char idata frame_dog; //红外帧看门狗,限定时间未接收完成清除工作void time0_isr() interrupt 1{unsigned char tmp;if (ir_table[ir_ptr++]) {IR_OUT = 0;} else {IR_OUT = 1;}ir_ptr %= IR_SIGNAL_TOTAL;tmp = ir_check_table[ir_ptr];if ((!IR_FRONT && tmp) || (IR_FRONT && !tmp)) front_signal++;if ((!IR_LEFT && tmp) || (IR_LEFT && !tmp))left_signal++;if ((!IR_RIGHT && tmp) || (IR_RIGHT && !tmp)) right_signal++;if ((!IR_BACK && tmp) || (IR_BACK && !tmp)) back_signal++;if (ir_ptr == 0) {// com_putchar(front_signal);if (front_signal >= IR_SIGNAL_VALID) front_obj = 1;elsefront_obj = 0;if (back_signal >= IR_SIGNAL_VALID) back_obj = 1;elseback_obj = 0;if (left_signal >= IR_SIGNAL_VALID)left_obj = 1;elseleft_obj = 0;if (right_signal >= IR_SIGNAL_VALID) right_obj = 1;elseright_obj = 0;front_signal = 0;back_signal = 0;left_signal = 0;right_signal = 0;}}void time0_initialize(void){TMOD &= ~0x0F; /* clear timer 0 mode bits */ TMOD |= 0x02; /* put timer 0 into MODE 2 */ TH0 = 0x5C; /* 256 -XTAL*dur/T1_12/1000000, dur=定时器的周期,以us为单位*/TL0 = 0x5C; /* 100us */PT0 = 0; /* 时钟0中断低优先级*/TR0 = 1;ET0 = 1;}static void wakeup (void) interrupt 2{}static void pca_isr (void) interrupt 6{unsigned char i, j;if (CCF1) {CCF1 = 0; //清PCA1中断标志LED1 = IR_RIGHT;LED2 = IR_RIGHT;if (!pca_int_count) { //第一次收到信号if (!IR_RIGHT) {ccap1 = pca_tick * 256 + CCAP1H;pca_int_count++;}} else { //已经收到一些信号period = pca_tick * 256 + CCAP1H - ccap1;ccap1 = pca_tick * 256 + CCAP1H;//com_putchar(period / 256);//com_putchar(period % 256);if (pca_int_count == 1) {if (period < MIN9MS || period > MAX9MS) { //9mspca_int_count = 0;frame_dog = 0;} elsepca_int_count++;} else if (pca_int_count == 2) {if (period > MIN225MS && period <MAX225MS) { //2.25mspca_int_total = 3;pca_int_count++;} else if (period > MIN45MS && period < MAX45MS) { //4.5mspca_int_total = 67;pca_int_count++;} else {pca_int_count = 0;frame_dog = 0;}} else {if (IR_RIGHT) {if (period > MIN056MS && period < MAX056MS) { //0.56msif (pca_int_count >= pca_int_total) { //帧接收完毕,下面进行有效分析.if (pca_int_total == 67) { //完整信号,含有引导信号,设备码8bit,设备反码8bit,命令字8bit,命令字反码8bitif ((data_buf[0] ^ data_buf[1] == 0xff) && (data_buf[2] ^ data_buf[3] == 0xff)) {com_putchar(data_buf[0]);com_putchar(data_buf[2]);if (data_buf[0] == 0x40) {switch (data_buf[2]) {case 0x5F: //左car_stat = 3;break;case 0x5B: //右car_stat = 4;break;case 0x5A: //上car_stat = 1;break;case 0x5E: //下car_stat = 2;break;case 0x56: //菜单car_stat = 0;break;case 0x0: //数字0car_stat = 0xff;break;case 0x12: //POWER// power_stat =~power_stat;break;default:break;}}}} else { //重复信号,仅含有引导信号}pca_int_count = 0;frame_dog = 0;} else {pca_int_count++;}} else {pca_int_count = 0;frame_dog = 0;}} else {j = (pca_int_count - 3) / 2;i = j / 8;j = j % 8;if (period > MIN168MS && period < MAX168MS) { //1.68ms// com_putchar(0x01);data_buf |= (0x01 << j);pca_int_count++;} else if (period > MIN056MS && period < MAX056MS) { //0.56ms// com_putchar(0x00);data_buf &= ~(0x01 << j);pca_int_count++;} else {pca_int_count = 0;frame_dog = 0;}}}}}if (CF) { //PCA计数溢出中断,19.6608MHZ晶体大约6.7ms溢出CF = 0;pca_tick++;if (led_tick++ >= 10) {led_tick = 0;if (led_mod_table[led_mod][led_ptr++]) {LED1 = 0;LED2 = 0;} else {LED1 = 1;LED2 = 1;}led_ptr %= 20;}if (pca_int_count) {frame_dog++;if (frame_dog >= 15) { //100ms后重新开始分析新的红外线数据包pca_int_count = 0;frame_dog = 0;}}}}void auto_power_down() {delay(30000);ISP_CUNTR = 0x20; //从AP复位并从AP执行}void main (void){unsigned char i;unsigned long j;MOTO_IN_A1 = 0;MOTO_IN_A2 = 0;MOTO_IN_B1 = 0;MOTO_IN_B2 = 0;P1M1 = 0xf0; //P1.7~P1.4 强推挽输出EA = 0;power_stat = 0;time0_initialize();com_initialize (); /* initialize interrupt driven serial I/O */com_baudrate (4800); /* setup for 1200 baud */ /*CMOD = 0x01; // #00000000B,PCA空闲计数,PCA计数源=Fosc/12,PCA溢出中断(做一个定时器使用)CCON = 0x00; //PCA中断标志清0,PCA停止计数CL = 0x0;CH = 0x0;CCAPM1 = 0x31; //PCA1上升下降沿捕获*/CMOD = 0x03; /* #00000010B,PCA空闲计数,PCA计数源=fosc/2,PCA溢出中断*/CCON = 0x00; //PCA中断标志清0,PCA停止计数CL = 0x0;CH = 0x0;CCAPM1 = 0x31; //PCA1上升下降沿捕获CCAPM0 = 0x42; //PCA0工作模式:8位pwm PCA_PWM0 = 0x00;CCAP0L = 0x20;CCAP0H = 0x20;EPCA_LVD = 1; //允许PCA和低压检测中断car_stat = 0;pca_tick = 0;pca_int_count = 0;frame_dog = 0;EA = 1; /* Enable Interrupts */CR = 1; //启动PCA计数while (1){if (power_stat) {// auto_power_down();}if (left_obj || right_obj || front_obj || back_obj) { LED1 = 0;LED2 = 0;}switch (car_stat) {case 0:MOTO_IN_A1 = 0;MOTO_IN_A2 = 0;MOTO_IN_B1 = 0;MOTO_IN_B2 = 0;break;case 0xff:if (!front_obj) { //中线传感器检测到黑线MOTO_IN_A1 = 1;MOTO_IN_A2 = 0;MOTO_IN_B1 = 1;MOTO_IN_B2 = 0;j = 50000;while (j) {j--;if (front_obj)break;}} else {TURNLEFT:MOTO_IN_A1 = 0;MOTO_IN_A2 = 1;MOTO_IN_B1 = 1;MOTO_IN_B2 = 0;j = 1000000;while (1) {j--;if (!front_obj) {goto EXIT;}if (!left_obj)goto TURNLEFT;if (!back_obj)goto TURNRIGHT;if (j == 0)goto EXIT;}TURNRIGHT:MOTO_IN_A1 = 1;MOTO_IN_A2 = 0;MOTO_IN_B1 = 0;MOTO_IN_B2 = 1;j = 1000000;while (j) {j--;if (!front_obj)goto EXIT;if (!back_obj)goto TURNRIGHT;if (!left_obj)goto TURNLEFT;}}EXIT:/*MOTO_IN_A1 = 0;MOTO_IN_A2 = 0;MOTO_IN_B1 = 0;MOTO_IN_B2 = 0;delay(5000);*/break;default:break;}PCON |= 0x01;}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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() ; } } //********************************************************
#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 BTurnLeft_1() { RightStop ; BLeftMotor ; } //******************************************************** // 前进 //******************************************************** void FCar() { FLeftMotor ; FRightMotor ; } //******************************************************** // 后退 //******************************************************** void BCar() { BLeftMotor ; BRightMotor ; } //******************************************************** // 壁障程序 //******************************************************** void Obstacle_Avoid(void) {
//******************************************************** void TurnLeft_Fast() { BLeftMotor ; FRightMotor ; } //******************************************************** // 右转 //******************************************************** void TurnRight_1() { RightStop ; FLeftMotor ; } //******************************************************** // 快速右转 //******************************************************** void TurnRight_Fast() { BRightMotor ; FLeftMotor ; } //******************************************************** // 用倒退的方式进行车头右转 //******************************************************** void BTurnRight_1() { LeftStop ; BRightMotor ; }
智能小车红外避障 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 #define RightStop 2 = 0 ; P0_3 = 0 P0_4 = 0 ; P0_5 = 0
//******************************************************** main() { Timer0Setup(500) ; Timer1Setup(50) ; Obstacle_Avoid() ; // Follow() ; }
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) {
相关文档
最新文档