单片机89C51用汇编制作跑马灯P1控制8个LED(D1~D8)灯实现流水灯的效果,高手速度来

2024-11-02 10:23:15
推荐回答(1个)
回答1:

很酷的流水灯
#include
typedef unsigned char uchar;
#define Ton 40
#define LED P0
sbit leds=P1^4;
sbit LED0 = P0^0;
sbit LED1 = P0^1;
sbit LED2 = P0^2;
sbit LED3 = P0^3;
sbit LED4 = P0^4;
sbit LED5 = P0^5;
sbit LED6 = P0^6;
sbit LED7 = P0^7;

unsigned char Maikuan=0;
static unsigned char ledon[8] = {0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
uchar Occupy_led; //占空比越大,灯越亮
bit light;
uchar m;

void Inittime()
{
TMOD = 0x02; //定时器0,工作方式2,自动重装
TH0 = 256-Ton;
TL0 = 256-Ton;
ET0 = 1;
EA = 1;
TR0 = 1;
}
//定时器0中断
void time0() interrupt 1 using 0
{
Maikuan++;
}

void main()
{
unsigned char i = 0,j = 0; //初始化i,j
leds=0;
Inittime();
while (1)
{
//第1,3,5,7个灯半亮,其余的全亮。
for(i=254; i>0; i--)
{
while ( Maikuan!=20)
{
LED = 0x00;
}

while ( Maikuan!=200)
{
LED = 0x55;
}

Maikuan = 0;
}

//最难的:8个灯从第一个开始依次渐亮,直到最后一个。再从最后一个起渐暗,直到第一个。如此循环往复。

for(j = 0;j<=7;j++)
{
i = 0;
while(i!=255) // 由暗变亮
{
if( Maikuan == 0) // 点亮
{
LED = ledon[j];
}
if(Maikuan == i) // 熄灭
{
LED = 0xff;
}
if( Maikuan== 255)
{
i++;
}
}
}

for(j=7;j>=0;j--)
{
i = 0;
while(i!=255) // 由亮变暗
{
if(Maikuan == 0) // 熄灭
{
LED = 0xff;
}
if( Maikuan == i) // 点亮
{
LED = ledon[j];
}
if( Maikuan == 255)
{
i++;
}
}
}

//所有的灯逐渐从暗到全亮,再由全亮到暗。

for (m=254; m>0; m--)
{
while (Maikuan!=Occupy_led)
{
LED = 0x00;
}

while (Maikuan!=127)
{
LED = 0xff;
}

Maikuan = 0;

if (light)
{
Occupy_led++;
}
else
{
Occupy_led--;
}

if (Occupy_led==128)
{
Occupy_led = 127;
light = !light;
}
if (Occupy_led>128)
{
Occupy_led = 0;
light = !light;
}
}
}
}