本帖最后由 小小新 于 2016-8-31 10:52 编辑
小车通电前进一段过了那个延时以后就一直前进一点后退一点
#include<reg52.h>
unsigned char date;
#define uint unsigned int
#define uchar unsigned char
sbit IN11=P1^0; //
sbit IN12=P1^1;
sbit IN21=P1^2;
sbit IN22=P1^3;
sbit IN31=P1^4;
sbit IN32=P1^5;
sbit IN41=P1^6;
sbit IN42=P1^7;
sbit GD0=P0^4; //
sbit GD1=P0^5;
sbit GD2=P0^6;
sbit hongwai1=P1^1; //
sbit hongwai2=P1^3;
//-----------------------------------
unsigned char i,j,a,b,c,d;
void delayms(uint x);
int saomiao();
int houmian();
void HZQJ();
void QJ();
void YZ90();
void YZ55();
void ZZ90();
void ZZ55();
void HZQJ();
void Initial_com(void);
//------------------------------------
void delayms(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void Initial_com(void)
{
EA=1;
ES=1;
ET1=1;
TMOD=0x20;
PCON=0x00;
SCON=0x50;
TH1=0xfd;
TL1=0xfd;
TR1=1;
}
//-------------------------------------
int jcby() //
{
int a=0;
if((hongwai1==0)&&(hongwai2==1))
a=1;
else if((hongwai1==1)&&(hongwai2==0))
a=2;
else if((hongwai1==0)&&(hongwai2==0))
a=3;
else
a=0;
return(a);
}
//-------------------------------------
int saomiao()
int b=0;
if((GD0==0)&&(GD2==0))
b=1;
else if((GD0==1)&&(GD2==0))
b=7;
else if((GD0==0)&&(GD2==1))
b=6;
else
b=0;
return(b);
}
//-----------------------------------------------
int houmian()
{
int d=0;
if(GD1==0)
d=1;
else
d=0;
return(d);
}
//--------------------------------------------------
void QJ()
{
IN11=1;
IN12=0;
IN21=1;
IN22=0;
IN31=0;
IN32=1;
IN41=0;
IN42=1;
}
void YZ90()
{
IN11=0;
IN12=1;
IN21=0;
IN22=1;
IN31=0;
IN32=1;
IN41=0;
IN42=1;
delayms(480);
}
void ZZ90()
{
IN11=1;
IN12=0;
IN21=1;
IN22=0;
IN31=1;
IN32=0;
IN41=1;
IN42=0;
delayms(480);
}
void YZ55()
{ IN11=0;
IN12=1;
IN21=0;
IN22=1;
IN31=0;
IN32=1;
IN41=0;
IN42=1;
delayms(300);
}
void ZZ55()
{
IN11=1;
IN12=0;
IN21=1;
IN22=0;
IN31=1;
IN32=0;
IN41=1;
IN42=0;
delayms(300);
}
void HZQJ()
{
IN11=0;
IN12=1;
IN21=0;
IN22=1;
IN31=1;
IN32=0;
IN41=1;
IN42=0;
delayms(950);
IN11=1;
IN12=0;
IN21=1;
IN22=0;
IN31=0;
IN32=1;
IN41=0;
IN42=1;
delayms(500);
}
//-------------------------------------------------
void main()
{
QJ();
delayms(5000); //????
while(1)
{
a=jcby(); //¼ì2a±ßÔμ
if(a==1)
{
ZZ90();
}
if(a==2)
{
YZ90();
}
if(a==3)
{
HZQJ();
}
if(a==0)
{
QJ();
}
b=saomiao(); //¼ì2aÕÏ°-
if(b==1)
{
QJ();
}
if(b==7)
{
YZ55();
}
if(b==6)
{
ZZ55();
}
if(b==0)
{
QJ();
}
d=houmian();
{
HZQJ();
}
if(d==0)
{
QJ();
}
if(RI)
{
date=SBUF;
SBUF=date;
RI=0;
}
}
}
|