电子时钟程序

电子时钟程序,第1张

//=======主函数==================================

ORG 0000H

LJMP MAIN

ORG000BH

LJMP T00

//ORG001BH

//LJMP T11

ORG 0030H

//=======程序初始化=============================

MAIN:

MOV TMOD,#11H

MOV TH0,#3CH

MOV TL0,#0B0H

MOV TH1,#0F0H

MOV TL1,#0B0H

SETB EA

SETB ET0

// SETB ET1

SETB TR0

// SETB TR1

CLR P3.4

MOV R7,#00H 秒值

MOV 30H,#00H 秒个位

MOV 31H,#00H 秒十位值

MOV 32H,#00H 分个位

MOV 33H,#00H 分十位

MOV 34H,#00H 时个位

MOV 35H,#00H 时十位

MOV 44H,#00H 秒表毫秒十位

MOV 45H,#00H 秒表秒毫秒百位

MOV 46H,#00H 秒表秒个位

MOV 47H,#00H 秒表秒十位

MOV 48H,#00H 秒表分个位

MOV 49H,#00H 秒表分十位

MOV P2,#0F0H P2口按键初始化

MOV 40H,#01H 数码管休眠值 /流水灯/秒表开关数

MOV 41H,#08H 流水灯循环值

MOV 42H,#00H 秒表暂停

// ACALL T11

//-----------------------------------------------

MAIN1:

MOV P0,#0FFH

MOV P1,#0FFH

MOV R0,40H

CJNE R0,#01H,ACL1

ACALL MAIN2时钟显示

ACL1:CJNE R0,#02H,ACL2

ACALL MAIN3秒表

ACL2:CJNE R0,#03H,ACL3

ACALL LED流水灯

ACL3:ACALL KEY 按键扫描

// ACALL BAO定时报警

LJMP MAIN1

//=======报警子程序============================

BAO:MOV R4,33H

CJNE R4,#03H,YY1

CLR P3.2

ACALL KEY

LJMP YY2

YY1:CJNE R4,#05H,YY2

SETB P3.2

ACALL KEY

YY2:RET

//======时钟扫描子程序=======================

MAIN2:

MOV A,30H

MOV DPTR,#TAB

MOVC A,@A+DPTR

MOV P0,A

CLR P1.0

ACALL DELAY

MOV A,31H

MOV DPTR,#TAB

MOVC A,@A+ DPTR

MOV P0,A

CLR P1.1

SETB P1.0

ACALL DELAY

MOV A,32H

MOV DPTR,#TAB

MOVC A,@A+DPTR

MOV P0,A

CLR P1.2

SETB P1.1

ACALL DELAY

MOV A,33H

MOV DPTR,#TAB

MOVC A,@A+DPTR

MOV P0,A

CLR P1.3

SETB P1.2

ACALL DELAY

MOV A,34H

MOV DPTR,#TAB

MOVC A,@A+DPTR

MOV P0,A

CLR P1.4

SETB P1.3

ACALL DELAY

MOV A,35H

MOV DPTR,#TAB

MOVC A,@A+DPTR

MOV P0,A

CLR P1.5

SETB P1.4

ACALL DELAY

MOV P0,#7FH

MOV P1,#0EBH

ACALL DELAY

MOV P1,#0FFH

RET

//========秒表扫描子程序=========================

MAIN3:

MOV A,44H

MOV DPTR,#TAB

MOVC A,@A+DPTR

MOV P0,A

CLR P1.0

ACALL DELAY

MOV A,45H

MOV DPTR,#TAB

MOVC A,@A+ DPTR

MOV P0,A

CLR P1.1

SETB P1.0

ACALL DELAY

MOV A,46H

MOV DPTR,#TAB

MOVC A,@A+DPTR

MOV P0,A

CLR P1.2

SETB P1.1

ACALL DELAY

MOV A,47H

MOV DPTR,#TAB

MOVC A,@A+DPTR

MOV P0,A

CLR P1.3

SETB P1.2

ACALL DELAY

MOV A,48H

MOV DPTR,#TAB

MOVC A,@A+DPTR

MOV P0,A

CLR P1.4

SETB P1.3

ACALL DELAY

