单片机时钟程序 24小时制

急需谢谢!
2024-12-18 04:51:11
推荐回答(3个)
回答1:

C语言的:
#include
#include

unsigned char ledbuf[8];
code unsigned char ledmap[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,
0x92, 0x82, 0xf8, 0x80, 0x90}; //8段显示

void delay(unsigned char cnt)
{
unsigned char i;
while(cnt--!=0)
for(i=100;i!=0;i--);
}

#define tick 7554
#define T100us (256-134)
unsigned char hour,minute,second,ankey;
unsigned int c100us;

void sees()
{
ledbuf[0]=ledmap[hour/10];
ledbuf[1]=ledmap[hour%10];
ledbuf[2]=0xbf;
ledbuf[3]=ledmap[minute/10];
ledbuf[4]=ledmap[minute%10];
ledbuf[5]=0xbf;
ledbuf[6]=ledmap[second/10];
ledbuf[7]=ledmap[second%10];
}

void displayled()
{

char i;
unsigned char pos;
pos=0x01;
for(i=7;i>=0;i--)
{
P2=0x255;
P0=ledbuf[i];
P2=~pos;
delay(2);
P2=0x255;
pos<<=1;
}
}

void key()
{

if(P32==0||P33==0||P35==0)
delay(2);
if(P32==0||P33==0||P35==0)
{
if(P32==0)
{
hour++;
if(hour==24)
hour=0;
while(P32==0)
{
sees();
displayled();
}
}
if(P33==0)
{
minute++;
if(minute==60)
minute=0;
while(P33==0)
{
sees();
displayled();
}
}
if(P35==0)
{
second++;
if(second==60)
second=0;
while(P35==0)
{
sees();
displayled();
}
}
}

}

void t0int()interrupt 1
{
c100us--;
if(c100us==0)
{
c100us=tick;
second++;

if(second==60)
{
second=0;
minute++;

if(minute==60)
{
minute=0;
hour++;
if(hour==24)hour=0;
}
}
}
}

void main()
{

TMOD=0x02;
TH0=T100us;
TL0=T100us;
IE=0x82;
hour=0;
minute=0;
second=0;
c100us=tick;
TR0=1;

while(1)
{
sees();
displayled();
key();
}
}

汇编的:
ORG 0000H
AJMP MAIN
ORG 000BH
AJMP TIME

ORG 0300H
MAIN:
MOV P3,#0FFH;
MOV 25H,#00H;
MOV R0,#40H;
MOV 20H,#00H;
MOV 21H,#00H;
MOV 22H,#00H;
MOV 23H,#00H;
MOV P2,#0FFH
MOV IP,#02H;
MOV IE,#82H;
MOV TMOD,#01H;
MOV TL0,#0B0H;
MOV TH0,#3CH;
SETB TR0;
MOV SP,#50H;

NEXT:
LCALL DISP;
LCALL KEY;
JZ NEXT;
LCALL ANKEY;
SJMP NEXT;
NOP
NOP;
NOP;

TIME:
PUSH ACC;
PUSH PSW;
MOV TL0,#0B4H;
MOV TH0,#3CH;
INC 20H;
MOV A,20H;
CJNE A,#20,RETI1;

MOV 20H,#00H;
MOV A,21H;
INC A;
MOV 21H,A;
CJNE A,#3CH,RETI1;
MOV 21H,#00H;
MOV A,22H;
ADD A,#01H;

MOV 22H,A;
CJNE A,#3CH,RETI1;
MOV 22H,#00H;
MOV A,23H;
ADD A,#01H;

MOV 23H,A;
CJNE A,#18H,RETI1;
MOV 23H,#00H;

RETI1:
POP PSW;
POP ACC;
RETI;
NOP
NOP

DISP:

MOV A,21H;

MOV B,#10;
DIV AB;
MOV 41H,A;
MOV 40H,B;

MOV A,22H;

MOV B,#10;
DIV AB;
MOV 43H,A;
MOV 42H,B;

MOV A,23H;

MOV B,#10;
DIV AB;
MOV 45H,A;
MOV 44H,B;

MOV R0,#40H
LCALL DISP2; 1
CLR P2.0;
LCALL DELAY;
SETB P2.0;

INC R0;
LCALL DISP2;
CLR P2.1;
LCALL DELAY;
SETB P2.1;

MOV P0,#0bfh;
CLR P2.2 ;
LCALL DELAY;
SETB P2.2;

