内容主要来自B站小蜜蜂老师讲解~
实验要求:
实现代码:
#include"reg52.h"
void Delay(unsigned int t)
{
while(t--);
while(t--);
}
void HC138(unsigned char n)
{
switch(n)
{
case 4:P2=(P2&0x1f)|0x80;break;
case 5:P2=(P2&0x1f)|0xa0;break;
case 6:P2=(P2&0x1f)|0xc0;break;
case 7:P2=(P2&0x1f)|0xe0;break;
}
}
void output(unsigned char channel,unsigned char dat)
{
HC138(channel);
P0=dat;
}
void LED_running()
{
unsigned char i;
for(i=0;i<3;i++)
{
output(4,0x00);
Delay(60000);
Delay(60000);
output(4,0xff);
Delay(60000);
Delay(60000);
}
for(i=1;i<=8;i++)
{
output(4,0xff<<i);
Delay(60000);
}
output(5,0x10);
Delay(60000);
Delay(60000);
output(5,0x00);
for(i=1;i<=8;i++)
{
output(4,~(0xff<<i));
Delay(60000);
}
output(5,0x40);
Delay(60000);
Delay(60000);
output(5,0x00);
}
void Initsystem()
{
output(5,0x00);
}
void main()
{
Initsystem();
while(1)
{
LED_running();
}
}