MOV A,49H

MOV DPTR,#TAB

MOVC A,@A+DPTR

MOV P0,A

CLR P1.5

SETB P1.4

ACALL DELAY

MOV P0,#7FH

MOV P1,#0EBH

ACALL DELAY

MOV P1,#0FFH

RET

//=======定时器T0中断服务========================

T00:MOV TH0,#0D8H

MOV TL0,#0F0H

INC R7

CJNE R7,#64H,AA

MOV R7,#00H

//-------时钟跑值--------------------------------

TK1:INC 30H 每秒钟秒个位增一

MOV R6,30H

CJNE R6,#0AH,AA

MOV 30H,#00H

TK2:INC 31H

MOV R6,31H

CJNE R6,#06H,AA

MOV 31H,#00H

TK3:INC 32H

MOVR6,32H

CJNE R6,#0AH,AA

MOV32H,#00H

TK4:INC33H

MOVR6,33H

CJNE R6,#06H,AA

MOV33H,#00H

TK5:INC34H

MOVR5,34H

MOVR6,35H

CJNE R5,#04H,TK6

CJNE R6,#02H,TK6

MOV34H,#00H

MOV35H,#00H

TK6:CJNE R5,#0AH,AA

MOV34H,#00H

INC35H

//-------秒表跑值----------------------

AA:

MOV R5,42H

CJNE R5,#01H,TK7

INC 44H

TK7:MOV R5,44H

CJNE R5,#0AH,AA1

MOV 44H,#00H

INC 45H

MOV R5,45H

CJNE R5,#0AH,AA1

MOV 45H,#00H

INC 46H

MOV R5,46H

CJNE R5,#0AH,AA1

MOV 46H,#00H

INC 47H

MOV R5,47H

CJNE R5,#06H,AA1

MOV 47H,#00H

INC 48H

MOV R5,48H

CJNE R5,#0AH,AA1

MOV 48H,#00H

INC 49H

MOV R5,49H

CJNE R5,#06H,AA1

MOV 49H,#00H

AA1:RETI

//====定时器T1中断服务子程序====================

//T11:MOV TH1,#0F0H

// MOV TL1,#0B0H

// RETI

//******定时时间调整子程序*************************//

TBAO:

MOV 50H,#00H

MOV 51H,#00H

MOV 52H,#00H

MOV 53H,#00H

MOV 54H,#00H

MOV 55H,#00H

MOV R3,50H

DJNZ 30H,RET1

RET1: RET

//======键盘扫描子程序==========================

KEY:MOV P2,#0F0H

MOV R3,P2

CJNE R3,#0F0H,KE0

LJMP KK13

KE0:CJNE R3,#0E0H,KE1 P2.4行

MOV P2,#0EFH

JB P2.3,KK1 //用作调定时时间 K13 秒个位增一

ACALL DELAY1

ACALL TK1

KK1:JB P2.2,KK2 K14 秒十增一

ACALL DELAY1

ACALL TK2

KK2:JB P2.1,KK3 K15 分个位增一

ACALL DELAY1

ACALL TK3

KK3:JB P2.0,KE1 K16 分十位增一

ACALL DELAY1

ACALL TK4

KE1:MOV P2,#0DFH P2.5行

JB P2.3,KK4 K9 时个位增一

ACALL DELAY1

ACALL TK5

KK4:JB P2.2,KK5 K10 时十位增一

ACALL DELAY1

INC 35H

MOV R3,35H

CJNE R3,#03H,HH0

MOV 35H,#00H

LJMP KK5

HH0:CJNE R3,#02H,HH1

LJMP KK5

HH1:CJNE R3,#01H,HH2

LJMP KK5

HH2:MOV 35H,#00H

KK5:JB P2.1,KK6

ACALL DELAY1 K11开/关蜂鸣器

CPL P3.2

KK6:JB P2.0,KE2 K12 秒表暂停

MOV 42H,#00H

KE2:MOV P2,#0BFH P.6行

JB P2.3,KK7K5 时钟暂停/取消暂停

ACALL DELAY1

CPL ET0

KK7:JB P2.2,KK8 K6 显示/休眠/流水灯

ACALL DELAY2

INC 40H

