#include<reg52.h>
#define uc unsigned char
sbit KEY1 = P3^1;
sbit KEY2 = P3^0;
void delay(uc c)
{
uc a,b;
for(;c>0;c--)
for(a=38;a>0;a--)
for(b=130;b>0;b--);
}
void main()
{
uc LEDNum = 0;
P2 = 0xFE;
while(1)
{
if(KEY1 == 0)
{
delay(1);
while(KEY1 == 0);
delay(1);
if(LEDNum >= 7) LEDNum = 0;
else
LEDNum++;
P2=~(0x01<<LEDNum);
}
if(KEY2 == 0)
{
delay(1);
while(KEY2 == 0);
delay(1);
if(LEDNum <= 0) LEDNum = 7;
else
LEDNum--;
P2=~(0x01<<LEDNum);
}
}
}
- 接线:
- P2接LED模块(注意正反)
- key1接P3^1
- key2接P3^0