智能循迹小车程序代码

合集下载

循迹小车代码

循迹小车代码

#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();}}}。

循迹小车程序代码

循迹小车程序代码

//(在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延时

智能循迹小车程序

智能循迹小车程序

智能小车程序(共三个)第一个:#include "reg52.h"#define det_Dist 2.55 //单个脉冲对应的小车行走距离,其值为车轮周长/4#define RD 9 //小车对角轴长度#define PI 3.1415926#define ANG_90 90#define ANG_90_T 102#define ANG_180 189/*============================全局变量定义区============================*/sbit P10=P1^0; //控制继电器的开闭sbit P11=P1^1; //控制金属接近开关sbit P12=P1^2; //控制颜色传感器的开闭sbit P07=P0^7; //控制声光信号的开启sbit P26=P2^6; //接收颜色传感器的信号,白为0,黑为1sbit P24=P2^4; //左sbit P25=P2^5; //右接收左右光传感器的信号,有光为0unsigned char mType=0; //设置运动的方式,0 向前1 向左2 向后3 向右unsigned char Direction=0; //小车的即时朝向0 朝上1 朝左2 朝下3 朝右unsigned sX=50; unsigned char sY=0; //小车的相对右下角的坐标CM(sX,sY)unsigned char StartTask=0; //获得铁片后开始执行返回卸货任务,StartTask置一unsigned char Inter_EX0=0; // 完成一个完整的任务期间只能有一次外部中断// Inter_EX0记录外部中断0的中断状态// 0 动作最近的前一次未中断过,// 1 动作最近的前一次中断过unsigned char cntIorn=0; //铁片数unsigned char bkAim=2; //回程目的地,0为A仓库,1为B仓库,2为停车场,//(在MAIN中接受铁片颜色判断传感器的信号来赋值)unsigned char Light_Flag=0;//进入光引导区的标志(1)unsigned int cntTime_5Min=0;//时间周期数,用于T0 精确定时unsigned int cntTime_Plues=0; //霍尔开关产生的脉冲数/*============================全局变量定义区============================*//*------------------------------------------------*//*-----------------通用延迟程序-------------------*//*------------------------------------------------*/void delay(unsigned int time) // time*0.5ms延时{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];}/*-----------------------------------------------*//*-------------------传感器模块------------------*//*-----------------------------------------------*//*光源检测程序: *//*用于纠正小车运行路线的正确性*/unsigned char LightSeek(){ void Display(unsigned char);bit l,r;l=P24;r=P25;if(l==0&&r==1){//Display(1);return (3); //偏左,向右开}if(r==0&&l==1){//Display(3);return(1); //偏右,向左开}if((l==1&&r==1)||(l==0&&r==0)){//Display(9);return(0); //没有偏离,前进}}/*铁片检测程序: *//*判断铁片的颜色,设定bkAim,0为A仓库,1为B仓库,2为停车场*/ void IornColor(){delay(4000);bkAim=(int)(P26);Display((int)(P26)+2);}/*-----------------------------------------------*//*------------------运动控制模块-----------------*//*-----------------------------------------------*//*====基本动作层:完成基本运动动作的程序集====*//*运动调整程序: *//*对小车的运动进行微调*/void ctrMotor_Adjust(unsigned char t){if(t==0){P2=P2&240|11; //用来解决两电机不对称的问题delay(6);}if(t==3){P2=P2&250; //向左走delay(1);}if(t==1){P2=(P2&245);delay(1); //向右走}P2=((P2&240)|15);delay(10);}/*直走程序: *//*控制小车运动距离,dist为运动距离(cm),type为运动方式(0 2)*/ /*只改变小车sX 和sY的值而不改变Direction的值. */ void ctrMotor_Dist(float dist,unsigned char type){unsigned char t=0;mType=type;P2=((P2&240)|15);cntTime_Plues=(int)(dist/det_Dist);while(cntTime_Plues){if(Inter_EX0==1&&StartTask==0){cntTime_Plues=0;break;}if(Light_Flag==1) t=LightSeek();if(type==0) //向前走{P2=P2&249;delay(40);ctrMotor_Adjust(t);}if(type==2) //向后退{P2=P2&246;delay(50);ctrMotor_Adjust(t);}P2=((P2&240)|15);if(mType==2) delay(60);//刹车制动0.5mselse delay(75);}}/*拐弯程序: *//*控制小车运动角度,type为运动方式(1 3)*//*只改变小车Direction的值而不改变sX 和sY的值*/void ctrMotor_Ang(unsigned char ang,unsigned char type,unsigned char dir) {unsigned char i=0;mType=type;P2=((P2&240)|15);cntTime_Plues=(int)((PI*RD*90/(180*det_Dist)*1.2)*ang/90);while(cntTime_Plues){if(Inter_EX0==1&&StartTask==0){cntTime_Plues=0;break;}if(type==1) //向左走{P2=P2&250;delay(100);ctrMotor_Adjust(0);}if(type==3) //向右走{P2=P2&245;delay(100);ctrMotor_Adjust(0);}P2=((P2&240)|15);delay(50);//刹车制动0.5ms}if(!(Inter_EX0==1&&StartTask==0)){Direction=dir;}}/*====基本路线层:描述小车基本运动路线的程序集====*//*当小车到达仓库或停车场时,放下铁片或停车(0,1为仓库,2为停车场)*/void rchPlace(){unsigned int time,b,s,g;time=(int)(cntTime_5Min*0.065535);//只有一个数码管时,轮流显示全过程秒数个十百b=time%100;s=(time-b*100)%100;g=(time-b*100-s*10)%10;if(bkAim==2){//到达停车场了,停车EA=0;P2=((P2&240)|15);while(1){Display(10); //Ndelay(2000);Display(cntIorn);delay(2000);Display(11);//Adelay(2000);Display(b);delay(2000);Display(s);delay(2000);Display(g);delay(2000);}}else{if(Inter_EX0==1&&StartTask==1)P10=0; //到达仓库,卸下铁片}}/*无任务模式: *//*设置小车的固定运动路线,未发现铁片时的运动路线*/void BasicRoute(){ //Light_Flag=1;ctrMotor_Dist(153,0);//Light_Flag=0;ctrMotor_Ang(ANG_90,1,1);ctrMotor_Dist(100-sX,0);ctrMotor_Dist(125,2);ctrMotor_Dist(73,0);ctrMotor_Ang(ANG_90,1,2);//Light_Flag=1;ctrMotor_Dist(153,0);//Light_Flag=0;ctrMotor_Ang(ANG_180,1,0);rchPlace();}/*任务模式: *//*设置小车的发现铁片后的运动路线*/void TaskRoute(){//基本运行路线表,记载拐弯0 向前1 左拐2 向后3 右拐,正读去A区;反读去B区StartTask=1;ctrMotor_Ang(ANG_90_T,1,2);if(bkAim==1) //仓库A{ctrMotor_Dist(10,0);P2=((P2&240)|15);delay(60);ctrMotor_Ang(ANG_90_T,1,3);ctrMotor_Dist(100-sX,2);ctrMotor_Ang(ANG_90_T,1,2);Light_Flag=1;ctrMotor_Dist(153,2);Light_Flag=0;// ctrMotor_Ang(208,1,0);}else if(bkAim==0) //仓库B{ctrMotor_Dist(10,0);P2=((P2&240)|15);delay(60);ctrMotor_Ang(ANG_90_T,1,3);ctrMotor_Dist(100-sX,0);ctrMotor_Ang(ANG_90_T,1,0);Light_Flag=1;ctrMotor_Dist(153,2);Light_Flag=0;//ctrMotor_Ang(208,1,0);}delay(5000);rchPlace();}/*---------------------------------------------*//*-------------------主程序段------------------*/ /*---------------------------------------------*/void main(){delay(4000);P2=0xff; //初始化端口P07=0;P1=0;TMOD=0x01; //初始化定时器0/1 及其中断TL0=0;TH0=0;TR0=1;ET0=1;ET1=1;IT0=1; //初始化外部中断EX0=1;IT1=1;EX1=1;EA=1;P11=1;while(1){Display(cntIorn);bkAim=2;BasicRoute();if(Inter_EX0==1){TaskRoute();//按获得铁片后的路线运动IE0=0;EX0=1;}Inter_EX0=0;}}/*----------------------------------------------------*//*----------------------中断程序段--------------------*//*----------------------------------------------------*//*定时器0中断程序: *//*当时间过了5分钟,则就地停车并进入休眠状态*/ void tmOver(void) interrupt 1{cntTime_5Min++;TL0=0;TH0=0;if(cntTime_5Min>=4520){Display(5);P2=((P2&240)|15);EA=0; //停车程序P07=1;delay(4000);PCON=0X00;while(1);}}/*外部中断0中断程序: *//*发现铁片,发出声光信号并将铁片吸起,发光二极管和蜂鸣器*//*并联在一起(设接在P07). 0为A仓库,1为B仓库,2为停车场*/ void fndIorn(void) interrupt 0{unsigned char i;P10=1;P2=((P2&240)|15); //停车P07=1;delay(1000);//刹车制动0.5msP07=0;Inter_EX0=1;cntIorn++;Display(cntIorn);for(i=0;i<40;i++){P2=P2&249;delay(2);P2=((P2&240)|15);delay(2);}P2=P2&249;delay(100);P2=((P2&240)|15); //停车IornColor(); //判断铁片黑白,设置bkAimfor(i=0;i<95;i++)P2=P2&249;delay(3);P2=((P2&240)|15);delay(2);}P2=((P2&240)|15); //停车delay(4000); //把铁片吸起来EX0=0;}/*外部中断1中断程序: *//*对霍尔开关的脉冲记数,对小车的位置进行记录,以便对小车进行定位*/ void stpMove(void) interrupt 2{cntTime_Plues--;if(Direction==0) //向上{if(mType==0) sY+=det_Dist;else if(mType==2)sY-=det_Dist;}else if(Direction==1) //向左{if(mType==0) sX+=det_Dist;else if(mType==2)sX-=det_Dist;}else if(Direction==2) //向下{if(mType==0) sY-=det_Dist;else if(mType==2)sY+=det_Dist;}else if(Direction==3) //向右{if(mType==0) sX-=det_Dist;else if(mType==2)sX+=det_Dist;}第二个:#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit moto1=P1^5;sbit moto2=P1^6;sbit moto3=P2^0;sbit moto4=P2^1;sbit en1=P1^7;sbit en2=P2^2;//*循迹口七个红外传感器*///////////////sbit left1=P1^0;//*左边传感器*//sbit left2=P1^1;sbit left3=P1^2;sbit mid=P1^3;//*黑线位置*//sbit right1=P1^4;sbit right2=P2^3;sbit right3=P2^4;//*右边传感器*//////////////// sbit hled=P0^0;sbit bled=P0^1;sbit lled=P0^2;sbit rled=P0^3;sbit bizhang=P2^5;uchar pro_head;uchar pro_back;uchar i;uchar j; //前后占空比标志void delay(uint z){uchar i;while(z--){for(i=0;i<121;i++);}}/********初始化定时器,中断************/ void init(){TMOD=0x01;TH0=(65536-100)/256;TL0=(65536-100)%256;EA=1;TR0=1;en1=1;en2=1;}void time0(void) interrupt 1{i++;j++;if(i<=pro_back){en1=1;}else{en1=0;}if(i==40){en1=~en1;i=0;}if(j<=pro_head){en2=1;}else{en2=0;}if(j==40){en2=~en2;j=0;}TH0=(65536-100)/256;TL0=(65536-100)%256;}void qianjin()//*直行*///////////////////// {pro_back=15;pro_head=5;moto1=0;moto2=0;moto4=0;lled=1;rled=1;bled=1;}void turn_right1()//*右转1函数*//{pro_back=10;pro_head=15;moto1=0;moto2=1;moto3=1;moto4=0;}。

