怎样利用51单片机实现时钟功能程序?

怎样利用51单片机实现时钟功能程序?,第1张

#define uchar unsigned char //  (先定义一下方便使用)

#define uint unsigned int

#define ulong unsigned long

#include <reg52.h>//包括一个52标准内核的头文件

uchar code zixing[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}

uchar code ziwei[]={0x10,0x20,0x40,0x80}

uchar *h,*m

uchar hour=12,min=0,sec=0,h_ding,m_ding,count=0,ms_50,ms_10,miaobiao_sec

sbit dp=P0^7

sbit k1=P3^2

sbit k2=P3^3

sbit k3=P3^4

sbit sound=P2^3

sfr ZIXING=0x80//p0口输出字形

sfr ZIWEI=0xa0//P2口输出字位

typedef enum //枚举类型:4种状态//

{

CLOCK,

SET_RING1,

SET_RING2,

MIAOBIAO_RUN,

}STATE

STATE status

typedef struct //定时时间的数据结构//

{

uchar hour

uchar min

}DINGSHI_TIME

DINGSHI_TIME dingshi_time[2]

#define NO_KEY -1 //定义一下方便使用

#define KEY_1 1

#define KEY_2 2

#define KEY_3 3

bit show_on_flag

//#######定时器T0中断服务程序########//

Timer0() interrupt 1

{

TH0=0x3cTL0=0xb4//50ms

ms_50++

if(ms_50==20) {sec++ms_50=0}

if(sec==60) {sec=0min++}

if(min==60) {min=0hour++}

if(hour==24) {hour=0}

}

//#######定时器T1中断服务程序########//

Timer1() interrupt 3

{

TH1=0xd8TL1=0xf4//10ms

ms_10++

if(ms_10==100)

{

miaobiao_sec++

ms_10=0

}

if(miaobiao_sec==60)

miaobiao_sec=0

}

//##############数码管显示#####################//

show(uchar time_high,uchar time_low)

{

uchar m

uint n,k

k=time_high*1000/10+time_low

for(m=0m<4m++)

{

ZIXING=zixing[k%10]

if(ms_50<10)

dp=0

ZIWEI=~ziwei[m]

for(n=0n<500n++)

ZIXING=0xff

for(n=0n<100n++)

k=k/10

}

}

//显示 on 定时编号//

show_on(uchar id) //id:定时编号//

{

uchar m

uint n

for(m=0m<2m++)

{

ZIXING=zixing[id%10]

dp=0

ZIWEI=~ziwei[m]

for(n=0n<500n++)

ZIXING=0xff

for(n=0n<80n++)

id=id/10

}

ZIXING=0xc8//字母n//

ZIWEI=~ziwei[2]

for(n=0n<500n++)

ZIXING=0xff

for(n=0n<200n++)

ZIXING=0xc0//字母0//

ZIWEI=~ziwei[3]

for(n=0n<500n++)

ZIXING=0xff

for(n=0n<200n++)

}

/*************启动秒表***************/

void run_miaobiao()

{

ms_10=0

miaobiao_sec=0

TH1=0xd8//标准是0xd8e4

TL1=0xf4

// TR1=1//启动定时器1

}

显示秒表:

void show_miaobiao()

{

show(miaobiao_sec,ms_10)

}

朋友以下是本人历时一个月殚精竭虑自主开发的一个8位51单片机时钟程序,望吾之兄弟姐妹习之,此程序乃采用一键控制,通过感知按下键持续时间的长短来进行调整时间,闹铃,6只共阳数码管输出显示,此外还包括整点铃声功能,在看程序之前我先交代一下运行该程序的硬件要求:12M晶振,P0端口驱动显示,P2.0到P2.5控制显示扫描,P2.7输出铃声,注意:铃声功放三极管应采用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 P2.0,C

MOV P2,#0FFH

MOV A,R3

MOVC A,@A+DPTR

MOV P0,A

MOV P2.1,C

MOV P2,#0FFH

MOV A,R4

MOVC A,@A+DPTR

MOV P0,A

MOV C,01H

MOV P2.2,C

MOV P2,#0FFH

MOV A,R5

MOVC A,@A+DPTR

MOV P0,A

MOV P2.3,C

MOV P2,#0FFH

MOV A,R6

MOVC A,@A+DPTR

MOV P0,A

MOV C,02H

MOV P2.4,C

MOV P2,#0FFH

MOV A,R7

MOVC A,@A+DPTR

MOV P0,A

MOV P2.5,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,P2.7

CPL C

MOV P2.7,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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7800672.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存