时钟设计基本上就是通过电子 定时器定时 到1s给一个中断 让单片机去执行中断 跳转至相应的数码管位 然后修改当前显示值 再设置到10进位加一 之后就出来数字时钟的效果 弄懂定时器 一切就顺下来了
这是一个C得时钟程序 你看明白思想 就好说汇编的语句了
#include <reg52h>
#include <intrinsh>
#define uchar unsigned char
sbit dula=P3^7;
sbit L1=P2^0;
sbit L2=P2^1;
sbit L3=P2^2;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
uchar aa;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
uchar nummiao,numfen,numshi,t0;
void delayms(uchar xms)
{
uchar i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
TMOD=0x11;
TH0=(65536-50000)%256;
TL0=(65536-50000)/256;
EA=1;
ET0=1;
}
void display(uchar nummiao,numfen,numshi)
{
uchar shishi,geshi,shifen,gefen,shimiao,gemiao;
shimiao=nummiao/10;
gemiao=nummiao%10;
shifen=numfen/10;
gefen=numfen%10;
shishi=numshi/10;
geshi=numshi%10;
dula=1;
P0=table[shimiao];
dula=0;
L1=0;
L2=1;
L3=1;
delayms(5);
dula=1;
P0=table[gemiao];
dula=0;
L1=1;
L2=1;
L3=1;
delayms(5);
dula=1;
P0=0x40;
dula=0;
L1=1;
L2=0;
L3=1;
delayms(5);
dula=1;
P0=table[gefen];
dula=0;
L1=0;
L2=0;
L3=1;
delayms(5);
dula=1;
P0=table[shifen];
dula=0;
L1=1;
L2=1;
L3=0;
delayms(5);
dula=1;
P0=0x40;
dula=0;
L1=0;
L2=1;
L3=0;
delayms(5);
dula=1;
P0=table[geshi];
dula=0;
L1=1;
L2=0;
L3=0;
delayms(5);
dula=1;
P0=table[shishi];
dula=0;
L1=0;
L2=0;
L3=0;
delayms(5);
}
void keyscan()
{
if(key1==0)
{
delayms(10);
if(key1==0)
{
numshi++;
if(numshi==24)
numshi=0;
while(!key1);
}
}
if(key2==0)
{
delayms(10);
if(key2==0)
{
if(numfen==60)
numfen=0;
numfen++;
while(!key2);
}
}
if(key3==0)
{
delayms(10);
if(key3==0)
{
nummiao=0;
while(!key3);
}
}
if(key4==0)
{
delayms(10);
if(key4==0)
{
while(!key4);
TR0=~TR0;
}
}
}
void main()
{
init();
aa=0xfe;
while(1)
{
P1=aa;
if(nummiao==60)
{
numfen++;
nummiao=0;}
if(numfen==60)
{ numshi++;
numfen=0;}
if(numshi==24)
numshi=0;
keyscan();
display(nummiao,numfen,numshi);
}
}
void t0time()interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
t0++;
if(t0==20)
{
t0=0;
nummiao++;}
aa=_crol_(aa,1);
}
我这写了一个!使用数码管显示的!可以用键盘校对时间!你参考下。还写了一个液晶显示的!你要的话也可以发给你!
#include<reg52h>
#define uchar unsigned char
#define uint unsigned int
sbit K1=P3^0;
sbit K2=P3^1;
sbit K3=P3^2;
uchar table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
uchar table1[]={0x5b,0x66,0x6d,0x6f,0x6d,0x6f};
uchar table2[]={0,1,0,0,0,0};
int shi=1,fen=0,miao=0;
uchar a,i,j,temp=-2,temp1;
void sfm(uchar dz,uchar shu);//将计的数的十位和个位分开
void display();//将时间显示出来
void display1(uchar dz);//只显示要校对的数
void init();//初始化
void jiaoshi();//定义一个校时函数
void delay(uchar z)//定义一个延时函数
{
uchar x,y;
for(x=0;x<50;x++)
for(y=0;y<z;y++);
}
void main()
{
init();
while(1)
{
jiaoshi();
}
}
void init()
{
EA=1;
ET0=1;
ET1=1;
TR1=1;
TR0=0;
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
K1=K2=1;
temp1=0;
while(TR1)
{
for(i=0;i<6;i++)
{
delay(3);
P1=i;
P2=table1[i];
delay(3);
}
}
}
void sfm(uchar dz,uchar shu)
{
if(shu==60&dz!=0) shu=0;
table2[dz]=shu/10;
table2[dz+1]=shu%10;
}
void display()
{
uchar j;
for(j=0;j<6;j++)
{
delay(3);
P1=j;
P2=table[table2[j]];
delay(3);
}
}
void display1(uchar dz)
{
uchar j;
for(j=0;j<2;j++)
{
delay(3);
P1=dz+j;
P2=table[table2[dz+j]];
delay(3);
}
}
void jiaoshi()
{
uchar a;
if(K1==0)
{
delay(5);
if(K1==0)
{
TR0=0;
a++;
temp+=2;
if(a==4|temp==6)
{
a=0;
temp=-2;
TR0=1;
}
}
}
while(!K1)
{
delay(5);
display1(temp);
}
while(!K1);
display1(temp);
if(K2==0)
{
delay(5);
if(K2==0)
{
if(a==1)
{
temp=0;
if(shi==24)
shi=0;
shi++;
sfm(0,shi);
}
if(a==2)
{
temp=2;
if(fen==59)
fen=-1;
fen++;
sfm(2,fen);
}
if(a==3)
{
temp=4;
if(miao==59)
miao=-1;
miao++;
sfm(4,miao);
}
}
}
while(!K2)
{
delay(5);
display1(temp);
}
while(!K2);
if(K3==0)
{
delay(5);
if(K3==0)
{
if(a==1)
{
shi--;
if(shi==0)
shi=24;
sfm(0,shi);
}
if(a==2)
{
fen--;
if(fen==-1)
fen=59;
sfm(2,fen);
}
if(a==3)
{
miao--;
if(miao==-1)
miao=59;
sfm(4,miao);
}
}
}
while(!K3)
{
delay(5);
display1(temp);
}
while(!K3);
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
a++;
if(a==16)
{
a=0;
miao++;
sfm(4,miao);
if(miao==60)
{
miao=0;
display();
fen++;
sfm(2,fen);
if(fen==60)
{
fen=0;
shi++;
sfm(0,shi);
if(shi==25)
{
shi=1;
sfm(0,shi);
}
}
}
}
display();
}
void timer1() interrupt 3
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
j++;
if(j==20)
{
TR0=1;
TR1=0;
}
}
额。。。。。这个东西就是零碎的东西加起来变成一个一个整体的啊,讲个思路,写个中断服务程序,里面的是led灯亮的程序,和1秒的计时程序,写个beep子程序控制蜂鸣器,写个display子程序,控制icl0809,主程序循环控制,显示数码管,显示am,过了12,进入中断,显示pm,这个其实很简单的,我才大三,单片机才学了四个星期,我都会做,相信你能行的。。。
朋友以下是本人历时一个月殚精竭虑自主开发的一个8位51单片机时钟程序,望吾之兄弟姐妹习之,此程序乃采用一键控制,通过感知按下键持续时间的长短来进行调整时间,闹铃,6只共阳数码管输出显示,此外还包括整点铃声功能,在看程序之前我先交代一下运行该程序的硬件要求:12M晶振,P0端口驱动显示,P20到P25控制显示扫描,P27输出铃声,注意:铃声功放三极管应采用pnp管。以下是该程序:
ORG 00H
SETB TR0
JMP START
ORG 000BH
LJMP T0
START: MOV IE,#82H
MOV TMOD,#01H
MOV DPTR,#TAB
MOV SP,#60H
MOV 20H,#0
MOV R0,#0
MOV R1,#0
MOV R2,#0
MOV R3,#0
MOV R4,#0
MOV R5,#0
MOV R6,#0
MOV R7,#0
MOV 50H,#0
MOV 51H,#0
MOV 52H,#0
MOV 53H,#0
MOV 54H,#0
MOV 55H,#0
MOV A,TL0
ADD A,0B5H
MOV TL0,A
MOV TH0,#3CH
LOOP: ANL 20H,#08H
UNSV: MOV R1,#0
LCALL DISPLAY
JB 03H,LOP1
LCALL SOUND
LCALL HDLY
LOP1: MOV P3,#0FEH ;按键监视
MOV A,P3
CJNE A,#0FEH,LOP2
MOV R1,#0
JMP LOOP
LOP2: MOV R1,#2 ;误按判断
LOP3: LCALL HD
DJNZ R1,LOP3
JNB 04H,LOOP
CLR 04H
LOP4: INC R1
MOV C,00H ;调秒
CPL C
MOV 00H,C
LCALL HDLY
CJNE R1,#40,LOP5
JMP LOOP
LOP5: JNB 05H,LOP4
CLR 05H
MOV R1,#0
LCALL HD
CLR 00H
JB 04H,TF
MOV R2,#0
MOV R3,#0
JMP LOP4
TF: CLR 04H
LOP6: INC R1
MOV C,01H ;调分
CPL C
MOV 01H,C
LCALL HDLY
CJNE R1,#40,LOP7
JMP LOOP
LOP7: JNB 05H,LOP6
CLR 05H
MOV R1,#0
LCALL HD
CLR 01H
JB 04H,TS
INC R4
MOV R1,#0
CJNE R4,#10,LOP6
MOV R4,#0
INC R5
CJNE R5,#6,LOP6
MOV R5,#0
JMP LOP6
TS: CLR 04H
LOP8: INC R1
MOV C,02H ;调时
CPL C
MOV 02H,C
LCALL HDLY
CJNE R1,#40,LOP9
JMP LOOP
LOP9: JNB 05H,LOP8
CLR 05H
MOV R1,#0
LCALL HD
CLR 02H
JB 04H,TN
INC R6
CJNE R7,#2,NO2
CJNE R6,#4,LOP8
MOV R7,#0
MOV R6,#0
NO2: CJNE R6,#10,LOP8
MOV R6,#0
INC R7
JMP LOP8
TN: CLR 04H ;调闹铃
SETB RS0
CLR RS1
MOV R2,#0
MOV R3,#0
MOV R4,#0
MOV R5,#0
MOV R6,#0
MOV R7,#0
NS: INC R1 ;闹铃秒调整
MOV C,00H
CPL C
MOV 00H,C
LCALL HDLY
CJNE R1,#40,NS1
JMP NH2
NS1: JNB 05H,NS
CLR 05H
MOV R1,#0
LCALL HD
CLR 00H
JB 04H,NF
INC R2
CJNE R2,#10,NS
MOV R2,#0
INC R3
CJNE R3,#6,NS
MOV R3,#0
JMP NS
NF: CLR 04H ;闹铃分
NF1: INC R1
MOV C,01H
CPL C
MOV 01H,C
LCALL HDLY
CJNE R1,#40,NF2
LJMP NH2
NF2: JNB 05H,NF1
CLR 05H
MOV R1,#0
LCALL HD
CLR 01H
JB 04H,NH
INC R4
CJNE R4,#10,NF1
MOV R4,#0
INC R5
CJNE R5,#6,NF1
MOV R5,#0
JMP NF1
NH: CLR 04H ;闹铃时调整
NH1: INC R1
MOV C,02H
CPL C
MOV 02H,C
LCALL HDLY
CJNE R1,#40,NH3
NH2: MOV 50H,R2
MOV 51H,R3
MOV 52H,R4
MOV 53H,R5
MOV 54H,R6
MOV 55H,R7
CLR RS0
CLR RS1
LJMP LOOP
NH3: JNB 05H,NH1
CLR 05H
MOV R1,#0
LCALL HD
CLR 02H
JB 04H,NH2
INC R6
CJNE R7,#2,NOT2
CJNE R6,#4,NH1
MOV R6,#0
MOV R7,#0
JMP NH1
NOT2: CJNE R6,#10,NH1
MOV R6,#0
INC R7
JMP NH1
;子程序区
TIMES: CJNE R0,#20,LOOP16
MOV R0,#0
INC R2
CJNE R2,#10,LOOP16
MOV R2,#0
INC R3
CJNE R3,#6,LOOP16
MOV R3,#0
ADMINI: INC R4
CJNE R4,#10,LOOP16
MOV R4,#0
INC R5
CJNE R5,#6,LOOP16
MOV R5,#0
HOUR: INC R6
LCALL SOUND ;整点铃声
CJNE R7,#2,LOOP14
CJNE R6,#4,LOOP16
MOV R6,#0
MOV R7,#0
JMP LOOP16
LOOP14: CJNE R6,#10,LOOP16
MOV R6,#0
INC R7
LOOP16: RET
DISPLAY:MOV P2,#0FFH
MOV A,R2
MOVC A,@A+DPTR
MOV P0,A
MOV C,00H
MOV P20,C
MOV P2,#0FFH
MOV A,R3
MOVC A,@A+DPTR
MOV P0,A
MOV P21,C
MOV P2,#0FFH
MOV A,R4
MOVC A,@A+DPTR
MOV P0,A
MOV C,01H
MOV P22,C
MOV P2,#0FFH
MOV A,R5
MOVC A,@A+DPTR
MOV P0,A
MOV P23,C
MOV P2,#0FFH
MOV A,R6
MOVC A,@A+DPTR
MOV P0,A
MOV C,02H
MOV P24,C
MOV P2,#0FFH
MOV A,R7
MOVC A,@A+DPTR
MOV P0,A
MOV P25,C
MOV P2,#0FFH
RET
HDLY: MOV 40H,#2
HDL1: MOV 41H,#25
HDL2: MOV 42H,#100
HDL3: DJNZ 42H,HDL4
DJNZ 41H,HDL2
DJNZ 40H,HDL1
JMP HDOT
SOUT: SETB 05H
HDOT: RET
HDL4: MOV P3,#0FEH
MOV A,P3
CJNE A,#0FEH,SOUT
CLR 05H
LCALL DISPLAY
JMP HDL3
HD: MOV 30H,#5
HD1: MOV 31H,#50
HD2: MOV 32H,#100
HD3: LCALL DISPLAY
DJNZ 32H,HD4
DJNZ 31H,HD2
DJNZ 30H,HD1
SETB 04H ;计满标志位
JMP HDOUT
HD4: MOV P3,#0FEH
MOV A,P3
CJNE A,#0FEH,HD3
MOV 33H,#50
HD5: MOV 34H,#100
DJNZ 34H,$
DJNZ 33H,HD5
MOV P3,#0FEH
MOV A,P3
CJNE A,#0FEH,HD3
CLR 04H
HDOUT: RET
SOUND: MOV 30H,#10
SO1: MOV 31H,#100
SO2: MOV 32H,#3
MOV C,P27
CPL C
MOV P27,C
SO3: MOV 33H,#25
DJNZ 33H,$
DJNZ 32H,SO3
DJNZ 31H,SO2
DJNZ 30H,SO1
RET
T0: PUSH PSW
PUSH ACC
CLR RS1
CLR RS0
MOV A,TL0
ADD A,#0B5H
MOV TL0,A
MOV TH0,#3CH
INC R0
LCALL TIMES
MOV A,R2
CJNE A,50H,TO
SETB 03H
MOV A,R3
CJNE A,51H,TO
MOV A,R7
CJNE A,55H,TO
MOV A,R6
CJNE A,54H,TO
MOV A,R5
CJNE A,53H,TO
MOV A,R4
CJNE A,52H,TO
MOV A,R2
ADD A,R3
ADD A,R4
ADD A,R5
ADD A,R6
ADD A,R7
JZ TO
CLR 03H
TO: POP ACC
POP PSW
RETI
TAB: DB 0C0H 0F9H 0A4H 0B0H 099H
DB 092H 082H 0F8H 080H 090H
END
直接verilog代码就可以了吧?
以前写的一个代码,供参考。
module clock(clk,rst,set, set_typ, set_data, yr, mon, dt, hr, min, sec,
alarm_en, alm_typ, alm_yr, alm_mon, alm_dt, alm_hr, alm_min, alm_sec, alarm_output);
input clk,rst,set;
input [2:0] set_typ; //
input [6:0] set_data;//
output [6:0] yr, mon, dt, hr, min, sec;
input alarm_en;
input [2:0] alm_typ; //
input [6:0] alm_yr, alm_mon, alm_dt, alm_hr, alm_min, alm_sec;
output alarm_output;
parameter C_FR = 32'd20_000_000-32'd1; //定义系统时钟20MHz
reg [31:0] fr_cnt;
reg [3:0] sec_cnt;
reg pp1s; //秒脉冲
//==================================================
//fr_cnt
always@(posedge clk)//
if(!rst)
fr_cnt <= 32'b0;
else if (fr_cnt >= C_FR)
fr_cnt <= 32'b0;
else
fr_cnt <= fr_cnt + 1'b1;
//pp1s
always@(posedge clk)//
if(!rst)
pp1s <= 1'b0;
else if (fr_cnt == C_FR)
pp1s <= 1'b1;
else
pp1s <= 1'b0;
///time counter
always@(posedge clk)
if(!rst)
begin
yr <= 7'b0;
mon <= 7'b0;
dt <= 7'b0;
hr <= 7'b0;
min <= 7'b0;
sec <= 7'b0;
end
else if (set)
begin
case (set_typ)
3'b000: yr <= set_data;
3'b001: mon <= set_data;
3'b010: dt <= set_data;
3'b011: hr <= set_data;
3'b100: min <= set_data;
3'b101: sec <= set_data;
end
else if (pp1s)
begin
if (sec >= 7'd59)
sec <= 7'd0;
else
sec <= sec + 1'b1;
if (sec >= 7'd59)
begin
if (min >= 7'd59)
min <= 7'd0;
else
min <= min + 1'b1;
end
if (sec >= 7'd59 && min >= 7'd59)
begin
if (hr >= 7'd23)
hr <= 7'd0;
else
hr <= hr + 1'b1;
end
///data,mon, year, 大月小月,闰年等,依此类推
//
end
//=================================
//alarm
always@(posedge clk)
if(!rst)
alarm_output <= 1'b0;
else if (alarm_en)
case (alm_typ)
3'b000:
if (yr == alm_yr && mon == alm_mon && dt == alm_dt && hr == alm_hr && min == alm_min && sec == alm_sec)
alarm_output <= 1'b1;
else
alarm_output <= 1'b0;
3'b001:
if (mon == alm_mon && dt == alm_dt && hr == alm_hr && min == alm_min && sec == alm_sec)
alarm_output <= 1'b1;
else
alarm_output <= 1'b0;
3'b010:
if (dt == alm_dt && hr == alm_hr && min == alm_min && sec == alm_sec)
alarm_output <= 1'b1;
else
alarm_output <= 1'b0;
3'b011:
if (hr == alm_hr && min == alm_min && sec == alm_sec)
alarm_output <= 1'b1;
else
alarm_output <= 1'b0;
3'b100:
if (min == alm_min && sec == alm_sec)
alarm_output <= 1'b1;
else
alarm_output <= 1'b0;
default
alarm_output <= 1'b0;
endcase
endmodule
#include<REG2051H>
code senen_seg[10]={0x81,0xe7,0x92,0xa2,0xe4,0xa8,0x88,0xe3,0x80,0xa0}; //P17(冒号)口高电平
bit key1_enter=0,key2_enter=0,key3_enter=0,countdown_mark=0,stopwatch_mark=0,count_mark=0,bell_mark=0; //状态标志
unsigned char program=0,program_variable=0,count_bit=0,count=0;
unsigned char hour=10,minute=10,second=0; //时间变量
unsigned char delayed_hour=22,delayed_minute=10,delayed_second=0; //定时变量
unsigned char count_hour=0,count_minute=0,count_second=0; //计时计数变量
unsigned char count_time=0,count_count=0;
void delay(unsigned int t) //延时子程序
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<10;j++)
;
}
void time0_init(void){ //定时计数0器初始化
EA=0;
TR0=0;
TMOD=0x01;
TH0=0xec;
TL0=0x73;
ET0=1;
TR0=1;
EA=1;
}
static void timer0_isr(void) interrupt TF0_VECTOR using 1 //定时计数0器中断函数
{
TR0=0;
TH0=0xec;
TL0=0x73;
TR0=1;
count_time++; //时钟计时程序
if(count_time>=199){
count_time=0;
second++;
if(second>=60){
second=0;
minute++;
if(minute>=60){
minute=0;
hour++;
if(hour>=24)hour=0;
}
}
}
if(delayed_hour==hour && delayed_minute==minute && second<4) P3_7=0;
else P3_7=1;
if(countdown_mark==1){ //倒计时程序
count_count++;
if(count_count>=199 && (count_second!=0|count_minute!=0|count_hour!=0)){
count_count=0;
count_second--;
if(count_second>=60){
count_second=59;
count_minute--;
if(count_minute>=60){
count_minute=59;
count_hour--;
if(count_hour>=100) count_hour=99;
}
}
}
if(count_second==0&&count_minute==0&&count_hour==0&&count_count<=12000) P3_7=0;
else P3_7=1;
if(count_count>=15000) count_count=14000;
}
if(stopwatch_mark==1){ //秒表程序
count_count++;
if(count_count>=2){
count_count=0;
count_second++;
if(count_second>=100){
count_second=0;
count_minute++;
if(count_minute>=60){
count_minute=0;
count_hour++;
if(count_hour>=60) count_hour=0;
}
}
}
}
}
unsigned char show_key (void){
unsigned char x=0,y=0;
switch (program){
case 0: P1&=senen_seg[second%10]; //时钟秒的个位
break;
case 1: if(count_time>=90) P1&=senen_seg[second%10]; //校正秒的个位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟秒的个位
else P1&=senen_seg[delayed_second%10];
break;
case 3: if(count_bit>=0) P1&=senen_seg[count_second%10];//倒计时秒的个位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_second%10]; //秒表秒的个位
break;
case 5: P1&=senen_seg[count_second%10]; //计数器个位
break;
}
P3_3=0;
delay(10);
if(P3_5==0){ //功能键1识别
key1_enter=1;
if(count<=254)count++;
}
if(P3_4==0) key2_enter=1; //功能键2识别
if(P3_2==0) key3_enter=1; //功能键3识别
P3_3=1;
P1|=0xff;
switch (program){
case 0: P1&=senen_seg[second/10]; //时钟秒的十位
break;
case 1: if(count_time>=90) P1&=senen_seg[second/10]; //校正秒的十位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟秒的十位
else P1&=senen_seg[delayed_second/10];
break;
case 3: if(count_bit>=1) P1&=senen_seg[count_second/10];//倒计时秒的十位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_second/10]; //秒表秒的十位
break;
case 5: P1&=senen_seg[count_second/10]; //计数器十位
break;
}
P3_1=0;
delay(10);
P3_1=1;
P1|=0xff;
switch (program){
case 0: P1&=senen_seg[minute%10]; //时钟分的个位
break;
case 1: if(count_time>=90) P1&=senen_seg[minute%10]; //校正分的个位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟分的个位
else P1&=senen_seg[delayed_minute%10];
break;
case 3: if(count_bit>=2) P1&=senen_seg[count_minute%10];//倒计时分的个位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_minute%10]; //秒表分的个位
break;
case 5: P1&=senen_seg[count_minute%10]; //计数器百位
break;
}
P3_2=0;
delay(10);
P3_2=1;
P1|=0xff;
switch (program){
case 0: P1&=senen_seg[minute/10]; //时钟秒的个位
break;
case 1: if(count_time>=90) P1&=senen_seg[minute/10]; //校正秒的个位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟秒的个位
else P1&=senen_seg[delayed_minute/10];
break;
case 3: if(count_bit>=3) P1&=senen_seg[count_minute/10];//倒计时秒的个位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_minute/10]; //秒表秒的个位
break;
case 5: P1&=senen_seg[count_minute/10]; //计数器千位
break;
}
P3_5=0;
delay(10);
P3_5=1;
P1|=0xff;
switch (program){
case 0: P1&=senen_seg[hour%10]; //时钟时的个位
break;
case 1: if(count_time>=90) P1&=senen_seg[hour%10]; //校正时的个位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟时的个位
else P1&=senen_seg[delayed_hour%10];
break;
case 3: if(count_bit>=4) P1&=senen_seg[count_hour%10];//倒计时时的个位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_hour%10]; //秒表时的个位
break;
case 5: P1&=senen_seg[count_hour%10]; //计数器万位
break;
}
P3_0=0;
delay(10);
if(P3_4==1 && key2_enter==1){
x=3; //确认功能键2识别,返回3
key2_enter=0;
}
P3_0=1;
P1|=0xff;
switch (program){
case 0: P1&=senen_seg[hour/10]; //时钟时的个位
break;
case 1: if(count_time>=90) P1&=senen_seg[hour/10]; //校正时的个位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟时的个位
else P1&=senen_seg[delayed_hour/10];
break;
case 3: if(count_bit>=5)P1&=senen_seg[count_hour/10]; //倒计时时的个位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_hour/10]; //秒表时的个位
break;
case 5: P1&=senen_seg[count_hour/10]; //计数器万位
break;
}
P3_4=0;
delay(10);
if(P3_5==1 && key1_enter==1){ //确认功能键1识别,执行退出或进入下一个功能
if(count>=127) x=1; //确认功能键1为长按,返回1
else x=2; //确认功能键1为短按,返回2
key1_enter=0;
count=0;
}
if(P3_2==1 && key3_enter==1){
x=4; //确认功能键3识别,返回4
key3_enter=0;
}
P3_4=1;
P1|=0xff;
if(program<=1 && count_time>=100) P1&=0xff; //校时和显示功能时钟时冒号闪烁
if(program<=1 && count_time<=100) P1&=0x7f;
if(program==2) P1&=0x7f; //设置闹钟功能时冒号长亮
if(program==3) P1&=0xff; //设置倒计时功能时冒号长灭
if(program==4) P1&=0x7f; //设置秒表功能时冒号长亮
if(program==5) P1&=0xff; //设置计数时冒号长灭
y=x;
x=0;
return y;
}
void main(){ //主程序
P1=0xff;
P3=0xff;
time0_init();
while(1){
switch(program){
case 0: while(program==0){ //时钟菜单
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=1;
break;
}
}
break; //校时菜单
case 1: while(program==1){
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=2;
break;
case 3: hour++;
if(hour>=24)hour=0;
break;
case 4: minute++;
if(minute>=60)minute=0;
break;
}
}
break;
case 2: while(program==2){ //闹钟菜单
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=3;
break;
case 3: delayed_hour++;
if(delayed_hour>=25)delayed_hour=0;
break;
case 4: delayed_minute++;
if(delayed_minute>=60)delayed_minute=0;
break;
}
}
break;
case 3: while(program==3){ //倒计时菜单
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=4;
break;
case 3: count_bit++;
if(count_bit>=7)count_bit=0;
break;
case 4: switch(count_bit){
case 0: count_second+=1;
break;
case 1: count_second+=10;
break;
case 2: count_minute+=1;
break;
case 3: count_minute+=10;
break;
case 4: count_hour+=1;
break;
case 5: count_hour+=10;
break;
case 6: break;
}
if(count_hour>=100) count_hour-=100;
if(count_minute>=60) count_minute-=60;
if(count_second>=60) count_second-=60;
break;
}
if(count_bit==6) countdown_mark=1;
else countdown_mark=0;
}
break;
case 4: count_hour=0; //秒表菜单
count_minute=0;
count_second=0;
while(program==4){
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=5;
break;
case 3: stopwatch_mark=~stopwatch_mark;
break;
case 4: if(stopwatch_mark==0){
count_hour=0;
count_minute=0;
count_second=0;
}
break;
}
}
break; //计数器菜单
case 5: count_hour=0;
count_minute=0;
count_second=0;
while(program==5){
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=0;
break;
case 3: count_second++;
if(count_second>=100){
count_second=0;
count_minute++;
if(count_minute>=100){
count_minute=0;
count_hour++;
if(count_hour>=100)count_hour=0;
}
}
break;
case 4: count_hour=0;
count_minute=0;
count_second=0;
break;
}
if(P3_7==0){
while(P3_7==0) show_key();
count_second++;
if(count_second>=100){
count_second=0;
count_minute++;
if(count_minute>=100){
count_minute=0;
count_hour++;
if(count_hour>=100)count_hour=0;
}
}
}
}
break;
}
if(program!=3) countdown_mark=0;
if(program!=4) stopwatch_mark=0;
}
}
以上就是关于单片机数字时钟汇编程序设计分析全部的内容,包括:单片机数字时钟汇编程序设计分析、数字时钟C语言设计.用的是51单片机。 00 秒 00 分 00 小时 还有小数点。 麻烦把程序代码告诉我把、单片机课程设计 设计制作一个24小时制多功能数字钟等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)