MOV R3,40H

CJNE R3,#04H,KK8

MOV 40H,#01H

KK8:JB P2.1,KK9

// ACALL DELAY3 K7 秒表清0

MOV 44H,#00H

MOV 45H,#00H

MOV 46H,#00H

MOV 47H,#00H

MOV 48H,#00H

MOV 49H,#00H

KK9:JB P2.0,KE3 K8 秒表开始跑

MOV 42H,#01H

KE3:MOV P2,#7FH P2.7行

JB P2.3,KK10 K1 秒十位减一

ACALL DELAY1

MOV R3,31H

CJNE R3,#00H,JA1

LJMP KK10

JA1:DEC 31H

KK10:JB P2.2,KK11K2分个位减一

ACALL DELAY1

MOV R3,32H

CJNE R3,#00H,JA2

LJMP KK11

JA2: DEC 32H

KK11:JB P2.1,KK12 K3 分十位减一

ACALL DELAY1

MOV R3,33H

CJNE R3,#00H,JA3

LJMP KK12

JA3:DEC 33H

KK12:JB P2.0,KK13 K4 时个位减一

ACALL DELAY1

MOV R3,34H

CJNE R3,#00H,JA4

LJMP KK13

JA4:DEC 34H

KK13:RET

//=====流水灯子程序===========================

LED: MOV A,#0feH

MOV 41H,#08H

MOV P0,#0FFH

LED1:

MOV P1,A

MOV R3,#100

D0: ACALL DELAY

DJNZ R3,D0

MOV R3,41H

DJNZ R3,AAA

LJMP BB

AAA:RL A

MOV 41H,R3

ACALL KEY

LJMP LED1

BB:

MOV 41H,#07H

CC: MOV P1,A

MOV R3,#30

D1: ACALL DELAY

DJNZ R3,D1

MOV R3,41H

DJNZ R3,DD

LJMP LED0

DD:RR A

MOV 41H,R3

ACALL KEY

LJMP CC

LED0:RET

//=====延时子程序==============================

DELAY:MOV R1,#250

DEL1:NOP

NOP

NOP

NOP

DJNZ R1,DEL1

RET

//====调用时钟显示作延时========================

DELAY1:MOV R2,#20

DELA3: ACALL MAIN2

DJNZ R2,DELA3

RET

//====调用秒表显示作延时========================

DELAY3:MOV R2,#20

DELA5:ACALL MAIN3

DJNZ R2,DELA5

RET

//====调用延时子程序作延时======================

DELAY2:MOV R2,#100

DELA4:ACALL DELAY

DJNZ R2,DELA4

RET

//======共阳数码管段码表=================================

TAB:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,87H

END

源程序如下: ORG 0000H 程序开始入口

LJMP START

ORG 0003H 外中断0中断程序入口

RETI

ORG 000BH 定时器T0中断程序入口

LJMP INTT0 跳至INTTO执行

ORG 0013H 外中断1中断程序入口

RETI

ORG 001BH 定时器T1中断程序入口

LJMP INTT1

ORG 0023H 串行中断程序入口地址

RETI

---------------主程序----------------------

START:MOV R0,#70H 70给R0,清70-7FH显示内存

MOV R7,#0FH

CLEARDISP:MOV @R0,#00H0给R0中的数为地址的内存中存放(70H)

INC R0

DJNZR7,CLEARDISP

MOV 78H,#2 默认时间为12:00,闹钟5:00

MOV 79H,#1

MOV 74H,#2 防止上电时数码管显示00小时

MOV 75H,#1

MOV 66H,#1 中断退出时66H为1时,分、时计时单元数据移入显存标志

MOV 68H,#1 上电默认闹钟开状态

MOV 7DH,#6 闹钟时十位

MOV 7CH,#3

MOV 69H,#0

MOV 7AH,#0AH放入"熄灭符"数据

MOV TMOD,#11H 设T0、T1为16位定时器

MOV TL0,#0B0H 50MS定时初值(T0计时用)

MOV TH0,#3CH

SETBEA 总中断开放

SETBET0 允许T0中断

SETBTR0 T0定时器开始计时

MOV R4,#14H 1秒定时用初值(50MS×20)