智能循迹小车电磁组C语言源代码(stc12c5a芯片)

智能循迹小车电磁组C语言源代码(stc12c5a芯片)

智能循迹小车电磁组C语言源代码(stc12c5a芯片)#include#include#define FOSC 18432000L#define BAUD 9600#define ADC_POWER 0x80 //ADC power control bit 电源控制位#define ADC_FALG 0x10 //ADC complete flag 标志位#define ADC_START 0x08 //ADC start control bit 启动控制位#define ADC_SPEEDLL 0x00 //420 clocks#define ADC_SPEEDL 0x20 //280 clocks#define ADC_SPEEDH 0x40 //140 clocks#define ADC_SPEEDHH 0x60 //70 clockstypedef unsigned char BYTE;typedef unsigned int WORD;float bj1,bj2;int cg1,cg2,go;go=0x05; //电机驱动设为0101BYTE ch=0;void InitADC();void Delay(WORD n);void kongzhi();void PWM();void GetADCResult(BYTE ch);void main(){P0=0X00; //P0口的LED灯全亮InitADC(); //初始化ADIE=0xa0;PWM(); //调用PWM函数while(1){GetADCResult(ch); //读取AD值并赋值给变量kongzhi(); //调用控制函数}}void GetADCResult(BYTE ch) //读取AD的函数{ADC_CONTR &=!ADC_FALG;for(ch=0;ch<2;ch++){switch(ch){case 0: ADC_CONTR=0xe9; //定义P1.1为AD转换 1110 1001 _nop_();_nop_();_nop_();_nop_();while(!(ADC_CONTR&ADC_FALG));ADC_CONTR&=~ADC_FALG; //清除falg位cg1=ADC_RES; //把传到P1.1口的AD值(二进制)赋值给cg1 break;case 1: ADC_CONTR=0xea; //定义P1.2口为AD转换1110 1010_nop_();_nop_();_nop_();_nop_();while(!(ADC_CONTR&ADC_FALG));ADC_CONTR&=~ADC_FALG;cg2=ADC_RES; //把传到P1.2口的AD值(二进制)赋值给cg2 break;default: break;}}if(++ch>=2) ch=0;}void InitADC() //初始化AD函数{P1ASF=0XE7; // 1110 0111 //定义为AD转换的IO口P1M0=0xE7 ; // P1.7-P1.0:1110 0111P1M1=0xE7 ; // P1.7-P1.0:1110 0111ADC_RES=0;ADC_CONTR=0xe9;Delay(2);}void Delay(WORD n) //延时函数{WORD x;while(n--){x=5000;while(x--);}}void kongzhi(){bj1=((cg2*5/256)-(cg1*5/256)); //两个传感器所检测到的电压的差值bj2=((cg1*5/256)-(cg2*5/256));if((bj1<1)&&(bj2<1)) //全速{CCAP0H=0x08; //左轮CCAP1H=0x00; //右轮P3=go;}else if(bj1>4/3) //强左拐{CCAP0H=0x40; //左轮CCAP1H=0x00; //右轮P3=go;}else if(bj1>1&&bj1<4/3) //微左拐{CCAP0H=0x22; //左轮CCAP1H=0x00; //右轮P3=go;}else if(bj2>1&&bj2<4/3) //微右拐{CCAP0H=0x00; //左轮CCAP1H=0x18; //右轮P3=go;}else if(bj2>4/3) //强右拐{CCAP0H=0x00; //左轮CCAP1H=0x35; //右轮P3=go;}}void PWM(){CCON=0;CL=0;CH=0;CMOD=0X02;CCAP0H=CCAP0L=0X80;CCAPM0=0X42; //允许比较器功能、PWM脉宽输出CCAP1H=CCAP1L=0X80;PCA_PWM0=0x00; //组成9位P1.3PCA_PWM1=0x00; //组成9位P1.4CCAPM1=0X42;CR=1; //启动PCA计数器阵列}。

