//可調控的跑馬燈
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar ModeNo;//模式
uint Speed;
uchar TCount=0;////////////?????????????
uchar Idx;
uchar mb_Count=0;
bit Dirtect=1;
uchar code DSY_CODE[]={0xC0,0XF9, 0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
uint code sTable[]={0,1,3,5,7,9,15,100,200,230,300,350};
void Delay(uint x)
{
uchar i;
while(x--) for(i=0;i<120;i++);
}
uchar GetKey()//獲取按鍵信息
{
uchar K;
if(P2==0xFF) return 0;
Delay(10);
switch(P2)
{ case 0xFE: K=1;break; case 0xFD: K=2;break; case 0xFB: K=3;break; default: K=0;
} while (P2!=0xFF); return K;
}
void Led_Demo(uint Led16)
{
P1=(uchar)(Led16&0x00FF);
P0=(uchar)(Led16>>8);
}
void T0_INT() interrupt 1//模式內容
{
if(++TCount<Speed) return;/////////////////////?????????
TCount=0;
switch(ModeNo)
{
case 0:Led_Demo(0x0001<<mb_Count);break;
case 1:Led_Demo(0x8000>>mb_Count);break;
case 2:if(Dirtect)Led_Demo(0x000F<<mb_Count);
Else
Led_Demo(0xF000>>mb_Count);
if(mb_Count==15) Dirtect=!Dirtect;break;
case 3:if(Dirtect)Led_Demo(~(0x000F<<mb_Count));
else Led_Demo(~(0xF000>>mb_Count));
if(mb_Count==15) Dirtect=!Dirtect;
break;
case 4:if(Dirtect)Led_Demo(0x003F<<mb_Count);
else Led_Demo(0xFC00>>mb_Count);
if(mb_Count==15) Dirtect=!Dirtect;break;
case 5:if(Dirtect)Led_Demo(0x0001<<mb_Count);
else Led_Demo(0x8000>>mb_Count);
if(mb_Count==15) Dirtect=!Dirtect;
break;
case 6:if(Dirtect)Led_Demo(~(0x0001<<mb_Count));
else Led_Demo(~(0x8000>>mb_Count));
if(mb_Count==15) Dirtect=!Dirtect;break;
case 7:if(Dirtect)Led_Demo(0xFFFE<<mb_Count);
else Led_Demo(0x7FFF>>mb_Count);
if(mb_Count==15) Dirtect=!Dirtect;break;
default:break;
}
mb_Count=(mb_Count+1)%16;
}
void KeyProcess(uchar Key)//按鍵處理選擇模式
{
switch(Key)
{ case 1:
Dirtect=1;mb_Count=0;
ModeNo=(ModeNo+1)%8;
P3=DSY_CODE[ModeNo];break; case 2:
if(Idx>1) Speed=sTable[--Idx];break; case 3:
if(Idx<15) Speed=sTable[++Idx];
}
}
void main()
{
uchar Key;
P0=P1=P2=P3=0xFF;
ModeNo=0;Idx=4;
P3=DSY_CODE[ModeNo];
IE=0x82;
TMOD=0x00;
TR0=1;
while(1)
{
Key=GetKey();
if(Key!=0) KeyProcess(Key);
}
}
|