#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar shuzi[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x27,0x7f,0x6f,0x3f};
uchar show[]={0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};
uchar wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
sbit IN1 = P1^0;
sbit IN2 = P1^1;
sbit IN3 = P1^2;
sbit IN4 = P1^3;
sbit ENA = P1^4;
sbit ENB = P1^5;
sbit speed_add = P1^6;
sbit speed_mus = P1^7;
sbit forward = P3^5;
sbit reversal = P3^6;
sbit A_OR_B = P3^7;
sbit TZ=P3^4;
sbit A8=P3^0;
sbit A9=P3^1;
bit flag;
uchar count=0;
uchar speedA = 10;
uchar speedB = 10;
uint A_count=0,B_count=0,time1=0;
int flag1=0,flag2=0,AR=0,BR=0;
float r1 = 0,r2=0;
void delay(unsigned int j)
{
unsigned int x;
for(x=0;x<j;x++);
}
void A_forward()
{
IN1=1;
IN2=0;
}
void A_reversal()
{
IN1=0;
IN2=1;
}
void B_forward()
{
IN3=1;
IN4=0;
}
void B_reversal()
{
IN3=0;
IN4=1;
}
void display()
{
int i;
if(AR==0||BR==0)show[0]=0x3f;
if(AR==1||BR==1)show[0]=0x40;
if(!flag){
if(flag1==1)
{
show[1] = shuzi[(100-speedA)/100];
show[2] = shuzi[(100-speedA)/10];
show[3] = shuzi[(100-speedA)%10];
show[4] = shuzi[(int)r1/100];
show[5] = shuzi[(int)r1/10%10];
show[6] = shuzi[(int)r1%10]|0x80;
show[7] = shuzi[(int)(r1*10)%10];
flag1=0;
}
for(i=0; i<8; i++)
{
P0 = show[i];
A8 = 1;
A8 = 0;
P0 = ~wei[i];
A9 = 1;
A9 = 0;
delay(1);
P0 = 0xff;
A9 = 1;
A9 = 0;
}
}
if(flag){
if(flag2==1)
{
show[1] = shuzi[(100-speedB)/100];
show[2] = shuzi[(100-speedB)/10];
show[3] = shuzi[(100-speedB)%10];
show[4] = shuzi[(int)r2/100];
show[5] = shuzi[(int)r2/10%10];
show[6] = shuzi[(int)r2%10]|0x80;
show[7] = shuzi[(int)(r2*10)%10];
flag2=0;
}
for(i=0; i<8; i++)
{
P2 = show[i];
A8 = 1;
A8 = 0;
P2 = ~wei[i];
A9 = 1;
A9 = 0;
delay(1);
P2 = 0xff;
A9 = 1;
A9 = 0;
}
}
}
void key_scan()
{
if(speed_add==0)
{
delay(1);
if(speed_add==0)
{
if(flag == 0)
{
if(speedA>0)speedA-=5;
}
else if(flag == 1)
{
if(speedB>0)speedB-=5;
}
while(!speed_add);
return ;
}
}
if(speed_mus==0)
{
delay(1);
if(speed_mus==0)
{
if(flag==0)
{
if(speedA<=20)speedA+=5;
}
if(flag==1)
{
if(speedB<=20)speedB+=5;
}
}
while(!speed_mus);
return;
}
if(forward==0)
{
delay(1);
if(forward==0)
{
if(flag == 0)
{
A_forward();
AR=0;
}
if(flag == 1)
{
B_forward();
BR=0;
}
}
while(!forward);
return ;
}
if(reversal==0)
{
delay(1);
if(reversal==0)
{
if(flag == 0)
{
A_reversal();
AR=1;
}
if(flag == 1)
{
B_reversal();
BR=1;
}
}
while(!reversal);
return ;
}
if(A_OR_B==0)
{
delay(1);
if(A_OR_B==0)
{
flag=~flag;
}
while(!A_OR_B);
return ;
}
if(TZ==0)
{
delay(1);
if(TZ==0)
{
if(flag==0)
{
IN1=0;
IN2=0;
}
if(flag==1)
{
IN3=0;
IN4=0;
}
}
}
}
void main()
{
TMOD=0x11;
TH0=(65536-10)/256;
TL0=(65536-10)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
ET1=1;
TR1=1;
IT0=1;
EX0=1;
IT1=1;
EX1=1;
flag=0;
while(1)
{
display();
key_scan();
}
}
void zhongduan1() interrupt 0
{
A_count++;
}
void zhongduan2() interrupt 2
{
B_count++;
}
void DTime1() interrupt 1
{
TR0=0;
TH0=(65536-10)/256;
TL0=(65536-10)%256;
count++;
if(count==100)
count=0;
if(count<speedA)
{
ENA=0;
}
else{
ENA=1;
}
if(count<speedB)
{
ENB=0;
}
else{
ENB=1;
}
TR0=1;
}
void DTime2() interrupt 3
{
TR1=0;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
time1++;
if(time1==20)
{
r1=60*A_count/24.0;
r2=60*B_count/24.0;
time1=0;
A_count=0;
B_count=0;
flag1=1;
flag2=1;
}
TR1=1;
}
- 1
- 2
- 3
前往页