循迹小车程序代码(带解释说明)

循迹小车程序代码(带解释说明)

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;}}}}。

循迹小车编程

循迹小车编程
#define width 300//mid3在黑线上到脱离黑线的时间差500--》10-->2000--》60000--30000---》27000
#define check_right 500 //check_right若先检测到左边黑线,并且左边已出黑线,判断右端是否压黑线时间拖延2000--》20--》200--》500
while(1)//循环检测红外对管采集的电平信号
{
// line_straight();
detect_infrared();
}
}
}
else
{
IN3=1; IN4=1;
}
}
void turn_right()//右转,赋值
{
Duty_left =turn_speed_left;
Duty_right=turn_speed_right;
(ENA==1)
{
IN1=0; IN2=1;//左正
}
else
{
IN1=1; IN2=1;//左正
#define correct_speed 35//校正时的低速端的占空比//
#define turn_speed_left 20
#define turn_speed_right 20//(25,30)--(10,15)--(18,23)
//定义反应时间
#definelenth 40000//length检测到黑线到启动转动的时间间隔10000--》100--》500--》2000--80000--76000--68000
{
straight();//这里的直走是在不管红外检测结果的直行
delay(lenth);//直走一段时间
if(mid3==m1)//如果中间是黑线

智能循迹小车源程序

智能循迹小车源程序

#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;}。

智能循迹避障小车C语言代码

智能循迹避障小车C语言代码
{
array[0]=(sec%10);
array[1]=(sec/10);
array[2]=(min%10);
array[3]=(min/10);
}
void display()
{
P0=tab[array[0]];
P2=0X01;
delay();
sbit P37=P3^7;
sfr WDT_CONTR=0Xe1;
uint line,flag_stop=1,flag_signal; //小车停止标志位 ,障碍物检测标志位
uint flag_zuo=0,flag_you=0; //偏离黑线标志位
uint number=0;
uint sec=0,min=0,count=0;//时间初始化
delay();
P2=0X00;
}
void delay()
{
uint i;
for(i=500;i>0;i--);
}
void check()
{
P10=1;
_nop_();
_nop_();
flag_signal=P10;
if(flag_signal==0) //检测到障碍物
P2=0X00;
P0=tab[array[1]];
P2=0X02;
delay();
P2=0X00;
P0=tab[array[2]];
P2=0X04;
delay();
P2=0X00;
P0=tab[array[3]];
P2=0X08;
{
P14=0,P15=1,P16=1,P17=1;

arduino智能循迹小车代码(三个循迹模块)

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]); }。

智能寻迹小车调试程序

智能寻迹小车调试程序
hyou1 = 1;
hyou2 = 0;
}
void TurnRight()//右转
{Leabharlann qzuo1 = 1;qzuo2 = 0;
hzuo1 = 1;
hzuo2 = 0;
qyou1 = 0;
qyou2 = 0;
hyou1 = 0;
hyou2 = 0;
}
void Xiaoying()
{
qzuo1=qzuo2=qyou1=qyou2=hzuo1=hzuo2=hyou1=hyou2=0 ;
if(LED1==1&&LED2==1&&LED3==0&&LED4==0&&LED5==1)//直行超出范围后速度变化不明显
{ RUN(); delay(30) ;Xiaoying() ;}
if(LED1==1&&LED2==0&&LED3==0&&LED4==1&&LED5==1)//直行
{ RUN(); delay(30) ;Xiaoying() ;}
void delay(int i)
{
while(i--);
}
void RUN()//向前
{
qzuo1 = 1;
qzuo2 = 0;
hzuo1 = 1;
hzuo2 = 0;
qyou1 = 1;
qyou2 = 0;
hyou1 = 1;
hyou2 = 0;
}
void STOP()//停车
{
qzuo1 = 0;
voiddelayintvoidrun向前voidstop停车voidturnleft左转voidturnright右转voidxiaoyingvoidmainvoidifled11led21led30led41led51直行delay值越小速度越小范围在10120为最佳控制范围run

智能小车循迹,避障,红外遥控C语言知识学习代码

智能小车循迹,避障,红外遥控C语言知识学习代码

//智能小车避障、循迹、红外遥控C语言代码//实现功能有超声波避障,红外遥控智能小车,红外传感器实现小车自动循迹,1602显示小车的工作状态,另有三个独立按键分别控制三种状态的转换//注:每个小车的引脚配置都不一样,要注意引脚的配置,但是我的代码注释比较多,看起来比较容易一点#include <reg52.h>#include <math.h>#include"lcd.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 =2 sbit TRIG=P3^3; //超声波发送引脚定义/////红外控制引脚配置sbit KEY2=P3^7; //红外接收器数据线兼循迹按键state_total= 0 sbit KEY1=P3^4; //独立按键p3.4控制自动避障state_total=1 uchar 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;}}if(KEY2==0) //检测按键s3是否按下{Delay10ms(); //消除抖动if(KEY2==0){state_total=2; //总状态定义0为自动循迹模块1为自动避障模块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的前导低电平信号。

循迹小车设计(附代码)

循迹小车设计(附代码)
turn_left1();
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为控制电机的输出口*/

智能循迹小车电磁组C语言源代码(stc12c5a芯片)

智能循迹小车电磁组C语言源代码(stc12c5a芯片)
}
void InitADC() //初始化AD函数
{
P1ASF=0XE7; // 1110 0111 //定义为AD转换的IO口
P1M0=0xE7 ; // P1.7-P1.0:1110 0111
case 1: ADC_CONTR=0xea; //定义P1.2口为AD转换 1110 1010
_nop_();
_nop_();
_nop_();
_nop_();
while(!(ADC_CONTR&ADC_FALG));
while(1){
GetADCResult(ch); //读取AD值并赋值给变量
kongzhi(); //调用控制函数
}
}
void GetADCResult(BYTE ch) //读取AD的函数
{
ADC_CONTR &=!ADC_FALG;
while(!(ADC_CONTR&ADC_FALG));
ADC_CONTR&=~ADC_FALG; //清除falg位
cg1=ADC_RES; //把传到P1.1口的AD值(二进制)赋值给cg1
break;
CCAP1H=0x00; //右轮
P3=go;
}
else if(bj2>1&&bj2<4/3) //微右拐
{
CCAP0H=0x00; //左轮
CCAP1H=0x18; //右轮
}
void PWM()
{
CCON=0;
CL=0;
CH=0;
CMOD=0X02;
CCAP0H=CCAP0L=0X80;

智能循迹小车程序

智能循迹小车程序
{
IN5=0;
IN6=1;
IN7=0;
IN8=1;
dj1=15;
dj2=15;
}
void turnleft2()//小车前进向左微调
{
IN5=0;
IN6=1;
IN7=0;
IN8=1;
dj1=7;
dj2=20;
}
void turnright2()//小车前进向右微调
{
IN5=0;
IN6=1;
IN7=0;
{
turnright2();
}
if(HW1==0&&HW2==1&&HW3==1&&HW4==1)
{
turnleft2();
}
if(HW1==1&&HW2==1&&HW3==1&&HW4==0)
{
turnright2();
}
if(num2==3&&HW1==0&&HW2==0&&HW3==0&&HW4==0)
sbit ENB=P3^3;
sbit IN5=P2^4;//电机
sbit IN6=P2^5;
sbit IN7=P2^6;
sbit IN8=P2^7;
void delay(uint x)//延时1ms
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<120;j++);
}
void qianjin()//小车前进
{
turnright2();
}

智能循迹小车详细源代码程序MSPID

智能循迹小车详细源代码程序MSPID

巡线车程序(完整版)1 #ifndef _Macro.h_2 #define _Macro.h_3 #include <msp430x14x.h>4 #include <intrinsics.h>5 #define uchar unsigned char6 #define uint unsigned int7 #define one 11.118 #define LMAX 19999 #define RMAX 399910 #define CPU_F ((double)8000000)11 #define delay_us(x)__delay_cycles((long)(CPU_F*(double)x/1000000.0))1213 #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))14 #define PC 20 // 比例放大系数15 #define IC 0 //积分放大系数16 #define DC 85 //大系数17 #define LEFTOUT TACCR118 #define RIGHTOUT TACCR219 #define SensorIn P5IN20 #define F 5000//5000hz21 #define Period (8000000/F)22 #define EnableLeftPos P3OUT|=BIT123 #define UnenableLeftPos P3OUT&=~BIT12425 #define EnableLeftNeg P3OUT|=BIT026 #define UnenableLeftNeg P3OUT&=~BIT02728 #define EnableRightPos P3OUT|=BIT229 #define UnenableRightPos P3OUT&=~BIT23031 #define EnableRightNeg P3OUT|=BIT332 #define UnenableRightNeg P3OUT&=~BIT33334 #define Basic_Left 100//百分之八十35 #define Basic_Right 100//Basic_Left36 #define MAX (100)37 #define MIN (-100)38 #define foreward 139 #define backward 040 #define max_speed 10041 #define min_speed -10042 #define key 0434445 #define left_1 146 #define left_2 247 #define left_3 348 #define left_4 449 #define left_5 550 #define left_6 651 #define left_7 7//右直角5253 #define right_1 -154 #define right_2 -255 #define right_3 -356 #define right_4 -457 #define right_5 -558 #define right_6 -659 #define right_7 -7//左直角60 #endif[cpp]view plaincopy61 #include "Macro.h"62 #include "sensor.h"63 void Motorstop()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)//反转72 {73 EnableLeftNeg;74 UnenableLeftPos;75 }76 else if(direction==foreward)//正转77 {78 EnableLeftPos;79 UnenableLeftNeg;80 }81 LEFTOUT=Period/100*speed;82 }83 void MotorRight(int speed,int direction)84 {85 if(speed>max_speed)speed=max_speed;8687 if(direction==backward)//反转88 {89 EnableRightNeg;90 UnenableRightPos;91 }92 else if(direction==foreward)//正转93 {94 EnableRightPos;95 UnenableRightNeg;96 }97 RIGHTOUT=Period/100*speed;98 }99 void MotorDrive(int PIDout)100 {101 int speedleft,speedright;102 speedleft=Basic_Left PIDout;103 speedright=Basic_Right-PIDout;104105 if(speedleft<0)106 MotorLeft(speedleft,backward);//反转107 else MotorLeft(speedleft,foreward);//正转108109 if(speedright<0)110 MotorRight(speedright,backward);//反转111 else MotorRight(speedright,foreward);//正转112 }113 void Rangle(float angle)114 {115 // TBCTL|=TBCLR;116 TBCCR1=LMAX (unsigned int)(angle*one);117 }[cpp]view plaincopy118 //下面是小车的程序。

智能循迹小车详细源代码程序(MSP430,PID)

智能循迹小车详细源代码程序(MSP430,PID)
80 }
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 {

51的智能循迹小车代码

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;//有错时前进}。

智能循迹小车程序代码

智能循迹小车程序代码

/****************************************************************************硬件连接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();} }。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 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();
} }。

相关文档
最新文档