假设如下这八个灯依次接在P0口的八个口上,高电平为灯亮,低电平为灯灭,程序如下
ORG 0000H
AJMP START
START:MOV P0,#01H;()只有第一个灯亮
ACALL DELAY;延迟,不加延迟的话 你根本看不起清到底那个灯亮的
MOV P0,#02H()只有第二个灯亮
ACALL DELAY
MOV P0,#04H()只有第三个灯亮
ACALL DELAY
MOV P0,#08H()只有第四个灯亮
ACALL DELAY
MOV P0,#10H()只有第五个灯亮
ACALL DELAY
MOV P0,#20H()只有第六个灯亮
ACALL DELAY
MOV P0,#40H()只有第七个灯亮
ACALL DELAY
MOV P0,#80H()只有第八个灯亮
ACALL DELAY
AJMP START;周而复始
DELAY: MOV R6,#40延迟程序,晶振为3.57MHZ时大概是0.1S
0.1SL1: MOV R5,#246
0.1SL2: NOP
DJNZ R5,0.1SL2
DJNZ R6,0.1SL1
RET
END
#include<reg51.h>sbit LED0=P0^0
sbit wei=P1^1
sbit duan=P1^0
sbit lcden=P1^7
sbit dianzhen=P1^3
sbit leden=P1^2
void Delay(unsigned int t)
void main (void)
{
unsigned int CYCLE=600,PWM_LOW=0//定义周期并赋值
lcden=0
P0=0x00
dianzhen=0
P0=0xff
wei=0
duan=0
while (1) //主循环
{
LED0=1
Delay(150000)//特意加延时,可以看到熄灭的过程
for(PWM_LOW=1PWM_LOW<CYCLEPWM_LOW++)
{
LED0=0
Delay(PWM_LOW)
LED0=1
Delay(CYCLE-PWM_LOW)
}
for(PWM_LOW=CYCLE-1PWM_LOW>0PWM_LOW--)
{
LED0=0
Delay(PWM_LOW)
LED0=1
Delay(CYCLE-PWM_LOW)
}
}
}
void Delay(unsigned int t)
{
while(t--)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)