#include<reg51.h>
#define uchar unsigned char
bit flag;
uchar a=1,t=100;
uchar code tab1[]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};
uchar code tab2[]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1};
sfr P1M1 = 0X91;
sfr P1M0 = 0X92;
sfr P2M1 = 0X95;
sfr P2M0 = 0X96;
void delay(uchar ms)
{
uchar j;
while(ms--)
for(j=0;j<100;j++);
}
void keyscan()
{
P1M1 = 0;
P2M1 = 0;
P1M0 = 0XFF;
P2M0 = 0XFF;
if(P2!=0xff)
{
delay(5);
if(P2!=0xff)
{
switch(P2)
{
case 0xfe:a=1;break;//正转
case 0xfd:a=2;break;//反转
case 0xfb:if(t>50)t=t-10;else t=50;break;//加速
case 0xf7:if(t<150)t=t+10;else t=150;break;//减速
case 0xef:flag=1;break;//开始
case 0xdf:flag=0;break;//停止
}
while(P2!=0xff);
}
}
}
void time0_init() //定时器T0初始化
{
TMOD=0x01; //设置定时器0工作方式1
TH0=(65536-50000)/256;//定时50ms取高八位
TL0=(65536-50000)%256; //取低八位
ET0=1; //开定时器0中断使能
EA=1; //开总中断使能
TR0=1; //开启中断
}
void time0() interrupt 1 //定时器T0入口函数
{
TH0=(65536-50000)/256; //定时50ms重新赋初值
TL0=(65536-50000)%256;
keyscan();
}
void main()
{
uchar i;
time0_init();
while(1)
{
if(flag)
{
if(a==1)
{
for(i=0;i<8;i++)
{
P1=tab1[i];
delay(t);
}
}
if(a==2)
{
for(i=0;i<8;i++)
{
P1=tab2[i];
delay(t);
}
}
}
else
P1=0x00;
}
} |