時(shí)鐘+秒表+流水燈的單片機(jī)匯編程序
//HJSMSY-V1.0單片機(jī)開發(fā)板測(cè)試程序
//---------------------------------------------
// 電腦鐘>>
//---------------------------------------------
//=======主函數(shù)==================================
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP T00
// ORG 001BH
// 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 ;秒個(gè)位值
MOV 31H,#00H ;秒十位值
MOV 32H,#00H ;分個(gè)位
MOV 33H,#00H ;分十位
MOV 34H,#00H ;時(shí)個(gè)位
MOV 35H,#00H ;時(shí)十位
MOV 44H,#00H ;秒表毫秒十位
MOV 45H,#00H ;秒表秒毫秒百位
MOV 46H,#00H ;秒表秒個(gè)位
MOV 47H,#00H ;秒表秒十位
MOV 48H,#00H ;秒表分個(gè)位
MOV 49H,#00H ;秒表分十位
MOV P2,#0F0H ;P2口按鍵初始化
MOV 40H,#01H ;數(shù)碼管休眠值/流水燈/秒表開關(guān)數(shù)
MOV 41H,#08H ;流水燈循環(huán)值
MOV 42H,#00H ;秒表暫停
// ACALL T11
//-----------------------------------------------
MAIN1:
MOV P0,#0FFH
MOV P1,#0FFH
MOV R0,40H
CJNE R0,#01H,ACL1
ACALL MAIN2 ;時(shí)鐘顯示
ACL1:CJNE R0,#02H,ACL2
ACALL MAIN3 ;秒表
ACL2:CJNE R0,#03H,ACL3
ACALL LED ;流水燈
ACL3:ACALL KEY ;按鍵掃描
//ACALL BAO ;定時(shí)報(bào)警
LJMP MAIN1
//=======報(bào)警子程序============================
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
//======時(shí)鐘掃描子程序=======================
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
//=======定時(shí)器T0中斷服務(wù)========================
T00:MOV TH0,#0D8H
MOV TL0,#0F0H
INC R7
CJNE R7,#64H,AA
MOV R7,#00H
//-------時(shí)鐘跑值--------------------------------
TK1:INC 30H;每秒鐘秒個(gè)位增一
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
MOV R6,32H
CJNE R6,#0AH,AA
MOV 32H,#00H
TK4:INC 33H
MOV R6,33H
CJNE R6,#06H,AA
MOV 33H,#00H
TK5:INC 34H
MOV R5,34H
MOV R6,35H
CJNE R5,#04H,TK6
CJNE R6,#02H,TK6
MOV 34H,#00H
MOV 35H,#00H
TK6:CJNE R5,#0AH,AA
MOV 34H,#00H
INC 35H
//-------秒表跑值----------------------
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
CJNER5,#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
//====定時(shí)器T1中斷服務(wù)子程序====================
//T11:MOV TH1,#0F0H
// MOV TL1,#0B0H
//RETI
//******定時(shí)時(shí)間調(diào)整子程序*************************//
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 //用作調(diào)定時(shí)時(shí)間 ;K13 秒個(gè)位增一
ACALL DELAY1
ACALL TK1
KK1:JB P2.2,KK2;K14 秒十增一
ACALL DELAY1
ACALL TK2
KK2:JB P2.1,KK3;K15分個(gè)位增一
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 時(shí)個(gè)位增一
ACALL DELAY1
ACALL TK5
KK4:JB P2.2,KK5;K10 時(shí)十位增一
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 開/關(guān)蜂鳴器
CPL P3.2
KK6:JB P2.0,KE2 ;K12 秒表暫停
MOV 42H,#00H
KE2:MOV P2,#0BFH;P.6行
JB P2.3,KK7;K5時(shí)鐘暫停/取消暫停
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,KK11 ;K2 分個(gè)位減一
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時(shí)個(gè)位減一
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
//=====延時(shí)子程序==============================
DELAY:MOV R1,#250
DEL1:NOP
NOP
NOP
NOP
DJNZ R1,DEL1
RET
//====調(diào)用時(shí)鐘顯示作延時(shí)========================
DELAY1:MOV R2,#20
DELA3: ACALL MAIN2
DJNZ R2,DELA3
RET
//====調(diào)用秒表顯示作延時(shí)========================
DELAY3:MOV R2,#20
DELA5:ACALL MAIN3
DJNZ R2,DELA5
RET
//====調(diào)用延時(shí)子程序作延時(shí)======================
DELAY2:MOV R2,#100
DELA4:ACALL DELAY
DJNZ R2,DELA4
RET
//======共陽(yáng)數(shù)碼管段碼表=================================
TAB:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,87H
END
51單片機(jī)相關(guān)文章:51單片機(jī)教程
蜂鳴器相關(guān)文章:蜂鳴器原理
評(píng)論