GardenRobot.c:
#include"reg52.h"
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit magInduction1=P2^7;
sbit magInduction2=P2^6;
sbit magInduction3=P2^5;
sbit magInduction4=P2^4;
uchar Duty_left,Duty_right,i=0,j=0;
uint time=0,s;
sbit in1=P1^0;
sbit in2=P1^1;
sbit in3=P1^2;
sbit in4=P1^3;
sbit e1=P2^3;
sbit e2=P2^2;
sbit RX=P0^4;
sbit TX=P0^5;
sbit jd=P3^7;
void init()
{
TMOD=0X01;
TH0=(65536-50)/256;
TL0=(65536-50)%256;
EA=1;
ET0=1;
TR0=1;
e1=1;
e2=1;
}
void init2()
{
TMOD=0x10;
TH1=0;
TL1=0;
TR1=1;
ET1=1;
EA=1;
}
void startmodule()
{
uint n;
TX=1;
for(n=0;n<21;n++)
_nop_();
TX=0;
}
void count()
{
time=TH1*256+TL1;
TH1=0;
TL1=0;
s=(time*1.7)/100;
}
void stop()
{
in1=0;
in2=0;
in3=0;
in4=0;
}
void delay(unsigned int k)
{
unsigned int x,y;
for(x=0;x<k;x++)
for(y=0;y<2000;y++);
}
void turnRight()
{
Duty_left=29;
Duty_right=20;
in1=1;
in2=0;
in3=0;
in4=1;
}
void turnRight2()
{
Duty_left=29;
Duty_right=20;
in1=1;
in2=0;
in3=0;
in4=1;
}
void turnLeft()
{
Duty_left=18;
Duty_right=26;
in1=0;
in2=1;
in3=1;
in4=0;
}
void turnLeft2()
{
Duty_left=20;
Duty_right=30;
in1=0;
in2=1;
in3=1;
in4=0;
}
void forward()
{
Duty_left=25;
Duty_right=25;
in1=1;
in2=0;
in3=1;
in4=0;
}
void forward2()
{
Duty_left=45;
Duty_right=35;
in1=1;
in2=0;
in3=1;
in4=0;
}
void timer() interrupt 1
{
i++;
j++;
if(i<=Duty_left)
e1=1;
else
e1=0;
if(i>100)
{
e1=1;
i=0;
}
if(j<=Duty_right)
e2=1;
else
e2=0;
if(j>100)
{
e2=0;
j=0;
}
TH0=(65536-50)/256;
TL0=(65536-50)%256;
}
void main()
{
init();
init2();
while(1)
{
startmodule();
while(!RX);
TR1=1;
while(RX);
TR1=0;
count();
if(magInduction1==0&&magInduction2==1&&magInduction3==1&&magInduction4==1&&s>40)
turnRight();
if(magInduction1==0&&magInduction2==0&&magInduction3==1&&magInduction4==1&&s>40)
turnRight();
if(magInduction1==1&&magInduction2==0&&magInduction3==0&&magInduction4==1&&s>40)
forward();
if(magInduction1==1&&magInduction2==1&&magInduction3==1&&magInduction4==1&&s>40)
forward();
if(magInduction1==1&&magInduction2==1&&magInduction3==0&&magInduction4==0&&s>40)
turnLeft();
if(magInduction1==1&&magInduction2==1&&magInduction3==1&&magInduction4==0&&s>40)
turnLeft();
if(magInduction2==0&&magInduction1==1&&magInduction3==1&&magInduction4==1&&s>40)
turnRight();
if(magInduction3==0&&magInduction1==1&&magInduction2==1&&magInduction4==1&&s>40)
turnLeft();
if(magInduction1==0&&magInduction2==1&&magInduction3==1&&magInduction4==1&&s<40)
{
stop();
jd=0;
delay(30);
jd=1;
turnRight2();
delay(8);
}
if(magInduction1==0&&magInduction2==0&&magInduction3==1&&magInduction4==1&&s<40)
{
stop();
jd=0;
delay(30);
jd=1;
turnRight2();
delay(8);
}
if(magInduction1==1&&magInduction2==0&&magInduction3==0&&magInduction4==1&&s<40)
{
stop();
jd=0;
delay(30);
jd=1;
forward2();
delay(8);
}
if(magInduction1==1&&magInduction2==1&&magInduction3==1&&magInduction4==1&&s<40)
{
stop();
jd=0;
delay(30);
jd=1;
forward2();
delay(8);
}
if(magInduction1==1&&magInduction2==1&&magInduction3==0&&magInduction4==0&&s<40)
{
stop();
jd=0;
delay(30);
jd=1;
turnLeft2();
delay(8);
}
if(magInduction1==1&&magInduction2==1&&magInduction3==1&&magInduction4==0&&s<40)
{
stop();
jd=0;
delay(30);
jd=1;
turnLeft2();
delay(8);
}
}
}
源文件下载:
链接:GardenRobot.c
提取码:iu32