避障程序
#include"reg51.h"
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uint time=0,timer=0;
ulong S=0,T=0;
sbit RX1=P2^6;
sbit TX1=P2^7;
sbit RX2=P3^6;
sbit TX2=P3^7;
uint lf0=0,lf1=0;
uint lb0=0,lb1=0;
uint rf0=0,rf1=0;
uint rb0=0,rb1=0;
sbit inputL=P2^0;sbit inputR=P2^1;
sbit a1=P0^7;sbit a2=P0^6;
sbit b1=P0^5;sbit b2=P0^4;
sbit c1=P1^7;sbit c2=P1^6;
sbit d1=P1^5;sbit d2=P1^4;
void Delay(uint xms)
{
uchar i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void forward(int pwm,int time)
{
int a=0;
while(a<time)
{
a1=0,a2=1;
b1=0,b2=1;
c1=0,c2=1;
d1=0,d2=1;
Delay(pwm);
a1=0,a2=0;
b1=0,b2=0;
c1=0,c2=0;
d1=0,d2=0;
Delay(10-pwm);
a++;
}
}
void turnleft(int pwm,int time)
{
int a=0;
while(a<time)
{
a1=1,a2=0;
b1=1,b2=0;
c1=0,c2=1;
d1=0,d2=1;
Delay(pwm);
a1=0,a2=0;
b1=0,b2=0;
c1=0,c2=0;
d1=0,d2=0;
Delay(10-pwm);
a++;
}
}
void turnright(int pwm,int time)
{
int a=0;
while(a<time)
{
a1=0,a2=1;
b1=0,b2=1;
c1=1,c2=0;
d1=1,d2=0;
Delay(pwm);
a1=0,a2=0;
b1=0,b2=0;
c1=0,c2=0;
d1=0,d2=0;
Delay(10-pwm);
a++;
}
}
void back(int pwm,int time)
{
int a=0;
while(a<time)
{
a1=1,a2=0;
b1=1,b2=0;
c1=1,c2=0;
d1=1,d2=0;
Delay(pwm);
a1=0,a2=0;
b1=0,b2=0;
c1=0,c2=0;
d1=0,d2=0;
Delay(10-pwm);
a++;
}
}
void maichong()
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();
}
void qidong1()
{
TX1=1;
maichong();
TX1=0;
}
void qidong2()
{
TX2=1;
maichong();
TX2=0;
}
void Conut1()
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(long)(time*0.17);
}
void Conut2()
{
timer=TH0*256+TL0;
TH0=0;
TL0=0;
T=(long)(timer*0.17);
}
void kaishi()
{
TMOD=0x01;
TH0=0;
TL0=0;
ET0=1;
EA=1;
}
void scan()
{
if(S>200&&T>200)
{
forward(5,5);
}
if(S>200&&T<200)
{
turnleft(5,5);
}
if(S<200&&T>200)
{
turnright(5,5);
}
if(S<200&&T<200)
{
back(5,5);
}
}
void main()
{
kaishi();
while(1)
{
qidong1();
while(!RX1); //當RX為零時等待
TR0=1; //開啟計數
while(RX1); //當RX為1計數并等待
TR0=0; //關閉計算
Conut1(); //計算
qidong2();
while(!RX2); //當RX為零時等待
TR0=1; //開啟計數
while(RX2); //當RX為1計數并等待
TR0=0; //關閉計算
Conut2(); //計算
scan();
}
}
循跡程序
#include"reg51.h"
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit a1=P0^7;sbit a2=P0^6;
sbit b1=P0^5;sbit b2=P0^4;
sbit c1=P1^7;sbit c2=P1^6;
sbit d1=P1^5;sbit d2=P1^4;
uint time=0,timer=0;
sbit inputL=P2^0;sbit inputR=P2^1;
void Delay(uint xms)
{
uchar i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void forward(int pwm,int time)
{
int a=0;
while(a<time)
{
a1=0,a2=1;
b1=0,b2=1;
c1=0,c2=1;
d1=0,d2=1;
Delay(pwm);
a1=0,a2=0;
b1=0,b2=0;
c1=0,c2=0;
d1=0,d2=0;
Delay(10-pwm);
a++;
}
}
void turnleft(int pwm,int time)
{
int a=0;
while(a<time)
{
a1=0,a2=1;
b1=0,b2=1;
c1=1,c2=0;
d1=1,d2=0;
Delay(pwm);
a1=0,a2=0;
b1=0,b2=0;
c1=0,c2=0;
d1=0,d2=0;
Delay(10-pwm);
a++;
}
}
void turnright(int pwm,int time)
{
int a=0;
while(a<time)
{
a1=1,a2=0;
b1=1,b2=0;
c1=0,c2=1;
d1=0,d2=1;
Delay(pwm);
a1=0,a2=0;
b1=0,b2=0;
c1=0,c2=0;
d1=0,d2=0;
Delay(10-pwm);
a++;
}
}
void scan()
{
if(inputL==0&&inputR==0)
{
forward(3,5);
}
if(inputL==1&&inputR==1)
{
forward(3,5);
}
if(inputL==1&&inputR==0)
{
turnleft(3,5);
}
if(inputL==0&&inputR==1)
{
turnright(3,5);
}
}
void kaishi()
{
TMOD|=0X11;
TH1=0xff;
TL1=0x9c;
ET1=1;
TR1=1;
}
void main()
{
kaishi();
EA=1;
while(1)
{
scan();
}
}
|