74HC138三八译码器和74HC573锁存器和74HC02或非门
P0口通过锁存器来控制单片机
138译码器又受到P25,26,27的控制
#include "reg52.h"
sbit HC_138A=P2^5;
sbit HC_138B=P2^6;
sbit HC_138C=P2^7;
void Delay(unsigned int t)
{
while(t--);
while(t--);
}
void LED_Running()
{
HC_138C=1;
HC_138B=0;
HC_138A=0;
P0=0x00;
Delay(60000);
P0=0xFF;
Delay(60000);
}
void main()
{
while(1)
{
LED_Running();
}
}
例题:让8路LED指示灯闪烁3遍然后熄灭,接着依次点亮LED指示灯,最后依次熄灭指示灯。
#include "reg52.h"
sbit HC_138A=P2^5;
sbit HC_138B=P2^6;
sbit HC_138C=P2^7;
void Delay(unsigned int t)
{
while(t--);
while(t--);
}
void LED_Running()
{
unsigned char i;
HC_138C=1;
HC_138B=0;
HC_138A=0;
for(i=0;i<3;i++)
{
P0=0x00;
Delay(60000);
P0=0xFF;
Delay(60000);
}
Delay(60000);
for(i=1;i<=8;i++)
{
P0=0xff<<i;
Delay(60000);
}
for(i=1;i<=8;i++)
{
P0=~(0xff<<i);
Delay(60000);
}
}
void main()
{
while(1)
{
LED_Running();
}
}