#include<reg51.h>
#include<intrins.h>
//²½½øÂí´ïÇý¶¯½Ó¿Ú
sbit PH1 = P1^0;
sbit PH2 = P1^1;
sbit I01 = P1^2;
sbit I11 = P1^3;
sbit I02 = P1^4;
sbit I12 = P1^5;
//°´¼ü¿Ú¶¨Òå
sbit LS138A=P2^5;
sbit LS138B=P2^6;
sbit LS138C=P2^7;
sbit K1 = P2^4;
sbit K2 = P2^3;
sbit K3 = P2^2;
sbit K4 = P2^1;
//È«¾Ö±äÁ¿
unsigned char temp,Time,flag,hz;
unsigned int speed=0,show=0;
unsigned int LedOut[8];
unsigned char code Disp_Tab[17]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0¡¢1¡¢2¡¢3¡¢4¡¢5¡¢6¡¢7¡¢8¡¢9¡¢A¡¢b¡¢C¡¢d¡¢E¡¢FµÄÏÔʾÂë
//°´¼üÑÓʱº¯Êý
void delay10ms(unsigned char c)
{
unsigned char a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
//Âö³åÑÓʱº¯Êý
void delay(unsigned char t)
{
unsigned char i,j;
for(i=0;i<t;i++)
for(j=0;j<110;j++);
}
//¶¨Ê±Æ÷³õʼ»¯
void TimerConfiguration()
{
TMOD = 0x01; //ÉèÖù¤×÷·½Ê½1
TH0 = 0x3C; //¼Æʱ³õÖµ
TL0 = 0xB0; //
EA = 1; //´ò¿ª×ÜÖжÏ
ET0 = 1; //´ò¿ª´ò¿ª¶¨Ê±Æ÷
TR0 = 1; //ÔËÐж¨Ê±Æ÷
}
//¶¯Ì¬ÊýÂë¹ÜÏÔʾËÙ¶È
void display()
{
unsigned char i;
LedOut[0]=Disp_Tab[show%10000/1000];
LedOut[1]=Disp_Tab[show%1000/100];
LedOut[2]=Disp_Tab[show%100/10]&0x7f;
LedOut[3]=Disp_Tab[show%10];
for( i=0; i<8; i++)
{
P0 = LedOut[i];
switch(i) //λѡ
{
case 0:LS138A=0; LS138B=0; LS138C=0; break;
case 1:LS138A=1; LS138B=0; LS138C=0; break;
case 2:LS138A=0; LS138B=1; LS138C=0; break;
case 3:LS138A=1; LS138B=1; LS138C=0; break;
case 4:LS138A=0; LS138B=0; LS138C=1; break;
case 5:LS138A=1; LS138B=0; LS138C=1; break;
case 6:LS138A=0; LS138B=1; LS138C=1; break;
case 7:LS138A=1; LS138B=1; LS138C=1; break;
}
// j=150;
// while(j--);
P0=0x00;//ÏûÒþ
}
}
//µç»úÕýÏòÔËÐк¯Êý
void ForwardRun() //µ¥ËÄÅÄÇý¶¯·½Ê½£¬Ã¿Ò»²½µÄת½ÇΪ7.5¶È
{
//A/
if(Time==2)
{
PH1 = 0;
I01 = 0;
I11 = 0;
PH2 = 0;
I02 = 1;
I12 = 1;
speed++;
}
//B
if(Time==4)
{
PH1 = 0;
I01 = 1;
I11 = 1;
PH2 = 1;
I02 = 0;
I12 = 0;
speed++;
}
//A
if(Time==6)
{
PH1 = 1;
I01 = 0;
I11 = 0;
PH2 = 1;
I02 = 1;
I12 = 1;
speed++;
}
//B/
if(Time==8)
{
PH1 = 1;
I01 = 1;
I11 = 1;
PH2 = 0;
I02 = 0;
I12 = 0;
speed++;
}
flag = 1;
}
//µç»ú·´ÏòÔËÐк¯Êý
void BackRun()
{
//B/
if(Time==2)
{
PH1 = 1;
I01 = 1;
I11 = 1;
PH2 = 0;
I02 = 0;
I12 = 0;
speed++;
}
//A
if(Time==4)
{
PH1 = 1;
I01 = 0;
I11 = 0;
PH2 = 1;
I02 = 1;
I12 = 1;
speed++;
}
//B
if(Time==6)
{
PH1 = 0;
I01 = 1;
I11 = 1;
PH2 = 1;
I02 = 0;
I12 = 0;
speed++;
}
//A/
if(Time==8)
{
PH1 = 0;
I01 = 0;
I11 = 0;
PH2 = 0;
I02 = 1;
I12 = 1;
speed++;
}
flag = 0;
}
void main()
{
Time = 0;
TimerConfiguration();
while(1)
{
display();
if(Time==20)
{
show = (speed*7.5*60)/360;
Time = 0;
show = 0;
speed = 0;
}
if(K1==0)
{
delay10ms(1);
if(K1==0)
{
temp=1;
}
}
if(K2==0)
{
delay10ms(1);
if(K2==0)
{
temp=2;
}
}
if(K3==0)
{
delay10ms(1);
if(K3==0)
{
temp = 3;
}
}
if(K4==0)
{
delay10ms(1);
if(K3==0)
{
temp = 4;
}
}
switch(temp)
{
case 1: ForwardRun();break;
case 2: BackRun(); break;
case 3:
if(flag == 1)
{
ForwardRun();
}
else
{
BackRun();
}
break;
case 4:
if(flag == 1)
{
ForwardRun();
}
else
{
BackRun();
}
break;
}
}
}
void Timer0() interrupt 1
{
TH0 = 0x3C;
TL0 = 0xB0;
Time++;
}