1.1控制一个灯亮
void main()
{
P2=0xFE;
}
1.2控制一个灯闪烁
void Delay500ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2=0xFE;
Delay500ms();
P2=0xFF;
Delay500ms();
}
}
1.3流水灯
void Delay500ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2=0xFE;
Delay500ms();
P2=0xFD;
Delay500ms();
P2=0xFB;
Delay500ms();
P2=0xF7;
Delay500ms();
P2=0xEF;
Delay500ms();
P2=0xDF;
Delay500ms();
P2=0xBF;
Delay500ms();
P2=0x7F;
Delay500ms();
}
}
2.1独立按键控制亮灭
void main()
{
while(1)
{
if(P3_1 == 0)
{
P2_0=0;
}
else
{
P2_0=1;
}
}
}
2.2独立按键控制LED状态
void Delay(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
}
while (--i);
xms--;
}
}
void main()
{
while(1)
{
if(P3_1==0)
{
Delay(20);
while(P3_1==0);
Delay(20);
P2_0=~P2_0;
}
}
}
2.3独立按键控制LED显示二进制
void Delay(unsigned int xms) //@12.000MHz
{
while(xms)
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
}
while (--i);
xms--;
}
}
void main()
{
unsigned char LED_Num=0;//表示寄存器
while(1)
{
if(P3_1 == 0)
{
Delay(20);
while(P3_1 == 0);
Delay(20);
//默认P2后为高电平 1111 1111
LED_Num++;//0000 0001
P2=~LED_Num;//1111 1110
}
}
}
2.4独立按键控制LED移位
void Delay(unsigned int xms) //@12.000MHz
{
while(xms)
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
}
while (--i);
xms--;
}
}
unsigned char LED_Num = 0;
void main()
{
P2=~0x01;//初始化
while(1)
{
if(P3_1 == 0)
{
Delay(20);
while(P3_1 == 0);
Delay(20);
LED_Num++;
if(LED_Num>=8)
LED_Num=0;
P2=~(0x01<<LED_Num);//注意取反与括号的先后顺序
}
if(P3_0 == 0)
{
Delay(20);
while(P3_0 == 0);
Delay(20);
if(LED_Num==0)
LED_Num=7;
else
LED_Num--;
P2=~(0x01<<LED_Num);
}
}
}