资源描述
智能循迹小车,小车按中字笔画行驶/*“中”字笔画轨迹*/#include#include /内部包含延时函数 _nop_();#define uchar unsigned char#define uint unsigned int unsigned char dj1=0;unsigned char dj2=0;uchar t=0;uchar num1=0,num2=0,num3=0;sbit HW1=P00; /红外对管位定义sbit HW2=P01;sbit HW3=P02;sbit HW4=P03;sbit ENA=P32; /PWM输入sbit ENB=P33;sbit IN5=P24; /电机sbit IN6=P25;sbit IN7=P26;sbit IN8=P27;void delay(uint x) /延时1msuint i,j;for(i=0;ix;i+)for(j=0;j120;j+);void qianjin() /小车前进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;IN8=1;dj1=20;dj2=7;void right()IN5=0;IN6=1;IN7=1;IN8=0;dj1=20;dj2=25;void left()IN5=1;IN6=0;IN7=0;IN8=1;dj1=25;dj2=20;void stop() /小车后退dj1=0;dj2=0;void init() /初始化TMOD=0x01;TH0=(65536-500)/256;TL0=(65536-500)%256;EA=1;ET0=1;TR0=1;void timer0() interrupt 1 using 1/定时器0中断TH0=(65536-500)/256;TL0=(65536-500)%256;t+;if(tdj1)ENA=1;elseENA=0;if(t=50)t=0;void main()init();while(1)if(num1=0)if(HW1=0&HW2=1&HW3=1&HW4=0)qianjin();if(HW1=0&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=1&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=1&HW2=0&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=0&HW3=1&HW4=0)turnright2();if(HW1=0&HW2=0&HW3=1&HW4=1)turnright2();if(HW1=0&HW2=0&HW3=0&HW4=1)turnright2();if(HW1=0&HW2=1&HW3=1&HW4=1)turnleft2();if(HW1=1&HW2=1&HW3=1&HW4=0)turnright2();if(num2=0&HW1=0&HW2=0&HW3=0&HW4=0)delay(500);if(num2=0&HW1=0&HW2=0&HW3=0&HW4=0)stop();delay(50);num1=1;delay(50);if(num1=1)if(num2=0&HW1=0&HW2=0&HW3=0&HW4=0)right();if(HW1=0&HW2=1&HW3=1&HW4=0)num2=1;delay(50);qianjin();if(HW1=0&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=1&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=1&HW2=0&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=0&HW3=1&HW4=0)turnright2();if(HW1=0&HW2=0&HW3=1&HW4=1)turnright2();if(HW1=0&HW2=0&HW3=0&HW4=1)turnright2();if(HW1=0&HW2=1&HW3=1&HW4=1)turnleft2();if(HW1=1&HW2=1&HW3=1&HW4=0)turnright2();if(num2=1&HW1=0&HW2=0&HW3=0&HW4=0)delay(500);if(num2=1&HW1=0&HW2=0&HW3=0&HW4=0)stop();delay(50);num1=2;delay(50); if(num1=2)if(num2=1&HW1=0&HW2=0&HW3=0&HW4=0)right();if(HW1=0&HW2=1&HW3=1&HW4=0)num2=2;delay(50);qianjin();if(HW1=1&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=1&HW2=0&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=0&HW3=1&HW4=1)turnright2();if(HW1=0&HW2=0&HW3=1&HW4=0)turnright2();if(HW1=0&HW2=0&HW3=0&HW4=1)turnright2();if(num3=0&HW1=1&HW2=1&HW3=1&HW4=1)num3=1;delay(10);stop();delay(100);qianjin();if(HW1=0&HW2=1&HW3=1&HW4=1)turnleft2();if(HW1=1&HW2=1&HW3=1&HW4=0)turnright2();if(num2=2&HW1=0&HW2=0&HW3=0&HW4=0)delay(500);if(num2=2&HW1=0&HW2=0&HW3=0&HW4=0)stop();delay(50);num1=3;delay(50);if(num1=3)if(num2=2&HW1=0&HW2=0&HW3=0&HW4=0)right();if(HW1=0&HW2=1&HW3=1&HW4=0)num2=3;delay(50);qianjin();if(HW1=1&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=1&HW2=0&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=0&HW3=1&HW4=1)turnright2();if(HW1=0&HW2=0&HW3=1&HW4=0)turnright2();if(HW1=0&HW2=0&HW3=0&HW4=1)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)delay(500);if(num2=3&HW1=0&HW2=0&HW3=0&HW4=0)stop();delay(50);num1=4;delay(50);if(num1=4)if(num2=3&HW1=0&HW2=0&HW3=0&HW4=0)right();if(HW1=0&HW2=1&HW3=1&HW4=0)num2=4;delay(50);qianjin();if(HW1=1&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=1&HW2=0&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=0&HW3=1&HW4=1)turnright2();if(HW1=0&HW2=0&HW3=1&HW4=0)turnright2();if(HW1=0&HW2=0&HW3=0&HW4=1)turnright2();if(num3=1&HW1=1&HW2=1&HW3=1&HW4=1)num3=2;delay(10);stop();delay(100);qianjin();if(HW1=0&HW2=1&HW3=1&HW4=1)turnleft2();if(HW1=1&HW2=1&HW3=1&HW4=0)turnright2();if(num2=4&HW1=0&HW2=0&HW3=0&HW4=0)delay(500);if(num2=4&HW1=0&HW2=0&HW3=0&HW4=0)stop();delay(50);num1=5;delay(50);if(num1=5)if(num2=4&HW1=0&HW2=0&HW3=0&HW4=0)left();if(HW1=0&HW2=1&HW3=1&HW4=0)num2=5;delay(50);qianjin();if(HW1=1&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=1&HW2=0&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=0&HW3=1&HW4=1)turnright2();if(HW1=0&HW2=0&HW3=1&HW4=0)turnright2();if(HW1=0&HW2=0&HW3=0&HW4=1)turnright2();if(HW1=0&HW2=1&HW3=1&HW4=1)turnleft2();if(HW1=1&HW2=1&HW3=1&HW4=0)turnright2();if(num2=5&HW1=1&HW2=1&HW3=1&HW4=1)stop();delay(100);num1=6;delay(50);if(num1=6)if(num2=5&HW1=1&HW2=1&HW3=1&HW4=1)left();if(HW1=0&HW2=1&HW3=1&HW4=0)num2=6;delay(50);num3=3;delay(50);qianjin();if(HW1=1&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=1&HW2=0&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=0&HW3=1&HW4=1)turnright2();if(HW1=0&HW2=0&HW3=1&HW4=0)turnright2();if(HW1=0&HW2=0&HW3=0&HW4=1)turnright2();if(HW1=0&HW2=1&HW3=1&HW4=1)turnleft2();if(HW1=1&HW2=1&HW3=1&HW4=0)turnright2();if(num3=3&HW1=1&HW2=1&HW3=1&HW4=1)num3=4;delay(10);stop();delay(100);qianjin();if(num2=6&HW1=0&HW2=0&HW3=0&HW4=0)delay(500);if(num2=6&HW1=0&HW2=0&HW3=0&HW4=0)stop();delay(500);num1=7;delay(50);if(num1=7)if(num2=6&HW1=0&HW2=0&HW3=0&HW4=0)right();if(HW1=0&HW2=1&HW3=1&HW4=0)num2=7;delay(50);num3=4;delay(50);qianjin();if(HW1=1&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=1&HW3=0&HW4=0)turnleft2();if(HW1=1&HW2=0&HW3=0&HW4=0)turnleft2();if(HW1=0&HW2=0&HW3=1&HW4=1)turnright2();if(HW1=0&HW2=0&HW3=1&HW4=0)turnright2();if(HW1=0&HW2=0&HW3=0&HW4=1)turnright2();if(HW1=0&HW2=1&HW3=1&HW4=1)turnleft2();if(HW1=1&HW2=1&HW3=1&HW4=0)turnright2();if(num3=4&HW1=1&HW2=1&HW3=1&HW4=1)num3=5;delay(10);stop();delay(100);qianjin();if(num3=5&HW1=1&HW2=1&HW3=1&HW4=1)num3=6;delay(10);stop();delay(100);qianjin();if(num2=7&HW1=0&HW2=0&HW3=0&HW4=0)delay(100);if(num2=7&HW1=0&HW2=0&HW3=0&HW4=0)stop();delay(90000);
展开阅读全文