实验8.1 用P1口作为输出口实现循环彩灯。(至少要有5种形式)
这个题目可以说是非常开放的一个题目啦,各自发挥自己的想象力来创造出更多的彩灯模式。
C语言程序:
#include<reg51.h>
#define uchar unsigned char
uchar a=0;
uchar mode5[8]={0x80,0x01,0x40,0x02,0x20,0x04,0x10,0x08};
uchar mode6[8]={0x81,0x42,0x24,0x18,0x24,0x42,0x81};
timer0()interrupt 1{
a++;
TH0=0x3c;
TL0=0xb0;
}
main(){
uchar mode=0;
uchar cnt=0;
uchar h=0;
uchar l=0;
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
EA=1;
ET0=1;
TR0=1;
P1=0x01;
while(1){
if(a==20){
if(mode<=4){
a=0;
}else if(mode<=6){
a=10;
}else{
a=0;
}
switch(mode){
case 0:{
P1<<=1;
P1+=1;
cnt++;
break;
}
case 1:{
P1<<=1;
cnt++;
break;
}
case 2:{
P1>>=1;
P1=P1|0x80;
cnt++;
break;
}
case 3:{
P1>>=1;
cnt++;
break;
}
case 4:{
P1=~P1;
cnt++;
break;
}
case 5:{
P1=mode5[cnt++];
break;
}
case 6:{
P1=mode6[cnt++];
break;
}
case 7:{
h>>=1;
h=h|0x80;
l<<=1;
l++;
P1=h|l;
cnt++;
break;
}
}
if(mode==0&&cnt==7){
cnt=0;
mode++;
}
if((mode==1||mode==2||mode==3||mode==5||mode==6)&&cnt==8){
cnt=0;
mode++;
}
if((mode==4||mode==7)&&cnt==4){
cnt=0;
mode++;
if(mode==8){
h=0;
l=0;
mode=0;
}
}
}
}
}
汇编程序的模式和C语言的不一样,因为不是我写的模式,但是这个程序跳转的框架是我写的,也是比较的麻烦的。
ORG 0000H
AJMP START1
ORG 000BH
AJMP IP0
ORG 0030H
START1:
MOV A,#01H
MOV P1,A
MOV R7,#00H
MOV TMOD,#01H
MOV TH0,#3CH
MOV TL0,#0B0H
SETB ET0
SETB EA
MOV R6,#14H
SETB TR0
F:MOV P1,A
CJNE A,#80H,X
JMP Y
E:MOV P1,A
CJNE A,#01H,X
JMP Y
D:
JMP Y
H:MOV A,01H
MOV P1,A
JMP Y
L:MOV P1,A
CJNE A,#0FFH,X
JMP Y
I:MOV P1,A
CJNE A,#00H,X
MOV A,#80H
MOV P1,A
JMP Y
M:MOV P1,A
CJNE A,#0FFH,X
JMP Y
N:MOV P1,A
CJNE A,#00H,X
JMP Y
Y:INC R7
CJNE R7,#8,X
MOV R7,#0
MOV A,#01H
X:SJMP $
IP0:
DJNZ R6,RET0
MOV R6,#14H
PUSH ACC
MOV A,R7
ADD A,R7
MOV DPTR,#MODETAB
JMP @A+DPTR
RET0:
MOV TH0,#3CH
MOV TL0,#0B0H
SETB TR0
RETI
MODETAB:
AJMP MODE1
AJMP MODE2
AJMP MODE3
AJMP MODE4
AJMP MODE5
AJMP MODE6
AJMP MODE7
AJMP MODE8
MODE1:
POP ACC
RL A
POP DPL
POP DPH
MOV DPTR,#F
PUSH DPL
PUSH DPH
RETI
MODE2:
POP ACC
RR A
POP DPL
POP DPH
MOV DPTR,#E
PUSH DPL
PUSH DPH
RETI
MODE3:
POP ACC
MOV A,#0FFH
MOV P1,A
SJMP DELAY1
J:POP DPL
POP DPH
MOV DPTR,#D
PUSH DPL
PUSH DPH
RETI
MODE4:
POP ACC
MOV A,#00H
MOV P1,A
SJMP DELAY2
K:POP DPL
POP DPH
MOV DPTR,#H
PUSH DPL
PUSH DPH
RETI
MODE5:
POP ACC
RL A
INC A
POP DPL
POP DPH
MOV DPTR,#L
PUSH DPL
PUSH DPH
RETI
MODE6:
POP ACC
RL A
DEC A
POP DPL
POP DPH
MOV DPTR,#I
PUSH DPL
PUSH DPH
RETI
MODE7:
POP ACC
RR A
ORL A,#80H
POP DPL
POP DPH
MOV DPTR,#M
PUSH DPL
PUSH DPH
RETI
MODE8:
POP ACC
RR A
ANL A,#7FH
POP DPL
POP DPH
MOV DPTR,#N
PUSH DPL
PUSH DPH
RETI
DELAY1:
MOV R0,#10H
L3:MOV R1,#500
L1:MOV R3,#500
L2:DJNZ R3,L2
DJNZ R1,L1
DJNZ R0,L3
AJMP J
DELAY2:
MOV R0,#10H
L6:MOV R1,#500
L4:MOV R3,#500
L5:DJNZ R3,L5
DJNZ R1,L4
DJNZ R0,L6
AJMP K
END
实验8.2 设计一个简易数字钟。选择一位开关作为数字钟的开关,开关合上后,数字钟开始运行。led8–led13表示为6位二进制数,显示数字钟的秒值;led14–led15表示为2位二进制数,显示数字钟的分钟值,如达到4分钟,分钟值又从0开始;如此循环往复。
汇编程序:
ORG 0000H
AJMP START
ORG 000BH
AJMP IP0
ORG 0030H
START:
MOV P1,#00H
MOV TMOD,#01H
MOV TH0,#3CH
MOV TL0,#0B0H
SETB ET0
SETB EA
MOV R7,#14H
MOV R0,#0
MOV R1,#0
SETB TR0
SJMP $
IP0:
DJNZ R7,RET0
MOV R7,#14H
INC R0
CJNE R0,#3CH,F
MOV R0,#00H
INC R1
CJNE R1,#04H,F
MOV R1,#00H
F:
MOV A,R1
RR A
RR A
ADD A,R0
MOV P1,A
RET0:
MOV TH0,#3CH
MOV TL0,#0B0H
RETI
END
C语言程序:
#include<reg51.h>
uchar a=0;
uchar miao=0;
uchar fen=0;
timer0()interrupt 1{
a++;
TH0=0x3c;
TL0=0xb0;
}
main(){
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
EA=1;
ET0=1;
TR0=1;
P1=0x00;
while(1){
if(a==20){
miao=P1&0x3f;
fen=P1&0xc0;
miao++;
if(miao>=60){
miao=0;
fen+=0x40;
}
P1=miao|fen;
}
}
}