START1:LCALL DISPLAY 显示子程序

LCALL BEEP闹钟查询

JNB P1.7,SETTIMEP1.7口为0时转时间调整程序

JMP START1 P1.7口为1时跳回START1

-----------------------时间闹钟调整系统--------------------------

NFLAG:MOV A,68H 设置闹钟开关状态

CJNE A,#1,BEE68

MOV 68H,#0

JMP E6

BEE68:MOV 68H,#1

E6:MOV 66H,#8 中断退出66H不为1时分、时计时单元数据移入显存标志

MOV 72H,7BH

MOV 73H,7CH

MOV 74H,7DH

MOV 75H,7EH

LCALLDDL

LCALLDDL

MOV 72H,68H

MOV 73H,69H

MOV 74H,7AH

MOV 75H,7AH

LCALLDDL

LCALLDDL

MOV 66H,#1

LJMP START1

SETTIME:LCALLDDL

JB P1.7,NFLAG 键按下时间小于1秒,设置闹钟开关状态并查看闹钟时间,不关走时,确保准确,大于1秒调时

MOV TL1,#0B0H T1闪烁定时初值

MOV TH1,#3CH

MOV R2,#06H进入调时状态,赋闪烁定时初值

MOV 66H,#8 调闹钟时保持走时,关闭时钟显示数据

SETB ET1允许T1中断

SETB TR1开启定时器T1

SET1:LCALLDISPLAY调用显示,防止键按下无显示

JNB P1.7,SET1 P1.7口为0等待键释放

MOV R5,#00H清设置类型闪烁标志

SETN1:INC R5 闹钟分调整

SET5:LCALLDISPLAY

JB P1.7,SET5

SEETN1:LCALLDDL有键按下大于1秒分时间连续加(0.5秒加1),小于1秒转调时状态

JB P1.7,SET6 键释放查询,键释放自动转调时

MOV R0,#7CH

LCALL ADD1

MOV A,R3

CLR C

CJNEA,#60H,HHN1

HHN1:JCSEETN1

LCALL CLR0

JMP SEETN1

SET6:INC R5 闹钟时调整

SEET6:LCALLDISPLAY

JB P1.7,SEET6

SEETNH1:LCALLDDL

JB P1.7,SETF

MOV R0,#7EH

LCALLADD1

MOV A,R3

CLR C

CJNEA,#24H,HOUU1

HOUU1:JC SEETNH1

LCALL CLR0

JMP SEETNH1

SETF:LCALLDISPLAY

JB P1.7,SETF

LCALLDDL

JNB P1.7,SETOUT 短按调时,长按退出

CLR ET0

CLR TR0

MOV 70H,#00H设定后的时间从00秒开始走时

MOV 71H,#00H

INC R5

SET3:LCALLDISPLAY

JB P1.7,SET3

SETMM:LCALLDDL

JB P1.7,SET4

MOV R0,#77H

LCALL ADD1

MOV A,R3

CLR C

CJNEA,#60H,MMH

MMH:JC SETMM

LCALL CLR0

AJMP SETMM

SET4:INC R5

SEET4:LCALLDISPLAY

JB P1.7,SEET4

SETHH:LCALLDDL

JB P1.7,SETOUT1

MOV R0,#79H

LCALLADD1

MOV A,R3

CLR C

CJNEA,#24H,HOUU

HOUU:JC SETHH

LCALL CLR0

AJMP SETHH

SETOUT1:SETB ET0

SETB TR0 计时开始

SETOUT:MOV R5,#00H 清设置类型闪烁标志

CLRTR1 关闭T1

CLRET1 关T1中断

MOV 66H,#1

SETOUT2:LCALLDISPLAY

JNB P1.7,SETOUT2

LJMPSTART1

--------------------------延时1秒钟-----------------------

DDL:MOV 18H,#36

DDL0:MOV 17H,#239

DDL1:LCALLDISPLAY

DJNZ 17H,DDL1

DJNZ 18H,DDL0

RET

----------------------------T0中断程序------------------------

INTT0: PUSH ACC 打包

PUSH PSW

CLR ET0

CLR TR0

MOV A,#0B7H

ADD A,TL0

MOV TL0,A

