智能循迹小车程序代码
循迹小车代码
#include<reg52.h>#define uchar unsigned char#define uint unsigned intuint a,d,y,z,h,q;sbit out1 = P0^0 ;//电机驱动输出控制管脚配置sbit out2 = P0^1 ;sbit out3 = P0^2 ;sbit out4 = P0^3 ;sbit in1 = P2^0;//循迹模块的信号输入管脚配置sbit in2 = P2^1;sbit in3 = P2^2;sbit in4 = P2^3;sbit in5 = P2^4;delay(uint a);int zuozhuan(uint z)//控制小车做左转动作(参数z和a可以用来调节左转时间,本程序设定//为不同参数表示每次调用程序左转时间相同不可变,没有调节转弯时间的功能){out1=1;out2=0;out3=0;out4=1;delay(a);}int youzhuan(uint y)//控制小车做右转动作(参数y和a可以用来调节右转时间,本程序设定//为不同参数表示每次调用程序右转时间相同不可变,没有调节转弯时间的功能){out1=0;out2=1;out3=1;out4=0;}int houtui(uint h)//控制小车可以使小车倒退行驶(参数功能与转弯类似){out1=0;out2=1;out3=0;out4=1;delay(a);out1=0;out2=0;out3=0;out4=0;delay(20);}int dengdai (uint d) //使小车停止当前所有动作,停止时间可调。
{out1=0;out2=0;out3=0;out4=0;delay(d);}void qianjin(q)//使小车向前行驶,行驶时间可调{out1=1;out2=0;out3=1;out4=0;delay(q);}void xunji(){P1=P1|0XF0; d elay(5);if(in1==0){dengdai();youzhuan(100);while(in3==1);}else if(in5==0){dengdai();zuozhuan(100);while(in3==1);}else if(in2==0){dengdai();youzhuan(30);}else if(in4==0){dengdai();zuozhuan(30);}else if(in3==0) {qianjin(20); }else {qianjin(20);}}int delay(uint a)//延时子函数{uint x,y;for(x=a;x>0;x--)for(y=110;y>0;y--);}void main(){delay(2000);while(1)xunji();}#include<reg52.h>#define uchar unsigned char#define uint unsigned int//D0-D7:f,b,a,e,d,h,c,g 共阴依次编码//74LS04反相器驱动数码管uchar code table[10] = {0x5F,0x42,0x9E,0xD6,0xC3,0xD5,0xDD,0x46,0xDF,0xD7}; uchar i = 0; //用于0-3数码管轮流显示uint j = 0; //计时的次数uint time=0; //计时uint pwm=16; //占空比uint speed; //调制PWM波的当前的值sbit R=P3^2; //右边传感器P3^2 sbit L=P3^3; //左边传感器P3^3 //电机驱动口定义sbit ENB=P1^0; //前轮电机停止控制使能sbit ENA=P1^1; //后轮控制调速控制端口sbit IN1=P1^2; //前轮sbit IN2=P1^3; //前轮sbit IN3=P1^4; //后轮sbit IN4=P1^5; //后轮void Init() {TMOD = 0x12; //定时器0用方式2,定时器1用方式1TH0=(256-200)/256; //pwmTL0=(256-200)/256;TH1 = 0x0F8; //定时2msTL1 = 0x30;EA = 1;ET0 = 1;ET1 = 1;TR0 = 1;TR1 = 1;}void tim0(void) interrupt 1 //产生PWM{speed ++;if(speed <= pwm) //pwm 就相当于占100的比例{ENA = 1;}else if(speed < 100){ENA = 0;}elsespeed = 0;}void time1() interrupt 3 //定时2ms{TH1 = 0x0F8;TL1 = 0x30;i =(i+1) % 4; //0-3循环j++;if(i == 0) //显示最低位数码管{P0 = 0x00; //段选清零防止乱码P0 = table[time%10]; //送段码信号}if(i == 1){P0 = 0x00;P0 = table[time/ 10% 10];}if(i == 2){P0 = 0x00;P0 = table[time/ 100% 10];}if(i == 3){P0 = 0x00;P0 = table[time/ 1000% 10];}P2 = ( 0x10 << i ); //送位选信号if(j == 500){time ++;//500次为1秒j = 0;}}void forward() //前进{IN2=1;IN1=0;}void back() //后退{IN2=0;IN1=1;}void left() //左转{ENB=1; //打开使能IN3=1;IN4=0;}void right() //右转{ENB=1; //打开使能IN3=0;IN4=1;}void stop() //停止{IN2=0;IN1=0;IN3=0;IN4=0;}void main(){Init(); //定时器初始化while(1){if(R==0 && L==1) //右传感器检测到黑线{right(); //右转forward();while(R==0 && L==1);}if(R==1 && L==0) //左传感器检测到黑线{left();forward();while(R==1 && L==0);}if(R==0 && L==0) //都检测到黑线则停止{stop();j = 0; //定时器仍在工作,计数时间清空}else //都没有检测到黑线{ENB = 0; //关闭转向控制forward();}}}。
智能小车循迹、避障、红外遥控C语言代码
智能小车循迹、避障、红外遥控C语言代码//智能小车避障、循迹、红外遥控 C 语言代码// 实现功能有超声波避障,红外遥控智能小车,红外传感器实现小车自动循迹, 1602 显示小车的工作状态,另有三个独立按键分别控制三种状态的转换// 注:每个小车的引脚配置都不一样,要注意引脚的配置,但是我的代码注释比较多,看起来比较容易一点#include <> #include <> #include"" #include <> #define uchar unsigned char #define uint unsigned int uchar ENCHAR_PuZh1[8]=" uchar ENCHAR_PuZh2[8]=" uchar ENCHAR_PuZh3[8]=" uchar ENCHAR_PuZh4[8]=" uchar ENCHAR_PuZh5[8]=" run back stop left right "; ";//1602 显示数组H. H. H. uchar ENCHAR_PuZh6[8]=" xunji "; uchar ENCHAR_PuZh7[8]=" bizhang"; uchar ENCHAR_PuZh8[8]=" yaokong"; #define HW P2 #define PWM /****************************** P1 //红外传感器引脚配置P2k 口/* L298N 管脚定义*/ 超声波引脚控制******************************/ sbit ECHO=P3A2; sbit TRIG=P3A3;///// 红外控制引脚配置 sbit sbituchar KEY2=P3A7; KEY 仁 P3M;state_total=3,state_2=0;// 2 为红外遥控 ucharuchar time_1 uchar 局变量 // 超声波接收引脚定义 // 超声波发送引脚定义// 红外接收器数据线 // 独立按键控制总状态控制全局变量 state_1,DAT; // 红外扫描标志位time_1=0,time_2=0;// 定时器1 中断全局变量控制转弯延时计数也做延时一次time,timeH,timeL,state=0;// 超声波测量缓冲变量count=0;//1602 显示计数兼红外遥控按键state_total =2 兼循迹按键state_total= 0 自动避障 state_total=10 为自动循迹模块 1 为自动避障模块 time_ 2 控制 PWM 脉冲计数state 为超声波状态检测控制全uint /**************************/ unsigned char IRC0M[7]; // 红外接收头接收数据缓存unsigned char Number,distance[4],date_data[8]={0,0,0,0,0,0,0,0}; /********* voidvoid voidIRC0M[2 ]存放的为数据 // 红外接收缓存变量 **/ IRdelay(char x); //x* 红外头专用 delay run(); 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; TH1=0Xfe; TL1=0x0c; TF0=0; TF1=0; ET0=1; ET1=1; EA=1;// 设置定时器 0 1 // 装入初值定时一次为工作方式 1 16 位初值定时器2000hz// 定时器 // 定时器 // 允许定时器// 允许定时器 0 方式 1 计数溢出标志 1 方式 1 计数溢出标志 0 中断溢出 1 中断溢出//开总中断 if(state_total==1)// 为超声波模块时初始化 {TRIG=0; ECHO=0; EX0=0; IT0=1;}if(state_total==2)// 发射引脚低电平 // 接收引脚低电平 // 关闭外部中断// 由高电平变低电平,触发外部中断 0// 红外遥控初始化{ IT1=1; EX1=1;TRIG=1;}del ay(60);} void main(){ uint i; delay(50); init_test(); TR1=1; LCD1602_Init() ; delay(50); while(state_2==0)// 外部中断 1 为负跳变触发 // 允许外部中断 1 // 为高电平 I/O 口初始化// 等待硬件操作// 开启定时器 1{if(KEY1==0){Delay10ms(); // 消除抖动 if(KEY1==0) {state_total=0; // 总状态定义 0 为自动循迹模块 1 为自动避障模块2 为红外遥控while((i<30)&&(KEY1==0))// 检测按键是否松开{Delay10ms(); i++;}i=0;}}if(TRIG==0){while((i<30)&&(TRIG==0))// 检测按键是否松开{Delay10ms(); i++;}i=0;}if(KEY2==0){while((i<30)&&(KEY2==0))// 检测按键是否松开{Delay10ms(); i++; }i=0;// 检测按键 s1 是否按下//检测按键s2是否按下障模块Delay10ms(); // 消除抖动 if(TRIG==0) { state_total=1; 2 为红外遥控//总状态定义 0 为自动循迹模块 1 为自动避// 检测按键 s3 是否按下障模块Delay10ms(); // 消除抖动 if(KEY2==0) { state_total=2; 2 为红外遥控// 总状态定义 0 为自动循迹模块1 为自动避}}} init_test();delay(50); // 等待硬件操作50us TR1=0; // 关闭定时器 1 if(state_total==1) {//SPEED=90; bizhang_test();} if(state_total==0) {// SPEED=98; 电平// 自动循迹速度控制// 自动循迹速度控制高电平持续次数占空比为10 的低电平高电平持续次数占空比为40 的低xunji_test(); }if(state_total== 2){//SPEED=98; // 自动循迹速度控制高电平持续次数占空比为40 的低电平hongwai_test(); }void 断号init0_suspend(void)2 外部中断0 4 串口中断外部中断 1timeH=TH0;timeL=TL0;state=1;EX0=0;}void 断号0{if(state_total==1) { TH0=0X00;TL0=0x00;}if(state_total==0) { TH0=0Xec;TL0=0x78;time_1++;interrupt 0 //3 为定时器 1 的中断号 1 定时器0 的中// 记录高电平次数//// 标志状态为// 关闭外部中断1,表示已接收到返回信号//3 为定时器 1 的中断号2 外部中断0 4 串口中断time0_suspend0(void) interrupt 1外部中断 1// 自动避障初值装入// 装入初值// 自动循迹初值装入// 装入初值定时一次200hz// 控制转弯延时计数1 定时器0 的中}}void IR_IN(void){unsigned char j,k,N=0;EX1 = 0; IRdelay(5); if (TRIG==1) { EX1 =1; return;}//确认IR 信号出现//等IR 变为高电平,跳过 9ms 的前导低电平信号。
51的智能循迹小车代码
#include<reg52.h>#define sense P1 /*宏定义光电传感器端口*/#define input1 P0 /*左电机的IN1,IN2定义在P0口*/#define input2 P2 /*右电机的IN3,IN4及ENA,ENB定义在P2口*/ //宏定义电机的具体端口sbit MOTO1_INT1=P0^0;sbit MOTO1_INT2=P0^1;sbit MOTO1_ENA=P2^0;sbit MOTO2_INT3=P2^5;sbit MOTO2_INT4=P2^4;sbit MOTO2_ENB=P2^2;//宏定义传感器的具体端口sbit sense_L=P1^3;sbit sense_R=P1^6;//宏定义金属传感器端口sbit METAL=P1^1;//宏定义DELAY函数中的一些变量int Dtime1=20000;int Dtime2=2000;int i=0;unsigned char SIGNAL(void); //传感器信号分析函数void DELAY(void); //延时函数void main(){int a;MOTO1_INT1=1; //使车开始时运动MOTO1_INT2=0;MOTO1_ENA=1;MOTO2_INT3=1;MOTO2_INT4=0;MOTO2_ENB=1;while(1){if(METAL==1) //有金属时车停止DELAY();else{a=SIGNAL();switch(a){case 1:input2=0x25;break; //前进case 2:input2=0x24;break; //左转case 3:input2=0x21;break; //右转default:break;}}}}void DELAY() //延时函数{input2=0x20; //使驱动芯片的两个使能端为0,使两个电机停转for(i=0;i<Dtime1;i++); //实现延时,用DTIME控制input2=0x25; //使驱动芯片的两个使能端为1,使两个电机重新转for(i=0;i<Dtime2;i++);}unsigned char SIGNAL(){unsigned char Re;Re=sense&0x28;if(Re==40)return 1;//前进else if(Re==8)return 2;//左转else if(Re==32)return 3;//右转else return 1;//有错时前进}。
循迹小车程序代码
//(在MAIN中接受铁片颜色判断传感器的信号来赋值) unsigned char Light_Flag=0;//进入光引导区的标志(1) unsigned int cntTime_5Min=0;//时间周期数,用于 T0 精确定时 unsigned int cntTime_Plues=0; //霍尔开关产生的脉冲数 /*============================全局变量定义区 ============================*/ /*------------------------------------------------*/ /*-----------------通用延迟程序-------------------*/ /*------------------------------------------------*/ void delay(unsigned int time) { unsigned int i,j; for(j=0;j<time;j++) { for(i=0;i<60;i++) {;} } } /*-----------------------------------------------*/ /*-------------------显示控制模块----------------*/ /*-----------------------------------------------*/ /*数码管显示,显示铁片的数目(设接在P0,共阴)*/ void Display(unsigned char n) { char Numb[12]= {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x37,0x77}; P0=Numb[n]; } // time*0.5ms延时
循迹小车程序 带注释
left_qian;
//左边电机前进
right_hou;
//右边电机后退
}
else if((left_2==1)&&(left_1==1)&&(righ_1==1)&&(righ_2==0)) //右转弯 幅度大
{
zkb_l=100;
//左边占空比100%
zkb_r=100;
//右边占空比100%
left_qian;
/*********************************************************************/
void timer0() interrupt 1
{
TH0=0xff;
//定时器0高8位初值(65536-100)/256;
TL0=0x9c;
//定时器0低8位初值(65536-100)%256;
//右边占空比20%
left_qian;
//左边电机前进
right_qian;
//右边电机前进
}
else if((left_2==1)&&(left_1==1)&&(righ_1==0)&&(righ_2==0)) //右转弯 幅度中
{
zkb_l=100;
//左边占空比100%
zkb_r=40;
//右边占空比40%
//左转弯 幅度大
{
zkb_l=100;
//左边占空比100%
zkb_r=100;
//右边占空比100%
left_hou;
//左边电机后退
right_qian;
//右边电机前进
循迹小车程序
#include<reg52.h>sbit IN1=P3^3;sbit IN2=P3^4;sbit IN3=P3^5;sbit IN4=P3^6;sbit PWM1=P1^0;sbit PWM2=P1^1;//传感器管脚位声明? sbit L1=P2^0;//左侧第一个对管?sbit L2=P2^1;//左侧第二个对管?sbit R1=P2^3;//右侧第一个对管sbit R2=P2^4;//右侧第二个对管?//int count1=0;int count2=0;//左电机前进void forward_turn1(){IN1=1;IN2=0;}//左电机后退?void back_turn1(){IN1=0;IN2=1;}//右电机前进?void forward_turn2(){IN3=0;IN4=1;}//右电机后退?void back_turn2(){IN3=1;IN4=0;}//左电机速度控制?void speed1(int ct,int sd){if(ct<=sd)PWM1=1;elsePWM1=0;}//右电机速度控制void speed2(int ct,int sd){if(ct<=sd)PWM2=1;elsePWM2=0;}//小车直线前进?V oid stop(){IN1=1;1N2=1;IN3=1;IN4=1;}void advance(int ct1,int sd1,int ct2,int sd2) {forward_turn1();forward_turn2();speed1(ct1,sd1);speed2(ct2,sd2);}//小车左转?void left_turn(int ct1,int sd1,int ct2,int sd2) {forward_turn1();forward_turn2();speed1(ct1,sd1);speed2(ct2,sd2);}//小车右转?void right_turn(int ct1,int sd1,int ct2,int sd2) {forward_turn1();forward_turn2();speed1(ct1,sd1);speed2(ct2,sd2);}//主函数main(){TMOD=0X11;TH0=(65536-1000)/256;TL0=(65536-1000)%256;EA=1;ET0=1;TR0=1;TMOD=0X11;TH0=(65536-1000)/256;TL0=(65536-1000)%256;EA=1;ET0=1;TR0=1;while(1){if(L1==0&&R1==0&&L2==0&&R2==0)//小车未检测到黑线???{advance(count1,500,count2,500);}If(L1==0&&L2==1&&R1==1&&R2==0){advance(count1,500,count2,500);}if(L1==1&&R1==0&&L2==0&&R2==0)//左边内侧检测到黑线,小左转??? {left_turn(count1,200,count2,700);}if(L1==0&&R1==0&&L2==1&&R2==0)//左边外侧检测到黑线,大左转??? {left_turn(count1,200,count2,500);}if(L1==0&&R1==1&&L2==0&&R2==0)//右边内侧检测到黑线,小右转??? {right_turn(count1,700,count2,200);}if(L1==0&&R1==0&&L2==0&&R2==1)//右边外侧检测到黑线,大右转??? {right_turn(count1,500,count2,200);}if(L1==1&&R1==0&&L2==1&&R2==0)//左边两侧检测到黑线,中左转?? {left_turn(count1,200,count2,600);}if(L1==0&&R1==1&&L2==0&&R2==0)//右边两侧检测到黑线,中右转{left_turn(count1,600,count2,200);}if(L1==1&&R1==1&&L2==1&&R2==1)//都在线上,停{Stop();a}}}//中断服务程序?void time0()interrupt 1{TH0=(65536-1000)/256;TL0=(65536-1000)%256;count1++;if(count1>=1000)count1=0;}void time1()interrupt 1{TH0=(65536-1000)/256;TL0=(65536-1000)%256;count2++;if(count2>=1000)count2=0;}。
循迹小车程序代码(带解释说明)
int Left_motor_go=6; //左电机前进(IN1)int Left_motor_back=7; //左电机后退(IN2)int Right_motor_go=9; // 右电机前进(IN3)int Right_motor_back=10; // 右电机后退(IN4)const int SensorRight = 3; //右循迹红外传感器(P3.2 OUT1)const int SensorLeft = 4; //左循迹红外传感器(P3.3 OUT2)int SL; //左循迹红外传感器状态int SR; //右循迹红外传感器状态void setup(){//初始化电机驱动IO为输出方式pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM)pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)pinMode(SensorRight, INPUT); //定义右循迹红外传感器为输入pinMode(SensorLeft, INPUT); //定义左循迹红外传感器为输入}//=======================智能小车的基本动作========================= //void run(int time) // 前进void run(){digitalWrite(Right_motor_go,HIGH); // 右电机前进digitalWrite(Right_motor_back,LOW);analogWrite(Right_motor_go,20);//PWM比例0~255调速,左右轮差异略增减analogWrite(Right_motor_back,0);digitalWrite(Left_motor_go,LOW); // 左电机前进digitalWrite(Left_motor_back,HIGH);analogWrite(Left_motor_go,0);//PWM比例0~255调速,左右轮差异略增减analogWrite(Left_motor_back,127 );//delay(time * 100); //执行时间,可以调整}//void brake(int time) //刹车,停车void brake(){digitalWrite(Right_motor_go,LOW);digitalWrite(Right_motor_back,LOW);digitalWrite(Left_motor_go,LOW);digitalWrite(Left_motor_back,LOW);//delay(time * 100);//执行时间,可以调整}//void left(int time) //左转(左轮不动,右轮前进) void left(){digitalWrite(Right_motor_go,HIGH);// 右电机前进digitalWrite(Right_motor_back,LOW);analogWrite(Right_motor_go,1);analogWrite(Right_motor_back,0);//PWM比例0~255调速digitalWrite(Left_motor_go,LOW); //左轮后退digitalWrite(Left_motor_back,LOW);analogWrite(Left_motor_go,0);analogWrite(Left_motor_back,0);//PWM比例0~255调速//delay(time * 100);//执行时间,可以调整}void right(){digitalWrite(Right_motor_go,LOW); //右电机后退digitalWrite(Right_motor_back,LOW);analogWrite(Right_motor_back,0);//PWM比例0~255调速digitalWrite(Left_motor_go,LOW);//左电机前进digitalWrite(Left_motor_back,HIGH);analogWrite(Left_motor_go,0);analogWrite(Left_motor_back,127);//delay(time * 100);//执行时间,可以调整}void back(int time){digitalWrite(Right_motor_go,LOW); //右轮后退digitalWrite(Right_motor_back,HIGH);analogWrite(Right_motor_go,0);analogWrite(Right_motor_back,60);//PWM比例0~255调速digitalWrite(Left_motor_go,HIGH); //左轮后退digitalWrite(Left_motor_back,LOW);analogWrite(Left_motor_go,60);analogWrite(Left_motor_back,0);//PWM比例0~255调速delay(time * 100); //执行时间,可以调整}//==========================================================void loop(){//有信号为LOW 没有信号为HIGH 检测到黑线输出高检测到白色区域输出低SR = digitalRead(SensorRight);//有信号表明在白色区域,车子底板上L1亮;没信号表明压在黑线上,车子底板上L1灭SL = digitalRead(SensorLeft);//有信号表明在白色区域,车子底板上L2亮;没信号表明压在黑线上,车子底板上L2灭if (SL == LOW&&SR==LOW)run(); //调用前进函数else if (SL == HIGH & SR == LOW)// 左循迹红外传感器,检测到信号,车子向右偏离轨道,向左转left();else if (SR == HIGH & SL == LOW) // 右循迹红外传感器,检测到信号,车子向左偏离轨道,向右转right();else // 都是黑色, 停止brake();}。
智能红外循迹小车程序
#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;}}}}。
智能循迹小车程序代码
/****************************************************************************硬件连接P1_4接驱动模块ENA使能端,输入PWM信号调节速度P1_5接驱动模块ENB使能端,输入PWM信号调节速度P1_0 P1_1接IN1 IN2 当P1_0=1,P1_1=0; 时左电机正转驱动蓝色输出端OUT1 OUT2接左电机P1_0 P1_1接IN1 IN2 当P1_0=0,P1_1=1; 时左电机反转P1_2 P1_3接IN3 IN4 当P1_2=1,P1_3=0; 时右电机正转驱动蓝色输出端OUT3 OUT4接右电机P1_2 P1_3接IN3 IN4 当P1_2=0,P1_3=1; 时右电机反转P1_0接四路寻迹模块接口第一路输出信号即中控板上面标记为OUT1P1_1接四路寻迹模块接口第二路输出信号即中控板上面标记为OUT2P1_2接四路寻迹模块接口第三路输出信号即中控板上面标记为OUT3P1_3接四路寻迹模块接口第四路输出信号即中控板上面标记为OUT4八路寻迹传感器有信号(白线)为0 没有信号(黑线)为1****************************************************************************/#include<AT89x51.H>#define Right_moto_pwm P1_4 //接驱动模块ENA使能端,输入PWM信号调节速度#define Left_moto_pwm P1_5 //接驱动模块ENB使能端,输入PWM 信号调节速度#define Left_1_led P2_0 //四路寻迹模块接口第一路#define Left_2_led P2_1 //四路寻迹模块接口第二路#define Right_1_led P2_2 //四路寻迹模块接口第三路#define Right_2_led P2_3 //四路寻迹模块接口第四路#define Left_moto_go {P1_0=0,P1_1=1;} //左电机前进#define Left_moto_back {P1_0=1,P1_1=0;} //左电机后退#define Left_moto_stop {P1_0=1,P1_1=1;} //左电机停转#define Right_moto_go {P1_2=0,P1_3=1;} //右电机前转#define Right_moto_back {P1_2=1,P1_3=0;} //右电机后退#define Right_moto_stop {P1_2=1,P1_3=1;} //右电机停转#define uchar unsigned char#define uint unsigned intuchar pwm_val_left =0;uchar push_val_left =0; //左电机占空比N/10uchar pwm_val_right =0;uchar push_val_right=0; //右电机占空比N/10bit Right_moto_stp=1;bit Left_moto_stp =1;/**************************************************************** ********/void run(void)//前进函数{push_val_left =13; //PWM 调节参数1-20 1为最慢,20是最快改这个值可以改变其速度push_val_right =15; //PWM 调节参数1-20 1为最慢,20是最快改这个值可以改变其速度Left_moto_go ;//左电机前进Right_moto_go ; //右电机前进}/**************************************************************** ********/void left(void) //左转函数{push_val_left =8;push_val_right =9;Right_moto_go;//右电机继续Left_moto_stop;//左电机停走}/************************************************************************/void right(void)//右转函数{push_val_left =8;push_val_right =9;Right_moto_stop;//右电机停走Left_moto_go;//左电机继续}void Delayms(uint x){uchar i;while(x--)for(i=0;i<120;i++);}void stop(void){Right_moto_stop;//右电机停走Left_moto_stop;//左电机停走Delayms(3000);run();Delayms(100);}/*************************PWM调制电机转速********************************/void pwm_out_left_moto(void) //左电机调速,调节push_val_left的值改变电机转速,占空比{if(Left_moto_stp){if(pwm_val_left<=push_val_left)Left_moto_pwm=1;elseLeft_moto_pwm=0;if(pwm_val_left>=20)pwm_val_left=0;}elseLeft_moto_pwm=0;}void pwm_out_right_moto(void) //右电机调速,调节push_val_left的值改变电机转速,占空比{if(Right_moto_stp){if(pwm_val_right<=push_val_right)Right_moto_pwm=1;elseRight_moto_pwm=0;if(pwm_val_right>=20)pwm_val_right=0;}elseRight_moto_pwm=0;}/***************************************************/void xunji(){switch(P2&0x0f){case 0x00: // 全部没有压线,直走run(); break;case 0x01:// 右压线,左转left(); break;case 0x02: // 右压线,左转left(); break;case 0x04: // 左压线,转右right(); break;case 0x08: // 左压线,右转right();break;case 0x0f:stop();break;default:run(); break;}}/***********TIMER0中断服务子函数产生PWM信号**********/ void timer0()interrupt 1 using 2{TH0=0XF8; //2Ms定时TL0=0X30;pwm_val_left++;pwm_val_right++;pwm_out_left_moto();pwm_out_right_moto();}/***************************************************/ void main(void){TMOD=0X01;TH0= 0XF8; //2ms定时TL0= 0X30;TR0= 1;ET0= 1;EA = 1;while(1)/*无限循环*/{xunji();} }。
智能循迹小车源程序
#include<reg52.h>sbit lsig=P2^4; //循迹IO口定义sbit msig=P2^5;sbit rsig=P2^6;sbit gl1=P2^0; //电机IO口定义sbit gl2=P2^1;sbit gr1=P2^2;sbit gr2=P2^3;sbit c1=P1^3; //测速IO口定义sbit c2=P1^4;sbit rs=P1^0; //lcd液晶定义sbit rw=P1^1;sbit lcde=P1^2;#define uchar unsigned char#define uint unsigned intuchar code tab1[]=" JH UNIVERSITY ";uchar code tab4[]=" WUXIN XUEYUAN ";uchar code tab5[]=" NAME: LIULI ";uchar code tab2[]=" DIANXIN ONE ";uchar code tab3[]="V=0.48 m/s ";uchar t=0,a,i,f1,f2,num1=100,num2=100,m,n=0; uint vc[10]={54,50,53,51,57,53,55,51,50,54}; static uint b=0;static int p,q=0;uint temp1=0,temp2=0,v1,v2,v;void delay2(uint xms) //延时{uint i,j;for(i=xms;i>0;i--)for(j=110;j>0;j--);}void delay(void) //误差 -0.000000000001us {unsigned char a,b;for(b=15;b>0;b--)for(a=152;a>0;a--);}/**1602显示**/void write_com(uchar com) //写指令{rw=0;rs=0;P0=com;delay2(1);lcde=1;lcde=0;}void write_date(uchar date) //写数据{rs=1;rw=0;P0=date;delay2(1);lcde=1;lcde=0;}void init_lcd(){write_com(0x06);delay();write_com(0x0c);delay();write_com(0x38);delay();write_com(0x01);delay();write_com(0x80);i=0;while(tab1[i]!='\0'){write_date(tab1[i++]);delay2(150);}delay2(200);write_com(0x80);i=0;while(tab4[i]!='\0'){write_date(tab4[i++]);delay2(150);}delay2(200);write_com(0x80);i=0;while(tab2[i]!='\0'){write_date(tab2[i++]);delay2(150);}delay2(200);write_com(0x80);i=0;while(tab5[i]!='\0'){write_date(tab5[i++]);delay2(150);}write_com(0xc0);i=0;while(tab3[i]!='\0'){write_date(tab3[i++]);delay();}while(1);}void pwm(uchar num1,uchar num2){if(t>=100)t=0;if(t<num1){gl1=1;gl2=0;}else{gl1=0;gl2=0;}if(t<num2){gr1=1;gr2=0;}else{gr1=0;gr2=0;}}/********高效延时********/void delay3(){uint j;for(j=7500;j>0;j--){if((rsig==0)&&(lsig==0)&&(msig==1)){p=1;pwm(80,80);}else if((rsig==0)&&(lsig==1)&&(msig==0)){p=2;pwm(30,80);}else if((rsig==0)&&(lsig==1)&&(msig==1)){p=3;pwm(40,60);}else if((rsig==1)&&(lsig==0)&&(msig==0)){p=4;pwm(80,30);}else if((rsig==1)&&(lsig==0)&&(msig==1)){p=5;pwm(60,40);}else if ((rsig==0)&&(lsig==0)&&(msig==0)){if((p==2)||(p==3))pwm(30,80);else if ((p==4)||(p==5))pwm(80,30);//else pwm(80,80);}}}/************************计算小车转速**********************************/ /*ceshu(void ){if(c1==1) f1=1;else if(c1==0&&f1==1){f1=0;temp1++;}if(c2==1) f2=1;else if(c2==0&&f2==1){f2=0;temp2++;}if(m==20) //每秒脉冲数{v1=temp1*4;// 每次脉冲走4cmv2=temp2*4;if(v1>999)v1=0;if(v2>999)v2=0;m=0;temp1=0;temp2=0;}v=(v1+v2)/2;return v;}*/void display1(void) //LCD显示数据 1{write_com(0xc0+5);write_date('0'+vc[b]%10);delay();write_com(0xc0+4);write_date('0'+vc[b]/10%10);delay();write_com(0xc0+2);write_date('0'+vc[b]/100);delay();}void init (){TMOD=0x11;IE=0x8b;IT0=1;EX0=1;TH0 = 0xfe;TL0 = 0x33;TH1=0x4c;TL1=0xd0;gl1=1;gl2=1;gr1=1;gr2=1;}main(){t=0;init();TR0=1; //定时器0打开TR1=1; //定时器1打开while(1){if((rsig==0)&&(lsig==0)&&(msig==1)){p=1;pwm(80,80);}else if((rsig==0)&&(lsig==1)&&(msig==0)){p=2;pwm(30,80);}else if((rsig==0)&&(lsig==1)&&(msig==1)){p=3;pwm(40,60);}else if((rsig==1)&&(lsig==0)&&(msig==0)){p=4;pwm(80,30);}else if((rsig==1)&&(lsig==0)&&(msig==1)){p=5;pwm(60,40);}else if((rsig==1)&&(lsig==1)&&(msig==1)){delay2(100);q++;if(q==4){while(n+1){delay3();vc[n]=v;n++;if(n==10) break;}}if(q==5){pwm(0,0);delay2(5000);init_lcd();}}else if ((rsig==0)&&(lsig==0)&&(msig==0)){if((p==2)||(p==3))pwm(30,80);else if ((p==4)||(p==5))pwm(80,30);//else pwm(80,80);}}}void interrupt0() interrupt 0{display1();b++;if(b==9)b=0;delay2(20);}void timer() interrupt 1 //定时器0{TH0 = 0xfe;TL0 = 0x33;t++;}void T1_time()interrupt 3 //定时器1 {TH1=0x4c;TL1=0xd0;m++;if(c1==1) f1=1;else if(c1==0&&f1==1){f1=0;temp1++;}if(c2==1) f2=1;else if(c2==0&&f2==1){f2=0;temp2++;}if(m==20) //每秒脉冲数{v1=temp1*4;// 每次脉冲走4cmv2=temp2*4;if(v1>999)v1=0;if(v2>999)v2=0;m=0;temp1=0;temp2=0;}v=(v1+v2)/2+35;}。
寻迹小车源代码
#include <89c52.h>#define uint unsigned int#define uchar unsigned char/*********************电机驱动管脚定义****************************/ sbit en1=P1^0;sbit en2=P1^1;sbit s1=P1^2;sbit s2=P1^3;sbit s3=P1^4;sbit s4=P1^5;sbit OE =P2^5;sbit EOC=P2^6;sbit ST =P2^7;sbit CLK =P2^4;sbit ADDA=P2^0;void display(uchar num)/**************************全局变量*******************************/ uchar t=0; /* 中断计数器*/uchar m1=0; /* 电机1速度值*/uchar m2=0; /* 电机2速度值*/uchar tmp1,tmp2;/*********************延时函数*************************************/ void delay(uint x){uint i,j;for(i=x;i>0;i--)for(j=110;j>0;j--);}/*********************定时器初始化函数***************************/ void ini_timer0(void) //定时器0{EA=1;ET0=1;TMOD=0x02;TH0=156; //重装初值使定时器定在100usTL0=156;TR0=1;}void ini_timer1(void) //定时器1{EA=1;ET0=1;TMOD=0x02;TH0=156; //重装初值使定时器定在100us}/**********************主函数********************************/void main(){uchar ADC=0; //存放转换后的数据CLK=1;TMOD=0x01;TH0=0xff; //初始化TL0=0x38;EA=1;ET0=1; // 允许中断TR0=1;ADDA=1; //选择ADC0808的通道3while(1){ST=0;ST=1; //上升沿清除ADC寄存器ST=0; //下降沿启动转换while(!EOC); //等待转换结束OE=1; //允许输出delay(5);ADC=P1;OE=0; //关闭输出display(ADC);}}/************************小车启动函数********************************/ void start_car(void) //启动函数,开启定时器,暂时处于停车待机状态{s1=0;s2=0;s3=0;s4=0;en1=1;en2=1;}/**********************电机处理函数*****************************/ void motor(uchar index, uchar speed){if(index==1) /* 电机1的处理*/{m1=speed;}if(index==2) /* 电机2的处理*/{m2=speed;s3=1;s4=0;}}/***********************T0中断服务程序**************************/void timer0() interrupt 1{if(m1==0||m2==0)t=0;if(t==0) /* 1个PWM周期完成后才会接受新数值*/{tmp1=m1;tmp2=m2;}if(t<tmp1)en1=1;elseen1=0; /* 产生电机1的PWM信号*/if(t<tmp2)en2=1;elseen2=0; /* 产生电机2的PWM信号*/t++;if(t>=100)t=0; /* 1个PWM信号由100次中断产生*/}/********************* T1中断服务程序******************************************/ void time1() interrupt 1{CLK=~CLK;TH0=0xff; //初始化TL0=0x38;}void display(uchar num){P2=0X00; //数码管位P0=seg[num/100]; //数码管段delay(5);P2=0X01; //数码管位P0=seg[num%100/10]; delay(5);P2=0X02; //数码管位P0=seg[num%10]; delay(5);}。
arduino智能循迹小车代码(三个循迹模块)
arduino智能循迹⼩车代码(三个循迹模块)#include <Servo.h>int leftMotor1 = 3;int leftMotor2 = 5;int rightMotor1 = 6;int rightMotor2 = 11;int sum=0;void setup() {Serial.begin(9600);pinMode(leftMotor1, OUTPUT);pinMode(leftMotor2, OUTPUT);pinMode(rightMotor1, OUTPUT);pinMode(rightMotor2, OUTPUT);pinMode(A0, INPUT);pinMode(A1, INPUT);pinMode(A2, INPUT);}void loop() {tracing();}void tracing(){int data[4];data[0]=analogRead(A0);data[1]=analogRead(A1);data[2]=analogRead(A2);if(data[0]<210&&data[1]>500&&data[2]<210)//向前⾛{analogWrite(3,100);analogWrite(5,0);analogWrite(6,100);analogWrite(11,0);}if(data[0]>500 &&data[1]<210 && data[2]<210) // ⼩车偏左{analogWrite(3,0);analogWrite(5,0);analogWrite(6,120);analogWrite(11,0);}if(data[0]>500&&data[1]>500&&data[2]<210) //⼩车偏⼤左{analogWrite(3,0);analogWrite(5,120);analogWrite(6,120);analogWrite(11,0);}if(data[0]<210&&(data[1]-30)<210&&data[2]>500) //⼩车偏右{analogWrite(3,120);analogWrite(5,0);analogWrite(6,0);analogWrite(11,0);}if(data[0]<210&&data[1]>500&&data[2]>500) //⼩车偏⼤右{analogWrite(3,120);analogWrite(5,0);analogWrite(6,0);analogWrite(11,120);}if(data[0]>500&&data[1]>500&&data[2]>500) //左右都检测到⿊线是停⽌{analogWrite(3,0);analogWrite(5,0);analogWrite(6,0); analogWrite(11,0);}Serial.print(data[0]); Serial.print("---"); Serial.print(data[1]-30); Serial.print("---"); Serial.print(data[2]); Serial.print("---"); Serial.println(data[3]); }。
循迹小车设计(附代码)
if(d5==1&&d6==0&&d7==0)
turn_left2();
}
void init()//初始化函数;
{
TMOD=0X01;
TH0=(65536-100)/256; //设置计时时间长度100us
TL0=(65536-100)%256;
ET0=1;
EA=1;
TR0=1;
基于单片机的智能寻迹小车
一.方案设计与论证
1.1控制模块采用STC89C52单片机
设计中采用了一款十分常用的51系列单片机作为处理器,特点是价格低廉、使用方便,且可与其他处理器进行通讯。
系统时钟:晶振频率1/12,本设计采用12M晶振,因此系统时钟为1us。
I/O口资源:4个通用8位准双向I/O口(P0、P1、P2、P3,其中P3为特殊功能口)。
{
d0=0;
d1=1;
d2=0;
d3=0;
zkb1=30;
zkb2=0;
}
void turn_right2()//2级右转
{
d0=0;
d1=1;
d2=0;
d3=0;
zkb1=50;
zkb2=0;
}
void xunji() /*检测到黑线输出为高电平1,检测到白色为低电平0*/
{
if((d5==1&&d6==1&&d7==1)||(d5==0&&d6==1&&d7==0))
#define uint unsigned int
uchar x3,x4;
uint zkb1,zkb2,t=0;
sbit d0=P1^0; /*d1到d3为控制电机的输出口*/
智能循迹小车程序
L#include<reg52.h>#define uchar unsigned char#define uint unsigned int//D0-D7:f,b,a,e,d,h,c,g 共阴依次编码//74LS04反相器驱动数码管uchar code table[10] = {0x5F,0x42,0x9E,0xD6,0xC3,0xD5,0xDD,0x46,0xDF,0xD7};uchar i = 0; //用于0-3数码管轮流显示uint j = 0; //计时的次数uint time=0; //计时uint pwm=16; //占空比uint speed; //调制PWM波的当前的值sbit R=P3^2; //右边传感器P3^2sbit L=P3^3; //左边传感器P3^3//电机驱动口定义sbit E NB=P1^0; //前轮电机停止控制使能sbit E NA=P1^1; //后轮控制调速控制端口sbit I N1=P1^2; //前轮sbit I N2=P1^3; //前轮sbit I N3=P1^4; //后轮sbit I N4=P1^5; //后轮void Init(){TMOD = 0x12; //定时器0用方式2,定时器1用方式1TH0=(256-200)/256; //pwmTL0=(256-200)/256;TH1 = 0x0F8; //定时2msTL1 = 0x30;EA = 1;ET0 = 1;ET1 = 1;TR0 = 1;TR1 = 1;}void tim0(void) interrupt 1 //产生PWM{speed ++;if(speed <= pwm) //pwm 就相当于占100的比例{ENA = 1;}else if(speed < 100){ENA = 0;}elsespeed = 0;}void time1() interrupt 3 //定时2ms{TH1 = 0x0F8;TL1 = 0x30;i =(i+1) % 4; //0-3循环j++;if(i == 0) //显示最低位数码管{P0 = 0x00; //段选清零防止乱码P0 = table[time%10]; //送段码信号}if(i == 1){P0 = 0x00;P0 = table[time/ 10% 10];}if(i == 2){P0 = 0x00;P0 = table[time/ 100% 10];}if(i == 3){P0 = 0x00;P0 = table[time/ 1000% 10];}P2 = ( 0x10 << i ); //送位选信号if(j == 500){time ++; //500次为1秒j = 0;}}void forward() //前进{IN2=1;IN1=0;}void back() //后退{IN2=0;IN1=1;}void left() //左转{ENB=1; //打开使能IN3=1;IN4=0;}void right() //右转{ENB=1; //打开使能IN3=0;IN4=1;}void stop() //停止{IN2=0;IN1=0;IN3=0;IN4=0;}void main(){Init(); //定时器初始化while(1){if(R==0 && L==1) //右传感器检测到黑线{right(); //右转forward();while(R==0 && L==1);}if(R==1 && L==0) //左传感器检测到黑线{left();forward();while(R==1 && L==0);}if(R==0 && L==0) //都检测到黑线则停止{stop();j = 0; //定时器仍在工作,计数时间清空}else //都没有检测到黑线{ENB = 0; //关闭转向控制forward();}}}。
智能小车代码
智能小车代码一、主程序#include<reg51.h>#include <INTRINS.H>#include "pwm.h"#include "delay.h"#include "tracking.h"#define uchar unsigned char#define uint unsigned intsbit front=P3^3; /*前边红外避障*/sbit right1=P3^4; /*右前红外避障*/sbit right2=P3^5; /*右后红外避障*/sbit left1=P3^6; /*左前红外避障*/sbit left2=P3^7; /*左后红外避障*/sbit shine0=P1^0; /*前趋光*/sbit shine1=P1^1; /*左趋光*/sbit shine2=P1^2; /*右趋光*/sbit beef=P1^3;sbit led=P2^4; /*声光报警led灯*/uchar flag=0;void main(void){ uchar k;int i;beef=1;pwm_init();/*******从A到B******************************/ while(right1==0 || right2==0 ){if(left1==0 && left2==0)go(15,1);else if(left1==1 && left2==0)go(3,15);else if(left1==0 && left2==1)go(15,3);elsego(14,15);}/******转弯进入Ⅰ区***************/go(20,-20); //左转delay(4);TR0=0;stop();delay(1000);TR0=1;go(20,15);delay(180);while(front==1) //检测到白板前直走{go(17,15);}TR0=0;stop();delay(200);TR0=1;go(20,-20); //检测到白板就左转delay(4);TR0=0;stop();delay(1000);TR0=1;while(left1==0 || left2==0) //靠左边白板直走{if(left1==0 && left2==0)go(15,1);else if(left1==1 && left2==0)go(3,15);else if(left1==0 && left2==1)go(15,3);elsego(14,15);}while(right1==0 || right2==0){if(left1==0 && left2==0)go(15,1);else if(left1==1 && left2==0)go(3,15);else if(left1==0 && left2==1)go(15,3);elsego(14,15);}while(front==1){go(15,12);}TR0=0;stop();delay(1000);/*****左转进入Ⅱ区**************/TR0=1;go(-20,20);delay(4);TR0=0;stop();delay(1000);TR0=1;tracking_stop(); //打开循迹,当3个红外同检测到黑线时TR0=0;stop();k=100; //停留5 秒,并发出断续声光报警。
智能循迹小车详细源代码程序(MSP430,PID)
81 LEFTOUT=Period/100*speed;
82 }
83 void MotorRight(int speed,int direction)
84 {
85 if(speed>max_speed)speed=max_speed;
86
72 {
73 EnableLeftNeg;
74 UnenableLeftPos;
75 }
76 else if(direction==foreward)//正转
77 {
78 EnableLeftPos;
79 UnenableLeftNeg;
17 #define LEFTOUT TACCR1
18 #define RIGHTOUT TACCR2
19 #define SensorIn P5IN
20 #define F 5000//5000hz
21 #define Period (8000000/F)
64 {
65 LEFTOUT=0;
66 RIGHTOUT=0;
67 }
68 void MotorLeft(int speed,int direction)
69 {
70 if(speed>max_speed)speed=max_speed;
71 if(direction==backward)//反转
183 TACCTL2|=OUTMOD_7;//
184 LEFTOUT=0;
185 RIGHTOUT=0;
186 }
187
188 float abs(float a)
189 {
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/****************************************************************
************
硬件连接
P1_4接驱动模块ENA使能端,输入PWM信号调节速度
P1_5接驱动模块ENB使能端,输入PWM信号调节速度
P1_0 P1_1接IN1 IN2 当P1_0=1,P1_1=0; 时左电机正转驱动蓝色输出
端OUT1 OUT2接左电机
P1_0 P1_1接IN1 IN2 当P1_0=0,P1_1=1; 时左电机反转
P1_2 P1_3接IN3 IN4 当P1_2=1,P1_3=0; 时右电机正转驱动蓝色输出
端OUT3 OUT4接右电机
P1_2 P1_3接IN3 IN4 当P1_2=0,P1_3=1; 时右电机反转
P1_0接四路寻迹模块接口第一路输出信号即中控板上面标记为OUT1
P1_1接四路寻迹模块接口第二路输出信号即中控板上面标记为OUT2
P1_2接四路寻迹模块接口第三路输出信号即中控板上面标记为OUT3
P1_3接四路寻迹模块接口第四路输出信号即中控板上面标记为OUT4
八路寻迹传感器有信号(白线)为0 没有信号(黑线)为1
*****************************************************************
***********/
#include<AT89x51.H>
#define Right_moto_pwm P1_4 //接驱动模块ENA使能端,输入PWM
信号调节速度
#define Left_moto_pwm P1_5 //接驱动模块ENB使能端,输入PWM 信号调节速度
#define Left_1_led P2_0 //四路寻迹模块接口第一路
#define Left_2_led P2_1 //四路寻迹模块接口第二路
#define Right_1_led P2_2 //四路寻迹模块接口第三路
#define Right_2_led P2_3 //四路寻迹模块接口第四路
#define Left_moto_go {P1_0=0,P1_1=1;} //左电机前进
#define Left_moto_back {P1_0=1,P1_1=0;} //左电机后退
#define Left_moto_stop {P1_0=1,P1_1=1;} //左电机停转
#define Right_moto_go {P1_2=0,P1_3=1;} //右电机前转
#define Right_moto_back {P1_2=1,P1_3=0;} //右电机后退
#define Right_moto_stop {P1_2=1,P1_3=1;} //右电机停转
#define uchar unsigned char
#define uint unsigned int
uchar pwm_val_left =0;
uchar push_val_left =0; //左电机占空比N/10
uchar pwm_val_right =0;
uchar push_val_right=0; //右电机占空比N/10
bit Right_moto_stp=1;
bit Left_moto_stp =1;
/**************************************************************** ********/
void run(void)//前进函数
{
push_val_left =13; //PWM 调节参数1-20 1为最慢,20是最快改这个值可以改变其速度
push_val_right =15; //PWM 调节参数1-20 1为最慢,20是最快改这个值可以改变其速度
Left_moto_go ;//左电机前进
Right_moto_go ; //右电机前进
}
/**************************************************************** ********/
void left(void) //左转函数
{
push_val_left =8;
push_val_right =9;
Right_moto_go;//右电机继续
Left_moto_stop;//左电机停走
}
/********************************************************************
****/
void right(void)//右转函数
{
push_val_left =8;
push_val_right =9;
Right_moto_stop;//右电机停走
Left_moto_go;//左电机继续
}
void Delayms(uint x)
{
uchar i;
while(x--)for(i=0;i<120;i++);
}
void stop(void)
{
Right_moto_stop;//右电机停走
Left_moto_stop;//左电机停走
Delayms(3000);
run();
Delayms(100);
}
/*************************PWM调制电机转速********************************/
void pwm_out_left_moto(void) //左电机调速,调节push_val_left的值改变电机转速,占空比
{
if(Left_moto_stp)
{
if(pwm_val_left<=push_val_left)
Left_moto_pwm=1;
else
Left_moto_pwm=0;
if(pwm_val_left>=20)
pwm_val_left=0;
}
else
Left_moto_pwm=0;
}
void pwm_out_right_moto(void) //右电机调速,调节push_val_left的值改变电机转速,占空比
{
if(Right_moto_stp)
{
if(pwm_val_right<=push_val_right)
Right_moto_pwm=1;
else
Right_moto_pwm=0;
if(pwm_val_right>=20)
pwm_val_right=0;
}
else
Right_moto_pwm=0;
}
/***************************************************/
void xunji()
{
switch(P2&0x0f)
{
case 0x00: // 全部没有压线,直走
run(); break;
case 0x01:// 右压线,左转
left(); break;
case 0x02: // 右压线,左转
left(); break;
case 0x04: // 左压线,转右
right(); break;
case 0x08: // 左压线,右转
right();break;
case 0x0f:
stop();break;
default:
run(); break;
}
}
/***********TIMER0中断服务子函数产生PWM信号**********/ void timer0()interrupt 1 using 2
{
TH0=0XF8; //2Ms定时
TL0=0X30;
pwm_val_left++;
pwm_val_right++;
pwm_out_left_moto();
pwm_out_right_moto();
}
/***************************************************/ void main(void)
{
TMOD=0X01;
TH0= 0XF8; //2ms定时
TL0= 0X30;
TR0= 1;
ET0= 1;
EA = 1;
while(1)/*无限循环*/
{
xunji();
} }。