#include<reg52.h>
typedef unsigned int u16;
sbit d1=P1^3;
sbit d2=P3^5;
sbit d3=P3^6;
sbit d4=P3^7;
sbit left_a =P0^0; //正转
sbit left_b =P0^1;
sbit right_a =P0^2; //正转
sbit right_b = P0^3;
sbit beef=P0^7;
void delay(u16 s)
{
u16 n,m;
for(s=n;n>0;n--)
for(m=200;m>0;m--);
}
u16 code smg[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
u16 code wei[10]={0xef,0xdf,0xbf,0x7f};
u16 i,num=0,hs,miao,fen;
u16 shuju;
void chuankou()
{
EA=1;//打开中断总开关
TMOD=0X21;//设置定时器工作方式2
TH1=0XFD;//设置波特率 9600
TL1=0XFD;
TR1=1;//定时器1开始记数
TH0=0XFC;
TL0=0X18;
ET0=1;
TR0=1;
SCON=0X50;//串口控制
ES=1;//打开串口中断开关
}
void lsd()
{
while(shuju=0x04)
{
d1=0;
delay(100);
d1=1;
d2=0;
delay(100);
d2=1;
d3=0;
delay(100);
d3=1;
d4=0;
delay(100);
}
}
void smgxianshi()
{
while(shuju=0x01)
{
if(num>=100)
{
num=0;
hs++;
if(hs>=10)
{
hs=0;
miao++;
if(miao>=60)
{
miao=0;
fen++;
if(fen>=10)
{
fen=0;
miao=0;
hs=0;
}
}
}
}
for(i=0;i<4;i++)
{
if(i==4) i=0;
P0=0X00;
switch(i)
{
case 0:P2=wei[i];P0=smg[fen];break;
case 1:P2=wei[i];P0=smg[miao/10];break;
case 2:P2=wei[i];P0=smg[miao%10];break;
case 3:P2=wei[i];P0=smg[hs];break;
}
delay(10);
}
}
}
void main()
{
chuankou();
while(!RI)
RI=0;
shuju=SBUF;
switch(shuju)
{
case 0x01:smgxianshi();break;
case 0x02:TR0=~TR0;break;
case 0x03:beef=~beef;break;
case 0x04:lsd();break;
case 0x05:
{
left_a = 1; //正转
left_b = 0;
right_a = 1; //正转
right_b = 0;
break;
}
case 0x06:
{
left_a = 0; //反转
left_b = 1;
right_a = 0; //反转
right_b = 1;
break;
}
case 0x07:
{
left_a = 0; //不转
left_b = 0;
right_a = 1; //正转
right_b = 0;
break;
}
case 0x08:
{
left_a = 1; //正转
left_b = 0;
right_a = 0; //不转
right_b = 0;
break;
}
}
}
void timer0() interrupt 1
{
TH0=0XFC;
TL0=0X18;
num++;
}
void ck() interrupt 4
{
RI=0;
shuju=SBUF;
}