MOV A,#3CH

ADDC A,TH0

MOV TH0,A

SETB TR0

DJNZ R4, OUTT0 20次中断未到中断退出

ADDSS: MOV R4,#14H 20次中断到(1秒)重赋初值

MOV R0,#71H 指向秒计时单元(70-71H)

ACALL ADD1 调用加1程序(加1秒 *** 作)

MOV A,R3 秒数据放入A(R3为2位十进制数组合)

CLR C 清进位标志

CJNE A,#60H,ADDMM

ADDMM: JC OUTT01小于60秒退出

ACALL CLR0 等于或大于60秒清0

MOV R0,#77H 指向分计时单元(76H-77H)

ACALL ADD1

MOV A,R3

CLR C

CJNE A,#60H,ADDHH

ADDHH: JC OUTT0

ACALL CLR0

MOV R0,#79H 指向小时计时单元(78H-79H)

ACALL ADD1

MOV A,R3

CLR C

CJNE A,#24H,HOUR

HOUR: JCOUTT0

ACALL CLR0

OUTT0: MOV A,66H 查询标志

CJNE A,#1,OUTT01

MOV 72H,76H 中断退出时将分、时计时单元数据移入对应显示单元

MOV 73H,77H

MOV 74H,78H

MOV 75H,79H

OUTT01:POP PSW 出栈

POP ACC

SETB ET0 开放T0

RETI中断返回

---------------------------加1程序--------------------------

ADD1: MOV A,@R0 取当前计时单元数据到A

DEC R0 指向前一地址

SWAP A A中数据高四位与低四位交换

ORL A,@R0 前一地址中数据放入A中低四位

ADD A,#01H A加1 *** 作

DA A 十进制调整

MOV R3,A 移入R3寄存器

ANL A,#0FH 高四位变0

MOV@R0,A 放回前一地址单元

MOV A,R3 取回R3中暂存数据

INCR0 指向当前地址单元

SWAP A A中数据高四位与低四位交换

ANL A,#0FH 高四位变0

MOV @R0,A 数据放入当削地址单元中

RET

CLR0: CLR C

CLR A 清累加器

MOV @R0,A 清当前地址单元

DEC R0 指向前一地址

MOV @R0,A 前一地址单元清0

RET 子程序返回

-------------------------调时闪烁程序----------------------

INTT1: PUSHACC

PUSHPSW

MOV TL1,#0B0H

MOV TH1,#3CH

DJNZR2,INTT1OUT0.3秒未到退出中断(50MS中断6次)

MOV R2,#06H 重装0.3秒定时用初值

CPL 02H0.3秒定时到对闪烁标志取反

JB 02H,FLASH1 02H位为1时显示单元"熄灭"

MOV A,R5

CJNEA,#01H,NL

JMP NNN1

NL:CJNEA,#02H,NL1

LJMPNNN1

NL1:CJNEA,#03H,NL2

LJMPNLL

NL2:CJNEA,#04H,INTT1OUT

LJMPNLL

NLL:MOV 72H,76H

MOV 73H,77H

MOV 74H,78H

MOV 75H,79H

AJMPINTT1OUT

NNN1:MOV 72H,7BH

MOV 73H,7CH

MOV 74H,7DH

MOV 75H,7EH

INTT1OUT:POP PSW 恢复现场

POP ACC

RETI 中断退出

FLASH1:MOV A,R5

CJNEA,#01H,ML

LJMPMNN1

ML:CJNEA,#02H,ML1

LJMPMNN2

ML1:CJNEA,#03H,MN

LJMPMLL

MN:CJNEA,#04H,INTT1OUT

LJMPMHL

MLL:MOV 72H,7AH

MOV 73H,7AH 显示单元(72-73H),将不显示分数据

MOV 74H,78H

MOV 75H,79H

AJMPINTT1OUT

MHL:MOV 72H,76H

MOV 73H,77H

MOV 74H,7AH 显示单元(74-75H),小时数据将不显示

MOV 75H,7AH

AJMPINTT1OUT

MNN1:MOV 72H,7AH

MOV 73H,7AH 显示单元(72-73H),将不显示闹钟分数据

MOV 74H,7DH

