#include "regx52.h"
void Delay(int time)
{ while(time--) //The while loop takes 10us { } }
void Timer0Init(void) //1 millisecond @11.0592MHz
{
TMOD &= 0xF0; //Set the timer mode
TMOD |= 0x01; //Set the timer mode
TL0 = 0x66; //Set the initial timer value
TH0 = 0xFC; //Set the initial timer value
TF0 = 0; //Clear TF0 Flag
TR0 = 1; //Timer 0 starts timing
ET0 = 1;
EA = 1;
PT0 = 0;
}
int i,count,LEDMode;
// What needs to be executed after the interrupt
void Timer0_motion() interrupt 1
{
TL0 = 0x66; //Set the initial timing value
TH0 = 0xFC; //Set the initial timing value
count++;
if(count == 500 )
{
if(LEDMode == 0) //LED light moves left
{ if(i>=8)i=0; P2=~(0x01<<i); i++; }
if(LEDMode == 1) //LED灯右移
{
if(i<0)i=7;
P2=~(0x01<<i);
i--;
}
count=0;
}
}
//Signal processing for independent keys
int Key(void)
{
int KeyNumber = 0;
if(P3_1==0){Delay(1000);while(P3_1==0);Delay(1000);KeyNumber = 1;}
if(P3_0==0){Delay(2000);while(P3_0==0);Delay(2000);KeyNumber = 2;}
if(P3_2==0){Delay(2000);while(P3_2==0);Delay(2000);KeyNumber = 3;}
if(P3_3==0){Delay(2000);while(P3_3==0);Delay(2000);KeyNumber = 4;}
return KeyNumber;
}
int main()
{ int KeyNum; //Timer initialization Timer0Init(); while(1) { KeyNum=Key(); //Receive and return the status of independent keys if(KeyNum) { if(KeyNum == 1) { LEDMode++ ; if(LEDMode>=2)LEDMode=0; } } } return 0; }
Experimental phenomena