include<reg51.h>
unsigned char n = 0;
sbit key = P3^4;
sbit led = P1^0;
void delay()
{
unsigned int i, j;
for(i = 0; i < 100; i++) for(j = 0; j < 500; j++);
}
void readkey(void)
{
unsigned char i, j;
if(!key) {
for(i = 0; i < 10; i++)
for(j = 0; j < 120; j++); //延时消抖
if(!key) {
while(!key); //等待按键释放
n++;
n %= 4; //n = 0~3
}
}
}
main()
{
while(1) {
switch(n) {
case 0: led = 1; readkey(); break; //熄灭
case 1: delay(); readkey(); //慢速闪烁
case 2: delay(); readkey(); //中速闪烁
case 3: delay(); readkey(); led = ~led; //快速闪烁
}
}
}
大致是这个意思,可以参考一下。 |