很酷的流水灯
#include <reg51h>
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;
}
}
}
}
以12MHz晶振为例
ORG 000H
AJMP START
START:
SETB C ;跑马灯,一直循环255次
MOV R0,#0FFH
MOV A,#0
JX1:RLC A
MOV P1,A
LCALL DELAY500MS
DJNE R0,JX1
SETB C
MOV R0,#8
MOV A,#0
JX:RLC A ;P10>>>P17依次亮1S
MOV P1,A
LCALL DELAY1S
DJNE R0,JX
;灯全亮,1S
MOV P1,#0FFH
LCALL DELAY1S
MOV P1,#0
MOV P1,#01010101B ;偶数灯亮1S
LCALL DELAY1S
MOV P1,#0
MOV P1,#10101010B ;奇数灯闪2次
LCALL DELAY500MS
MOV P1,#0
LCALL DELAY500MS
MOV P1,#10101010B
LCALL DELAY500MS
MOV P1,#0
AJMP $
DEALY500MS:
MOV R7,#4
D1S1:MOV R6,#250
D1S:MOV R5,#250
DJNZ R5,$
DJNZ R6,D1S
DJNZ R7,D1S1
RET
DELAY1S:
MOV R7,#8
D1S1:MOV R6,#250
D1S:MOV R5,#250
DJNZ R5,$
DJNZ R6,D1S
DJNZ R7,D1S1
RET
差不多应该是这样的试试看吧:)
复位键开关用硬件做个就行 ORG 0000H LJMP START ORG 30H START: MOV A,#0FEH LOOP: MOV P1,A RL A LCALL DELAY LJMP LOOP ;以下延时子程序 DELAY: MOV R7,#250 D1: MOV R6,#250 D2: DJNZ R6,D2 DJNZ R7,D1 RET end
//假设晶振12MHZ,P1控制8个LED低电平亮
#include "reg51h"
unsigned int YSJS=0;
signed char LED=0XFE;
void main(void)
{
TMOD |= 0X10; //定时器1工作于方式1,16位定时器
TH1 = 0XFE; //定时器1定时时间=500uS
TL1 = 0X0C;
ET1 = 1; //使能定时器1中断
TR1 =1;
EA =1; //开总中断
while(1)
{
}
}
void TIMER1(void) interrupt 3 //定时器1中断函数使用默认寄存器组
{
TH1 = 0XFE; //定时器1定时时间=500us
TL1 = 0X0C;
YSJS++;
if(YSJS==2000) //500uS2000=1000000uS=1S
{
P1 = LED;
LED=LED<<1;
LED=LED | 1
if(LED==0XFF)LED=0XFE;
YSJS = 0
}
}
ORG 0000H
LJMP MAIN
ORG 0003H
LJMP EXT0
ORG 000BH
LJMP T0ISR
ORG 0030H
MAIN:
MOV TMOD,#01H
MOV R2,#0FEH
MOV TH0,#HIGH(65536-50000)
MOV TL0,#LOW(65536-50000)
SETB TR0
SETB ET0
SETB IT0
SETB EX0
SETB EA
CLR 00H
MOV R3,#10
MOV R4,#00H
SJMP $
EXT0:
SETB 00H
RETI
T0ISR:
CLR TR0
MOV TH0,#HIGH(65536-50000)
MOV TL0,#LOW(65536-50000)
SETB TR0
JB 00H,SHAN
MOV A,R2
MOV P0,A
RL A
MOV R2,A
RETI
SHAN:
MOV A,R4
MOV P0,A
CPL A
MOV R4,A
DJNZ R3,T0E
MOV R3,#10
CLR 00H
T0E:
RETI
END
int runlinght()
{int i,j;
j=1;
for(i=0;i<8;i++)
{P[i]=(j<<i); //假设输出高点亮
timedelay();
};
时间的问题,这里你只要在延时函数里解决就OK了,你在主函数里面计算调用点灯函数的次数,再用这个标志去改变延时函数的参数。
以上就是关于单片机89C51用汇编制作跑马灯P1控制8个LED(D1~D8)灯实现流水灯的效果,高手速度来全部的内容,包括:单片机89C51用汇编制作跑马灯P1控制8个LED(D1~D8)灯实现流水灯的效果,高手速度来、请单片机高手进来帮一下忙,谢谢了,急需,用89C51单片机语言编写跑马灯程序、AT89C51跑马灯程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)