INC R0;
LCALL DISP2;
CLR P2.3;
LCALL DELAY;
SETB P2.3;

INC R0;
LCALL DISP2;
CLR P2.4;
LCALL DELAY;
SETB P2.4;

MOV P0,#0BFH;
CLR P2.5;
LCALL DELAY;
SETB P2.5;

INC R0;
LCALL DISP2;
CLR P2.6;
LCALL DELAY;
SETB P2.6;

INC R0;
LCALL DISP2;
CLR P2.7;
LCALL DELAY;
SETB P2.7;
MOV R0,#40H;

DISP2:
MOV DPTR,#TABLE;
MOV A,@R0;
MOVC A,@A+DPTR;
MOV P0,A;
RET

DELAY:
MOV R6,#255;
D2:DJNZ R6,D2;
RET;

TABLE:DB 0c0h,0f9h,0a4h,0b0h,99h,92h,82h,0f8h,80h,90h
; 0 1 2 3 4 5 6 7 8 9

;按键判断程序
KEY:
MOV A,P3;
CPL A;
ANL A,#3CH;
JZ RETX ;无键按下则返回
LCALL DISP ;
LCALL DELAY;

MOV A,P3;
CPL A;
ANL A,#3CH;
JZ RETX ;无键按下则返回

MOV R6,A ;将键值存入R6。
LOOP2: LCALL DISP ;
MOV A,P3;
CPL A;
ANL A,#3CH;
JNZ LOOP2 ;等待键释放
MOV A,R6;
RETX:
MOV P3,#0FFH;
RET;
NOP
NOP
;按键处理子程序
ANKEY: CLR EA;关中断
LX: MOV A,R6;
JB ACC.2,L1;是功能键转L1
JB ACC.3,L2;是确认键转L2
JB ACC.4,L3;是减1键转L3
JNB ACC.5,L12;不是增1键,转L12
JB 2BH.4,L6;判断使哪一位(时、分、秒)的值加1
JB 2DH.4,L8;
JB 2FH.4,L9;

L12: LCALL DISP;
LCALL DISP;
LCALL KEY;判断有无键按下。
JZ L12;
LJMP LX;

L2: MOV 25H,#00H ;确认键处理程序
CLR 2BH.4;
CLR 2DH.4;
CLR 2FH.4;
SETB EA;
RET;

L3: JB 2BH.4,L61;减一键处理程序
JB 2DH.4,L81;
JB 2FH.4,L91;
AJMP L12;

L1: MOV A,25H;功能键处理程序
JZ LB1;
JB ACC.0,LB2;
JB ACC.1,LB3;
JNB ACC.2,L12;

LB1: MOV 25H,#01H;25H单元是标志位,(25H)=01H调节时单元的值
SETB 2BH.4;
CLR 2DH.4;
CLR 2FH.4;
AJMP L12;

LB3: MOV 25H,#04H;25H单元是标志位,(25H)=04H调节秒单元的值
SETB 2FH.4;
CLR 2DH.4;
CLR 2BH.4;
AJMP L12;

LB2: MOV 25H,#02H;25H单元是标志位,(25H)=02H调节分单元的值
SETB 2DH.4;
CLR 2BH.4;
CLR 2FH.4;
AJMP L12;

L61: AJMP L611;
L81: AJMP L811;
L91: AJMP L911;
L112:AJMP L12;

L6: MOV A,23H;时加一
INC A;
MOV 23H,A;
CJNE A,#24H,L12;
MOV 23H,#00H;
AJMP L12;

L8: MOV A,22H;分加一
INC A;
MOV 22H,A;
CJNE A,#60H,L12;
MOV 22H,#00H;
AJMP L12;

L9: MOV A,21H;秒加一
INC A;
MOV 21H,A;
CJNE A,#60H,L12;
MOV 21H,#00H;
AJMP L12;

L611: MOV A,23H;时减一
DEC A;

MOV 23H,A;
CJNE A,#00H,L112;
MOV 23H,#23H;
AJMP L12;

L811: MOV A,22H;分减一
DEC A;

MOV 22H,A;
CJNE A,#00H,L112;
MOV 22H,#59H
AJMP L12;

L911: MOV A,21H;秒减一
DEC A;

MOV 21H,A;
CJNE A,#00H,L112;
MOV 21H,#59H;
AJMP L12;
NOP
NOP
END

回答2:

没什么难的啊,就是个显示程序,再用个定时器就好了- -!

回答3:

我真想自己试着编这个程序。