八位数码管+流水灯
原理图
思路:要使数码管和led亮起都与使能端E3相关,要使数码管与流水灯同时运行,需要在两者间快速切换,初始化先让数码管亮起,进入循环,每次循环先让使能端E3置0,进行数码管的动态扫描,然后使能E3置1,LED显示,计数并延时,计数达到50时,使LED左移,并将计数归零,重复这个过程。
#define uint unsigned int
#define uchar unsigned char
uchar arrSeg7Select[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f};
uchar arrDigitSelect[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
sbit sbtLedSel = P2 ^ 3;
uchar uiLed = 0x01;
uint uiLedCnt = 0;
uchar i = 0;
void Init()
{
P0M1 = 0x00;
P0M0 = 0xff;
P2M1 = 0x00;
P2M0 = 0x08;
sbtLedSel = 0;
}
void delay_ms( uint n )
{
while( n )
{
uchar i, j;
i = 11;
j = 190;
do
{
while ( --j );
}
while ( --i );
n--;
}
}
void main()
{
Init();
while( 1 )
{
sbtLedSel = 0;
for( i = 0; i < 8; i++ )
{
P0 = 0;
P2 = arrDigitSelect[i];
P0 = arrSeg7Select[i + 1];
delay_ms( 1 );
}
uiLedCnt++;
sbtLedSel = 1;
P0 = uiLed;
delay_ms( 1 );
if( uiLedCnt == 50 )
{
if( uiLed == 0x80 )
uiLed = 0x01;
else
uiLed = uiLed << 1;
uiLedCnt = 0;
}
}
}
|