MOV 75H,7EH

AJMPINTT1OUT

MNN2:MOV 72H,7BH

MOV 73H,7CH

MOV 74H,7AH

MOV 75H,7AH 显示单元(74-75H),闹钟小时数据将不显示

AJMPINTT1OUT

-------------------------显示-------------------------------

DISPLAY:MOVDPTR,#DISDATA

MOVA,70H

MOVC A,@A+DPTR

MOVP3,A

CLRP1.0

NOP

NOP

NOP

SETB P1.0

MOVA,71H

MOVC A,@A+DPTR

MOVP3,A

CLRP1.1

NOP

NOP

NOP

SETB P1.1

MOVA,72H

MOVC A,@A+DPTR

MOVP3,A

CLRP1.2

NOP

NOP

NOP

SETB P1.2

MOVA,73H

MOVC A,@A+DPTR

MOVP3,A

CLRP1.3

NOP

NOP

NOP

SETB P1.3

MOVA,74H

MOVC A,@A+DPTR

MOVP3,A

CLRP1.4

NOP

NOP

NOP

SETB P1.4

MOVA,75H

MOVC A,@A+DPTR

MOVP3,A

CLRP1.5

NOP

NOP

NOP

SETB P1.5

RET

BEEP:MOV A,68H 查询标志

CJNE A,#1,BEERE

MOV A,78H 查询闹钟时个位

CJNE A,7DH,BEERE

MOV A,79H 查询闹钟时十位

CJNE A,7EH,BEERE

MOV A,76H 查询闹钟分个位

CJNE A,7BH,BEERE

MOV A,77H 查询闹钟分十位

CJNE A,7CH,BEERE

BB:CLR P1.6

LCALL DDL

SETB P1.6

LCALL DDL

JNB P1.7,BEERR

BEERE:RET

BEERR:SETB P1.6

MOV 68H,#0

LCALL DISPLAY

JNB P1.7,BEERR

JMP BEERE

DISDATA:DB 0C0H,0F9H,64H,70H,59H,52H,42H,0F8H,40H,50H,0FFH 0123456789空白

END

51单片机在数码管上显示的数字钟程序:

#include "at89x51.h"

unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}

unsigned char dispbitcode[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}

unsigned char dispbuf[]={0,0,16,0,0,16,0,0}

unsigned char dispbitcnt

unsigned char second

unsigned char minite

unsigned char hour

unsigned int tcnt

unsigned char mstcnt

unsigned char i,j

void main(void)

{

TMOD=0x02

TH0=0x06

TL0=0x06

TR0=1

ET0=1

EA=1

while(1)

{

if(P0_0==0)

{

for(i=5i>0i--)

for(j=248j>0j--)

if(P0_0==0)

{

second++

if(second==60)

{

second=0

}

dispbuf[0]=second%10

dispbuf[1]=second/10

while(P0_0==0)

}

}

if(P0_1==0)

{

for(i=5i>0i--)

for(j=248j>0j--)

if(P0_1==0)

{

minite++

if(minite==60)

{

minite=0

}

dispbuf[3]=minite%10

dispbuf[4]=minite/10

while(P0_1==0)

}

}

if(P0_2==0)

{

for(i=5i>0i--)

for(j=248j>0j--)

if(P0_2==0)

{

hour++

if(hour==60)

{

hour=0

}

dispbuf[6]=hour%10

dispbuf[7]=hour/10

while(P0_2==0)

}

}

}

}

void t0(void) interrupt 1 using 0

{

mstcnt++

if(mstcnt==8)

{

mstcnt=0

P1=dispcode[dispbuf[dispbitcnt]]

P3=dispbitcode[dispbitcnt]

dispbitcnt++

if(dispbitcnt==8)

{

dispbitcnt=0

}

}

tcnt++

if(tcnt==8)

{

tcnt=0

second++

if(second==60)

{

second=0

minite++

if(minite==60)

{

minite=0

hour++

if(hour==24)

{

hour=0

}

}

}

dispbuf[0]=second%10

dispbuf[1]=second/10

dispbuf[3]=minite%10

dispbuf[4]=minite/10

dispbuf[6]=hour%10

dispbuf[7]=hour/10

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存