#include<reg52.h>
void Delay(unsigned int t)
{
while(t--);
while(t--);
}
void Select_HC138(unsigned char n)
{
switch(n)
{
case 4:
P2 = (P2 & 0x1f) | 0x80; //控制LED
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0; //控制蜂鸣器
break;
}
}
void LED()
{
unsigned char i;
for(i=0;i<3;i++)
{
P0 = 0x00; //点亮LED(低电平点亮)
Delay(60000);
Delay(60000);
P0 = 0xff;
Delay(60000);
Delay(60000);
}
for(i=1;i<=8;i++)
{
P0 = 0xff << i; //依次点亮LED
Delay(60000);
Delay(60000);
}
for(i=1;i<=8;i++)
{
P0 = ~(0xff << i);
Delay(60000);
Delay(60000);
}
}
void main()
{
Select_HC138(5);
P0 = 0x00; //关闭蜂鸣器
Select_HC138(4);
while(1)
{
LED();
}
}