#include<reg51.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,30,50,100,200,230,280,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_TNT() 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;  }      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;     
Speed=sTable[Idx];     
 P3=DSY_CODE[ModeNo];     
IE=0x82;     
TMOD=0x00;     
TR0=1;     
while(1)     
{          
Key=GetKey();          
if(Key!=0)  KeyProcess(Key);    
 } }