智能循迹小车程序
- 格式:doc
- 大小:44.50 KB
- 文档页数:14
智能循迹小车,小车按中字笔画行驶
/************“中”字笔画轨迹*************/
#include
#include
#define uint unsigned int
unsigned char dj1=0;
unsigned char dj2=0;
uchar t=0;
uchar num1=0,num2=0,num3=0;
sbit HW1=P0^0; //红外对管位定义
sbit HW2=P0^1;
sbit HW3=P0^2;
sbit HW4=P0^3;
sbit ENA=P3^2; //PWM输入
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 for(j=0;j<120;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(t else ENA=0; if(t else ENB=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)