本帖最后由 meimiao88 于 2013-12-18 18:40 编辑
#include<reg52.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
sbit left_f = P2^4; //a左前
sbit left_b = P2^5;
sbit right_f = P2^6; //a右前
sbit right_b = P2^7;
sbit c1 =P3^0; //a右后
sbit c2 =P3^1;
sbit d1 =P3^2; //a左后
sbit d2=P3^3;
unsigned int y;
unsigned char temp;
sbit huidu_3=P1^3;
sbit huidu_4=P1^4;
sbit huidu_5=P1^5;
sbit huidu_6=P1^6;
sbit huidu_7=P1^7;
sbit huidu_8=P1^0;
sbit huidu_9=P1^1;
sbit huidu_10=P1^2;
uchar count = 0;
void zz();
void init()
{
TH0 = (65535-10)/256;
TL0 = (65535-10)%256;
TMOD = 0x01;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void Timer0_int()interrupt 1 //定时器中断计数
{
TH0 = (65535-10)/256;
TL0 = (65535-10)%256;
count ++;
if(count >= 100)
{count = 0; }
}
void dianji_a(uchar speed)
{
if(count <= speed) //count计数变量
{
left_f = 1;
left_b = 0;
}
else
{
left_f = 1;
left_b = 1;
}
}
void dianji_b(uchar speed) //同上
{
if(count<= speed)
{
right_f =0;
right_b = 1;
}
else
{
right_f = 1;
right_b = 1;
}
}
void dianji_c(uchar speed)
{
if(count <= speed) //count计数变量
{
c1 = 0;
c2 = 1;
}
else
{
c1= 1;
c2 = 1;
}
}
void dianji_d(uchar speed)
{
if(count <= speed) //count计数变量
{
d1 = 1;
d2 =0;
}
else
{
d1= 1;
d2 = 1;
}
}
void bdianji_a(uchar speed)
{
if(count <= speed) //count计数变量
{
left_f = 0;
left_b = 1;
}
else
{
left_f = 1;
left_b = 1;
}
}
void bdianji_b(uchar speed) //同上
{
if(count<= speed)
{
right_f =1;
right_b = 0;
}
else
{
right_f = 1;
right_b = 1;
}
}
void bdianji_c(uchar speed)
{
if(count <= speed) //count计数变量
{
c1 = 1;
c2 = 0;
}
else
{
c1= 1;
c2 = 1;
}
}
void bdianji_d(uchar speed)
{
if(count <= speed) //count计数变量
{
d1 = 0;
d2 =1;
}
else
{
d1= 1;
d2 = 1;
}
}
void contorlA()
{
temp = P1;
if(temp==0xc0)
{
dianji_a(20);
dianji_b(20);
dianji_c(20);
dianji_d(20);
}
else if(temp==0x60)
{
dianji_a(40);
dianji_b(5);
dianji_c(5);
dianji_d(40);
}
else if(temp==0x81)
{
dianji_a(3);
dianji_b(50);
dianji_c(50);
dianji_d(3);
}
else if(temp==0x20)//5
{
dianji_a(50);
dianji_b(3);
dianji_c(3);
dianji_d(50);
}
else if(temp==0x01)//8
{
dianji_a(5);
dianji_b(50);
dianji_c(50);
dianji_d(5);
}
else if(temp==0x02)//9
{
dianji_a(0);
dianji_b(50);
dianji_c(50);
dianji_d(0);
}
else if(temp==0x10)//4
{
dianji_a(50);
dianji_b(0);
dianji_c(0);
dianji_d(50);
}
else if(temp==0xf8)
{
for(y=0;y<600;y++)
{
dianji_a(50);
bdianji_b(35);
bdianji_c(35);
dianji_d(50);
}
}
else if (temp==0x78)//3456
{
for(y=0;y<500;y++)
{
dianji_a(50);
bdianji_b(35);
bdianji_c(35);
dianji_d(50);
}
}
else if(temp==0xc7|temp==0x87)//678910
{
zz();
}
else
{
dianji_a(5);
dianji_b(5);
dianji_c(5);
dianji_d(5);
}
}
void zz()
{
temp=P1;
while(temp!=0xc0)
{
bdianji_a(35);
dianji_b(50);
dianji_c(50);
bdianji_d(35);
if(temp==0xc0)
{
contorlA;
}
}
}
void main()
{
init();
while(1)
{
contorlA();
}
} |