好像有点困难。
一、猜雹
#include
#include
#define uint unsigned int //宏定义
#define uchar unsignedchar //宏定义
sbit dula=P2^6; //数码管段选锁存端
sbit wela=P2^7; ////数码管位选锁存端
uchar code table[]={ //数码管显示编码
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code point[]={
0xbf,0x86,0xdb,0xcf,
0xe6,0xed,0xfd,0x87,
0xff,0xef,0xf7,0xfc,
0xb9,0xde,0xf9,0xf1};
uchar ct1,ct2,ct3,ct4,ct5,ct6;
void display(uchar,uchar,uchar,uchar,uchar,uchar); //函数声明
void delay(uint);
void main()
{ EA=1;
TMOD=0x01;
ET0=1;
P0=0xff;
TH0=(65536-10)/256;
TL0=(65536-10)%256;
TR0=1;
ct1=ct2=ct3=ct4=ct5=ct6=0;
while(1);
}
void stop() interrupt 1
{ TH0=(65536-10)/256;
TL0=(65536-10)%256;
ct1++;
display(ct6,ct5,ct4,ct3,ct2,ct1);
if(ct1==10)
{ ct1=0;
ct2++;
}
if(ct2==10)
{ ct2=0;
ct3++;
}
if(ct3==10)
{ ct3=0;
ct4++;
}
if(ct4==10)
{ ct4=0;
ct5++;
}
if(ct5==10)
{ ct5=0;
ct6++;
}
if(ct6==10)
{ ct6=0;
}
}
void display(uchar one,uchar two,uchar three,uchar four,uchar five,uchar six)
{
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=table[one];
dula=0;
delay(1);
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
dula=1;
P0=table[two];
dula=0;
delay(1);
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
dula=1;
P0=table[three];
dula=0;
delay(1);
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
dula=1;
P0=point[four];
dula=0;
delay(1);
P0=0xff;
wela=1;
P0=0xef;
wela=0;
dula=1;
P0=table[five];
dula=0;
delay(1);
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
dula=1;
P0=table[six];
dula=0;
delay(1);
P0=0xff;
}
void delay(uint z) //延时子函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
自己改时间
-----------------------------------------------------------------
二、有时钟功能,可以显示并不断刷新当前时间;并可以预先设置好响铃时间,在到达响铃穗告帆时间就会响铃
data segment
hour dw ? ;响铃时的小时数
minute dw ? ;响铃时的分钟数
ten dw 10
s1 db 'please set the time when the clock will ring(set it as Hour:Minite): $'
s2 db 'the ringing time is: $'
s3 db '友搜current time is: $'
s4 db 'Ring Ring Ring Ring Ring Ring Ring !!!!!!! $'
error db 'set time error!$'
data ends
code segment
assume cs:code,ds:data
;用于清除一行的字符
cleanLine proc
push ax
push bx
push cx
push dx
mov cx,85
mov ah,2h
mov dl,0dh
int 21h
clean:
mov dl,' '
int 21h
loop clean
mov dl,0dh
int 21h
pop dx
pop cx
pop bx
pop ax
ret
cleanLine endp
;用于显示一个回车换行
changeline proc
push ax
push bx
push cx
push dx
mov ah,2h
mov dl,0dh
int 21h
mov dl,0ah
int 21h
pop dx
pop cx
pop bx
pop ax
ret
changeline endp
inputNegativeNumber proc;用于从键盘中接受一个十进制数,存到bx中
push ax
push cx
push dx
mov bx,0
mov cl,0
mov ah,1
int 21h
cmp al,'-'
je k4
k0:
cmp al,30h
jb k3
cmp al,39h
ja k3
sub al,30h
mov ah,0
xchg ax,bx
push cx
mov cx,10
mul cx ;考虑溢出
pop cx
add bx,ax ;考虑溢出
k1:
mov ah,1
int 21h
jmp k0
k4:
mov cl,1
jmp k1
k3:
cmp cl,1
jne over
neg bx
over:
pop dx
pop cx
pop ax
ret
inputNegativeNumber endp
outputNumber proc;输出ax中的一个数字
push ax
push bx
push cx
push dx
mov cx,0
mov bx,10
cmp ax,0
jge p0
push ax
mov ah,2h
mov dl,'-'
int 21h
pop ax
neg ax
p0:
mov dx,0
div bx
push dx
inc cx
cmp ax,0
jnz p0
mov ah,2
cmp cx,2
jae p1
mov dx,0
push dx
inc cx
p1:
pop dx
add dx,30h
int 21h
loop p1
pop dx
pop cx
pop bx
pop ax
ret
outputNumber endp
setRingTime proc;用于设置响铃时间
push ax
push bx
push cx
push dx
mov bx,0
h:
mov ah,1h
int 21h
cmp al,30h
jb wrong
cmp al,32h
ja wrong
sub al,30h
mov ah,0
xchg ax,bx
mul ten
add bx,ax
mov ah,1h
int 21h
cmp al,30h
jb wrong
cmp al,39h
ja wrong
sub al,30h
mov ah,0
xchg ax,bx
mul ten
add bx,ax
mov hour,bx
mov ah,1h
int 21h
cmp al,':'
jnz wrong
mov bx,0
m:
mov ah,1h
int 21h
cmp al,30h
jb wrong
cmp al,36h
jae wrong
sub al,30h
mov ah,0
xchg ax,bx
mul ten
add bx,ax
mov ah,1h
int 21h
cmp al,30h
jb wrong
cmp al,39h
ja wrong
sub al,30h
mov ah,0
xchg ax,bx
mul ten
add bx,ax
mov minute,bx
jmp o
wrong:
call changeline
call changeline
mov ah,9h
lea dx,error
int 21h
mov ah,4ch
int 21h
o:
pop dx
pop cx
pop bx
pop ax
ret
setRingTime endp
showRingTime proc;用于显示响铃时间
push ax
push bx
push cx
push dx
lea bx,hour
mov ax,[bx]
call outputNumber
mov ah,2h
mov dl,':'
int 21h
add bx,2
mov ax,[bx]
call outputNumber
pop dx
pop cx
pop bx
pop ax
ret
showRingTime endp
getTime proc;用于截取系统时间
push ax
mov bx,0
mov ah,2ch
int 21h
mov al,ch
mov ah,0
cmp ax,hour
je e1
g1: call outputNumber
mov ah,2h
mov dl,':'
int 21h
mov ah,0
mov al,cl
cbw
cmp minute,ax
je e2
g2: call outputNumber
mov ah,2h
mov dl,':'
int 21h
mov ah,0
mov al,dh
cbw
call outputNumber
jmp ed
e1:
inc bx
jmp g1
e2:
inc bx
jmp g2
ed: pop ax
ret
getTime endp
delay proc;用于延时0.5秒
push ax
waiting:
in al,61h
and al,10h
cmp al,ah
je waiting
mov ah,al
loop waiting
pop ax
ret
delay endp
aSecond proc;用于延时1秒
push ax
push bx
push cx
push dx
mov bl,2
back:
mov cx,33144
call delay
dec bl
jnz back
pop dx
pop cx
pop bx
pop ax
ret
aSecond endp
main proc far
push ds
sub ax,ax
push ax
mov ax,data
mov ds,ax ;输出设置响铃时间的提示
mov ah,9h
lea dx,s1
int 21h
call setRingTime
call cleanLine;清除屏幕上光标所在这一行上的字符串,并把光标移动到行首
mov ah,9h
lea dx,s2
int 21h
call showRingTime ;输出设置好的响铃时间
call changeline
call changeline
time: ;用于每过一秒种刷新一次显示的时间
mov ah,9h
lea dx,s3
int 21h
call getTime
cmp bx,2 ;如果bx=2则已经到了设定的响铃时间
je ring
call aSecond
mov ah,2h
mov dl,0dh
int 21h
jmp time
ring: ;响铃
call changeline
call changeline
call changeline
mov ah,9h
lea dx,s4
int 21h
mov cx,40
last:
mov ah,2h
mov dl,07h
int 21h
call aSecond
call aSecond
call aSecond
loop last
finish:
ret
main endp
code ends
end main